Skip to content

Commit

Permalink
refactor!: make @ckb-lumos/lumos more convenient (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Dec 20, 2023
1 parent 6940207 commit 00c3035
Show file tree
Hide file tree
Showing 55 changed files with 626 additions and 1,439 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-pots-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/lumos": minor
---

BREAKING CHANGE: refine the export structure
48 changes: 0 additions & 48 deletions .github/workflows/canary-ckb2023.yml

This file was deleted.

24 changes: 24 additions & 0 deletions devtools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Devtools

## Mock Publication

If the release workflow is changed, it is important to test the new workflow. This can be done by publishing to a local registry. The following steps will setup a local registry and configure npm to use it.

```sh
npm install -g verdaccio
verdaccio --config ./devtools/verdaccio.yaml --listen=4873
npm config set registry http://localhost:4873
npm config set //localhost:4873/:_authToken fake
```

To test the release workflow on GitHub, the following step can be added to the workflow file.

```yaml
# TODO remove me when ready
- name: Release to Verdaccio
run: |
npm install -g verdaccio
npm config set registry http://localhost:4873
npm config set //localhost:4873/:_authToken fake
nohup verdaccio --config ./devtools/verdaccio.yaml --listen=4873 >/dev/null 2>&1 &
```
10 changes: 4 additions & 6 deletions examples/cardano-lock-namiwallet/CardanoWitnessLock.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { blockchain } from "@ckb-lumos/base";
import { molecule } from "@ckb-lumos/codec";
import { blockchain, table } from "@ckb-lumos/lumos/codec";

const { Byte32, Bytes } = blockchain;
const Byte64 = blockchain.createFixedHexBytesCodec(64);

export const CardanoWitnessLock = molecule.table(
export const CardanoWitnessLock = table(
{
pubkey: Byte32,
pubkey: blockchain.Byte32,
signature: Byte64,
sig_structure: Bytes,
sig_structure: blockchain.Bytes,
},
["pubkey", "signature", "sig_structure"]
);
9 changes: 4 additions & 5 deletions examples/cardano-lock-namiwallet/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bytes } from "@ckb-lumos/codec";
import { blockchain } from "@ckb-lumos/base";
import { hexify, blockchain } from "@ckb-lumos/lumos/codec";
import { BI, Cell, config, helpers, RPC, commons, Indexer } from "@ckb-lumos/lumos";
import {
COSESign1Builder,
Expand Down Expand Up @@ -144,7 +143,7 @@ export async function transfer(options: Options): Promise<string> {
);

const tmpWitnessArgs = blockchain.WitnessArgs.pack({ lock: placeHolder });
const witness = bytes.hexify(tmpWitnessArgs);
const witness = hexify(tmpWitnessArgs);

for (let i = 0; i < tx.inputs.toArray().length; i++) {
tx = tx.update("witnesses", (witnesses) => witnesses.push(witness));
Expand All @@ -166,15 +165,15 @@ export async function transfer(options: Options): Promise<string> {
const label = Label.new_int(Int.new_negative(BigNum.from_str("2")));
const CBORPubkey = signedKey.header(label)!;

const signedWitnessArgs = bytes.hexify(
const signedWitnessArgs = hexify(
CardanoWitnessLock.pack({
pubkey: CBORPubkey.as_bytes()!.buffer,
signature: COSESignature.signature().buffer,
sig_structure: toSign.buffer,
})
);

const signedWitness = bytes.hexify(blockchain.WitnessArgs.pack({ lock: signedWitnessArgs }));
const signedWitness = hexify(blockchain.WitnessArgs.pack({ lock: signedWitnessArgs }));
tx = tx.update("witnesses", (witnesses) => witnesses.set(0, signedWitness));

const signedTx = helpers.createTransactionFromSkeleton(tx);
Expand Down
5 changes: 4 additions & 1 deletion examples/cardano-lock-namiwallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
"author": "",
"license": "MIT",
"dependencies": {
"@ckb-lumos/lumos": "next",
"@ckb-lumos/lumos": "canary",
"@emurgo/cardano-message-signing-asmjs": "^1.0.1",
"@emurgo/cardano-serialization-lib-asmjs": "^10.0.4",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"parcel": "^2.9.3"
}
}
4 changes: 0 additions & 4 deletions examples/exchange-sudt-for-ckb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ckb-lumos/base": "canary",
"@ckb-lumos/bi": "canary",
"@ckb-lumos/codec": "canary",
"@ckb-lumos/common-scripts": "canary",
"@ckb-lumos/lumos": "canary",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
Expand Down
31 changes: 15 additions & 16 deletions examples/exchange-sudt-for-ckb/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Indexer, helpers, Address, Script, RPC, hd, config, Cell, commons, BI } from "@ckb-lumos/lumos";
import { sudt } from "@ckb-lumos/common-scripts";
import { BIish } from "@ckb-lumos/bi";
import { payFeeByFeeRate } from "@ckb-lumos/common-scripts/lib/common";
import { addCellDep } from "@ckb-lumos/common-scripts/lib/helper";
import { Indexer, helpers, Address, Script, RPC, hd, config, Cell, commons, BI, BIish } from "@ckb-lumos/lumos";
import { sudt, common } from "@ckb-lumos/lumos/common-scripts";
import { addCellDep } from "@ckb-lumos/lumos/helpers";
import { hexify, Uint128, blockchain } from "@ckb-lumos/lumos/codec";
import { computeScriptHash } from "@ckb-lumos/lumos/utils";
import { List } from "immutable";
import { computeScriptHash } from "@ckb-lumos/base/lib/utils";
import { bytes } from "@ckb-lumos/codec";
import { blockchain } from "@ckb-lumos/base";

export const { AGGRON4 } = config.predefined;
const CKB_RPC_URL = "https://testnet.ckb.dev/rpc";
Expand Down Expand Up @@ -53,7 +50,7 @@ export const issueSUDT = async (privateKey: string) => {
config: AGGRON4,
});

txSkeleton = await payFeeByFeeRate(txSkeleton, [account.address], 1000, undefined, { config: AGGRON4 });
txSkeleton = await common.payFeeByFeeRate(txSkeleton, [account.address], 1000, undefined, { config: AGGRON4 });
const tx = signTx(txSkeleton, [privateKey]);

const txHash = await rpc.sendTransaction(tx, "passthrough");
Expand Down Expand Up @@ -122,7 +119,7 @@ export async function transferCKB2SUDT(issuerPrivateKey: string, holderPrivateKe
break;
}
inputs = inputs.push(cell);
total = total.add(sudt.unpackAmount(cell.data));
total = total.add(Uint128.unpack(cell.data));
}
return inputs;
});
Expand All @@ -146,7 +143,7 @@ export async function transferCKB2SUDT(issuerPrivateKey: string, holderPrivateKe
lock: holderAccountInfo.lockScript,
type: issuerTypeScript,
},
data: sudt.packAmount(SUDTAmount),
data: hexify(Uint128.pack(SUDTAmount)),
};

