-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2341 from IntersectMBO/fix/2183--cip-129-support-…
…on-govtool feat(#2183): add support for CIP-129 governance identifiers
- Loading branch information
Showing
13 changed files
with
154 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { bech32 } from "bech32"; | ||
import { Buffer } from "buffer"; | ||
|
||
/** | ||
* Encodes a CIP129 identifier based on the provided transaction ID, index, and bech32 prefix. | ||
* @param txID - The transaction ID. | ||
* @param index - The index. | ||
* @param bech32Prefix - The bech32 prefix. | ||
* @returns The generated CIP129 identifier. | ||
*/ | ||
export const encodeCIP129Identifier = ( | ||
txID: string, | ||
index: string, | ||
bech32Prefix: string, | ||
) => { | ||
const govActionBytes = Buffer.from(txID + index, "hex"); | ||
const words = bech32.toWords(govActionBytes); | ||
return bech32.encode(bech32Prefix, words); | ||
}; | ||
|
||
/** | ||
* Decodes a CIP129 identifier. | ||
* @param cip129Identifier - The CIP129 identifier to decode. | ||
* @returns An object containing the decoded transaction ID, index, and prefix. | ||
*/ | ||
export const decodeCIP129Identifier = (cip129Identifier: string) => { | ||
const { prefix, words } = bech32.decode(cip129Identifier); | ||
const buffer = Buffer.from(bech32.fromWords(words)); | ||
const txID = buffer.subarray(0, 32).toString("hex"); | ||
const index = buffer.subarray(32).toString("hex"); | ||
return { txID, index, prefix }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
encodeCIP129Identifier, | ||
decodeCIP129Identifier, | ||
} from "../cip129identifier"; | ||
|
||
const txID = "0e52f799df70b3b74e57d3c7c89f5d44b6deffb3b8b3a718abf9bb41d33e3a92"; | ||
const index = "00"; | ||
const bech32Prefix = "gov_action"; | ||
|
||
describe("CIP-129 Governance Identifier", () => { | ||
describe("encodeCIP129Identifier", () => { | ||
it("should encode a CIP129 identifier correctly", () => { | ||
const result = encodeCIP129Identifier(txID, index, bech32Prefix); | ||
expect(result).toBe( | ||
"gov_action1pef00xwlwzemwnjh60ru386agjmdalanhze6wx9tlxa5r5e782fqqt7spu6", | ||
); | ||
}); | ||
}); | ||
|
||
describe("decodeCIP129Identifier", () => { | ||
it("should decode a CIP129 identifier correctly", () => { | ||
const result = decodeCIP129Identifier( | ||
encodeCIP129Identifier(txID, index, bech32Prefix), | ||
); | ||
expect(result).toEqual({ | ||
txID, | ||
index, | ||
prefix: bech32Prefix, | ||
}); | ||
}); | ||
}); | ||
}); |