Options
All
  • Public
  • Public/Protected
  • All
Menu

Package helpers

@ckb-lumos/helpers

Helper utilities for working with CKB transactions from lumos.

The difference between this and @ckb-lumos/base, is that @ckb-lumos/base contains only core definitions, while this module contains utilities used in a framework sense. One example is: you can pretty much use @ckb-lumos/base as a standalone library, while this library integrates more with config module so it knows whether the framework is running under testnet, or mainnet environment.

Usage

const { minimalCellCapacity, generateAddress, parseAddress } = require("@ckb-lumos/helpers")

// Get cell minimal capacity.
const result = minimalCellCapacity({
  cellOutput: {
    capacity: "0x174876e800",
    lock: {
      args: "0x36c329ed630d6ce750712a477543672adab57f4c",
      codeHash:
        "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
      hashType: "type",
    },
    type: null,
  },
  data: "0x",
  blockHash: null,
  blockNumber: null,
  outPoint: null,
})

// result will be 6100000000n shannons.

// Use `generateAddress` to get address from lock script.
const address = generateAddress({
  codeHash:
    "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
  hashType: "type",
  args: "0x36c329ed630d6ce750712a477543672adab57f4c",
})

// Then you will get mainnet address "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd", or you can generate testnet address by
const { predefined } = require("@ckb-lumos/config-manager")

const address = generateAddress({
  codeHash:
    "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
  hashType: "type",
  args: "0x36c329ed630d6ce750712a477543672adab57f4c",
}, { config: predefined.AGGRON4 })

// Will get testnet address "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83".

// Use `parseAddress` to get lock script from an address.
const script = parseAddress("ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd")

// TransactionSkeleton <=> Object

// Convert TransactionSkeleton to js object
const obj = transactionSkeletonToObject(txSkeleton)
// then your can write to json file
fs.writeFileSync("your file", JSON.stringify(obj))

// Or convert js object to TransactionSkeleton
// If your object is from json file, make sure `cellProvider` is working properly.
const txSkeleton = objectToTransactionSkeleton(obj)

Index

Interfaces

Type aliases

Variables

Functions

Type aliases

HashAlgorithm

HashAlgorithm: "ckb-blake2b-256" | "ckb-blake2b-160"

HashAlgorithm

HashAlgorithm: "ckb-blake2b-256" | "ckb-blake2b-160"

LiveCellFetcher

LiveCellFetcher: (outPoint: OutPoint) => Promisable<Cell>

Type declaration

LiveCellFetcher

LiveCellFetcher: (outPoint: OutPoint) => Promisable<Cell>

Type declaration

PredefinedScriptName

PredefinedScriptName: keyof typeof SCRIPTS

PredefinedScriptName

PredefinedScriptName: keyof typeof SCRIPTS

Promisable

Promisable<T>: Promise<T> | T

Type parameters

  • T

Promisable

Promisable<T>: Promise<T> | T

Type parameters

  • T

TransactionSkeletonType

TransactionSkeletonType: Record<TransactionSkeletonInterface> & Readonly<TransactionSkeletonInterface>

TransactionSkeletonType

TransactionSkeletonType: Record<TransactionSkeletonInterface> & Readonly<TransactionSkeletonInterface>

Variables

Const ADDRESS_FORMAT_FULL

ADDRESS_FORMAT_FULL: 0 = 0

full version identifies the hashType

Const ADDRESS_FORMAT_FULL

ADDRESS_FORMAT_FULL: 0 = 0

full version identifies the hashType

Const ADDRESS_FORMAT_FULLDATA

ADDRESS_FORMAT_FULLDATA: 2 = 2
deprecated

full version with hashType = "Data", deprecated

Const ADDRESS_FORMAT_FULLDATA

ADDRESS_FORMAT_FULLDATA: 2 = 2
deprecated

full version with hashType = "Data", deprecated

Const ADDRESS_FORMAT_FULLTYPE

ADDRESS_FORMAT_FULLTYPE: 4 = 4
deprecated

full version with hashType = "Type", deprecated

Const ADDRESS_FORMAT_FULLTYPE

ADDRESS_FORMAT_FULLTYPE: 4 = 4
deprecated

full version with hashType = "Type", deprecated

Const ADDRESS_FORMAT_SHORT

ADDRESS_FORMAT_SHORT: 1 = 1
deprecated

short version for locks with popular codeHash, deprecated

Const ADDRESS_FORMAT_SHORT

ADDRESS_FORMAT_SHORT: 1 = 1
deprecated

short version for locks with popular codeHash, deprecated

Const BECH32_LIMIT

BECH32_LIMIT: 1023 = 1023

Const BECH32_LIMIT

BECH32_LIMIT: 1023 = 1023

Let HAS_WARNED_FOR_DEPRECATED_ADDRESS

HAS_WARNED_FOR_DEPRECATED_ADDRESS: boolean = false

Const TransactionSkeleton

TransactionSkeleton: Factory<TransactionSkeletonInterface>

Const TransactionSkeleton

