@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.
deploy
script provides generateDeployWithDataTx
, generateDeployWithTypeIdTx
and generateUpgradeTypeIdDataTx
, these generators help in the process of deploying contracts.
p2pkh
scripts generates message for signing P2PKH transaction.
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/ckb-indexer")
const indexer = new Indexer("http://127.0.0.1:8114");
const tipHeader = {
compactTarget: '0x20010000',
dao: '0x49bfb20771031d556c8480d47f2a290059f0ac7e383b6509006f4a772ed50200',
epoch: '0xa0006002b18',
hash: '0x432451e23c26f45eaceeedcc261764d6485ea5c9a204ac55ad755bb8dec9a079',
nonce: '0x8199548f8a5ac7a0f0caef1620f37b79',
number: '0x1aef6',
parentHash: '0x63594a64108f19f6aed53d0dca9ab4075aac4379cb80b2097b0deac8fc16fd3b',
proposalsHash: '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 })
txSkeleton = await common.transfer(
txSkeleton,
fromInfos,
"ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
BigInt(3500 * 10 ** 8),
tipHeader,
)
txSkeleton = await common.transfer(
txSkeleton,
fromInfos,
"ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
BigInt(3500 * 10 ** 8),
tipHeader,
{ useLocktimeCellsFirst: false }
)
txSkeleton = await common.payFee(
txSkeleton,
fromInfos,
BigInt(1*10**8),
tipHeader,
)
txSkeleton = await common.prepareSigningEntries(
txSkeleton
)
const tx = sealTransaction(txSkeleton, contents)
Following script will show how to use DAO
script.
const { dao } = require("@ckb-lumos/common-scripts")
let txSkeleton = TransactionSkeleton({ cellProvider: indexer })
txSkeleton = await dao.deposit(
txSkeleton,
"ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
"ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
BigInt(1000*10**8),
)
const daoDepositedCells = await dao.listDaoCells(
indexer,
"ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
"deposit",
)
const daoDepositedCellCollector = new dao.CellCollector(
"ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
indexer,
"deposit",
)
for await (const inputCell of daoDepositedCellCollector.collect()) {
console.log(inputCell)
}
txSkeleton = await dao.withdraw(
txSkeleton,
daoDepositedCells[0],
)
Following script will show how to use sUDT
script.
const { sudt } = require("@ckb-lumos/common-scripts")
let txSkeleton = TransactionSkeleton({ cellProvider: indexer })
txSkeleton = await sudt.issueToken(
txSkeleton,
"ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
10000n,
);
const sudtToken = "0x1f2615a8dde4e28ca736ff763c2078aff990043f4cbf09eb4b3a58a140a0862d"
txSkeleton = await sudt.transfer(
txSkeleton,
["ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"],
sudtToken,
"ckb1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gq5f9mxs",
1000n,
"ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd",
);
Following script will show how to use deploy
script.
const { generateDeployWithDataTx, generateDeployWithTypeIdTx, generateUpgradeTypeIdDataTx, payFee } = require("@ckb-lumos/common-scripts");
const { Indexer } = require("@ckb-lumos/ckb-indexer");
const { initializeConfig, predefined } = require("@ckb-lumos/config-manager");
const { parseAddress } = require("@ckb-lumos/helpers");
initializeConfig(predefined.AGGRON4);
const CKB_RPC_URL = "http://localhost:8114";
const CKB_INDEXER_URL = "http://localhost:8116";
const indexer = new Indexer(CKB_INDEXER_URL, CKB_RPC_URL);
const address = "ckt1qyqptxys5l9vk39ft0hswscxgseawc77y2wqlr558h";
const outputScriptLock = parseAddress(address);
const scriptBinary = Uint8Array.of(1);
let deployOptions = {
cellProvider: indexer,
scriptBinary: scriptBinary,
outputScriptLock: outputScriptLock,
}
let txSkeleton = await generateDeployWithDataTx(deployOptions);
let txSkeleton = await generateDeployWithTypeIdTx(deployOptions);
txSkeleton = await payFee(txSkeleton, address, txFee);
const typeId = {
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
hashType: 'type',
args: '0x7abcd9f949a16b40ff5b50b56e62d2a6a007e544d8491bb56476693b6c45fd27'
}
const upgradeOptions = {
cellProvider: cellProvider,
scriptBinary: scriptBinary,
outputScriptLock: outputScriptLock,
typeId: typeId
}
let upgradeTxSkeleton = await generateUpgradeTypeIdDataTx(upgradeOptions);
Check Omnilock example and pw lock example for how to use p2pkh
script.