console.log(calculateSUDTAmountSum(issuerSUDTCells).toBigInt());
Expand All @@ -157,7 +154,7 @@ export async function transferCKB2SUDT(issuerPrivateKey: string, holderPrivateKe
lock: issuerAccountInfo.lockScript,
type: issuerTypeScript,
},
data: sudt.packAmount(calculateSUDTAmountSum(issuerSUDTCells).sub(SUDTAmount)),
data: hexify(Uint128.pack(calculateSUDTAmountSum(issuerSUDTCells).sub(SUDTAmount))),
};

SUDTTargetOutput.cellOutput.capacity = BI.from(helpers.minimalCellCapacity(SUDTTargetOutput)).toHexString();
Expand Down Expand Up @@ -238,7 +235,7 @@ export async function transferCKB2SUDT(issuerPrivateKey: string, holderPrivateKe
lock: "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
};

const witnessArgs = bytes.hexify(blockchain.WitnessArgs.pack(placeholderWitness));
const witnessArgs = hexify(blockchain.WitnessArgs.pack(placeholderWitness));

for (let index = 0; index < txSkeleton.inputs.size; index++) {
witnesses = witnesses.push(witnessArgs);
Expand All @@ -248,7 +245,9 @@ export async function transferCKB2SUDT(issuerPrivateKey: string, holderPrivateKe
});

console.log(txSkeleton.toJS());
txSkeleton = await payFeeByFeeRate(txSkeleton, [holderAccountInfo.address], 1000, undefined, { config: AGGRON4 });
txSkeleton = await common.payFeeByFeeRate(txSkeleton, [holderAccountInfo.address], 1000, undefined, {
config: AGGRON4,
});

