Skip to content

Commit

Permalink
refactor!: camelize exposed APIs (ckb-js#378)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: all exposed API are changed from snake-case to camel-case
  • Loading branch information
homura committed Aug 11, 2022
1 parent 635ca26 commit 7c17f90
Show file tree
Hide file tree
Showing 211 changed files with 17,593 additions and 11,540 deletions.
4 changes: 2 additions & 2 deletions examples/cardano-lock-namiwallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function App() {
setCardanoAddr(address);

const cardanoLock: Script = {
code_hash: CONFIG.SCRIPTS.CARDANO_LOCK.CODE_HASH,
hash_type: CONFIG.SCRIPTS.CARDANO_LOCK.HASH_TYPE,
codeHash: CONFIG.SCRIPTS.CARDANO_LOCK.CODE_HASH,
hashType: CONFIG.SCRIPTS.CARDANO_LOCK.HASH_TYPE,
args: address,
};
const ckbAddr = helpers.generateAddress(cardanoLock);
Expand Down
38 changes: 19 additions & 19 deletions examples/cardano-lock-namiwallet/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BI, Cell, config, core, helpers, Indexer, RPC, toolkit, utils, commons } from "@ckb-lumos/lumos";
import { bytes } from '@ckb-lumos/codec';
import { blockchain } from '@ckb-lumos/base';
import { BI, Cell, config, helpers, RPC, commons, Indexer } from "@ckb-lumos/lumos";
import {
COSESign1Builder,
HeaderMap,
Expand Down Expand Up @@ -83,7 +85,7 @@ export async function transfer(options: Options): Promise<string> {
const collectedCells: Cell[] = [];
const collector = indexer.collector({ lock: fromScript, type: "empty" });
for await (const cell of collector.collect()) {
collectedSum = collectedSum.add(cell.cell_output.capacity);
collectedSum = collectedSum.add(cell.cellOutput.capacity);
collectedCells.push(cell);
if (BI.from(collectedSum).gte(neededCapacity)) break;
}
Expand All @@ -93,15 +95,15 @@ export async function transfer(options: Options): Promise<string> {
}

const transferOutput: Cell = {
cell_output: {
cellOutput: {
capacity: BI.from(options.amount).toHexString(),
lock: toScript,
},
data: "0x",
};

const changeOutput: Cell = {
cell_output: {
cellOutput: {
capacity: collectedSum.sub(neededCapacity).toHexString(),
lock: fromScript,
},
Expand All @@ -112,11 +114,11 @@ export async function transfer(options: Options): Promise<string> {
tx = tx.update("outputs", (outputs) => outputs.push(transferOutput, changeOutput));
tx = tx.update("cellDeps", (cellDeps) =>
cellDeps.push({
out_point: {
tx_hash: CONFIG.SCRIPTS.CARDANO_LOCK.TX_HASH,
outPoint: {
txHash: CONFIG.SCRIPTS.CARDANO_LOCK.TX_HASH,
index: CONFIG.SCRIPTS.CARDANO_LOCK.INDEX,
},
dep_type: CONFIG.SCRIPTS.CARDANO_LOCK.DEP_TYPE,
depType: CONFIG.SCRIPTS.CARDANO_LOCK.DEP_TYPE,
})
);

Expand All @@ -132,25 +134,25 @@ export async function transfer(options: Options): Promise<string> {
let builder = COSESign1Builder.new(headers, Buffer.from(payload, "hex"), false);
let toSign = builder.make_data_to_sign().to_bytes();

const placeHolder = new toolkit.Reader(
const placeHolder = (
"0x" +
"00".repeat(
SerializeCardanoWitnessLock({
pubkey: new toolkit.Reader("0x" + "00".repeat(32)).toArrayBuffer(),
signature: new toolkit.Reader("0x" + "00".repeat(64)).toArrayBuffer(),
pubkey: new Uint8Array(32),
signature: new Uint8Array(64),
sig_structure: toSign.buffer,
}).byteLength
)
);

const tmpWitnessArgs = core.SerializeWitnessArgs(toolkit.normalizers.NormalizeWitnessArgs({ lock: placeHolder }));
const witness = new toolkit.Reader(tmpWitnessArgs).serializeJson();
const tmpWitnessArgs = blockchain.WitnessArgs.pack({ lock: placeHolder });
const witness = bytes.hexify(tmpWitnessArgs);

for (let i = 0; i < tx.inputs.toArray().length; i++) {
tx = tx.update("witnesses", (witnesses) => witnesses.push(witness));
}

const signLock = tx.inputs.get(0)?.cell_output.lock!;
const signLock = tx.inputs.get(0)?.cellOutput.lock!;
const messageGroup = commons.createP2PKHMessageGroup(tx, [signLock]);
const messageForSigning = messageGroup[0].message.slice(2);

Expand All @@ -166,21 +168,19 @@ 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 = new toolkit.Reader(
const signedWitnessArgs = bytes.hexify(
SerializeCardanoWitnessLock({
pubkey: CBORPubkey.as_bytes()!.buffer,
signature: COSESignature.signature().buffer,
sig_structure: toSign.buffer,
})
);

const signedWitness = new toolkit.Reader(
core.SerializeWitnessArgs(toolkit.normalizers.NormalizeWitnessArgs({ lock: signedWitnessArgs }))
).serializeJson();
const signedWitness = bytes.hexify(blockchain.WitnessArgs.pack({ lock: signedWitnessArgs }))
tx = tx.update("witnesses", (witnesses) => witnesses.set(0, signedWitness));

const signedTx = helpers.createTransactionFromSkeleton(tx);
const txHash = await rpc.send_transaction(signedTx, "passthrough");
const txHash = await rpc.sendTransaction(signedTx, "passthrough");

return txHash;
}
Expand All @@ -192,7 +192,7 @@ export async function capacityOf(address: string): Promise<BI> {

let balance = BI.from(0);
for await (const cell of collector.collect()) {
balance = balance.add(cell.cell_output.capacity);
balance = balance.add(cell.cellOutput.capacity);
}

return balance;
Expand Down
6 changes: 3 additions & 3 deletions examples/ckb-indexer-collector.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// This example shows how to use the CkbIndexer to collect cells

import { Script, Indexer as CkbIndexer, helpers, config, BI } from "@ckb-lumos/lumos";
import { Script, Indexer, helpers, config, BI } from "@ckb-lumos/lumos";

config.initializeConfig(config.predefined.AGGRON4);

const CKB_RPC_URL = "https://testnet.ckb.dev/rpc";
const CKB_INDEXER_URL = "https://testnet.ckb.dev/indexer";
const indexer = new CkbIndexer(CKB_INDEXER_URL, CKB_RPC_URL);
const indexer = new Indexer(CKB_INDEXER_URL, CKB_RPC_URL);

async function capacityOf(lock: Script): Promise<BI> {
const collector = indexer.collector({ lock });

let balance: BI = BI.from(0);
for await (const cell of collector.collect()) {
balance = balance.add(cell.cell_output.capacity);
balance = balance.add(cell.cellOutput.capacity);
}

return balance;
Expand Down
4 changes: 2 additions & 2 deletions examples/config-manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { config, helpers } from "@ckb-lumos/lumos";

const script = {
code_hash: config.predefined.AGGRON4.SCRIPTS.SECP256K1_BLAKE160.CODE_HASH,
hash_type: config.predefined.AGGRON4.SCRIPTS.SECP256K1_BLAKE160.HASH_TYPE,
codeHash: config.predefined.AGGRON4.SCRIPTS.SECP256K1_BLAKE160.CODE_HASH,
hashType: config.predefined.AGGRON4.SCRIPTS.SECP256K1_BLAKE160.HASH_TYPE,
args: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
};

Expand Down
4 changes: 2 additions & 2 deletions examples/omni-lock-metamask/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export function App() {
.enable()
.then(([ethAddr]: string[]) => {
const omniLock: Script = {
code_hash: CONFIG.SCRIPTS.OMNI_LOCK.CODE_HASH,
hash_type: CONFIG.SCRIPTS.OMNI_LOCK.HASH_TYPE,
codeHash: CONFIG.SCRIPTS.OMNI_LOCK.CODE_HASH,
hashType: CONFIG.SCRIPTS.OMNI_LOCK.HASH_TYPE,
// omni flag pubkey hash omni lock flags
// chain identity eth addr function flag()
// 00: Nervos 👇 00: owner
Expand Down
50 changes: 24 additions & 26 deletions examples/omni-lock-metamask/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BI, Cell, config, core, helpers, Indexer, RPC, toolkit, utils, commons } from "@ckb-lumos/lumos";
import { bytes } from '@ckb-lumos/codec';
import { blockchain } from '@ckb-lumos/base';
import { BI, Cell, config, helpers, Indexer, RPC, utils, commons } from "@ckb-lumos/lumos";
import { SerializeRcLockWitnessLock } from "./generated/omni";

export const CONFIG = config.createConfig({
Expand Down Expand Up @@ -63,7 +65,7 @@ export async function transfer(options: Options): Promise<string> {
const collectedCells: Cell[] = [];
const collector = indexer.collector({ lock: fromScript, type: "empty" });
for await (const cell of collector.collect()) {
collectedSum = collectedSum.add(cell.cell_output.capacity);
collectedSum = collectedSum.add(cell.cellOutput.capacity);
collectedCells.push(cell);
if (BI.from(collectedSum).gte(neededCapacity)) break;
}
Expand All @@ -73,15 +75,15 @@ export async function transfer(options: Options): Promise<string> {
}

const transferOutput: Cell = {
cell_output: {
cellOutput: {
capacity: BI.from(options.amount).toHexString(),
lock: toScript,
},
data: "0x",
};

const changeOutput: Cell = {
cell_output: {
cellOutput: {
capacity: collectedSum.sub(neededCapacity).toHexString(),
lock: fromScript,
},
Expand All @@ -94,51 +96,49 @@ export async function transfer(options: Options): Promise<string> {
cellDeps.push(
// omni lock dep
{
out_point: {
tx_hash: CONFIG.SCRIPTS.OMNI_LOCK.TX_HASH,
outPoint: {
txHash: CONFIG.SCRIPTS.OMNI_LOCK.TX_HASH,
index: CONFIG.SCRIPTS.OMNI_LOCK.INDEX,
},
dep_type: CONFIG.SCRIPTS.OMNI_LOCK.DEP_TYPE,
depType: CONFIG.SCRIPTS.OMNI_LOCK.DEP_TYPE,
},
// SECP256K1 lock is depended by omni lock
{
out_point: {
tx_hash: CONFIG.SCRIPTS.SECP256K1_BLAKE160.TX_HASH,
outPoint: {
txHash: CONFIG.SCRIPTS.SECP256K1_BLAKE160.TX_HASH,
index: CONFIG.SCRIPTS.SECP256K1_BLAKE160.INDEX,
},
dep_type: CONFIG.SCRIPTS.SECP256K1_BLAKE160.DEP_TYPE,
depType: CONFIG.SCRIPTS.SECP256K1_BLAKE160.DEP_TYPE,
}
)
);

const messageForSigning = (() => {
const hasher = new utils.CKBHasher();

const SECP_SIGNATURE_PLACEHOLDER = new toolkit.Reader(
const SECP_SIGNATURE_PLACEHOLDER = (
"0x" +
"00".repeat(
SerializeRcLockWitnessLock({
signature: new toolkit.Reader("0x" + "00".repeat(65)),
signature: new Uint8Array(65),
}).byteLength
)
);
const newWitnessArgs = { lock: SECP_SIGNATURE_PLACEHOLDER };
const witness = new toolkit.Reader(
core.SerializeWitnessArgs(toolkit.normalizers.NormalizeWitnessArgs(newWitnessArgs))
).serializeJson();
const witness = bytes.hexify(blockchain.WitnessArgs.pack(newWitnessArgs))

// fill txSkeleton's witness with 0
for (let i = 0; i < tx.inputs.toArray().length; i++) {
tx = tx.update("witnesses", (witnesses) => witnesses.push(witness));
}

// locks you want to sign
const signLock = tx.inputs.get(0)?.cell_output.lock!;
const signLock = tx.inputs.get(0)?.cellOutput.lock!;

const messageGroup = commons.createP2PKHMessageGroup(tx, [signLock], {
hasher: {
update: (message) => hasher.update(message.buffer),
digest: () => new Uint8Array(hasher.digestReader().toArrayBuffer()),
digest: () => new Uint8Array(bytes.bytify(hasher.digestHex())),
},
});

Expand All @@ -154,18 +154,16 @@ 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 = new toolkit.Reader(
core.SerializeWitnessArgs({
lock: SerializeRcLockWitnessLock({
signature: new toolkit.Reader(signedMessage),
}),
})
).serializeJson();
const signedWitness = bytes.hexify(blockchain.WitnessArgs.pack({
lock: bytes.hexify( SerializeRcLockWitnessLock({
signature: bytes.bytify(signedMessage),
})),
}))

tx = tx.update("witnesses", (witnesses) => witnesses.set(0, signedWitness));

const signedTx = helpers.createTransactionFromSkeleton(tx);
const txHash = await rpc.send_transaction(signedTx, "passthrough");
const txHash = await rpc.sendTransaction(signedTx, "passthrough");

return txHash;
}
Expand All @@ -177,7 +175,7 @@ export async function capacityOf(address: string): Promise<BI> {

let balance = BI.from(0);
for await (const cell of collector.collect()) {
balance = balance.add(cell.cell_output.capacity);
balance = balance.add(cell.cellOutput.capacity);
}

return balance;
Expand Down
4 changes: 2 additions & 2 deletions examples/omni-lock-secp256k1-blake160/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export function App() {
const pubkeyHash = key.privateKeyToBlake160(pk);

const omniLock: Script = {
code_hash: CONFIG.SCRIPTS.OMNI_LOCK.CODE_HASH,
hash_type: CONFIG.SCRIPTS.OMNI_LOCK.HASH_TYPE,
codeHash: CONFIG.SCRIPTS.OMNI_LOCK.CODE_HASH,
hashType: CONFIG.SCRIPTS.OMNI_LOCK.HASH_TYPE,
// omni flag pubkey hash omni lock flags
// chain identity eth addr function flag()
// 00: Nervos 👇 00: owner
Expand Down
Loading

0 comments on commit 7c17f90

Please sign in to comment.