Skip to content

Commit

Permalink
Merge pull request #557 from nervosnetwork/add-new-address-format
Browse files Browse the repository at this point in the history
feat: add full version address identifies the hash_type
  • Loading branch information
Keith-CY committed Sep 30, 2021
2 parents 00e7d87 + 949b8b5 commit b304eb8
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 198 deletions.
16 changes: 15 additions & 1 deletion packages/ckb-sdk-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ See [Full Doc](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/README.m
- `utils.privateKeyToAddress`: get address from private key
- `utils.pubkeyToAddress`: get address from public key
- `utils.bech32Address`: args to short/full version address
- `utils.fullPayloadToAddress`: script to full version address
- <del>`utils.fullPayloadToAddress`: script to full version address of obselete version, **deprecated and use `utils.scriptToAddress` instead**</del>
- `utils.parseAddress`: get address payload
- `utils.addressToScript`: get lock script from address
- `utils.scriptToAddress`: get full address of new version from script

- [Utils](#utils)

Expand Down Expand Up @@ -148,6 +149,19 @@ utils.addressToScript('ckb1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04d7xly
// }
```

```js
/**
* @description generate full address of new version from script, the address conforms to format type 0x00
* @tutorial https://github.com/nervosnetwork/rfcs/pull/239/
*/
utils.scriptToAddress({
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args":"0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
})
// ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4
```

### Utils

```plain
Expand Down
144 changes: 136 additions & 8 deletions packages/ckb-sdk-utils/__tests__/address/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
"basic": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c"],
"expected": [1, 0, 54, 195, 41, 237, 99, 13, 108, 231, 80, 113, 42, 71, 117, 67, 103, 42, 218, 181, 127, 76]
},
"full address of new version specifies hash_type = type": {
"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]
},
"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"
},
"should throw an error when its a full version address identifies the hash_type but code hash has invalid length": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x00", "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c135"],
"exception": "'0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c135' is not a valid code hash"
},
"should throw an error when its a full version address identifies the hash_type but hash_type is missing": {
"params": ["0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64", "0x00", "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"],
"exception": "hashType is required"
}
},
"fullPayloadToAddress": {
Expand Down Expand Up @@ -106,35 +122,47 @@
},
"data hash type full version address": {
"params": ["ckb1q2da0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xw3vumhs9nvu786dj9p0q5elx66t24n3kxgdwd2q8"],
"expected": [ 2, 155, 215, 224, 111, 62, 207, 75, 224, 242, 252, 210, 24, 139, 35, 241, 185, 252, 200, 142, 93, 75, 101, 168, 99, 123, 23, 114, 59, 189, 163, 204, 232, 179, 155, 188, 11, 54, 115, 199, 211, 100, 80, 188, 20, 207, 205, 173, 45, 85, 156, 108, 100 ]
"expected": [2, 155, 215, 224, 111, 62, 207, 75, 224, 242, 252, 210, 24, 139, 35, 241, 185, 252, 200, 142, 93, 75, 101, 168, 99, 123, 23, 114, 59, 189, 163, 204, 232, 179, 155, 188, 11, 54, 115, 199, 211, 100, 80, 188, 20, 207, 205, 173, 45, 85, 156, 108, 100]
},
"type hash type full version address": {
"params": ["ckb1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04d7xlydkr98kkxrtvuag8z2j8w4pkw2k6k4l5czfy37k"],
"expected": [ 4, 24, 146, 234, 64, 216, 43, 83, 198, 120, 255, 136, 49, 36, 80, 187, 177, 126, 22, 77, 122, 62, 10, 144, 148, 26, 165, 136, 57, 245, 111, 141, 242, 54, 195, 41, 237, 99, 13, 108, 231, 80, 113, 42, 71, 117, 67, 103, 42, 218, 181, 127, 76 ]
"expected": [4, 24, 146, 234, 64, 216, 43, 83, 198, 120, 255, 136, 49, 36, 80, 187, 177, 126, 22, 77, 122, 62, 10, 144, 148, 26, 165, 136, 57, 245, 111, 141, 242, 54, 195, 41, 237, 99, 13, 108, 231, 80, 113, 42, 71, 117, 67, 103, 42, 218, 181, 127, 76]
},
"full version address identifies the hash_type": {
"params": ["ckt1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqgqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgutrqyp"],
"expected": [0, 52, 25, 161, 192, 158, 178, 86, 127, 101, 82, 238, 122, 142, 207, 253, 100, 21, 92, 255, 224, 241, 121, 110, 110, 97, 236, 8, 141, 116, 12, 19, 86, 1, 0, 23, 79, 178, 190, 46, 93, 12, 26, 59, 134, 148, 248, 50, 53, 10, 51, 193, 104, 93, 71, 122, 12, 1, 1]
},
"should throw an error when short version address has invalid payload size": {
"params": ["ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqqm65l9j"],
"exception": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqqm65l9j is not a valid short version address"
"exception": "'ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqqm65l9j' is not a valid short version address"
},
"should throw an error when anyone can pay address has invalid payload size": {
"params": ["ckt1qyprdsefa43s6m882pcj53m4gdnj4k440axqqfmyd9c"],
"exception": "ckt1qyprdsefa43s6m882pcj53m4gdnj4k440axqqfmyd9c is not a valid short version address"
"exception": "'ckt1qyprdsefa43s6m882pcj53m4gdnj4k440axqqfmyd9c' is not a valid short version address"
},
"should throw an error when address type is invalid": {
"params": ["ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2"],
"exception": "ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2 is not a valid address"
"exception": "'ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2' is not a valid address"
},
"should throw an error when hash type is invalid": {
"params": ["ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2"],
"exception": "ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2 is not a valid address"
"exception": "'ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2' is not a valid address"
},
"should throw an error when code hash index is invalid": {
"params": ["ckt1qyzndsefa43s6m882pcj53m4gdnj4k440axqcth0hp"],
"exception": "ckt1qyzndsefa43s6m882pcj53m4gdnj4k440axqcth0hp is not a valid short version address"
"exception": "'ckt1qyzndsefa43s6m882pcj53m4gdnj4k440axqcth0hp' is not a valid short version address"
},
"should throw an error when full version address has invalid size": {
"params": ["ckb1qsqcjt4ypkpt20r83lugxyj9pwa30cty6737p2gfgx493qul2cgvrxhw"],
"exception": "ckb1qsqcjt4ypkpt20r83lugxyj9pwa30cty6737p2gfgx493qul2cgvrxhw is not a valid full version address"
"exception": "'ckb1qsqcjt4ypkpt20r83lugxyj9pwa30cty6737p2gfgx493qul2cgvrxhw' is not a valid full version address"
},
"should throw an error when full version address identifies the hash_type has invalid code hash": {
"params": ["ckb1qqv6rsy7kft87e2jaeaganlavs24ellq79ukumnpasyg6aqvzdtqzukxep"],
"exception": "'ckb1qqv6rsy7kft87e2jaeaganlavs24ellq79ukumnpasyg6aqvzdtqzukxep' is not a valid address"
},
"should throw an error when full version address identifies the hash_type has invalid hash type": {
"params": ["ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqcqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgaxsc2r"],
"exception": "'ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqcqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgaxsc2r' is not a valid address"
}
},
"addressToScript": {
Expand Down Expand Up @@ -185,6 +213,106 @@
"hashType": "type",
"args": "0x36c329ed630d6ce750712a477543672adab57f4c"
}
},
"full version address identifies hash_type = type": {
"params": ["ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4"],
"expected": {
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
},
"full version address identifies hash_type = data1": {
"params": ["ckt1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvq4nnw7qkdnnclfkg59uzn8umtfd2kwxceq225jvu"],
"expected": {
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176",
"hashType": "data1",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
}
},
"scriptToAddress": {
"full version mainnet address identifies hash_type = type": {
"params": [
{
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
],
"expected": "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4"
},
"full version testnet address identifies hash_type = type": {
"params": [
{
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
},
false
],
"expected": "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqgutnjd"
},
"full version mainnet address identifies hash_type = data1": {
"params": [
{
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176",
"hashType": "data1",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
}
],
"expected": "ckb1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvq4nnw7qkdnnclfkg59uzn8umtfd2kwxceqyclaxy"
},
"full version testnet address identifies hash_type = data1": {
"params": [
{
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176",
"hashType": "data1",
"args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
},
false
],
"expected": "ckt1qzn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvq4nnw7qkdnnclfkg59uzn8umtfd2kwxceq225jvu"
},
"should throw an error when args doesn't start with 0x": {
"params": [
{
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
"hashType": "type",
"args": "4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
}
],
"exception": "Hex string 4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101 should start with 0x"
},
"should throw an error when code hash doesn't start with 0x": {
"params": [
{
"codeHash": "3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
"hashType": "type",
"args": "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
}
],
"exception": "'3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356' is not a valid code hash"
},
"should throw an error when code hash has invalid length": {
"params": [
{
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c135",
"hashType": "type",
"args": "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
}
],
"exception": "'0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c135' is not a valid code hash"
},
"should throw an error when hash type is inavlid": {
"params": [
{
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
"hashType": "type1",
"args": "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
}
],
"exception": "'type1' is not a valid hash type"
}
}
}
19 changes: 19 additions & 0 deletions packages/ckb-sdk-utils/__tests__/address/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
parseAddress,
fullPayloadToAddress,
addressToScript,
scriptToAddress,
} = ckbUtils

