0.22.2 • Published 3 days ago

@ckb-lumos/common-scripts v0.22.2

Weekly downloads
28
License
MIT
Repository
github
Last release
3 days ago

@ckb-lumos/common-scripts

Common script implementation for lumos. Includes secp256k1_blake2b lock script, secp256k1_blake160_multisig lock script, dao type script, sudt type script now.

LocktimePool script includes secp256k1_blake160_multisig cells which with locktime in lock args (which args total length is 28 bytes, last 8 bytes is a since format locktime in BigUInt64LE encode) and DAO step2 cells.

common script allows you to transfer capacity from fromInfos to an address. It will use locktime pool cells first by default.

Usage

common script support new lock scripts provided by user, and pw-lock shows how to do it.

Following script will show how to use common script to transfer capacity to another address. secp256k1_blake160, secp256k1_blake160_multisig and locktime_pool script are similar to common, and common maybe a better choose.

const { common } = require('@ckb-lumos/common-scripts');
const { sealTransaction } = require("@ckb-lumos/helpers")
const { Indexer } = require("@ckb-lumos/indexer")

// We can use Indexer module as cell provider
const indexer = new Indexer("http://127.0.0.1:8114", "./indexer-data");

const tipHeader = {
  compact_target: '0x20010000',
  dao: '0x49bfb20771031d556c8480d47f2a290059f0ac7e383b6509006f4a772ed50200',
  epoch: '0xa0006002b18',
  hash: '0x432451e23c26f45eaceeedcc261764d6485ea5c9a204ac55ad755bb8dec9a079',
  nonce: '0x8199548f8a5ac7a0f0caef1620f37b79',
  number: '0x1aef6',
  parent_hash: '0x63594a64108f19f6aed53d0dca9ab4075aac4379cb80b2097b0deac8fc16fd3b',
  proposals_hash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  timestamp: '0x172f6b9a4cf',
  transactions_root: '0x282dbadcd49f3e229d997875f37f4e4f19cb4f04fcf762e9639145aaa667b6f8',
  extra_hash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  version: '0x0'
}

const fromInfos = [
  "ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
  {
    R: 0,
    M: 1,
    publicKeyHashes: ["0x36c329ed630d6ce750712a477543672adab57f4c"],
  },
]

let txSkeleton = TransactionSkeleton({ cellProvider: indexer })

// If using secp256k1_blake160_multisig lock script, put MultisigScript to `fromInfos` for generate signing messages.
// By default, `common.transfer` will use cells with locktime firstly. `tipHeader` is required when you want to spent cells with locktime.
txSkeleton = await common.transfer(
  txSkeleton,
  fromInfos,
  "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
  BigInt(3500 * 10 ** 8),
  tipHeader,
)

// Or you want to use cells without locktime firstly.
txSkeleton = await common.transfer(
  txSkeleton,
  fromInfos,
  "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
  BigInt(3500 * 10 ** 8),
  tipHeader,
  { useLocktimeCellsFirst: false }
)

// When you want to pay fee for transaction, just call `payFee`.
txSkeleton = await common.payFee(
  txSkeleton,
  fromInfos,
  BigInt(1*10**8),
  tipHeader,
)

// `prepareSigningEntries` will generate message for signing.
// Signing messages will fill in `txSkeleton.signingEntries`.
txSkeleton = await common.prepareSigningEntries(
  txSkeleton
)

// Then you can sign messages in order and get contents.
// NOTE: lumos not provided tools for generate signatures now.
// Call `sealTransaction` to get a transaction.
const tx = sealTransaction(txSkeleton, contents)

// Then you can send tx to a CKB node via RPC `send_transaction`.

Following script will show how to use DAO script.

const { dao } = require("@ckb-lumos/common-scripts")

let txSkeleton = TransactionSkeleton({ cellProvider: indexer })

// First, deposit capacity to dao.
txSkeleton = await dao.deposit(
  txSkeleton,
  "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd", // will gather inputs from this address.
  "ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs", // will generate a dao cell with lock of this address.
  BigInt(1000*10**8),
)

// Using `listDaoCells` to list all deposited cells.
const daoDepositedCells = await dao.listDaoCells(
  indexer,
  "ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
  "deposit",
)

// Or using `CellCollector`
const daoDepositedCellCollector = new dao.CellCollector(
  "ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
  indexer,
  "deposit",
)

for await (const inputCell of daoDepositedCellCollector.collect()) {
  console.log(inputCell)
}

// And pick one to withdraw.
// `fromInfo` only required for multisig script.
txSkeleton = await dao.withdraw(
  txSkeleton,
  daoDepositedCells[0],
)

// Then if want to unlock dao withdrew cells, just use `common.transfer`.

Following script will show how to use sUDT script.

const { sudt } = require("@ckb-lumos/common-scripts")
let txSkeleton = TransactionSkeleton({ cellProvider: indexer })

// issue an sudt token, will use the second param address to generate sudt token(it's lock hash).
txSkeleton = await sudt.issueToken(
  txSkeleton,
  "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
  10000n,
);

// and transfer sUDT
const sudtToken = "0x1f2615a8dde4e28ca736ff763c2078aff990043f4cbf09eb4b3a58a140a0862d"
txSkeleton = await sudt.transfer(
  txSkeleton,
  ["ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"],
  sudtToken,
  "ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
  1000n,
  "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
);
0.22.2

27 days ago

0.22.1

1 month ago

0.22.0

1 month ago

0.22.0-next.5

2 months ago

0.22.0-next.4

4 months ago

0.22.0-next.3

4 months ago

0.22.0-next.2

4 months ago

0.22.0-next.1

5 months ago

0.22.0-next.0

5 months ago

0.21.1

5 months ago

0.21.0

5 months ago

0.20.0

10 months ago

0.21.0-next.0

9 months ago

0.21.0-next.1

7 months ago

0.21.0-next.2

6 months ago

0.21.0-next.3

6 months ago

0.20.0-alpha.3

12 months ago

0.20.0-alpha.2

1 year ago

0.19.0

2 years ago

0.20.0-alpha.0

1 year ago

0.20.0-alpha.1

1 year ago

0.19.0-beta.0

2 years ago

0.19.0-alpha.3

2 years ago

0.19.0-alpha.2

2 years ago

0.19.0-alpha.1

2 years ago

0.17.0

2 years ago

0.19.0-alpha.0

2 years ago

0.18.0

2 years ago

0.18.0-rc7

2 years ago

0.18.0-rc6

2 years ago

0.17.0-rc9

2 years ago

0.17.0-rc10

2 years ago

0.17.0-rc7

2 years ago

0.17.0-rc6

2 years ago

0.18.0-rc5

2 years ago

0.18.0-rc3

2 years ago

0.18.0-rc4

2 years ago

0.17.0-rc8

2 years ago

0.18.0-rc2

2 years ago

0.18.0-rc1

3 years ago

0.17.0-rc5

3 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.14.2-rc6

3 years ago

0.14.2-rc5

3 years ago

0.14.2-rc4

3 years ago

0.14.2-rc3

3 years ago

0.14.2-rc2

3 years ago

0.14.2-rc1

3 years ago

0.14.1

3 years ago

0.14.0

3 years ago

0.13.3

3 years ago

0.13.2

3 years ago

0.13.1

4 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.11.0-rc2

4 years ago

0.11.0-rc1

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.4

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago