Skip to content

Commit

Permalink
feat(utils): set the default address prefix 'ckb'
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Set the default address prefix 'ckb' instead of 'ckt'
  • Loading branch information
Keith-CY committed Jul 16, 2020
1 parent bb23ce9 commit 3134eda
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
18 changes: 9 additions & 9 deletions packages/ckb-sdk-utils/__tests__/address/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
],
"expected": "ckt1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04d7xlydkr98kkxrtvuag8z2j8w4pkw2k6k4l5c02auef"
},
"default type = 0x02 and default prefix = ckt": {
"default type = 0x02 and default prefix = ckb": {
"params": [
{
"args": "0x36c329ed630d6ce750712a477543672adab57f4c",
"codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176"
}
],
"expected": "ckt1q2n9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5czshhac"
"expected": "ckb1q2n9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5c0nw668"
}
},
"bech32Address": {
Expand All @@ -47,13 +47,13 @@
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", { "prefix": "ckb" }],
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"default prefix = ckt": {
"default prefix = ckb": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", {}],
"expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"default option = { prefix: ckt, type: 0x01, codeHashIndex: 0x00 }": {
"default option = { prefix: ckb, type: 0x01, codeHashIndex: 0x00 }": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c"],
"expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"should throw an error when public key hash is not hex string": {
"params": ["36c329ed630d6ce750712a477543672adab57f4c"],
Expand All @@ -70,13 +70,13 @@
],
"expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
},
"default prefix = ckt": {
"default prefix = ckb": {
"params": ["0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01", {}],
"expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"default option = { prefix: ckt }": {
"params": ["0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01"],
"expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
}
},
"parseAddress": {
Expand Down
7 changes: 6 additions & 1 deletion packages/ckb-sdk-utils/__tests__/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,17 @@ describe('privateKeyToAddress', () => {
mainnetAddress: 'ckb1qyqw975zuu9svtyxgjuq44lv7mspte0n2tmqqm3w53',
testnetAddress: 'ckt1qyqw975zuu9svtyxgjuq44lv7mspte0n2tmqa703cd',
}
expect(privateKeyToAddress(fixture.privateKey)).toBe(fixture.testnetAddress)
expect(privateKeyToAddress(fixture.privateKey)).toBe(fixture.mainnetAddress)
expect(
privateKeyToAddress(fixture.privateKey, {
prefix: 'ckb',
}),
).toBe(fixture.mainnetAddress)
expect(
privateKeyToAddress(fixture.privateKey, {
prefix: 'ckt',
}),
).toBe(fixture.testnetAddress)
})

describe('transaction fee', () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/ckb-sdk-utils/src/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export enum AddressType {
TypeCodeHash = '0x04', // full version with hash type 'Type'
}

export type CodeHashIndex = '0x00' | string
export type CodeHashIndex = '0x00' | '0x01' | '0x02'

export interface AddressOptions {
prefix: AddressPrefix
Expand All @@ -23,7 +23,7 @@ export interface AddressOptions {
}

export const defaultAddressOptions = {
prefix: AddressPrefix.Testnet,
prefix: AddressPrefix.Mainnet,
type: AddressType.HashIdx,
codeHashOrCodeHashIndex: '0x00',
}
Expand Down Expand Up @@ -61,7 +61,7 @@ export const toAddressPayload = (
export const bech32Address = (
args: Uint8Array | string,
{
prefix = AddressPrefix.Testnet,
prefix = AddressPrefix.Mainnet,
type = AddressType.HashIdx,
codeHashOrCodeHashIndex = '0x00',
}: AddressOptions = defaultAddressOptions,
Expand All @@ -78,7 +78,7 @@ export const bech32Address = (
*/
export const fullPayloadToAddress = ({
args,
prefix = AddressPrefix.Testnet,
prefix = AddressPrefix.Mainnet,
type = AddressType.DataCodeHash,
codeHash,
}: {
Expand All @@ -96,7 +96,7 @@ export const fullPayloadToAddress = ({
export const pubkeyToAddress = (
pubkey: Uint8Array | string,
{
prefix = AddressPrefix.Testnet,
prefix = AddressPrefix.Mainnet,
type = AddressType.HashIdx,
codeHashOrCodeHashIndex = '0x00' as CodeHashIndex,
}: AddressOptions = defaultAddressOptions,
Expand Down

0 comments on commit 3134eda

Please sign in to comment.