describe('Test address module', () => {
Expand Down Expand Up @@ -120,4 +121,22 @@ describe('Test address module', () => {
}
})
})

describe('scriptToAddress', () => {
const fixtureTable = Object.entries(fixtures.scriptToAddress).map(([title, { params, expected, exception }]) => [
title,
params,
expected,
exception,
])
test.each(fixtureTable)(`%s`, (_title, params, expected, exception) => {
expect.assertions(1)
try {
const actual = scriptToAddress(...params)
expect(actual).toEqual(expected)
} catch (err) {
expect(err).toEqual(new Error(exception))
}
})
})
})
20 changes: 17 additions & 3 deletions packages/ckb-sdk-utils/__tests__/exceptions/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@
"params": ["Invalid Payload", "short"],
"expected": {
"code": 104,
"message": "Invalid Payload is not a valid short version address payload"
"message": "'Invalid Payload' is not a valid short version address payload"
}
},
"AddressException": {
"params": ["Invalid Address", "full"],
"params": ["Invalid Address", "", "full"],
"expected": {
"code": 104,
"message": "Invalid Address is not a valid full version address"
"message": "'Invalid Address' is not a valid full version address"
}
},
"CodeHashException": {
"params": ["0x"],
"expected": {
"code": 104,
"message": "'0x' is not a valid code hash"
}
},
"HashTypeException": {
"params": ["0x03"],
"expected": {
"code": 104,
"message": "'0x03' is not a valid hash type"
}
},
"OutLenTooSmallException": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ckb-sdk-utils/__tests__/exceptions/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const exceptions = require('../../lib/exceptions')
const fixtures = require('./fixtures.json')

describe.only('Test exceptions', () => {
describe('Test exceptions', () => {
const fixtureTable = Object.entries(fixtures).map(([exceptionName, { params, expected }]) => [
exceptionName,
params,
Expand Down
1 change: 1 addition & 0 deletions packages/ckb-sdk-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"@nervosnetwork/ckb-types": "0.43.0",
"bech32": "2.0.0",
"elliptic": "6.5.4",
"jsbi": "3.1.3",
"tslib": "2.3.1"
Expand Down
Loading

0 comments on commit b304eb8

Please sign in to comment.