Skip to content

Commit

Permalink
refactor(utils): remove redundant default options in the address module
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Remove the utils#defaultAddressOptions
  • Loading branch information
Keith-CY committed Aug 26, 2020
1 parent c5c59d7 commit d74caac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 45 deletions.
8 changes: 4 additions & 4 deletions packages/ckb-sdk-utils/__tests__/address/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", { "prefix": "ckb" }],
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"default prefix = ckb": {
"default prefix = ckb, type = 0x01 and codeHashOrCodeHashIndex = 0x00": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c", {}],
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"default option = { prefix: ckb, type: 0x01, codeHashIndex: 0x00 }": {
"default options = { prefix: ckb, type: 0x01, codeHashOrCodeHashIndex: 0x00 }": {
"params": ["0x36c329ed630d6ce750712a477543672adab57f4c"],
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
Expand All @@ -70,11 +70,11 @@
],
"expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
},
"default prefix = ckb": {
"default prefix = ckb, hashType = 0x01, and codeHashOrCodeHashIndex = 0x00": {
"params": ["0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01", {}],
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
},
"default option = { prefix: ckt }": {
"default options = { prefix: ckt, hashType: 0x01, codeHashOrCodeHashIndex: 0x00 }": {
"params": ["0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01"],
"expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
}
Expand Down
57 changes: 16 additions & 41 deletions packages/ckb-sdk-utils/src/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ export enum AddressType {
export type CodeHashIndex = '0x00' | '0x01' | '0x02'

export interface AddressOptions {
prefix: AddressPrefix
type: AddressType
codeHashOrCodeHashIndex: CodeHashIndex | CKBComponents.Hash256
}

export const defaultAddressOptions = {
prefix: AddressPrefix.Mainnet,
type: AddressType.HashIdx,
codeHashOrCodeHashIndex: '0x00',
prefix?: AddressPrefix
type?: AddressType
codeHashOrCodeHashIndex?: CodeHashIndex | CKBComponents.Hash256
}

/**
Expand Down Expand Up @@ -54,59 +48,40 @@ export const toAddressPayload = (
* @name bech32Address
* @description generate the address by bech32 algorithm
* @param args, used as the identifier of an address, usually the public key hash is used.
* @param {string} prefix, the prefix precedes the address.
* @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]} prefix, the prefix precedes the address, default to be ckb.
* @param {[string]} type, used to indicate which format is adopted to compose the address, default to be 0x01.
* @param {[string]} codeHashOrCodeHashIndex, the referenced code hash or code hash index the address binds to,
* default to be 0x00.
*/
export const bech32Address = (
args: Uint8Array | string,
{
prefix = AddressPrefix.Mainnet,
type = AddressType.HashIdx,
codeHashOrCodeHashIndex = '0x00',
}: AddressOptions = defaultAddressOptions,
{ prefix = AddressPrefix.Mainnet, type = AddressType.HashIdx, codeHashOrCodeHashIndex = '0x00' }: AddressOptions = {},
) => bech32.encode(prefix, bech32.toWords(toAddressPayload(args, type, codeHashOrCodeHashIndex)))

/**
* @name fullPayloadToAddress
* @description generate the address with payload in full version format.
* @param {string} args, used as the identifier of an address.
* @param {string} prefix, the prefix precedes the address.
* @param {string} type, used to indicate which format the address conforms to,
* @param {[string]} prefix, the prefix precedes the address, default to be ckb.
* @param {[string]} type, used to indicate which format the address conforms to, default to be 0x02,
* with hash type of Data or with hash type of Type.
* @param {string} codeHash, the code hash used in the full version payload.
*/
export const fullPayloadToAddress = ({
args,
prefix = AddressPrefix.Mainnet,
prefix,
type = AddressType.DataCodeHash,
codeHash,
}: {
args: string
prefix: AddressPrefix
type: AddressType.DataCodeHash | AddressType.TypeCodeHash
prefix?: AddressPrefix
type?: AddressType.DataCodeHash | AddressType.TypeCodeHash
codeHash: CKBComponents.Hash256
}) =>
bech32Address(args, {
prefix,
type,
codeHashOrCodeHashIndex: codeHash,
})
}) => bech32Address(args, { prefix, type, codeHashOrCodeHashIndex: codeHash })

export const pubkeyToAddress = (
pubkey: Uint8Array | string,
{
prefix = AddressPrefix.Mainnet,
type = AddressType.HashIdx,
codeHashOrCodeHashIndex = '0x00' as CodeHashIndex,
}: AddressOptions = defaultAddressOptions,
) => {
export const pubkeyToAddress = (pubkey: Uint8Array | string, options: AddressOptions = {}) => {
const publicKeyHash = blake160(pubkey)
return bech32Address(publicKeyHash, {
prefix,
type,
codeHashOrCodeHashIndex,
})
return bech32Address(publicKeyHash, options)
}

const isValidShortVersionPayload = (payload: Uint8Array) => {
Expand Down

0 comments on commit d74caac

Please sign in to comment.