diff --git a/CHANGELOG.md b/CHANGELOG.md index cf0b401e..85df128d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,19 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.103.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.1) (2022-05-31) + + +### Bug Fixes + +* add address format type validation in utils.toAddressPayload ([8cf043c](https://github.com/nervosnetwork/ckb-sdk-js/commit/8cf043cbf6d89eef90c1eb55958ba609e71d2bba)) +* fix default code hash/code hash index in toAddressPayload ([e72a29d](https://github.com/nervosnetwork/ckb-sdk-js/commit/e72a29d46615bc09fc9ff726e30abb5f70ac26d7)) +* reinforce restriction of address format type and bech32(m) ([816e8ea](https://github.com/nervosnetwork/ckb-sdk-js/commit/816e8eab1154ca393b12ed1272f0bf8c753ac448)) + + + + + # [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) diff --git a/README.md b/README.md index e3b6d367..61ff95d4 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ The ckb-sdk-js is still under development and aim for providing low-level APIs o # TypeDoc -- [Global](https://nervosnetwork.github.io/ckb-sdk-js/globals.html) -- [Core](https://nervosnetwork.github.io/ckb-sdk-js/classes/ckb.html) -- [RPC](https://nervosnetwork.github.io/ckb-sdk-js/classes/ckbrpc.html) -- [Types](https://nervosnetwork.github.io/ckb-sdk-js/modules/ckbcomponents.html) +- [Global](https://nervosnetwork.github.io/ckb-sdk-js/index.html) +- [Core](https://nervosnetwork.github.io/ckb-sdk-js/classes/_nervosnetwork_ckb_sdk_core.default.html) +- [RPC](https://nervosnetwork.github.io/ckb-sdk-js/classes/_nervosnetwork_ckb_sdk_rpc.default.html) +- [Types](https://nervosnetwork.github.io/ckb-sdk-js/modules/_nervosnetwork_ckb_types.CKBComponents.html) # Introduction diff --git a/lerna.json b/lerna.json index 0a0656ee..5d34baee 100644 --- a/lerna.json +++ b/lerna.json @@ -4,5 +4,5 @@ ], "npmClient": "yarn", "useWorkspaces": true, - "version": "0.103.0" + "version": "0.103.1" } diff --git a/packages/ckb-sdk-core/CHANGELOG.md b/packages/ckb-sdk-core/CHANGELOG.md index c882ba99..5a16602d 100644 --- a/packages/ckb-sdk-core/CHANGELOG.md +++ b/packages/ckb-sdk-core/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.103.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.1) (2022-05-31) + +**Note:** Version bump only for package @nervosnetwork/ckb-sdk-core + + + + + # [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) **Note:** Version bump only for package @nervosnetwork/ckb-sdk-core diff --git a/packages/ckb-sdk-core/package.json b/packages/ckb-sdk-core/package.json index db286bd6..c247c536 100644 --- a/packages/ckb-sdk-core/package.json +++ b/packages/ckb-sdk-core/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-core", - "version": "0.103.0", + "version": "0.103.1", "description": "JavaScript SDK for Nervos Network CKB Project", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -33,10 +33,10 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-rpc": "0.103.0", - "@nervosnetwork/ckb-sdk-utils": "0.103.0", - "@nervosnetwork/ckb-types": "0.103.0", + "@nervosnetwork/ckb-sdk-rpc": "0.103.1", + "@nervosnetwork/ckb-sdk-utils": "0.103.1", + "@nervosnetwork/ckb-types": "0.103.1", "tslib": "2.3.1" }, - "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" + "gitHead": "51d076e98f34cf15dda4bc5f3e434f1f6de54bb7" } diff --git a/packages/ckb-sdk-rpc/CHANGELOG.md b/packages/ckb-sdk-rpc/CHANGELOG.md index a30ce617..136e53c6 100644 --- a/packages/ckb-sdk-rpc/CHANGELOG.md +++ b/packages/ckb-sdk-rpc/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.103.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.1) (2022-05-31) + +**Note:** Version bump only for package @nervosnetwork/ckb-sdk-rpc + + + + + # [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) **Note:** Version bump only for package @nervosnetwork/ckb-sdk-rpc diff --git a/packages/ckb-sdk-rpc/package.json b/packages/ckb-sdk-rpc/package.json index 83baef46..9e8ae75c 100644 --- a/packages/ckb-sdk-rpc/package.json +++ b/packages/ckb-sdk-rpc/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-rpc", - "version": "0.103.0", + "version": "0.103.1", "description": "RPC module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js/packages/ckb-rpc#readme", @@ -33,12 +33,12 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-utils": "0.103.0", + "@nervosnetwork/ckb-sdk-utils": "0.103.1", "axios": "0.21.4", "tslib": "2.3.1" }, "devDependencies": { - "@nervosnetwork/ckb-types": "0.103.0" + "@nervosnetwork/ckb-types": "0.103.1" }, - "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" + "gitHead": "51d076e98f34cf15dda4bc5f3e434f1f6de54bb7" } diff --git a/packages/ckb-sdk-utils/CHANGELOG.md b/packages/ckb-sdk-utils/CHANGELOG.md index b097a03b..f5d7d670 100644 --- a/packages/ckb-sdk-utils/CHANGELOG.md +++ b/packages/ckb-sdk-utils/CHANGELOG.md @@ -3,6 +3,19 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.103.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.1) (2022-05-31) + + +### Bug Fixes + +* add address format type validation in utils.toAddressPayload ([8cf043c](https://github.com/nervosnetwork/ckb-sdk-js/commit/8cf043cbf6d89eef90c1eb55958ba609e71d2bba)) +* fix default code hash/code hash index in toAddressPayload ([e72a29d](https://github.com/nervosnetwork/ckb-sdk-js/commit/e72a29d46615bc09fc9ff726e30abb5f70ac26d7)) +* reinforce restriction of address format type and bech32(m) ([816e8ea](https://github.com/nervosnetwork/ckb-sdk-js/commit/816e8eab1154ca393b12ed1272f0bf8c753ac448)) + + + + + # [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) diff --git a/packages/ckb-sdk-utils/__tests__/address/fixtures.json b/packages/ckb-sdk-utils/__tests__/address/fixtures.json index 7273be5c..71b7016d 100644 --- a/packages/ckb-sdk-utils/__tests__/address/fixtures.json +++ b/packages/ckb-sdk-utils/__tests__/address/fixtures.json @@ -31,6 +31,10 @@ "should throw an error when its a full version address but hash_type is missing while code hash is not secp256k1 code hash": { "params": ["0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64", "0x00", "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3ccee"], "exception": "hashType is required" + }, + "should throw an error when address format type is invalid(0x03)": { + "params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x03", "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"], + "exception": "0x03 is not a valid address format type" } }, "fullPayloadToAddress": { @@ -141,8 +145,8 @@ "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] + "params": ["ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqj0k2lzuhgvrgacvxtrw69"], + "expected": [0, 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, 2, 79, 178, 190, 46, 93, 12, 26, 59, 134] }, "should throw an error when short version address has invalid payload size": { "params": ["ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqqm65l9j"], @@ -152,11 +156,7 @@ "params": ["ckt1qyprdsefa43s6m882pcj53m4gdnj4k440axqqfmyd9c"], "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" - }, - "should throw an error when hash type is invalid": { + "should throw an error when deprecated address has invalid address type format 0x03": { "params": ["ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2"], "exception": "'ckt1qwn9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5ctv25r2' is not a valid address" }, @@ -168,13 +168,29 @@ "params": ["ckb1qsqcjt4ypkpt20r83lugxyj9pwa30cty6737p2gfgx493qul2cgvrxhw"], "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 has invalid code hash(bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8)": { + "params": ["ckt1qq9a0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsjzla0h"], + "exception": "'ckt1qq9a0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsjzla0h' is not a valid address" + }, + "should throw an error when full version address has invalid hash_type 0x03": { + "params": ["ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqcvf0k9sc40s3azmpfvhyuudhahpsj72tseeza5p"], + "exception": "'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqcvf0k9sc40s3azmpfvhyuudhahpsj72tseeza5p' is not a valid address" + }, + "should throw an error when address format type is 0x00 but encode method is bech32": { + "params": ["ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq20k2lzuhgvrgacv4tmr88"], + "exception": "Address format type 0x00 doesn't match encode method bech32" + }, + "should throw an error when address format type is 0x01 but encode method is bech32m": { + "params": ["ckb1qyqylv479ewscx3ms620sv34pgeuz6zagaaqh0knz7"], + "exception": "Address format type 0x01 doesn't match encode method bech32m" + }, + "should throw an error when address format type is 0x02 but encode method is bech32m": { + "params": ["ckb1q2da0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsnajhch96rq68wrqn2tmhm"], + "exception": "Address format type 0x02 doesn't match encode method bech32m" }, - "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" + "should throw an error when address format type is 0x04 but encode method is bech32m": { + "params": ["ckt1qjda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsrzta3vx9tuy0gkc2t9e88rdldcvyhjjuguz6rt"], + "exception": "Address format type 0x04 doesn't match encode method bech32m" } }, "addressToScript": { diff --git a/packages/ckb-sdk-utils/__tests__/exceptions/fixtures.json b/packages/ckb-sdk-utils/__tests__/exceptions/fixtures.json index 09ca82af..7ae564a8 100644 --- a/packages/ckb-sdk-utils/__tests__/exceptions/fixtures.json +++ b/packages/ckb-sdk-utils/__tests__/exceptions/fixtures.json @@ -62,6 +62,20 @@ "message": "'0x03' is not a valid hash type" } }, + "AddressFormatTypeException": { + "params": [3], + "expected": { + "code": 104, + "message": "0x03 is not a valid address format type" + } + }, + "AddressFormatTypeAndEncodeMethodNotMatchException": { + "params": [3, "bech32"], + "expected": { + "code": 104, + "message": "Address format type 0x03 doesn't match encode method bech32" + } + }, "OutLenTooSmallException": { "params": [16, 32], "expected": { diff --git a/packages/ckb-sdk-utils/package.json b/packages/ckb-sdk-utils/package.json index a1a1ef98..8a3d9380 100644 --- a/packages/ckb-sdk-utils/package.json +++ b/packages/ckb-sdk-utils/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-utils", - "version": "0.103.0", + "version": "0.103.1", "description": "Utils module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -31,7 +31,7 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-types": "0.103.0", + "@nervosnetwork/ckb-types": "0.103.1", "bech32": "2.0.0", "elliptic": "6.5.4", "jsbi": "3.1.3", @@ -41,5 +41,5 @@ "@types/bitcoinjs-lib": "5.0.0", "@types/elliptic": "6.4.12" }, - "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" + "gitHead": "51d076e98f34cf15dda4bc5f3e434f1f6de54bb7" } diff --git a/packages/ckb-sdk-utils/src/address/index.ts b/packages/ckb-sdk-utils/src/address/index.ts index 86e76da8..5a3313b1 100644 --- a/packages/ckb-sdk-utils/src/address/index.ts +++ b/packages/ckb-sdk-utils/src/address/index.ts @@ -13,6 +13,8 @@ import { CodeHashException, HashTypeException, ParameterRequiredException, + AddressFormatTypeException, + AddressFormatTypeAndEncodeMethodNotMatchException, } from '../exceptions' const MAX_BECH32_LIMIT = 1023 @@ -31,6 +33,11 @@ export enum AddressType { TypeCodeHash = '0x04', // full version with hash type 'Type', deprecated } +export enum Bech32Type { + Bech32 = 'bech32', + Bech32m = 'bech32m', +} + /** * @description payload to a full address of new version */ @@ -102,6 +109,12 @@ export const toAddressPayload = ( throw new HexStringWithout0xException(args) } + if ( + ![AddressType.HashIdx, AddressType.DataCodeHash, AddressType.TypeCodeHash, AddressType.FullVersion].includes(type) + ) { + throw new AddressFormatTypeException(+type) + } + if ([AddressType.DataCodeHash, AddressType.TypeCodeHash].includes(type)) { /* eslint-disable max-len */ console.warn( @@ -181,8 +194,11 @@ export const pubkeyToAddress = (pubkey: Uint8Array | string, options: AddressOpt return bech32Address(publicKeyHash, options) } -const isValidShortVersionPayload = (payload: Uint8Array) => { - const [, index, ...data] = payload +const isValidShortVersionPayload = (payload: Uint8Array, bech32Type?: Bech32Type) => { + const [type, index, ...data] = payload + if (bech32Type !== Bech32Type.Bech32) { + throw new AddressFormatTypeAndEncodeMethodNotMatchException(type, bech32Type) + } /* eslint-disable indent */ switch (index) { case 0: // secp256k1 + blake160 @@ -207,23 +223,29 @@ const isValidShortVersionPayload = (payload: Uint8Array) => { /* eslint-enable indent */ } -const isPayloadValid = (payload: Uint8Array) => { +const isPayloadValid = (payload: Uint8Array, bech32Type: Bech32Type) => { const type = payload[0] const data = payload.slice(1) /* eslint-disable indent */ switch (type) { case +AddressType.HashIdx: { - isValidShortVersionPayload(payload) + isValidShortVersionPayload(payload, bech32Type) break } case +AddressType.DataCodeHash: case +AddressType.TypeCodeHash: { + if (bech32Type !== Bech32Type.Bech32) { + throw new AddressFormatTypeAndEncodeMethodNotMatchException(type, bech32Type) + } if (data.length < 32) { throw new AddressPayloadException(payload, 'full') } break } case +AddressType.FullVersion: { + if (bech32Type !== Bech32Type.Bech32m) { + throw new AddressFormatTypeAndEncodeMethodNotMatchException(type, bech32Type) + } const codeHash = data.slice(0, 32) if (codeHash.length < 32) { throw new CodeHashException(bytesToHex(codeHash)) @@ -254,17 +276,24 @@ export declare interface ParseAddress { * e.g. 0x | 01 | 00 | e2fa82e70b062c8644b80ad7ecf6e015e5f352f6 */ export const parseAddress: ParseAddress = (address: string, encode: 'binary' | 'hex' = 'binary'): any => { + let bech32Type: Bech32Type | undefined let payload: Uint8Array = new Uint8Array() try { const decoded = bech32.decode(address, MAX_BECH32_LIMIT) + bech32Type = Bech32Type.Bech32 payload = new Uint8Array(bech32.fromWords(new Uint8Array(decoded.words))) } catch { const decoded = bech32m.decode(address, MAX_BECH32_LIMIT) + bech32Type = Bech32Type.Bech32m payload = new Uint8Array(bech32m.fromWords(new Uint8Array(decoded.words))) } + try { - isPayloadValid(payload) + isPayloadValid(payload, bech32Type) } catch (err) { + if (err instanceof AddressFormatTypeAndEncodeMethodNotMatchException) { + throw err + } throw new AddressException(address, err.stack, err.type) } return encode === 'binary' ? payload : bytesToHex(payload) @@ -274,41 +303,47 @@ export const addressToScript = (address: string): CKBComponents.Script => { const payload = parseAddress(address) const type = payload[0] - if (type === +AddressType.FullVersion) { - const HASH_TYPE: Record = { - '00': 'data', - '01': 'type', - '02': 'data1', - } - const p = bytesToHex(payload) - - const codeHash = `0x${p.substr(4, 64)}` - const hashType = HASH_TYPE[p.substr(68, 2)] - const args = `0x${p.substr(70)}` - return { codeHash, hashType, args } - } + switch (type) { + case +AddressType.FullVersion: { + const HASH_TYPE: Record = { + '00': 'data', + '01': 'type', + '02': 'data1', + } + const p = bytesToHex(payload) - if (type === +AddressType.HashIdx) { - const codeHashIndices = [ - SECP256K1_BLAKE160, - SECP256K1_MULTISIG, - address.startsWith(AddressPrefix.Mainnet) ? ANYONE_CAN_PAY_MAINNET : ANYONE_CAN_PAY_TESTNET, - ] - const index = payload[1] - const args = payload.slice(2) - const script = codeHashIndices[index] - return { - codeHash: script.codeHash, - hashType: script.hashType, - args: bytesToHex(args), + const codeHash = `0x${p.substr(4, 64)}` + const hashType = HASH_TYPE[p.substr(68, 2)] + const args = `0x${p.substr(70)}` + return { codeHash, hashType, args } + } + case +AddressType.HashIdx: { + const codeHashIndices = [ + SECP256K1_BLAKE160, + SECP256K1_MULTISIG, + address.startsWith(AddressPrefix.Mainnet) ? ANYONE_CAN_PAY_MAINNET : ANYONE_CAN_PAY_TESTNET, + ] + const index = payload[1] + const args = payload.slice(2) + const script = codeHashIndices[index] + return { + codeHash: script.codeHash, + hashType: script.hashType, + args: bytesToHex(args), + } + } + case +AddressType.DataCodeHash: + case +AddressType.TypeCodeHash: { + const codeHashAndArgs = bytesToHex(payload.slice(1)) + const hashType = type === +AddressType.DataCodeHash ? 'data' : 'type' + return { + codeHash: codeHashAndArgs.substr(0, 66), + hashType, + args: `0x${codeHashAndArgs.substr(66)}`, + } + } + default: { + throw new AddressFormatTypeException(type) } - } - - const codeHashAndArgs = bytesToHex(payload.slice(1)) - const hashType = type === +AddressType.DataCodeHash ? 'data' : 'type' - return { - codeHash: codeHashAndArgs.substr(0, 66), - hashType, - args: `0x${codeHashAndArgs.substr(66)}`, } } diff --git a/packages/ckb-sdk-utils/src/exceptions/address.ts b/packages/ckb-sdk-utils/src/exceptions/address.ts index 4b93b770..2ce12f53 100644 --- a/packages/ckb-sdk-utils/src/exceptions/address.ts +++ b/packages/ckb-sdk-utils/src/exceptions/address.ts @@ -39,9 +39,27 @@ export class HashTypeException extends Error { } } +export class AddressFormatTypeException extends Error { + code = ErrorCode.AddressInvalid + + constructor(type: number) { + super(`0x${type.toString(16).padStart(2, '0')} is not a valid address format type`) + } +} + +export class AddressFormatTypeAndEncodeMethodNotMatchException extends Error { + code = ErrorCode.AddressInvalid + + constructor(type: number, bech32Type: 'bech32' | 'bech32m' | 'unknown' = 'unknown') { + super(`Address format type 0x${type.toString(16).padStart(2, '0')} doesn't match encode method ${bech32Type}`) + } +} + export default { AddressPayloadException, AddressException, CodeHashException, HashTypeException, + AddressFormatTypeException, + AddressFormatTypeAndEncodeMethodNotMatchException, } diff --git a/packages/ckb-types/CHANGELOG.md b/packages/ckb-types/CHANGELOG.md index 68f19f5b..29356d59 100644 --- a/packages/ckb-types/CHANGELOG.md +++ b/packages/ckb-types/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.103.1](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.1) (2022-05-31) + +**Note:** Version bump only for package @nervosnetwork/ckb-types + + + + + # [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) **Note:** Version bump only for package @nervosnetwork/ckb-types diff --git a/packages/ckb-types/package.json b/packages/ckb-types/package.json index c8b671ab..75db09b9 100644 --- a/packages/ckb-types/package.json +++ b/packages/ckb-types/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-types", - "version": "0.103.0", + "version": "0.103.1", "description": "Type module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -20,5 +20,5 @@ "bugs": { "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, - "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" + "gitHead": "51d076e98f34cf15dda4bc5f3e434f1f6de54bb7" }