0.109.1 • Published 2 months ago

@nervosnetwork/ckb-sdk-utils v0.109.1

Weekly downloads
2,825
License
MIT
Repository
github
Last release
2 months ago

ckb-sdk-utils

@nervosnetwork/ckb-sdk-utils is the utils module of @nervosnetwork/ckb-sdk-core, which provides necessary methods for the sdk, including encryption, key-pair generation, address generation and so on.

See Full Doc

Most Used Utilities

  • Address

    • utils.AddressPrefix
    • utils.AddressType
    • utils.privateKeyToAddress: get address from private key
    • utils.pubkeyToAddress: get address from public key
    • utils.bech32Address: args to short/full version address
    • utils.fullPayloadToAddress: script to full version address of obselete version, deprecated and use utils.scriptToAddress instead
    • utils.parseAddress: get address payload
    • utils.addressToScript: get lock script from address
    • utils.scriptToAddress: get full address of new version from script
  • Utils

    • utils.blake160
    • utils.bytesToHex
    • utils.hexToBytes
    • utils.toUint16Le
    • utils.toUint32Le
    • utils.toUint64Le
  • System Scripts

Address

/**
 * @description address prefix
 * @see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#wrap-to-address
 */
utils.AddressPrefix
// {
//   Mainnet: 'ckb', // mainnet prefix
//   Testnet: 'ckt', // testnet prefix
// }
/**
 * @description address payload format types
 * @see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#payload-format-types
 */
utils.AddressType
// {
//   HashIdx: '0x01',      // short version address
//   DataCodeHash: '0x02', // full version address with hash type = 'data'
//   TypeCodeHash: '0x04', // full version address with hash type = 'type'
// }
/**
 * @description get short version address by private key
 */
utils.privateKeyToAddress('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', {
  prefix: utils.AddressPrefix.Mainnet, // prefix is optional, default to 'ckb'
})
// ckb1qyqw975zuu9svtyxgjuq44lv7mspte0n2tmqqm3w53
/**
 * @description get short version address by public key
 */
utils.pubkeyToAddress('0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01', {
  prefix: utils.AddressPrefix.Testnet,
})
// ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83
/**
 * @description get short/full version address from args
 */
utils.bech32Address('0x36c329ed630d6ce750712a477543672adab57f4c', {
  prefix: utils.AddressPrefix.Mainnet,
  type: utils.AddressType.HashIdx,
  codeHashOrCodeHashIndex: '0x00',
})
// ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd
/**
 * @description get full version address by a lock script
 * @params args - lock.args
 * @params type - utils.AddressType.DataCodeHash if lock.hash_type = 'data'
 *                otherwise utils.AddressType.TypeCodeHash
 * @params prefix - utils.AddressPrefix.Mainnet or utils.AddressPrefix.Testnet
 * @params codeHash - lock.code_hash
 */
utils.fullPayloadToAddress({
  args: '0x36c329ed630d6ce750712a477543672adab57f4c',
  type: utils.AddressType.DataCodeHash,
  prefix: utils.AddressPrefix.Testnet,
  codeHash: '0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176',
})
// ckt1q2n9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5czshhac
/**
 * @description parse short version address
 *              the returned value is `type | index | args`,
 *              in this case `01 | 00 | 36...4c`
 */
utils.parseAddress('ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83', 'hex')
// 0x010036c329ed630d6ce750712a477543672adab57f4c

/**
 * @description parse full version address
 *              the returned value is ` type | code hash | args`
 *              in this case `02 | 9b...e8 | b3...64`
 */
utils.parseAddress(
  'ckb1q2da0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xw3vumhs9nvu786dj9p0q5elx66t24n3kxgdwd2q8',
  'hex',
)
// 0x029bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8b39bbc0b3673c7d36450bc14cfcdad2d559c6c64
/**
 * @description restore lock script from a short version address
 */
utils.addressToScript('ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83')
// {
//   codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
//   hashType: 'type',
//   args: '0x36c329ed630d6ce750712a477543672adab57f4c'
// }

/**
 * @description restore lock script from a full version address
 */
