From e72a29d46615bc09fc9ff726e30abb5f70ac26d7 Mon Sep 17 00:00:00 2001 From: Keith Date: Fri, 25 Mar 2022 20:23:47 +0800 Subject: [PATCH 1/3] fix: fix default code hash/code hash index in toAddressPayload 1. when address type is hash index, default code hash index should be secp256k1 code hash index 2. when address type is not hash index, default code hash should be secp256k1 code hash 3. when code hash is secp256k1, default hash type should be type Ref: https://github.com/nervosnetwork/ckb-sdk-js/issues/586 --- .../__tests__/address/fixtures.json | 20 +++++++++++++++---- packages/ckb-sdk-utils/src/address/index.ts | 15 +++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/ckb-sdk-utils/__tests__/address/fixtures.json b/packages/ckb-sdk-utils/__tests__/address/fixtures.json index 53fbc3fd..7273be5c 100644 --- a/packages/ckb-sdk-utils/__tests__/address/fixtures.json +++ b/packages/ckb-sdk-utils/__tests__/address/fixtures.json @@ -1,10 +1,22 @@ { "toAddressPayload": { - "basic": { + "should have default params for short version address": { "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": { + "should have default secp256k1 code hash index when address type is hash index": { + "params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x01"], + "expected": [1, 0, 54, 195, 41, 237, 99, 13, 108, 231, 80, 113, 42, 71, 117, 67, 103, 42, 218, 181, 127, 76] + }, + "should have default secp256k1 code hash when address type is not hash index": { + "params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x04", ""], + "expected": [4, 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, 54, 195, 41, 237, 99, 13, 108, 231, 80, 113, 42, 71, 117, 67, 103, 42, 218, 181, 127, 76] + }, + "should have default hash_type = type when full address of new version with secp256k1 code hash": { + "params": ["0x36c329ed630d6ce750712a477543672adab57f4c", "0x00", "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"], + "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, 1, 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 = data1": { "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] }, @@ -16,8 +28,8 @@ "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"], + "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" } }, diff --git a/packages/ckb-sdk-utils/src/address/index.ts b/packages/ckb-sdk-utils/src/address/index.ts index bedd6abf..86e76da8 100644 --- a/packages/ckb-sdk-utils/src/address/index.ts +++ b/packages/ckb-sdk-utils/src/address/index.ts @@ -89,12 +89,13 @@ export interface AddressOptions { * @see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md * @param {string | Uint8Array} args, use as the identifier of an address, usually the public key hash is used. * @param {string} type, used to indicate which format is adopted to compose the address. - * @param {string} codeHashOrCodeHashIndex, the referenced code hash or code hash index the address binds to + * @param {string} codeHashOrCodeHashIndex, the referenced code hash or code hash index the address binds to, + * default to be secp256k1 code hash/code hash index */ export const toAddressPayload = ( args: string | Uint8Array, type: AddressType = AddressType.HashIdx, - codeHashOrCodeHashIndex: CodeHashIndex | CKBComponents.Hash256 = '0x00', + codeHashOrCodeHashIndex?: CodeHashIndex | CKBComponents.Hash256, hashType?: CKBComponents.ScriptHashType, ): Uint8Array => { if (typeof args === 'string' && !args.startsWith('0x')) { @@ -108,6 +109,10 @@ export const toAddressPayload = ( ) } + if (!codeHashOrCodeHashIndex) { + codeHashOrCodeHashIndex = type === AddressType.HashIdx ? '0x00' : SECP256K1_BLAKE160.codeHash + } + if (type !== AddressType.FullVersion) { return new Uint8Array([ ...hexToBytes(type), @@ -116,6 +121,10 @@ export const toAddressPayload = ( ]) } + if (!hashType && codeHashOrCodeHashIndex === SECP256K1_BLAKE160.codeHash) { + hashType = SECP256K1_BLAKE160.hashType + } + if (!codeHashOrCodeHashIndex.startsWith('0x') || codeHashOrCodeHashIndex.length !== 66) { throw new CodeHashException(codeHashOrCodeHashIndex) } @@ -142,7 +151,7 @@ export const toAddressPayload = ( */ export const bech32Address = ( args: Uint8Array | string, - { prefix = AddressPrefix.Mainnet, type = AddressType.HashIdx, codeHashOrCodeHashIndex = '0x00' }: AddressOptions = {}, + { prefix = AddressPrefix.Mainnet, type = AddressType.HashIdx, codeHashOrCodeHashIndex = '' }: AddressOptions = {}, ) => bech32.encode(prefix, bech32.toWords(toAddressPayload(args, type, codeHashOrCodeHashIndex)), MAX_BECH32_LIMIT) /** From 381148346ef0243b12dfcd872ea15d9f59e07a9e Mon Sep 17 00:00:00 2001 From: Keith Date: Sat, 7 May 2022 18:56:34 +0800 Subject: [PATCH 2/3] chore: update package versions and changelogs --- CHANGELOG.md | 11 +++++++++++ lerna.json | 2 +- packages/ckb-sdk-core/CHANGELOG.md | 8 ++++++++ packages/ckb-sdk-core/package.json | 8 ++++---- packages/ckb-sdk-rpc/CHANGELOG.md | 8 ++++++++ packages/ckb-sdk-rpc/package.json | 6 +++--- packages/ckb-sdk-utils/CHANGELOG.md | 11 +++++++++++ packages/ckb-sdk-utils/package.json | 4 ++-- packages/ckb-types/CHANGELOG.md | 8 ++++++++ packages/ckb-types/package.json | 2 +- 10 files changed, 57 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5507356..cf0b401e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) + + +### Bug Fixes + +* fix default code hash/code hash index in toAddressPayload ([e72a29d](https://github.com/nervosnetwork/ckb-sdk-js/commit/e72a29d46615bc09fc9ff726e30abb5f70ac26d7)) + + + + + ## [0.102.3](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.2...v0.102.3) (2022-03-24) **Note:** Version bump only for package ckb-sdk-js diff --git a/lerna.json b/lerna.json index 6480dc0d..0a0656ee 100644 --- a/lerna.json +++ b/lerna.json @@ -4,5 +4,5 @@ ], "npmClient": "yarn", "useWorkspaces": true, - "version": "0.102.3" + "version": "0.103.0" } diff --git a/packages/ckb-sdk-core/CHANGELOG.md b/packages/ckb-sdk-core/CHANGELOG.md index aa847f39..c882ba99 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.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 + + + + + ## [0.102.3](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.2...v0.102.3) (2022-03-24) **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 dcb2eb7c..25f662f0 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.102.3", + "version": "0.103.0", "description": "JavaScript SDK for Nervos Network CKB Project", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -33,9 +33,9 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-rpc": "0.102.3", - "@nervosnetwork/ckb-sdk-utils": "0.102.3", - "@nervosnetwork/ckb-types": "0.102.3", + "@nervosnetwork/ckb-sdk-rpc": "0.103.0", + "@nervosnetwork/ckb-sdk-utils": "0.103.0", + "@nervosnetwork/ckb-types": "0.103.0", "tslib": "2.3.1" }, "gitHead": "82c6a77b990ccfb70a6a7f3ea1442f2d5c81f334" diff --git a/packages/ckb-sdk-rpc/CHANGELOG.md b/packages/ckb-sdk-rpc/CHANGELOG.md index 461df3e0..a30ce617 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.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 + + + + + ## [0.102.3](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.2...v0.102.3) (2022-03-24) **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 8bfabe48..604f2065 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.102.3", + "version": "0.103.0", "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.102.3", + "@nervosnetwork/ckb-sdk-utils": "0.103.0", "axios": "0.21.4", "tslib": "2.3.1" }, "devDependencies": { - "@nervosnetwork/ckb-types": "0.102.3" + "@nervosnetwork/ckb-types": "0.103.0" }, "gitHead": "82c6a77b990ccfb70a6a7f3ea1442f2d5c81f334" } diff --git a/packages/ckb-sdk-utils/CHANGELOG.md b/packages/ckb-sdk-utils/CHANGELOG.md index 129b0f60..b097a03b 100644 --- a/packages/ckb-sdk-utils/CHANGELOG.md +++ b/packages/ckb-sdk-utils/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.103.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.3...v0.103.0) (2022-05-09) + + +### Bug Fixes + +* fix default code hash/code hash index in toAddressPayload ([e72a29d](https://github.com/nervosnetwork/ckb-sdk-js/commit/e72a29d46615bc09fc9ff726e30abb5f70ac26d7)) + + + + + ## [0.102.3](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.2...v0.102.3) (2022-03-24) **Note:** Version bump only for package @nervosnetwork/ckb-sdk-utils diff --git a/packages/ckb-sdk-utils/package.json b/packages/ckb-sdk-utils/package.json index 5418a03d..a0ca5c15 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.102.3", + "version": "0.103.0", "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.102.3", + "@nervosnetwork/ckb-types": "0.103.0", "bech32": "2.0.0", "elliptic": "6.5.4", "jsbi": "3.1.3", diff --git a/packages/ckb-types/CHANGELOG.md b/packages/ckb-types/CHANGELOG.md index 3733dc4a..68f19f5b 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.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 + + + + + ## [0.102.3](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.102.2...v0.102.3) (2022-03-24) **Note:** Version bump only for package @nervosnetwork/ckb-types diff --git a/packages/ckb-types/package.json b/packages/ckb-types/package.json index fb2ffbaa..a53d299a 100644 --- a/packages/ckb-types/package.json +++ b/packages/ckb-types/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-types", - "version": "0.102.3", + "version": "0.103.0", "description": "Type module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", From 667595df0f908b43cafcd0fd9782880e6619aaf7 Mon Sep 17 00:00:00 2001 From: Keith Date: Mon, 9 May 2022 13:37:09 +0800 Subject: [PATCH 3/3] chore: update lerna hashes --- packages/ckb-sdk-core/package.json | 2 +- packages/ckb-sdk-rpc/package.json | 2 +- packages/ckb-sdk-utils/package.json | 2 +- packages/ckb-types/package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ckb-sdk-core/package.json b/packages/ckb-sdk-core/package.json index 25f662f0..db286bd6 100644 --- a/packages/ckb-sdk-core/package.json +++ b/packages/ckb-sdk-core/package.json @@ -38,5 +38,5 @@ "@nervosnetwork/ckb-types": "0.103.0", "tslib": "2.3.1" }, - "gitHead": "82c6a77b990ccfb70a6a7f3ea1442f2d5c81f334" + "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" } diff --git a/packages/ckb-sdk-rpc/package.json b/packages/ckb-sdk-rpc/package.json index 604f2065..83baef46 100644 --- a/packages/ckb-sdk-rpc/package.json +++ b/packages/ckb-sdk-rpc/package.json @@ -40,5 +40,5 @@ "devDependencies": { "@nervosnetwork/ckb-types": "0.103.0" }, - "gitHead": "82c6a77b990ccfb70a6a7f3ea1442f2d5c81f334" + "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" } diff --git a/packages/ckb-sdk-utils/package.json b/packages/ckb-sdk-utils/package.json index a0ca5c15..a1a1ef98 100644 --- a/packages/ckb-sdk-utils/package.json +++ b/packages/ckb-sdk-utils/package.json @@ -41,5 +41,5 @@ "@types/bitcoinjs-lib": "5.0.0", "@types/elliptic": "6.4.12" }, - "gitHead": "82c6a77b990ccfb70a6a7f3ea1442f2d5c81f334" + "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" } diff --git a/packages/ckb-types/package.json b/packages/ckb-types/package.json index a53d299a..c8b671ab 100644 --- a/packages/ckb-types/package.json +++ b/packages/ckb-types/package.json @@ -20,5 +20,5 @@ "bugs": { "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, - "gitHead": "82c6a77b990ccfb70a6a7f3ea1442f2d5c81f334" + "gitHead": "381148346ef0243b12dfcd872ea15d9f59e07a9e" }