-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add blake160, bech32, blake160PubkeyToAddress, pubkeyToA…
…ddress
- Loading branch information
Showing
9 changed files
with
496 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
{ | ||
"fromWords": { | ||
"invalid": [{ | ||
"exception": "Excess padding", | ||
"words": [14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21, 4, 20, 3, 17, 2, 29, 3, 0] | ||
}, | ||
{ | ||
"exception": "Non-zero padding", | ||
"words": [3, 1, 17, 17, 8, 15, 0, 20, 24, 20, 11, 6, 16, 1, 5, 29, 3, 4, 16, 3, 6, 21, 22, 26, 2, 13, 22, 9, 16, 21, 19, 24, 25, 21, 6, 18, 15, 8, 13, 24, 24, 24, 25, 9, 12, 1, 4, 16, 6, 9, 17, 1] | ||
} | ||
] | ||
}, | ||
"bech32": { | ||
"valid": [{ | ||
"string": "A12UEL5L", | ||
"prefix": "A", | ||
"hex": "", | ||
"words": [] | ||
}, | ||
{ | ||
"string": "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", | ||
"prefix": "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio", | ||
"hex": "", | ||
"words": [] | ||
}, | ||
{ | ||
"string": "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", | ||
"prefix": "abcdef", | ||
"hex": "00443214c74254b635cf84653a56d7c675be77df", | ||
"words": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] | ||
}, | ||
{ | ||
"string": "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", | ||
"prefix": "1", | ||
"hex": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", | ||
"words": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
}, | ||
{ | ||
"string": "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", | ||
"prefix": "split", | ||
"hex": "c5f38b70305f519bf66d85fb6cf03058f3dde463ecd7918f2dc743918f2d", | ||
"words": [24, 23, 25, 24, 22, 28, 1, 16, 11, 29, 8, 25, 23, 29, 19, 13, 16, 23, 29, 22, 25, 28, 1, 16, 11, 3, 25, 29, 27, 25, 3, 3, 29, 19, 11, 25, 3, 3, 25, 13, 24, 29, 1, 25, 3, 3, 25, 13] | ||
}, | ||
{ | ||
"string": "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq978ear", | ||
"prefix": "1", | ||
"hex": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", | ||
"words": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
"limit": 300 | ||
}, | ||
{ | ||
"string": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", | ||
"prefix": "bc", | ||
"words": [0, 14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21, 4, 20, 3, 17, 2, 29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22], | ||
"limit": 300 | ||
} | ||
], | ||
"invalid": [{ | ||
"string": "A12Uel5l", | ||
"exception": "Mixed-case string A12Uel5l" | ||
}, | ||
{ | ||
"string": " 1nwldj5", | ||
"exception": "Invalid prefix \\( \\)" | ||
}, | ||
{ | ||
"string": "abc1rzg", | ||
"exception": "abc1rzg too short" | ||
}, | ||
{ | ||
"string": "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", | ||
"exception": "Exceeds length limit" | ||
}, | ||
{ | ||
"string": "x1b4n0q5v", | ||
"exception": "Unknown character b" | ||
}, | ||
{ | ||
"string": "1pzry9x0s0muk", | ||
"exception": "Missing prefix for 1pzry9x0s0muk" | ||
}, | ||
{ | ||
"string": "pzry9x0s0muk", | ||
"exception": "No separator character for pzry9x0s0muk" | ||
}, | ||
{ | ||
"string": "1pzry9x0s0muk", | ||
"exception": "Missing prefix for 1pzry9x0s0muk" | ||
}, | ||
{ | ||
"string": "abc1rzgt4", | ||
"exception": "Data too short" | ||
}, | ||
{ | ||
"string": "s1vcsyn", | ||
"exception": "s1vcsyn too short" | ||
}, | ||
{ | ||
"prefix": "abc", | ||
"words": [128], | ||
"exception": "Non 5-bit word" | ||
}, | ||
{ | ||
"prefix": "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzfoobarfoobar", | ||
"words": [128], | ||
"exception": "Exceeds length limit" | ||
}, | ||
{ | ||
"prefix": "foobar", | ||
"words": [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20], | ||
"exception": "Exceeds length limit" | ||
}, | ||
{ | ||
"prefix": "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzfoobarfoobarfoobarfoobar", | ||
"words": [128], | ||
"limit": 104, | ||
"exception": "Exceeds length limit" | ||
}, | ||
{ | ||
"string": "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", | ||
"exception": "Exceeds length limit" | ||
}, | ||
{ | ||
"prefix": "abc\u00ff", | ||
"words": [18], | ||
"exception": "Invalid prefix \\(abc\u00ff\\)" | ||
}, | ||
{ | ||
"string": "li1dgmt3", | ||
"exception": "Data too short" | ||
}, | ||
{ | ||
"stringHex": "6465316c67377774ff", | ||
"exception": "Unknown character " | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { bech32, blake160, hexToBytes } from '..' | ||
|
||
export enum AddressPrefix { | ||
Mainnet = 'ckb', | ||
Testnet = 'ckt', | ||
} | ||
|
||
export enum AddressType { | ||
BinHash = '0x00', | ||
BinIdx = '0x01', | ||
} | ||
|
||
export enum AddressBinIdx { | ||
P2PH = 'P2PH', | ||
} | ||
|
||
export interface AddressOptions { | ||
prefix: AddressPrefix | ||
type: AddressType | ||
binIdx: AddressBinIdx | ||
} | ||
|
||
export const defaultAddressOptions = { | ||
prefix: AddressPrefix.Mainnet, | ||
type: AddressType.BinIdx, | ||
binIdx: AddressBinIdx.P2PH, | ||
} | ||
|
||
/** | ||
* @description payload = type(01) | bin-idx("P2PH" => "50|32|50|40") | blake160-formatted pubkey | ||
* @see https://github.com/nervosnetwork/ckb/wiki/Common-Address-Format | ||
*/ | ||
export const toAddressPayload = ( | ||
blake160Pubkey: string | Uint8Array, | ||
type: AddressType = AddressType.BinIdx, | ||
params: AddressBinIdx = AddressBinIdx.P2PH | ||
): Uint8Array => { | ||
if (typeof blake160Pubkey === 'string') { | ||
return new Uint8Array([...hexToBytes(type), ...Buffer.from(params), ...hexToBytes(blake160Pubkey)]) | ||
} | ||
return new Uint8Array([...hexToBytes(type), ...Buffer.from(params), ...blake160Pubkey]) | ||
} | ||
|
||
export const blake160PubkeyToAddress = ( | ||
blake160Pubkey: Uint8Array | string, | ||
{ | ||
prefix = AddressPrefix.Mainnet, | ||
type = AddressType.BinIdx, | ||
binIdx = AddressBinIdx.P2PH, | ||
}: AddressOptions = defaultAddressOptions | ||
) => bech32.encode(prefix, bech32.toWords(toAddressPayload(blake160Pubkey, type, binIdx))) | ||
|
||
export const pubkeyToAddress = ( | ||
pubkey: Uint8Array | string, | ||
{ | ||
prefix = AddressPrefix.Mainnet, | ||
type = AddressType.BinIdx, | ||
binIdx = AddressBinIdx.P2PH, | ||
}: AddressOptions = defaultAddressOptions | ||
) => { | ||
const blake160Pubkey = blake160(pubkey) | ||
return blake160PubkeyToAddress(blake160Pubkey, { | ||
prefix, | ||
type, | ||
binIdx, | ||
}) | ||
} | ||
|
||
export const parseAddress = (address: string, prefix: AddressPrefix = AddressPrefix.Mainnet) => { | ||
const decoded = bech32.decode(address) | ||
if (decoded.prefix !== prefix) { | ||
throw new Error('Prefix not matched') | ||
} | ||
return decoded.words | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.