Migrate From ckb-sdk-utils to Lumos
Summary
In order to unify CKB development ecology, the functions of ckb-sdk-utils
will be migrated to @ckb-lumos/lumos
, most
of the functions can be replaced by the corresponding functions directly, but the normalize.ts
need to be rewriten
with @ckb-lumos/codec
.
@lumos/codec
@lumos/codec provides a set of functions to pack(encode) and unpack(decode) data. related link: https://github.com/ckb-js/lumos/blob/develop/packages/codec
Migration list
- address/index.ts
- convertors/index.ts
- Crypto/Blake160.ts
- Serialization/basic.ts
- Serialization/script.ts
- Serialization/transaction.ts
- epochs.ts
- occupiedCapacity.ts
- sizes.ts
- index.ts
scriptToAddress
import { scriptToAddress } from "@nervosnetwork/ckb-sdk-utils"
import { helpers } from "@ckb-lumos/lumos"
const script = {
codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
args: "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64",
}
// before
scriptToAddress(script) // ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4
// after
helpers.encodeToAddress(script) // ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4
bech32Address
import { bech32Address } from "@nervosnetwork/ckb-sdk-utils"
import { helpers } from "@ckb-lumos/lumos"
// before
bech32Address("0x36c329ed630d6ce750712a477543672adab57f4c") // ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd
// after
helpers.generateSecp256k1Blake160Address("0x36c329ed630d6ce750712a477543672adab57f4c") // ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd
pubkeyToAddress
// migrate pubkeyToAddress example
import { pubkeyToAddress } from "@nervosnetwork/ckb-sdk-utils"
import { helpers, hd, config } from "@ckb-lumos/lumos"
const pubKey = "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01"
// normal case
// before
pubkeyToAddress(pubKey) // ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd
// after
helpers.generateSecp256k1Blake160Address(hd.key.publicKeyToBlake160(pubKey)) // ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd
// testnet
// before
pubkeyToAddress(pubKey, { prefix: AddressPrefix.Testnet }) // ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83
// after
helpers.generateSecp256k1Blake160Address(hd.key.publicKeyToBlake160(pubKey), {
config: config.predefined.AGGRON4,
}) // ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83
parseAddress
import { parseAddress } from "@nervosnetwork/ckb-sdk-utils"
import { helpers } from "@ckb-lumos/lumos"
import { predefined } from "@ckb-lumos/config-manager"
// before
parseAddress("ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83", "hex") // 0x010036c329ed630d6ce750712a477543672adab57f4c
// after
"0x0100" +
helpers
.addressToScript("ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83", {
config: predefined.AGGRON4,
})
.args.substring(2) // 0x010036c329ed630d6ce750712a477543672adab57f4c
addressToScript
import { addressToScript } from "@nervosnetwork/ckb-sdk-utils"
import { helpers } from "@ckb-lumos/lumos"
const address = "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"
// before
addressToScript(address)
// {
// codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
// hashType: 'type',
// args: '0x36c329ed630d6ce750712a477543672adab57f4c'
// }
// after
helpers.addressToScript(address)
// {
// codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8',
// hashType: 'type',
// args: '0x36c329ed630d6ce750712a477543672adab57f4c'
// }
toUint16Le
import { toUint16Le } from "@nervosnetwork/ckb-sdk-utils"
import { number, bytes } from "@ckb-lumos/codec"
const value = "0xabcd"
// before
toUint16Le(value)
// after
bytes.hexify(number.Uint16LE.pack(value))
toUint32Le
import { toUint32Le } from "@nervosnetwork/ckb-sdk-utils"
import { number, bytes } from "@ckb-lumos/codec"
const value = "0x12345678"
// before
toUint32Le(value)
// after
bytes.hexify(number.Uint32LE.pack(value))
toUint64Le
import { toUint64Le } from "@nervosnetwork/ckb-sdk-utils"
import { number, bytes } from "@ckb-lumos/codec"
const value = "0x1234567890abcdef"
// before
toUint64Le(value)
// after
bytes.hexify(number.Uint64LE.pack(value))
hexToBytes
import { hexToBytes } from "@nervosnetwork/ckb-sdk-utils"
import { bytes } from "@ckb-lumos/codec"
const hexfixture = "0xabcd"
// before
hexToBytes(hexfixture)
// after
bytes.bytify(hexfix)
bytesToHex
import { bytesToHex } from "@nervosnetwork/ckb-sdk-utils"
import { bytes } from "@ckb-lumos/codec"
const bytesfixture = "0xabcd"
// before
bytesToHex(bytesfixture)
// after
bytes.hexify(bytesfixture)
toBigEndian
import { toBigEndian } from "@nervosnetwork/ckb-sdk-utils"
import { number } from "@ckb-lumos/codec"
const fixture = "0x3ef9e8c069c925ef"
// before
toBigEndian(fixture) // 0xef25c969c0e8f93e
// after
bytes.hexify(bytes.bytify(fixture).reverse()) // 0xef25c969c0e8f93e
blake160
import { toBigEndian } from "@nervosnetwork/ckb-sdk-utils"
import { utils } from "@ckb-lumos/lumos"
const pk = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
// before
blake160(pk, "hex") // 7d0252b620af274af951e534e182c7a57df86da7
// after
new utils.CKBHasher().update(pk).digestHex().slice(0, 42).substring(2) // 7d0252b620af274af951e534e182c7a57df86da7
serializeArray
import { serializeArray } from "@nervosnetwork/ckb-sdk-utils"
import { bytes } from "@ckb-lumos/codec"
const fixtureArray = [104, 213, 67, 138]
// before
serializeArray(fixtureArray)
// after
bytes.hexify(fixtureArray)
serializeStruct
import { serializeStruct } from "@nervosnetwork/ckb-sdk-utils"
import { number, bytes, molecule } from "@ckb-lumos/codec"
// before
serializeStruct(
new Map<string, any>([
["f1", "0xab"],
["f2", "0x04030201"],
])
)
// after
const structCodec = molecule.struct(
{
f1: number.Uint8,
f2: number.Uint32BE,
},
["f1", "f2"]
)
bytes.hexify(
structCodec.pack({
f1: 0xab,
f2: 0x04030201,
})
)
serializeFixVec
import { serializeStruct } from "@nervosnetwork/ckb-sdk-utils"
import { fixvec } from "@ckb-lumos/codec/lib/molecule/layout"
import { number, bytes } from "@ckb-lumos/codec"
// before
serializeFixVec(["0x12"]) // 0x0100000012
// after
const fixvecCodec = fixvec(number.Uint8)
bytes.hexify(fixvecCodec.pack([0x12])) // 0x0100000012
serializeDynVec
import { serializeDynVec } from "@nervosnetwork/ckb-sdk-utils"
import { dynvec } from "@ckb-lumos/codec/lib/molecule/layout"
import { bytes, createBytesCodec } from "@ckb-lumos/codec"
// before
serializeDynVec([]) // 0x04000000
serializeDynVec(["0x02001234"]) // 0x0c0000000800000002001234
serializeDynVec(["0x02001234", "0x00000000", "0x02000567", "0x01000089", "0x0300abcdef"]) // 0x2d000000180000001c000000200000002400000028000000020012340000000002000567010000890300abcdef
// after
const bytesCodec = createBytesCodec<string>({
pack: (hex) => bytes.bytify(hex),
unpack: (buf) => bytes.hexify(buf),
})
const dynvecCodec = dynvec(bytesCodec)
bytes.hexify(dynvecCodec.pack([])) // 0x00000000
bytes.hexify(dynvecCodec.pack(["0x02001234"])) // 0x0c0000000800000002001234
bytes.hexify(dynvecCodec.pack(["0x02001234", "0x00000000", "0x02000567", "0x01000089", "0x0300abcdef"])) // 0x2d000000180000001c000000200000002400000028000000020012340000000002000567010000890300abcdef
serializeTable
import { serializeTable } from "@nervosnetwork/ckb-sdk-utils"
import { table } from "@ckb-lumos/codec/lib/molecule/layout"
import { bytes, number, createBytesCodec } from "@ckb-lumos/codec"
// before
serializeTable(
new Map([
["f1", "0xab"],
["f2", "0x04030201"],
])
) // 0x110000000c0000000d000000ab04030201
// after
const bytesCodec = createBytesCodec<string>({
pack: (hex) => bytes.bytify(hex),
unpack: (buf) => bytes.hexify(buf),
})
const tableCodec = table({ f1: bytesCodec, f2: bytesCodec }, ["f1", "f2"])
bytes.hexify(tableCodec.pack({ f1: "0xab", f2: "0x04030201" })) // 0x110000000c0000000d000000ab04030201
serializeOption
import { serializeOption } from "@nervosnetwork/ckb-sdk-utils"
import { option } from "@ckb-lumos/codec/lib/molecule/layout"
import { bytes, createBytesCodec } from "@ckb-lumos/codec"
// before
serializeOption() // 0x
serializeOption("0x") // 0x
serializeOption("0x0c0000000800000000000000") // 0x0c0000000800000000000000
// after
const bytesCodec = createBytesCodec<string>({
pack: (hex) => bytes.bytify(hex),
unpack: (buf) => bytes.hexify(buf),
})
const optionCodec = option(bytesCodec)
bytes.hexify(optionCodec.pack()) // 0x
bytes.hexify(optionCodec.pack("0x")) // 0x
bytes.hexify(optionCodec.pack("0x0c0000000800000000000000")) // 0x0c0000000800000000000000
serializeCodeHash
import { serializeCodeHash } from "@nervosnetwork/ckb-sdk-utils"
import { blockchain } from "@ckb-lumos/base"
import { bytes } from "@ckb-lumos/codec"
const hash = "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"
// before
serializeCodeHash(hash) // 0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88
// after
bytes.hexify(blockchain.Byte32.pack(hash)) // 0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88
serializeHashType
import { serializeHashType } from "@nervosnetwork/ckb-sdk-utils"
import { blockchain } from "@ckb-lumos/base"
import { bytes } from "@ckb-lumos/codec"
// before
serializeHashType("data") // 0x00
serializeHashType("type") // 0x01
// after
bytes.hexify(blockchain.HashType.pack("data")) // 0x00
bytes.hexify(blockchain.HashType.pack("type")) // 0x01
serializeArgs
import { serializeArgs } from "@nervosnetwork/ckb-sdk-utils"
import { blockchain } from "@ckb-lumos/base"
import { bytes } from "@ckb-lumos/codec"
const args = "0x8536c9d5d908bd89fc70099e4284870708b6632356aad98734fcf43f6f71c304"
// before
serializeArgs(args) // 0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88
// after
bytes.hexify(blockchain.Bytes.pack(args)) // 0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88
serializeScript
import { serializeScript } from "@nervosnetwork/ckb-sdk-utils"
import { blockchain } from "@ckb-lumos/base"
import { bytes } from "@ckb-lumos/codec"
// before
serializeScript({
codeHash: "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
args: "0x3954acece65096bfa81258983ddb83915fc56bd8",
hashType: "type",
}) // 0x4900000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e8801140000003954acece65096bfa81258983ddb83915fc56bd8
// after
bytes.hexify(
blockchain.Script.pack({
codeHash: "0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
args: "0x3954acece65096bfa81258983ddb83915fc56bd8",
hashType: "type",
})
) // 0x4900000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e8801140000003954acece65096bfa81258983ddb83915fc56bd8
serializeVersion
import { serializeVersion } from "@nervosnetwork/ckb-sdk-utils"
import { bytes, number } from "@ckb-lumos/codec"
// before
serializeVersion("0x0") // 0x00000000
serializeVersion("0x1") // 0x01000000
serializeVersion("0xabcd") // 0xcdab0000
// after
bytes.hexify(number.Uint32LE.pack("0x0")) // 0x00000000
bytes.hexify(number.Uint32LE.pack("0x1")) // 0x01000000
bytes.hexify(number.Uint32LE.pack("0xabcd")) // 0xcdab0000