TransactionSkeleton: Factory<TransactionSkeletonInterface> = Record<TransactionSkeletonInterface>({cellProvider: null,cellDeps: List(),headerDeps: List(),inputs: List(),outputs: List(),witnesses: List(),fixedEntries: List(),signingEntries: List(),inputSinces: ImmutableMap(),})

Const addressToScript

addressToScript: typeof parseAddress

Const addressToScript

addressToScript: parseAddress = parseAddress

bytify

bytify: bytify

hexify

hexify: hexify

Const scriptToAddress

scriptToAddress: typeof generateAddress
deprecated

please migrate to encodeToAddress, the short format address will be removed in the future

Const scriptToAddress

scriptToAddress: generateAddress = generateAddress
deprecated

please migrate to encodeToAddress, the short format address will be removed in the future

Functions

byteArrayToHex

  • Parameters

    • a: number[]

    Returns HexString

byteArrayToHex

  • Parameters

    • a: number[]

    Returns HexString

createHasher

  • Parameters

    Returns Hasher

createTransactionFromSkeleton

  • Parameters

    • txSkeleton: TransactionSkeletonType
    • Optional __namedParameters: { validate: undefined | false | true }
      • validate: undefined | false | true

    Returns Transaction

createTransactionFromSkeleton

  • Parameters

    • txSkeleton: TransactionSkeletonType
    • Default value __namedParameters: { validate: boolean } = {}
      • validate: boolean

    Returns Transaction

createTransactionSkeleton

createTransactionSkeleton

encodeToAddress

  • parse a lock script to an address

    Parameters

    • script: Script
    • Optional __namedParameters: { config: undefined | Config }

    Returns Address

encodeToAddress

  • parse a lock script to an address

    Parameters

    • script: Script
    • Default value __namedParameters: { config: undefined | Config } = {}

    Returns Address

encodeToConfigAddress

  • Parameters

    Returns string

  • Type parameters

    Parameters

    • args: HexString
    • scriptType: keyof C["SCRIPTS"]
    • options: { config?: C }
      • Optional config?: C

    Returns string

encodeToConfigAddress

  • encode a script to an address with args and a key of config

    example
    // parse a predefined lock to an address
    encodeToConfigAddress('0x12345678123456781234567812345678', 'SECP256K1_BLAKE160');
    // parse a custom lock to an address
    encodeToConfigAddress('0x12345678123456781234567812345678', 'MY_CUSTOM_LOCK', {
      SCRIPTS: {
        MY_CUSTOM_LOCK: {...}
      }
    })

    Parameters

    Returns string

  • encode a script to an address with args and a key of config

    example
    // parse a predefined lock to an address
    encodeToConfigAddress('0x12345678123456781234567812345678', 'SECP256K1_BLAKE160');
    // parse a custom lock to an address
    encodeToConfigAddress('0x12345678123456781234567812345678', 'MY_CUSTOM_LOCK', {
      SCRIPTS: {
        MY_CUSTOM_LOCK: {...}
      }
    })

    Type parameters

    Parameters

    • args: HexString
    • scriptType: keyof C["SCRIPTS"]
    • options: { config?: C }
      • Optional config?: C

    Returns string

generateAddress

  • deprecated

    please migrate to encodeToAddress, the short format address will be removed in the future

    Parameters

    • script: Script
    • Optional __namedParameters: { config: undefined | Config }

    Returns Address

generateAddress

  • deprecated

    please migrate to encodeToAddress, the short format address will be removed in the future

    Parameters

    • script: Script
    • Default value __namedParameters: { config: undefined | Config } = {}

    Returns Address

generatePredefinedAddress

  • generatePredefinedAddress(args: HexString, scriptType: string, __namedParameters?: { config: undefined | Config }): Address
  • deprecated

    please migrate to encodeToConfigAddress

    Parameters

    • args: HexString
    • scriptType: string
    • Default value __namedParameters: { config: undefined | Config } = {}

    Returns Address

generateSecp256k1Blake160Address

  • generateSecp256k1Blake160Address(args: HexString, __namedParameters?: { config: undefined | Config }): Address

generateSecp256k1Blake160Address

  • generateSecp256k1Blake160Address(args: HexString, __namedParameters?: { config: undefined | Config }): Address

generateSecp256k1Blake160MultisigAddress

  • generateSecp256k1Blake160MultisigAddress(args: HexString, __namedParameters?: { config: undefined | Config }): Address

generateSecp256k1Blake160MultisigAddress

  • generateSecp256k1Blake160MultisigAddress(args: HexString, __namedParameters?: { config: undefined | Config }): Address

hexToByteArray

  • Parameters

    Returns number[]

hexToByteArray

  • Parameters

    Returns number[]

locateCellDep

  • locateCellDep(script: Script, __namedParameters?: { config: undefined | Config }): CellDep | null
  • Parameters

    • script: Script
    • Optional __namedParameters: { config: undefined | Config }

    Returns CellDep | null

