Skip to content

Commit

Permalink
feat: support data2 in address transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Mar 13, 2024
1 parent 9e23972 commit 4c61674
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
33 changes: 33 additions & 0 deletions packages/ckb-sdk-utils/__tests__/address/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"params": ["0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64", "0x00", "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176", "data1"],
"expected": [0, 166, 86, 241, 114, 182, 180, 92, 36, 83, 7, 174, 181, 167, 163, 122, 23, 111, 0, 47, 111, 34, 233, 37, 130, 197, 139, 247, 186, 54, 46, 65, 118, 2, 179, 155, 188, 11, 54, 115, 199, 211, 100, 80, 188, 20, 207, 205, 173, 45, 85, 156, 108, 100]
},
"full address of new version specifies hash_type = data2": {
"params": ["0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64", "0x00", "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176", "data2"],
"expected": [0, 166, 86, 241, 114, 182, 180, 92, 36, 83, 7, 174, 181, 167, 163, 122, 23, 111, 0, 47, 111, 34, 233, 37, 130, 197, 139, 247, 186, 54, 46, 65, 118, 4, 179, 155, 188, 11, 54, 115, 199, 211, 100, 80, 188, 20, 207, 205, 173, 45, 85, 156, 108, 100]
},
"should throw an error when its a full version address identifies the hash_type but code hash doesn't start with 0x": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x00", "3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356"],
"exception": "'3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356' is not a valid code hash"
Expand Down Expand Up @@ -257,6 +261,14 @@
"hashType": "data1",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
},
"full version address identifies hash_type = data2": {
"params": ["ckt1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvp9nnw7qkdnnclfkg59uzn8umtfd2kwxceqskl3c2"],
"expected": {
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176",
"hashType": "data2",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
}
},
"scriptToAddress": {
Expand Down Expand Up @@ -302,6 +314,27 @@
],
"expected": "ckt1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvq4nnw7qkdnnclfkg59uzn8umtfd2kwxceq225jvu"
},
"full version mainnet address identifies hash_type = data2": {
"params": [
{
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176",
"hashType": "data2",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
],
"expected": "ckb1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvp9nnw7qkdnnclfkg59uzn8umtfd2kwxceq7y57jj"
},
"full version testnet address identifies hash_type = data2": {
"params": [
{
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176",
"hashType": "data2",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
},
false
],
"expected": "ckt1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvp9nnw7qkdnnclfkg59uzn8umtfd2kwxceqskl3c2"
},
"should throw an error when args doesn't start with 0x": {
"params": [
{
Expand Down
6 changes: 3 additions & 3 deletions packages/ckb-sdk-utils/__tests__/address/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('Test address module', () => {
})

describe('fullPayloadToAddress', () => {
const fixtureTable = Object.entries(
fixtures.fullPayloadToAddress,
).map(([title, { params, expected, exception }]) => [title, params, expected, exception])
const fixtureTable = Object.entries(fixtures.fullPayloadToAddress).map(
([title, { params, expected, exception }]) => [title, params, expected, exception],
)
test.each(fixtureTable)(`%s`, (_title, params, expected, exception) => {
expect.assertions(1)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"neither data nor type should throw an error": {
"hashType": "unknown",
"exception": "Hash type must be either of 'data' or 'type'"
"exception": "Hash type must be either of 'data', 'type', 'data1' or 'data2'"
}
},
"serializeScript": {
Expand Down
4 changes: 3 additions & 1 deletion packages/ckb-sdk-utils/src/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const scriptToPayload = ({ codeHash, hashType, args }: CKBComponents.Script): Ui
data = '00',
type = '01',
data1 = '02',
data2 = '04',
}

if (!HashType[hashType]) {
Expand Down Expand Up @@ -258,7 +259,7 @@ const isPayloadValid = (payload: Uint8Array, bech32Type: Bech32Type) => {
}

const hashType = parseInt(data[32].toString(), 16)
if (hashType > 2) {
if (![0, 1, 2, 4].includes(hashType)) {
throw new HashTypeException(`0x${hashType.toString(16)}`)
}

Expand Down Expand Up @@ -319,6 +320,7 @@ export const addressToScript = (address: string): CKBComponents.Script => {
'00': 'data',
'01': 'type',
'02': 'data1',
'04': 'data2',
}
const p = bytesToHex(payload)

Expand Down
2 changes: 1 addition & 1 deletion packages/ckb-sdk-utils/src/serialization/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const serializeHashType = (hashType: CKBComponents.ScriptHashType) => {
if (hashType === 'type') return '0x01'
if (hashType === 'data1') return '0x02'
if (hashType === 'data2') return '0x04'
throw new TypeError("Hash type must be either of 'data' or 'type'")
throw new TypeError("Hash type must be either of 'data', 'type', 'data1' or 'data2'")
}

/**
Expand Down

0 comments on commit 4c61674

Please sign in to comment.