Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports sighashall and sighashallonly for cobuild #661

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tricky-ties-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ckb-lumos/common-scripts": minor
"@ckb-lumos/cobuild": minor
---

feat: support `SighashAll` and `SighashAllOnly` variant for cobuild
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/test_cases.js
**/test_cases.js
**/generated/**/*
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const scopeEnumValues = [
"runner",
"e2e-test",
"molecule",
"cobuild",
];
const Configuration = {
extends: ["@commitlint/config-conventional"],
Expand Down
18 changes: 18 additions & 0 deletions examples/cobuild-sighash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# CoBuild Sighash Example

This code example demonstrates how to use `@ckb-lumos/cobuild` with a lock that supports the CoBuild protocol. We will be working with a fake schema for the sUDT script to mint sUDT through a human-readable signing message.
Copy link
Member

Choose a reason for hiding this comment

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

It's better to demonstrate it with xUDT because it's the mainly promoted token protocol

Copy link
Member

Choose a reason for hiding this comment

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

Any update on this conversation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will be going on, but the launch of CoBuild will be postponed and it's expected to change, so the PR has been slowed down

Copy link
Member

Choose a reason for hiding this comment

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

Got it


```
union SudtActions {
Mint,
}

table Mint {
to: Script,
amount: Uint128,
}
```

## Links

- [CoBuild](https://talk.nervos.org/t/ckb-transaction-cobuild-protocol-overview/7702)
13 changes: 13 additions & 0 deletions examples/cobuild-sighash/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lumos with Omni Lock</title>
</head>
<body>
<div id="root"></div>
<script src="index.tsx" type="module"></script>
</body>
</html>
91 changes: 91 additions & 0 deletions examples/cobuild-sighash/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import { commons, helpers, Script } from "@ckb-lumos/lumos";
import { asyncSleep, capacityOf, ethereum, mintSudt } from "./lib";

function App() {
const [ethAddr, setEthAddr] = useState("");
const [omniAddr, setOmniAddr] = useState("");
const [omniLock, setOmniLock] = useState<Script>();
const [balance, setBalance] = useState("-");

const [mintAddr, setMintAddress] = useState("");
const [mintAmount, setMintAmount] = useState("");

const [isSendingTx, setIsSendingTx] = useState(false);
const [txHash, setTxHash] = useState("");

useEffect(() => {
asyncSleep(100).then(() => {
if (ethereum.selectedAddress) connectToMetaMask();
ethereum.addListener("accountsChanged", connectToMetaMask);
});
}, []);

function connectToMetaMask() {
ethereum
.enable()
.then(([ethAddr]: string[]) => {
const omniLockScript = commons.omnilock.createOmnilockScript({ auth: { flag: "ETHEREUM", content: ethAddr } });

const omniAddr = helpers.encodeToAddress(omniLockScript);

setEthAddr(ethAddr);
setOmniAddr(omniAddr);
setOmniLock(omniLockScript);

return omniAddr;
})
.then((omniAddr) => capacityOf(omniAddr))
.then((balance) => setBalance(balance.div(10 ** 8).toString() + " CKB"));
}

function onMint() {
if (isSendingTx) return;
setIsSendingTx(true);

mintSudt({ amount: mintAmount, from: omniAddr, to: mintAddr })
.then(setTxHash)
.catch((e) => {
console.log(e);
alert(e.message || JSON.stringify(e));
})
.finally(() => setIsSendingTx(false));
}

if (!ethereum) return <div>MetaMask is not installed</div>;
if (!ethAddr) return <button onClick={connectToMetaMask}>Connect to MetaMask</button>;

return (
<div>
<ul>
<li>Ethereum Address: {ethAddr}</li>
<li>Nervos Address(Omni): {omniAddr}</li>
<li>
Current Omni lock script:
<pre>{JSON.stringify(omniLock, null, 2)}</pre>
</li>

<li>Balance: {balance}</li>
</ul>

<div>
<h2>Mint token to</h2>
<label htmlFor="address">Address</label>&nbsp;
<input id="address" type="text" onChange={(e) => setMintAddress(e.target.value)} placeholder="ckt1..." />
<br />
<label htmlFor="amount">Amount</label>
&nbsp;
<input id="amount" type="text" onChange={(e) => setMintAmount(e.target.value)} placeholder="shannon" />
<br />
<button onClick={onMint} disabled={isSendingTx}>
Mint
</button>
<p>Tx Hash: {txHash}</p>
</div>
</div>
);
}

const app = document.getElementById("root");
ReactDOM.render(<App />, app);
164 changes: 164 additions & 0 deletions examples/cobuild-sighash/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { BI, helpers, Indexer, RPC, config, Cell, CellDep, Script } from "@ckb-lumos/lumos";
import { Config } from "@ckb-lumos/lumos/config";
import { addCellDep, minimalCellCapacityCompatible, TransactionSkeleton } from "@ckb-lumos/lumos/helpers";
import { computeScriptHash } from "@ckb-lumos/lumos/utils";
import { AnyCodec, blockchain, bytes, table, Uint128, union, UnpackResult } from "@ckb-lumos/lumos/codec";
import { injectCapacity, payFee } from "@ckb-lumos/lumos/common-scripts/common";
import { prepareCobuildSighashSigningEntries, sealTransaction } from "@ckb-lumos/cobuild";
import { cobuild } from "@ckb-lumos/lumos/common-scripts/omnilock";

export const CONFIG: Config = {
PREFIX: config.TESTNET.PREFIX,
SCRIPTS: {
...config.TESTNET.SCRIPTS,
// TODO remove it when the testnet omnilock is updated
OMNILOCK: {
TX_HASH: "0x042485f2b1386f3156a1585de6fe38e3c866ffb5acbcea6cab61a37b9780e7b1",
HASH_TYPE: "type",
CODE_HASH: "0xc039461134d79c87929eca28cb89261ec3f66cc2b5f562063da863330870598b",
DEP_TYPE: "code",
INDEX: "0x0",
},
},
};

config.initializeConfig(CONFIG);

/**
* a helper function to enhance readability for the unpacked result of codecs
* @param original
* @param enhance
*/
function readable<T extends AnyCodec>(original: T, enhance: (value: UnpackResult<T>) => any): AnyCodec {
return {
...original,
unpack: (value) => enhance(original.unpack(value)),
};
}

const ReadableScript = readable(blockchain.Script, (script: Script) => helpers.encodeToAddress(script));
const ReadableU128 = readable(Uint128, (value) => value.toString());

// fake udt mint action
const MintUdt = table({ amount: ReadableU128, to: ReadableScript }, ["amount", "to"]);
// a fake schema, just for demonstration
// please do NOT use it in the real world sUDT
const FakeScheme = union({ MintUdt }, ["MintUdt"]);

const CKB_RPC_URL = "https://testnet.ckb.dev/rpc";
const rpc = new RPC(CKB_RPC_URL);
const indexer = new Indexer(CKB_RPC_URL);
// prettier-ignore
interface EthereumRpc {
(payload: { method: 'personal_sign'; params: [string /*from*/, string /*message*/] }): Promise<string>;
}

// prettier-ignore
export interface EthereumProvider {
selectedAddress: string;
isMetaMask?: boolean;
enable: () => Promise<string[]>;
addListener: (event: 'accountsChanged', listener: (addresses: string[]) => void) => void;
removeEventListener: (event: 'accountsChanged', listener: (addresses: string[]) => void) => void;
request: EthereumRpc;
}
// @ts-ignore
export const ethereum = window.ethereum as EthereumProvider;

export function asyncSleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

interface Options {
from: string;
to: string;
amount: string;
}

export async function mintSudt(options: Options): Promise<string> {
let txSkeleton = TransactionSkeleton({
cellProvider: { collector: (query) => indexer.collector({ type: "empty", ...query }) },
});

const udtType: Script = {
codeHash: CONFIG.SCRIPTS.SUDT.CODE_HASH,
hashType: CONFIG.SCRIPTS.SUDT.HASH_TYPE,
args: computeScriptHash(helpers.parseAddress(options.from)),
};
const toLock = helpers.parseAddress(options.to);

const message = {
actions: [
{
data: FakeScheme.pack({ type: "MintUdt", value: { amount: Number(options.amount), to: toLock } }),
scriptHash: computeScriptHash(udtType),
scriptInfoHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
},
],
};

const approved = window.confirm(
`You are going to send the message to blockchain \n ${JSON.stringify(
FakeScheme.unpack(message.actions[0].data),
null,
2
)}`
);

if (!approved) {
throw new Error("User has rejected");
}

const udtCell: Cell = {
cellOutput: { capacity: "0x0", lock: toLock, type: udtType },
data: bytes.hexify(Uint128.pack(Number(options.amount))),
};
const sudtOccupied = minimalCellCapacityCompatible(udtCell);
udtCell.cellOutput.capacity = sudtOccupied.toHexString();

txSkeleton = txSkeleton.update("outputs", (outputs) => outputs.push(udtCell));

const sudtCellDep: CellDep = {
outPoint: { txHash: CONFIG.SCRIPTS.SUDT.TX_HASH, index: CONFIG.SCRIPTS.SUDT.INDEX },
depType: CONFIG.SCRIPTS.SUDT.DEP_TYPE,
};
txSkeleton = addCellDep(txSkeleton, sudtCellDep);

txSkeleton = await injectCapacity(txSkeleton, [options.from], sudtOccupied);
// TODO a better way for fee calculation
txSkeleton = await payFee(txSkeleton, [options.from], 2000);

txSkeleton = prepareCobuildSighashSigningEntries(
txSkeleton,
// only one omnilock in the transaction
// so we can use a simple filter
() => true,
message
);

let signature = await ethereum.request({
method: "personal_sign",
params: [ethereum.selectedAddress, txSkeleton.signingEntries.get(0).message],
});
let v = Number.parseInt(signature.slice(-2), 16);
if (v >= 27) v -= 27;
signature = "0x" + signature.slice(2, -2) + v.toString(16).padStart(2, "0");

const signedTx = sealTransaction(txSkeleton, [cobuild.sealCobuildBlake2bFlow({ signature })], message);
const txHash = await rpc.sendTransaction(signedTx);

return txHash;
}

export async function capacityOf(address: string): Promise<BI> {
const collector = indexer.collector({
lock: helpers.parseAddress(address),
});

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

return balance;
}
24 changes: 24 additions & 0 deletions examples/cobuild-sighash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"private": true,
"name": "@lumos-examples/cobuild-sighash",
"description": "",
"scripts": {
"start": "parcel index.html",
"lint": "tsc --noEmit"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@ckb-lumos/lumos": "canary",
"@ckb-lumos/cobuild": "canary",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@ckb-lumos/molecule": "canary",
"parcel": "^2.9.3"
}
}
9 changes: 9 additions & 0 deletions examples/cobuild-sighash/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"lib": ["dom"],
"jsx": "react",
"module": "CommonJS",
"skipLibCheck": true,
"esModuleInterop": true
}
}
Loading
Loading