locateCellDep

  • locateCellDep(script: Script, __namedParameters?: { config: undefined | Config }): CellDep | null
  • Parameters

    • script: Script
    • Default value __namedParameters: { config: undefined | Config } = {}

    Returns CellDep | null

minimalCellCapacity

  • minimalCellCapacity(fullCell: Cell, __namedParameters?: { validate: undefined | false | true }): bigint
  • Parameters

    • fullCell: Cell
    • Optional __namedParameters: { validate: undefined | false | true }
      • validate: undefined | false | true

    Returns bigint

minimalCellCapacity

  • minimalCellCapacity(fullCell: Cell, __namedParameters?: { validate: boolean }): bigint
  • Parameters

    • fullCell: Cell
    • Default value __namedParameters: { validate: boolean } = {}
      • validate: boolean

    Returns bigint

minimalCellCapacityCompatible

  • minimalCellCapacityCompatible(fullCell: Cell, __namedParameters?: { validate: undefined | false | true }): BI
  • Parameters

    • fullCell: Cell
    • Optional __namedParameters: { validate: undefined | false | true }
      • validate: undefined | false | true

    Returns BI

minimalCellCapacityCompatible

  • minimalCellCapacityCompatible(fullCell: Cell, __namedParameters?: { validate: boolean }): BI
  • Parameters

    • fullCell: Cell
    • Default value __namedParameters: { validate: boolean } = {}
      • validate: boolean

    Returns BI

minimalScriptCapacity

  • minimalScriptCapacity(script: Script, __namedParameters?: { validate: undefined | false | true }): bigint
  • Parameters

    • script: Script
    • Optional __namedParameters: { validate: undefined | false | true }
      • validate: undefined | false | true

    Returns bigint

minimalScriptCapacity

  • minimalScriptCapacity(script: Script, __namedParameters?: { validate: boolean }): bigint
  • Parameters

    • script: Script
    • Default value __namedParameters: { validate: boolean } = {}
      • validate: boolean

    Returns bigint

minimalScriptCapacityCompatible

  • minimalScriptCapacityCompatible(script: Script, __namedParameters?: { validate: undefined | false | true }): BI
  • Parameters

    • script: Script
    • Optional __namedParameters: { validate: undefined | false | true }
      • validate: undefined | false | true

    Returns BI

minimalScriptCapacityCompatible

  • minimalScriptCapacityCompatible(script: Script, __namedParameters?: { validate: boolean }): BI
  • Parameters

    • script: Script
    • Default value __namedParameters: { validate: boolean } = {}
      • validate: boolean

    Returns BI

objectToTransactionSkeleton

objectToTransactionSkeleton

parseAddress

  • Parameters

    • address: Address
    • Optional __namedParameters: { config: undefined | Config }

    Returns Script

parseAddress

  • Parameters

    • address: Address
    • Default value __namedParameters: { config: undefined | Config } = {}

    Returns Script

parseDeprecatedCkb2019Address

  • parseDeprecatedCkb2019Address(address: string, __namedParameters: { config: undefined | Config }): Script
  • Parameters

    • address: string
    • __namedParameters: { config: undefined | Config }

    Returns Script

parseDeprecatedCkb2019Address

  • parseDeprecatedCkb2019Address(address: string, __namedParameters: { config: undefined | Config }): Script
  • Parameters

    • address: string
    • __namedParameters: { config: undefined | Config }

    Returns Script

parseFullFormatAddress

  • parseFullFormatAddress(address: Address, __namedParameters: { config: undefined | Config }): Script
  • Parameters

    • address: Address
    • __namedParameters: { config: undefined | Config }

    Returns Script

parseFullFormatAddress

  • parseFullFormatAddress(address: Address, __namedParameters: { config: undefined | Config }): Script
  • Parameters

    • address: Address
    • __namedParameters: { config: undefined | Config }

    Returns Script

sealTransaction

sealTransaction

transactionSkeletonToObject

transactionSkeletonToObject

validateP2PKHMessage

  • Validate a P2PKH(Pay to public key hash) message

    Parameters

    • messagesForSigning: BytesLike

      the message digest for signing. means hash(rawTransaction | extraData).

    • rawTransaction: RawTransaction

      raw transaction object

    • hashContentExceptRawTx: BytesLike

      content to be hashed other than rawTransaction, is generally processed witness

    • Optional hashAlgorithm: HashAlgorithm

      hash algorithm for signing. Default is "ckb-blake2b-256"

    Returns boolean

    the validate result. unless all messages equals, it will return false.

validateP2PKHMessage

  • Validate a P2PKH(Pay to public key hash) message

    Parameters

    • messagesForSigning: BytesLike

      the message digest for signing. means hash(rawTransaction | extraData).

    • rawTransaction: RawTransaction

      raw transaction object

    • hashContentExceptRawTx: BytesLike

      content to be hashed other than rawTransaction, is generally processed witness

    • Default value hashAlgorithm: HashAlgorithm = "ckb-blake2b-256"

      hash algorithm for signing. Default is "ckb-blake2b-256"

    Returns boolean

    the validate result. unless all messages equals, it will return false.

Generated using TypeDoc