@offckb/cli 0.1.0-rc5 → 0.1.0-rc7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -2
- package/dist/cfg/const.d.ts +5 -0
- package/dist/cfg/const.js +7 -1
- package/dist/cli.js +1 -2
- package/dist/cmd/build-lumos-config.d.ts +35 -0
- package/dist/cmd/build-lumos-config.js +35 -0
- package/dist/cmd/init.d.ts +1 -0
- package/dist/cmd/init.js +25 -3
- package/dist/util.d.ts +7 -0
- package/dist/util.js +51 -1
- package/package.json +3 -3
- package/templates/config.json +35 -0
- package/dist/cfg/select.d.ts +0 -1
- package/dist/cfg/select.js +0 -51
- package/templates/transfer/index.html +0 -13
- package/templates/transfer/index.tsx +0 -89
- package/templates/transfer/lib.ts +0 -153
- package/templates/transfer/package.json +0 -29
- package/templates/transfer/tsconfig.json +0 -10
- package/templates/transfer/yarn.lock +0 -2315
- package/templates/xudt/index.html +0 -13
- package/templates/xudt/index.tsx +0 -106
- package/templates/xudt/lib.ts +0 -179
- package/templates/xudt/package.json +0 -29
- package/templates/xudt/scheme.ts +0 -27
- package/templates/xudt/tsconfig.json +0 -10
- package/templates/xudt/yarn.lock +0 -2315
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8" />
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
-
<title>Issue Custom Token</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<div id="root"></div>
|
|
11
|
-
<script src="index.tsx" type="module"></script>
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
package/templates/xudt/index.tsx
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import React, { useEffect, useState } from 'react';
|
|
2
|
-
import ReactDOM from 'react-dom';
|
|
3
|
-
import { Cell, Script } from '@ckb-lumos/lumos';
|
|
4
|
-
import {
|
|
5
|
-
capacityOf,
|
|
6
|
-
computeLockScriptHashFromPrivateKey,
|
|
7
|
-
generateAccountFromPrivateKey,
|
|
8
|
-
issueToken,
|
|
9
|
-
queryIssuedTokenCells,
|
|
10
|
-
readTokenAmount,
|
|
11
|
-
} from './lib';
|
|
12
|
-
|
|
13
|
-
const app = document.getElementById('root');
|
|
14
|
-
ReactDOM.render(<App />, app);
|
|
15
|
-
|
|
16
|
-
export function App() {
|
|
17
|
-
const [privKey, setPrivKey] = useState('');
|
|
18
|
-
const [fromAddr, setFromAddr] = useState('');
|
|
19
|
-
const [fromLock, setFromLock] = useState<Script>();
|
|
20
|
-
const [balance, setBalance] = useState('0');
|
|
21
|
-
|
|
22
|
-
const [amount, setAmount] = useState('');
|
|
23
|
-
const [cells, setCells] = useState<Cell[]>([]);
|
|
24
|
-
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
const updateFromInfo = async () => {
|
|
27
|
-
const { lockScript, address } = generateAccountFromPrivateKey(privKey);
|
|
28
|
-
const capacity = await capacityOf(address);
|
|
29
|
-
setFromAddr(address);
|
|
30
|
-
setFromLock(lockScript);
|
|
31
|
-
setBalance(capacity.toString());
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
if (privKey) {
|
|
35
|
-
updateFromInfo();
|
|
36
|
-
}
|
|
37
|
-
}, [privKey]);
|
|
38
|
-
|
|
39
|
-
const onInputPrivKey = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
40
|
-
// Regular expression to match a valid private key with "0x" prefix
|
|
41
|
-
const priv = e.target.value;
|
|
42
|
-
const privateKeyRegex = /^0x[0-9a-fA-F]{64}$/;
|
|
43
|
-
|
|
44
|
-
const isValid = privateKeyRegex.test(priv);
|
|
45
|
-
if (isValid) {
|
|
46
|
-
setPrivKey(priv);
|
|
47
|
-
} else {
|
|
48
|
-
alert(
|
|
49
|
-
`Invalid private key: must start with 0x and 32 bytes length. Ensure you're using a valid private key from the offckb accounts list.`,
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const enabledIssue = +amount > 0 && +balance > 6100000000;
|
|
55
|
-
const enabledCheck = privKey.length > 0;
|
|
56
|
-
return (
|
|
57
|
-
<div>
|
|
58
|
-
<h1>
|
|
59
|
-
Issue Custom Token{' '}
|
|
60
|
-
<small>
|
|
61
|
-
<a href="https://github.com/XuJiandong/rfcs/blob/xudt/rfcs/0052-extensible-udt/0052-extensible-udt.md#xudt-witness">
|
|
62
|
-
{'(xUDT specs)'}
|
|
63
|
-
</a>
|
|
64
|
-
</small>
|
|
65
|
-
</h1>
|
|
66
|
-
<p></p>
|
|
67
|
-
<label htmlFor="private-key">Private Key: </label>
|
|
68
|
-
<input id="private-key" type="text" onChange={onInputPrivKey} />
|
|
69
|
-
<ul>
|
|
70
|
-
<li>CKB Address: {fromAddr}</li>
|
|
71
|
-
<li>
|
|
72
|
-
Current lock script:
|
|
73
|
-
<pre>{JSON.stringify(fromLock, null, 2)}</pre>
|
|
74
|
-
</li>
|
|
75
|
-
|
|
76
|
-
<li>Total capacity: {(+balance).toLocaleString()}</li>
|
|
77
|
-
</ul>
|
|
78
|
-
<br />
|
|
79
|
-
<label htmlFor="amount">Token Amount</label>
|
|
80
|
-
|
|
81
|
-
<input id="amount" type="text" onChange={(e) => setAmount(e.target.value)} />
|
|
82
|
-
<br />
|
|
83
|
-
<button disabled={!enabledIssue} onClick={() => issueToken(privKey, amount).catch(alert)}>
|
|
84
|
-
Issue token
|
|
85
|
-
</button>
|
|
86
|
-
<br />
|
|
87
|
-
<br />
|
|
88
|
-
<hr />
|
|
89
|
-
<p>after issued token, click the below button to check it</p>
|
|
90
|
-
<button disabled={!enabledCheck} onClick={() => queryIssuedTokenCells(privKey).then(setCells).catch(alert)}>
|
|
91
|
-
Check issued token
|
|
92
|
-
</button>
|
|
93
|
-
{cells.length > 0 && <h3>Result: all the cells which hosted this issued token</h3>}
|
|
94
|
-
{cells.map((cell, index) => (
|
|
95
|
-
<div key={index}>
|
|
96
|
-
<p>Cell #{index}</p>
|
|
97
|
-
<p>token amount: {readTokenAmount(cell.data).toNumber()}</p>
|
|
98
|
-
<p>issuer lockScript Hash: {computeLockScriptHashFromPrivateKey(privKey)}</p>
|
|
99
|
-
<p>token xudt args: {cell.cellOutput.type.args}</p>
|
|
100
|
-
<p>token holder's lockScript args: {cell.cellOutput.lock.args}</p>
|
|
101
|
-
<hr />
|
|
102
|
-
</div>
|
|
103
|
-
))}
|
|
104
|
-
</div>
|
|
105
|
-
);
|
|
106
|
-
}
|
package/templates/xudt/lib.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { helpers, Address, Script, hd, config, Cell, commons, BI, utils, CellDep } from '@ckb-lumos/lumos';
|
|
2
|
-
import { values, blockchain } from '@ckb-lumos/base';
|
|
3
|
-
import { indexer, lumosConfig, rpc } from './ckb';
|
|
4
|
-
import { TransactionSkeletonType } from '@ckb-lumos/helpers';
|
|
5
|
-
import { bytes, number } from '@ckb-lumos/codec';
|
|
6
|
-
import { xudtWitnessType } from './scheme';
|
|
7
|
-
|
|
8
|
-
config.initializeConfig(lumosConfig);
|
|
9
|
-
|
|
10
|
-
type Account = {
|
|
11
|
-
lockScript: Script;
|
|
12
|
-
address: Address;
|
|
13
|
-
pubKey: string;
|
|
14
|
-
};
|
|
15
|
-
export const generateAccountFromPrivateKey = (privKey: string): Account => {
|
|
16
|
-
const pubKey = hd.key.privateToPublic(privKey);
|
|
17
|
-
const args = hd.key.publicKeyToBlake160(pubKey);
|
|
18
|
-
const template = lumosConfig.SCRIPTS['SECP256K1_BLAKE160']!;
|
|
19
|
-
const lockScript = {
|
|
20
|
-
codeHash: template.CODE_HASH,
|
|
21
|
-
hashType: template.HASH_TYPE,
|
|
22
|
-
args: args,
|
|
23
|
-
};
|
|
24
|
-
const address = helpers.encodeToAddress(lockScript, { config: lumosConfig });
|
|
25
|
-
return {
|
|
26
|
-
lockScript,
|
|
27
|
-
address,
|
|
28
|
-
pubKey,
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const computeLockScriptHashFromPrivateKey = (privkey: string) => {
|
|
33
|
-
const { lockScript } = generateAccountFromPrivateKey(privkey);
|
|
34
|
-
return utils.computeScriptHash(lockScript);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export async function capacityOf(address: string): Promise<BI> {
|
|
38
|
-
const collector = indexer.collector({
|
|
39
|
-
lock: helpers.parseAddress(address, { config: lumosConfig }),
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
let balance = BI.from(0);
|
|
43
|
-
for await (const cell of collector.collect()) {
|
|
44
|
-
balance = balance.add(cell.cellOutput.capacity);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return balance;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function addCellDep(txSkeleton: TransactionSkeletonType, newCellDep: CellDep): TransactionSkeletonType {
|
|
51
|
-
const cellDep = txSkeleton.get('cellDeps').find((cellDep) => {
|
|
52
|
-
return (
|
|
53
|
-
cellDep.depType === newCellDep.depType &&
|
|
54
|
-
new values.OutPointValue(cellDep.outPoint, { validate: false }).equals(
|
|
55
|
-
new values.OutPointValue(newCellDep.outPoint, { validate: false }),
|
|
56
|
-
)
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
if (!cellDep) {
|
|
61
|
-
txSkeleton = txSkeleton.update('cellDeps', (cellDeps) => {
|
|
62
|
-
return cellDeps.push({
|
|
63
|
-
outPoint: newCellDep.outPoint,
|
|
64
|
-
depType: newCellDep.depType,
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return txSkeleton;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export async function issueToken(privKey: string, amount: string) {
|
|
73
|
-
const { lockScript } = generateAccountFromPrivateKey(privKey);
|
|
74
|
-
const xudtDeps = lumosConfig.SCRIPTS.XUDT;
|
|
75
|
-
const lockDeps = lumosConfig.SCRIPTS.SECP256K1_BLAKE160;
|
|
76
|
-
|
|
77
|
-
const xudtArgs = utils.computeScriptHash(lockScript) + '00000000';
|
|
78
|
-
const typeScript = {
|
|
79
|
-
codeHash: xudtDeps.CODE_HASH,
|
|
80
|
-
hashType: xudtDeps.HASH_TYPE,
|
|
81
|
-
args: xudtArgs,
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
let txSkeleton = helpers.TransactionSkeleton();
|
|
85
|
-
txSkeleton = addCellDep(txSkeleton, {
|
|
86
|
-
outPoint: {
|
|
87
|
-
txHash: lockDeps.TX_HASH,
|
|
88
|
-
index: lockDeps.INDEX,
|
|
89
|
-
},
|
|
90
|
-
depType: lockDeps.DEP_TYPE,
|
|
91
|
-
});
|
|
92
|
-
txSkeleton = addCellDep(txSkeleton, {
|
|
93
|
-
outPoint: {
|
|
94
|
-
txHash: xudtDeps.TX_HASH,
|
|
95
|
-
index: xudtDeps.INDEX,
|
|
96
|
-
},
|
|
97
|
-
depType: xudtDeps.DEP_TYPE,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const targetOutput: Cell = {
|
|
101
|
-
cellOutput: {
|
|
102
|
-
capacity: '0x0',
|
|
103
|
-
lock: lockScript,
|
|
104
|
-
type: typeScript,
|
|
105
|
-
},
|
|
106
|
-
data: bytes.hexify(number.Uint128LE.pack(amount)),
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// additional 0.001 ckb for tx fee
|
|
110
|
-
// the tx fee could calculated by tx size
|
|
111
|
-
// this is just a simple example
|
|
112
|
-
const capacity = helpers.minimalCellCapacity(targetOutput);
|
|
113
|
-
targetOutput.cellOutput.capacity = '0x' + capacity.toString(16);
|
|
114
|
-
const neededCapacity = BI.from(capacity.toString(10)).add(100000);
|
|
115
|
-
let collectedSum = BI.from(0);
|
|
116
|
-
const collected: Cell[] = [];
|
|
117
|
-
const collector = indexer.collector({ lock: lockScript, type: 'empty' });
|
|
118
|
-
for await (const cell of collector.collect()) {
|
|
119
|
-
collectedSum = collectedSum.add(cell.cellOutput.capacity);
|
|
120
|
-
collected.push(cell);
|
|
121
|
-
if (collectedSum >= neededCapacity) break;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (collectedSum.lt(neededCapacity)) {
|
|
125
|
-
throw new Error(`Not enough CKB, ${collectedSum} < ${neededCapacity}`);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const changeOutput: Cell = {
|
|
129
|
-
cellOutput: {
|
|
130
|
-
capacity: collectedSum.sub(neededCapacity).toHexString(),
|
|
131
|
-
lock: lockScript,
|
|
132
|
-
},
|
|
133
|
-
data: '0x',
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...collected));
|
|
137
|
-
txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(targetOutput, changeOutput));
|
|
138
|
-
/* 65-byte zeros in hex */
|
|
139
|
-
const lockWitness =
|
|
140
|
-
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
|
|
141
|
-
const outputTypeWitness = xudtWitnessType.pack({ extension_data: [] });
|
|
142
|
-
const witnessArgs = blockchain.WitnessArgs.pack({ lock: lockWitness, outputType: outputTypeWitness });
|
|
143
|
-
const witness = bytes.hexify(witnessArgs);
|
|
144
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.set(0, witness));
|
|
145
|
-
|
|
146
|
-
// signing
|
|
147
|
-
txSkeleton = commons.common.prepareSigningEntries(txSkeleton);
|
|
148
|
-
const message = txSkeleton.get('signingEntries').get(0)?.message;
|
|
149
|
-
const Sig = hd.key.signRecoverable(message!, privKey);
|
|
150
|
-
const tx = helpers.sealTransaction(txSkeleton, [Sig]);
|
|
151
|
-
console.log(tx);
|
|
152
|
-
|
|
153
|
-
const hash = await rpc.sendTransaction(tx, 'passthrough');
|
|
154
|
-
console.log('The transaction hash is', hash);
|
|
155
|
-
alert(`The transaction hash is ${hash}`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function queryIssuedTokenCells(privKey: string) {
|
|
159
|
-
const { lockScript } = generateAccountFromPrivateKey(privKey);
|
|
160
|
-
const xudtDeps = lumosConfig.SCRIPTS.XUDT;
|
|
161
|
-
|
|
162
|
-
const xudtArgs = utils.computeScriptHash(lockScript) + '00000000';
|
|
163
|
-
const typeScript = {
|
|
164
|
-
codeHash: xudtDeps.CODE_HASH,
|
|
165
|
-
hashType: xudtDeps.HASH_TYPE,
|
|
166
|
-
args: xudtArgs,
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const collected: Cell[] = [];
|
|
170
|
-
const collector = indexer.collector({ type: typeScript });
|
|
171
|
-
for await (const cell of collector.collect()) {
|
|
172
|
-
collected.push(cell);
|
|
173
|
-
}
|
|
174
|
-
return collected;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function readTokenAmount(amount: string) {
|
|
178
|
-
return number.Uint128LE.unpack(amount);
|
|
179
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@offckb/xudt",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"start": "parcel index.html",
|
|
8
|
-
"lint": "tsc --noEmit"
|
|
9
|
-
},
|
|
10
|
-
"keywords": [],
|
|
11
|
-
"author": "",
|
|
12
|
-
"license": "MIT",
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"@ckb-lumos/base": "0.21.1",
|
|
15
|
-
"@ckb-lumos/codec": "0.21.1",
|
|
16
|
-
"@ckb-lumos/lumos": "0.21.1",
|
|
17
|
-
"@types/react": "^18.0.25",
|
|
18
|
-
"@types/react-dom": "^18.0.9",
|
|
19
|
-
"react": "^18.2.0",
|
|
20
|
-
"react-dom": "^18.2.0"
|
|
21
|
-
},
|
|
22
|
-
"devDependencies": {
|
|
23
|
-
"crypto-browserify": "^3.12.0",
|
|
24
|
-
"parcel": "^2.9.3",
|
|
25
|
-
"path-browserify": "^1.0.0",
|
|
26
|
-
"process": "^0.11.10",
|
|
27
|
-
"stream-browserify": "^3.0.0"
|
|
28
|
-
}
|
|
29
|
-
}
|
package/templates/xudt/scheme.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { molecule, number } from '@ckb-lumos/codec';
|
|
2
|
-
import { BytesOpt, Byte32, Bytes } from '@ckb-lumos/codec/lib/blockchain';
|
|
3
|
-
const { vector, option, table } = molecule;
|
|
4
|
-
|
|
5
|
-
const { Uint8 } = number;
|
|
6
|
-
|
|
7
|
-
const Script = table(
|
|
8
|
-
{
|
|
9
|
-
codeHash: Byte32,
|
|
10
|
-
hashType: Uint8,
|
|
11
|
-
args: Bytes,
|
|
12
|
-
},
|
|
13
|
-
['codeHash', 'hashType', 'args'],
|
|
14
|
-
);
|
|
15
|
-
const ScriptOpt = option(Script);
|
|
16
|
-
const ScriptVecOpt = option(vector(Script));
|
|
17
|
-
|
|
18
|
-
// specs: https://github.com/XuJiandong/rfcs/blob/xudt/rfcs/0052-extensible-udt/0052-extensible-udt.md#xudt-witness
|
|
19
|
-
export const xudtWitnessType = table(
|
|
20
|
-
{
|
|
21
|
-
owner_script: ScriptOpt,
|
|
22
|
-
owner_signature: BytesOpt,
|
|
23
|
-
raw_extension_data: ScriptVecOpt,
|
|
24
|
-
extension_data: vector(Bytes),
|
|
25
|
-
},
|
|
26
|
-
['owner_script', 'owner_signature', 'raw_extension_data', 'extension_data'],
|
|
27
|
-
);
|