Skip to content

Commit

Permalink
feat: extends hash_type to 'data' | 'type' | 'data1' (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Oct 8, 2021
1 parent 1db1685 commit 30c49e8
Show file tree
Hide file tree
Showing 17 changed files with 476 additions and 200 deletions.
2 changes: 1 addition & 1 deletion packages/ckb-sdk-rpc/types/rpc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare module RPC {
Committed = 'committed',
}

export type ScriptHashType = 'data' | 'type'
export type ScriptHashType = CKBComponents.ScriptHashType

export type DepType = 'code' | 'dep_group'

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,64 @@
]
},
"expected": "0x120200000c000000c5010000b90100001c000000200000006e00000072000000a2000000a50100000000000002000000c12386705b5cbb312b693874f3edf45c43a274482e27b8df0fd80c8d3f5feb8b00000000010fb4945d52baf91e0dee2a686cdd9d84cad95b566a1d7409b970ee0a0f364f6002000000000000000001000000000000000000000031f695263423a4b05045dd25ce6692bb55d7bba2965d8be16b036e138e72cc6501000000030100000c000000a20000009600000010000000180000006100000000e87648170000004900000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88011400000059a27ef3ba84f061517d13f42cf44ed02061006135000000100000003000000031000000ece45e0979030e2f8909f76258631c42333b1e906fd9701ec3600a464a90b8f600000000006100000010000000180000006100000000506a41e15900004900000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88011400000059a27ef3ba84f061517d13f42cf44ed020610061140000000c0000001000000000000000000000004d000000080000004100000082df73581bcd08cb9aa270128d15e79996229ce8ea9e4f985b49fbf36762c5c37936caf3ea3784ee326f60b8992924fcf496f9503c907982525a3436f01ab32900"
},
"transaction containing data1 lock script": {
"transaction": {
"version": "0x0",
"cellDeps": [
{
"outPoint": {
"txHash": "0xace5ea83c478bb866edf122ff862085789158f5cbff155b7bb5f13058555b708",
"index": "0x0"
},
"depType": "depGroup"
}
],
"headerDeps": [],
"inputs": [
{
"since": "0x0",
"previousOutput": {
"txHash": "0xa563884b3686078ec7e7677a5f86449b15cf2693f3c1241766c6996f206cc541",
"index": "0x7"
}
}
],
"outputs": [
{
"capacity": "0x2540be400",
"lock": {
"codeHash": "0x709f3fda12f561cfacf92273c57a98fede188a3f1a59b1f888d113f9cce08649",
"hashType": "data",
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
},
"type": null
},
{
"capacity": "0x2540be400",
"lock": {
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
},
"type": null
},
{
"capacity": "0x2540be400",
"lock": {
"codeHash": "0x709f3fda12f561cfacf92273c57a98fede188a3f1a59b1f888d113f9cce08649",
"hashType": "data1",
"args": "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
},
"type": null
}
],
"outputsData": ["0x", "0x", "0x"],
"witnesses": [
"0x550000001000000055000000550000004100000070b823564f7d1f814cc135ddd56fd8e8931b3a7040eaf1fb828adae29736a3cb0bc7f65021135b293d10a22da61fcc64f7cb660bf2c3276ad63630dad0b6099001"
]
},
"expected": "0x390200000c000000d8010000cc0100001c00000020000000490000004d0000007d000000b00100000000000001000000ace5ea83c478bb866edf122ff862085789158f5cbff155b7bb5f13058555b708000000000100000000010000000000000000000000a563884b3686078ec7e7677a5f86449b15cf2693f3c1241766c6996f206cc54107000000330100001000000071000000d20000006100000010000000180000006100000000e40b540200000049000000100000003000000031000000709f3fda12f561cfacf92273c57a98fede188a3f1a59b1f888d113f9cce086490014000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d76100000010000000180000006100000000e40b5402000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d76100000010000000180000006100000000e40b540200000049000000100000003000000031000000709f3fda12f561cfacf92273c57a98fede188a3f1a59b1f888d113f9cce086490214000000c8328aabcd9b9e8e64fbc566c4385c3bdeb219d71c000000100000001400000018000000000000000000000000000000610000000800000055000000550000001000000055000000550000004100000070b823564f7d1f814cc135ddd56fd8e8931b3a7040eaf1fb828adae29736a3cb0bc7f65021135b293d10a22da61fcc64f7cb660bf2c3276ad63630dad0b6099001"
}
}
}
Loading

0 comments on commit 30c49e8

Please sign in to comment.