# @nervosnetwork/ckb-sdk-utils

> Utils module of @nervosnetwork/ckb-sdk-core

Latest version **0.109.5** (published 2025-02-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nervosnetwork/ckb-sdk-utils
pnpm add @nervosnetwork/ckb-sdk-utils
yarn add @nervosnetwork/ckb-sdk-utils
bun add @nervosnetwork/ckb-sdk-utils
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.109.5 |
| Published | 2025-02-14 |
| First published | 2019-02-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 270.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 41 |
| Author | Nervos <dev@nervos.org> |
| Maintainers | keith_chen, nervos_network, xuejie.nervos |

## Links

- npm: https://www.npmjs.com/package/@nervosnetwork/ckb-sdk-utils
- Repository: https://github.com/ckb-js/ckb-sdk-js
- Homepage: https://github.com/ckb-js/ckb-sdk-js#readme
- Issues: https://github.com/ckb-js/ckb-sdk-js/issues
- npm.io page: https://npm.io/package/@nervosnetwork/ckb-sdk-utils

## Dependencies (5)

- [jsbi](https://npm.io/package/jsbi.md) 3.1.3
- [tslib](https://npm.io/package/tslib.md) 2.3.1
- [bech32](https://npm.io/package/bech32.md) 2.0.0
- [elliptic](https://npm.io/package/elliptic.md) 6.6.1
- [@nervosnetwork/ckb-types](https://npm.io/package/@nervosnetwork/ckb-types.md) 0.109.5

## Recent versions

- 0.109.5 (latest) — 2025-02-14
- 0.109.4 — 2024-11-15
- 0.109.3 — 2024-08-01
- 0.109.2 — 2024-07-19
- 0.109.2-alpha.1 — 2024-07-18
- 0.109.2-alpha.0 — 2024-07-16
- 0.109.1 — 2024-03-13
- 0.109.0 — 2023-04-26
- 0.107.0 — 2023-04-03
- 0.103.1 — 2022-05-31
- 0.103.0 — 2022-05-09
- 0.102.3 — 2022-03-24
- 0.102.2 — 2022-03-02
- 0.102.1 — 2022-02-17
- 0.102.0 — 2022-02-17
- … 65 more at https://npm.io/package/@nervosnetwork/ckb-sdk-utils/versions

## README

# `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](https://github.com/ckb-js/ckb-sdk-js/blob/develop/README.md)

## Most Used Utilities

- [Address](#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
  - <del>`utils.fullPayloadToAddress`: script to full version address of obselete version, **deprecated and use `utils.scriptToAddress` instead**</del>
  - `utils.parseAddress`: get address payload
  - `utils.addressToScript`: get lock script from address
  - `utils.scriptToAddress`: get full address of new version from script

- [Utils](#utils)

  - `utils.blake160`
  - `utils.bytesToHex`
  - `utils.hexToBytes`
  - `utils.toUint16Le`
  - `utils.toUint32Le`
  - `utils.toUint64Le`

- [System Scripts](#system-scripts)

### Address

```js
/**
 * @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
// }
```

```js
/**
 * @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'
// }
```

```js
/**
 * @description get short version address by private key
 */
utils.privateKeyToAddress('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', {
  prefix: utils.AddressPrefix.Mainnet, // prefix is optional, default to 'ckb'
})
// ckb1qyqw975zuu9svtyxgjuq44lv7mspte0n2tmqqm3w53
```

```js
/**
 * @description get short version address by public key
 */
utils.pubkeyToAddress('0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01', {
  prefix: utils.AddressPrefix.Testnet,
})
// ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83
```

```js
/**
 * @description get short/full version address from args
 */
utils.bech32Address('0x36c329ed630d6ce750712a477543672adab57f4c', {
  prefix: utils.AddressPrefix.Mainnet,
  type: utils.AddressType.HashIdx,
  codeHashOrCodeHashIndex: '0x00',
})
// ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd
```

```js
/**
 * @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
```

```js
/**
 * @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
```

```js
/**
 * @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'
// }
```

```js
/**
 * @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

```plain
/**
 * @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
```

```js
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
```

```js
utils.parseEpoch('0x2003e80010000200')
// { length: '0x3e8', index: '0x10', number: '0x200' }
utils.serializeEpoch({ length: '0x3e8', index: '0x10', number: '0x200' })
// 0x2003e80010000200
```

```js
utils.rawTransactionToHash(rawTx)
// tx hash
```

```js
/**
 * @description get hash of a script
 */
utils.scriptToHash({
  codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  args: '0x01',
  hashType: 'type',
})
// 0xd39f84d4702f53cf8625da4411be1640b961715cb36816501798fedb70b6e0fb
```

### System Scripts

[System Scripts](https://github.com/ckb-js/ckb-sdk-js/blob/develop/packages/ckb-sdk-utils/src/systemScripts.ts)

---
_Source: https://npm.io/package/@nervosnetwork/ckb-sdk-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
