-
Notifications
You must be signed in to change notification settings - Fork 54
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
homura
wants to merge
6
commits into
develop
Choose a base branch
from
cobuild-sighash
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e68110
feat: init cobuild
homura c4f2576
feat: update omnilock binary
homura 1bd21fa
feat: support cobuild sighash
homura 9cfbb37
test(common-scripts): update omnilock
homura 2d4f117
feat(common-scripts): export cobuild from omnilock
homura ad38a37
chore: cobuild example for mint sudt
homura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
**/test_cases.js | ||
**/test_cases.js | ||
**/generated/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
``` | ||
union SudtActions { | ||
Mint, | ||
} | ||
|
||
table Mint { | ||
to: Script, | ||
amount: Uint128, | ||
} | ||
``` | ||
|
||
## Links | ||
|
||
- [CoBuild](https://talk.nervos.org/t/ckb-transaction-cobuild-protocol-overview/7702) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<input id="address" type="text" onChange={(e) => setMintAddress(e.target.value)} placeholder="ckt1..." /> | ||
<br /> | ||
<label htmlFor="amount">Amount</label> | ||
| ||
<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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it