// STEP2: sign the transaction skeleton
const tx = signTx(txSkeleton, [issuerPrivateKey, holderPrivateKey, issuerPrivateKey]);
Expand All @@ -274,7 +273,7 @@ export function calculateSUDTAmountSum(cells: Cell[]) {
let amount = BI.from(0);
for (const cell of cells) {
if (cell.cellOutput.type?.codeHash === AGGRON4.SCRIPTS.SUDT.CODE_HASH) {
amount = amount.add(sudt.unpackAmount(cell.data));
amount = amount.add(Uint128.unpack(cell.data));
}
}

Expand Down Expand Up @@ -310,7 +309,7 @@ export async function fetchSUDTBalance(address: string, issuerLockScript: Script
let amount = BI.from(0);

for await (const cell of collector.collect()) {
amount = amount.add(sudt.unpackAmount(cell.data));
amount = amount.add(Uint128.unpack(cell.data));
}
return amount;
}
Expand Down
14 changes: 14 additions & 0 deletions examples/misc/codec-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This example shows how to create a codec for UTF-8 strings.

import { createBytesCodec } from "@ckb-lumos/lumos/codec";

const stringCodec = createBytesCodec<string>({
pack: (str) => new TextEncoder().encode(str),
unpack: (bytes) => new TextDecoder().decode(bytes),
});

const encoded = stringCodec.pack("hello world");
const decoded = stringCodec.unpack(encoded);

console.log("encoded:", encoded);
console.log("decoded:", decoded);
2 changes: 1 addition & 1 deletion examples/misc/devnet-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// it does NOT contain all common configs, such as Anyone-can-pay, Omnilock, etc. that are deployed after genesis.

import { commons, config, hd, helpers, Indexer, RPC } from "@ckb-lumos/lumos";
import { parseUnit } from "@ckb-lumos/bi";
import { parseUnit } from "@ckb-lumos/lumos/utils";

const debug = (...args: unknown[]) => console.log(new Date().toJSON(), "[info]", ...args);

Expand Down
1 change: 0 additions & 1 deletion examples/misc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@ckb-lumos/bi": "canary",
"@ckb-lumos/lumos": "canary"
}
}
12 changes: 6 additions & 6 deletions examples/omni-lock-metamask/lib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytes } from "@ckb-lumos/codec";
import { blockchain } from "@ckb-lumos/base";
import { BI, Cell, helpers, Indexer, RPC, config, commons } from "@ckb-lumos/lumos";
import { blockchain, bytify, hexify } from "@ckb-lumos/lumos/codec";

const CKB_RPC_URL = "https://testnet.ckb.dev/rpc";
const rpc = new RPC(CKB_RPC_URL);
const indexer = new Indexer(CKB_RPC_URL);
Expand Down Expand Up @@ -30,7 +30,7 @@ interface Options {
to: string;
amount: string;
}
const SECP_SIGNATURE_PLACEHOLDER = bytes.hexify(
const SECP_SIGNATURE_PLACEHOLDER = hexify(
new Uint8Array(
commons.omnilock.OmnilockWitnessLock.pack({
signature: new Uint8Array(65).buffer,
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function transfer(options: Options): Promise<string> {
)
);

const witness = bytes.hexify(blockchain.WitnessArgs.pack({ lock: SECP_SIGNATURE_PLACEHOLDER }));
const witness = hexify(blockchain.WitnessArgs.pack({ lock: SECP_SIGNATURE_PLACEHOLDER }));

// fill txSkeleton's witness with placeholder
for (let i = 0; i < tx.inputs.toArray().length; i++) {
Expand All @@ -118,10 +118,10 @@ export async function transfer(options: Options): Promise<string> {
if (v >= 27) v -= 27;
signedMessage = "0x" + signedMessage.slice(2, -2) + v.toString(16).padStart(2, "0");

const signedWitness = bytes.hexify(
const signedWitness = hexify(
blockchain.WitnessArgs.pack({
lock: commons.omnilock.OmnilockWitnessLock.pack({
signature: bytes.bytify(signedMessage).buffer,
signature: bytify(signedMessage).buffer,
}),
})
);
Expand Down
4 changes: 1 addition & 3 deletions examples/omni-lock-metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@ckb-lumos/base": "next",
"@ckb-lumos/codec": "next",
"@ckb-lumos/lumos": "next",
"@ckb-lumos/lumos": "canary",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"react": "18.2.0",
Expand Down
Loading

2 comments on commit 00c3035

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-00c3035-20231220025235

npm install @ckb-lumos/[email protected]

@vercel
Copy link

@vercel vercel bot commented on 00c3035 Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.