utils.addressToScript('ckb1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04d7xlydkr98kkxrtvuag8z2j8w4pkw2k6k4l5czfy37k')
// {
//   codeHash: '0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2',
//   hashType: 'type',
//   args: '0x36c329ed630d6ce750712a477543672adab57f4c'
// }
/**
 * @description generate full address of new version from script, the address conforms to format type 0x00
 * @tutorial https://github.com/nervosnetwork/rfcs/pull/239/
 */
utils.scriptToAddress({
  "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
  "hashType": "type",
  "args":"0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"
})
// ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4

Utils

/**
 * @description get the blake160 digest of a message
 */
utils.blake160(
  new Uint8Array([ 2, 74, 80, 30, 253, 50, 142, 6, 44, 134, 117, 242, 54, 89, 112, 114, 140, 133, 156, 89, 43, 238, 239, 214, 190, 142, 173, 61, 144, 19, 48, 188, 1]),
  'hex',
)
// 36c329ed630d6ce750712a477543672adab57f4c
utils.bytesToHex(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]))
// 0x48656c6c6f20576f726c64
utils.hexToBytes('0x48656c6c6f20576f726c64')
// Uint8Array [ 72, 101, 108, 108, 111,  32, 87, 111, 114, 108, 100 ]
utils.toUint16Le('0xbcd')
// 0xcd0b
utils.toUint32Le('0x123456')
// 0x56341200
utils.toUint64Le('0x1234567890abcdef')
// 0xefcdab9078563412
utils.parseEpoch('0x2003e80010000200')
// { length: '0x3e8', index: '0x10', number: '0x200' }
utils.serializeEpoch({ length: '0x3e8', index: '0x10', number: '0x200' })
// 0x2003e80010000200
utils.rawTransactionToHash(rawTx)
// tx hash
/**
 * @description get hash of a script
 */
utils.scriptToHash({
  codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  args: '0x01',
  hashType: 'type',
})
// 0xd39f84d4702f53cf8625da4411be1640b961715cb36816501798fedb70b6e0fb

System Scripts

System Scripts

0.109.1

2 months ago

0.107.0

1 year ago

0.109.0

1 year ago

0.103.0

2 years ago

0.103.1

2 years ago

0.102.3

2 years ago

0.102.2

2 years ago

0.102.1

2 years ago

0.102.0

2 years ago

0.101.0

3 years ago

0.43.0

3 years ago

0.41.1

3 years ago

0.41.0

3 years ago

0.40.0

3 years ago

0.39.0

3 years ago

0.38.2

3 years ago

0.38.1

3 years ago

0.38.0

3 years ago

0.37.0

4 years ago

0.36.2

4 years ago

0.36.1

4 years ago

0.36.0

4 years ago

0.35.0

4 years ago

0.34.0

4 years ago

0.33.0

4 years ago

0.32.0

4 years ago

0.31.0

4 years ago

0.30.0

4 years ago

0.29.1

4 years ago

0.29.0

4 years ago

0.28.0

4 years ago

0.27.1

4 years ago

0.27.0

4 years ago

0.26.4

4 years ago

0.26.3

4 years ago

0.26.2

4 years ago

0.26.1

4 years ago

0.26.0

4 years ago

0.25.0

4 years ago

0.25.0-alpha.1

4 years ago

0.25.0-alpha.0

4 years ago

0.24.2

5 years ago

0.24.1

5 years ago

0.24.0

5 years ago

0.23.1

5 years ago

0.23.0

5 years ago

0.22.1

5 years ago

0.22.0

5 years ago

0.21.1

5 years ago

0.21.0

5 years ago

0.20.0

5 years ago

0.19.1

5 years ago

0.19.0

5 years ago

0.18.0

5 years ago

0.17.1

5 years ago

0.17.0

5 years ago

0.16.0

5 years ago

0.15.1

5 years ago

0.15.0

5 years ago

0.14.0

5 years ago

0.13.0

5 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.11.0-alpha.0

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.0.1-alpha.3

5 years ago

0.0.1-alpha.2

5 years ago

0.0.1-alpha.1

5 years ago

0.0.1-alpha.0

5 years ago