Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add full version address identifies the hash_type #557

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/ckb-sdk-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ See [Full Doc](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/README.m
- `utils.fullPayloadToAddress`: script to full version address
- `utils.parseAddress`: get address payload
- `utils.addressToScript`: get lock script from address
- `utils.scriptToAddress`: get full address from script

- [Utils](#utils)

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

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

### Utils

```plain
Expand Down
126 changes: 118 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,14 @@
"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]
},
"should throw an error when its a full version address identified the hash_type and vm_version 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 identified the hash_type and vm_version but code hash has invalid length":{
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x00", "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c135"],
"exception": "'0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c135' is not a valid code hash"
}
},
"fullPayloadToAddress": {
Expand All @@ -28,6 +36,17 @@
],
"expected": "ckt1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04d7xlydkr98kkxrtvuag8z2j8w4pkw2k6k4l5c02auef"
},
"data1 hash type":{
"params": [
{
"args": "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101",
"type": "0x00",
"prefix": "ckt",
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356"
}
],
"expected": "ckt1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqsqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqg8gw2zh"
},
"default type = 0x02 and default prefix = ckb": {
"params": [
{
Expand Down Expand Up @@ -106,35 +125,55 @@
},
"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 identified the hash_type and vm_version": {
"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 identified the hash_type and vm_version has invalid code hash": {
"params": ["ckb1qqv6rsy7kft87e2jaeaganlavs24ellq79ukumnpasyg6aqvzdtqzukxep"],
"exception": "'ckb1qqv6rsy7kft87e2jaeaganlavs24ellq79ukumnpasyg6aqvzdtqzukxep' is not a valid address"
},
"should throw an error when full version address identified the hash_type and vm_version has invalid hash type": {
"params": ["ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqcqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgaxsc2r"],
"exception": "'ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqcqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgaxsc2r' is not a valid address"
},
"should throw an error when full version address identified the hash_type and vm_version has invalid args length": {
"params": ["ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqghy4uyeq"],
"exception": "'ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqghy4uyeq' is not a valid address"
},
"should throw an error when full version address identified the hash_type and vm_version has args not matching args len": {
"params": ["ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqgqz7etutjapsdrhp55lqer2z3nc95963m6psqszx2kx5s"],
"exception": "'ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqgqz7etutjapsdrhp55lqer2z3nc95963m6psqszx2kx5s' is not a valid address"
}
},
"addressToScript": {
Expand Down Expand Up @@ -185,6 +224,77 @@
"hashType": "type",
"args": "0x36c329ed630d6ce750712a477543672adab57f4c"
}
},
"full version address identified the hash_type and vm_version": {
"params": ["ckt1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqgqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgutrqyp"],
"expected": {
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
"hashType": "type",
"args":"0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
}
}
},
"scriptToAddress": {
"full version mainnet address identified the hash_type and vm_version": {
"params": [
{
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
"hashType": "type",
"args":"0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
}
],
"expected": "ckb1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqgqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqg0yp5wq"
},
"full version testnet address identified the hash_type and vm_version": {
"params": [
{
"codeHash": "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
"hashType": "type",
"args":"0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a0c0101"
},
false
],
"expected": "ckt1qq6pngwqn6e9vlm92th84rk0l4jp2h8lurchjmnwv8kq3rt5psf4vqgqza8m903wt5xp5wuxjnurydg2x0qksh280gxqzqgutrqyp"
},
"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))
}
})
})
})
27 changes: 24 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,35 @@
"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"
}
},
"ArgsLenException": {
"params": [""],
"expected": {
"code": 104,
"message": "'' is not a valid args length"
}
},
"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
Loading