@offckb/cli 0.1.0-rc6 → 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 +35 -0
- 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 -212
- package/templates/xudt/lib.ts +0 -252
- package/templates/xudt/package.json +0 -29
- package/templates/xudt/scheme.ts +0 -27
- package/templates/xudt/tsconfig.json +0 -10
- package/templates/xudt/util.ts +0 -66
- 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,212 +0,0 @@
|
|
|
1
|
-
import React, { useEffect, useState } from 'react';
|
|
2
|
-
import ReactDOM from 'react-dom';
|
|
3
|
-
import { Cell, Script, Transaction, utils } from '@ckb-lumos/lumos';
|
|
4
|
-
import { issueToken, queryIssuedTokenCells, transferTokenToAddress } from './lib';
|
|
5
|
-
import { capacityOf, generateAccountFromPrivateKey, readTokenAmount } from './util';
|
|
6
|
-
|
|
7
|
-
const app = document.getElementById('root');
|
|
8
|
-
ReactDOM.render(<App />, app);
|
|
9
|
-
|
|
10
|
-
function IssuedToken() {
|
|
11
|
-
const [privKey, setPrivKey] = useState('');
|
|
12
|
-
const [lockScript, setLockScript] = useState<Script>();
|
|
13
|
-
const [balance, setBalance] = useState('0');
|
|
14
|
-
const [amount, setAmount] = useState('');
|
|
15
|
-
const [issuedTokenCell, setIssuedTokenCell] = useState<Cell>();
|
|
16
|
-
const [txHash, setTxHash] = useState<string>();
|
|
17
|
-
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
const updateFromInfo = async () => {
|
|
20
|
-
const { lockScript, address } = generateAccountFromPrivateKey(privKey);
|
|
21
|
-
const capacity = await capacityOf(address);
|
|
22
|
-
setLockScript(lockScript);
|
|
23
|
-
setBalance(capacity.toString());
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
if (privKey) {
|
|
27
|
-
updateFromInfo();
|
|
28
|
-
}
|
|
29
|
-
}, [privKey]);
|
|
30
|
-
|
|
31
|
-
const onInputPrivKey = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
32
|
-
const priv = e.target.value;
|
|
33
|
-
const privateKeyRegex = /^0x[0-9a-fA-F]{64}$/;
|
|
34
|
-
|
|
35
|
-
const isValid = privateKeyRegex.test(priv);
|
|
36
|
-
if (isValid) {
|
|
37
|
-
setPrivKey(priv);
|
|
38
|
-
} else {
|
|
39
|
-
alert(
|
|
40
|
-
`Invalid private key: must start with 0x and 32 bytes length. Ensure you're using a valid private key from the offckb accounts list.`,
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
const enabledIssue = +amount > 0 && +balance > 6100000000;
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
<>
|
|
48
|
-
<h2>Issue Custom Token</h2>
|
|
49
|
-
<p></p>
|
|
50
|
-
<label htmlFor="private-key">Private Key: </label>
|
|
51
|
-
<input id="private-key" type="text" onChange={onInputPrivKey} />
|
|
52
|
-
<ul>
|
|
53
|
-
<li>Balance(Total Capacity): {(+balance).toLocaleString()}</li>
|
|
54
|
-
<li>
|
|
55
|
-
Lock Script:
|
|
56
|
-
<pre>{JSON.stringify(lockScript, null, 2)}</pre>
|
|
57
|
-
</li>
|
|
58
|
-
<li>Lock Script Hash: {lockScript && utils.computeScriptHash(lockScript)}</li>
|
|
59
|
-
</ul>
|
|
60
|
-
<br />
|
|
61
|
-
<label htmlFor="amount">Token Amount: </label>
|
|
62
|
-
|
|
63
|
-
<input id="amount" type="number" onChange={(e) => setAmount(e.target.value)} />
|
|
64
|
-
<br />
|
|
65
|
-
<button
|
|
66
|
-
disabled={!enabledIssue}
|
|
67
|
-
onClick={() =>
|
|
68
|
-
issueToken(privKey, amount)
|
|
69
|
-
.then((result) => {
|
|
70
|
-
setIssuedTokenCell(result.targetOutput);
|
|
71
|
-
setTxHash(result.hash);
|
|
72
|
-
})
|
|
73
|
-
.catch(alert)
|
|
74
|
-
}
|
|
75
|
-
>
|
|
76
|
-
Issue Token
|
|
77
|
-
</button>
|
|
78
|
-
<div>
|
|
79
|
-
{txHash && issuedTokenCell && (
|
|
80
|
-
<>
|
|
81
|
-
<h4>Result</h4>
|
|
82
|
-
<li>Transaction hash: {txHash}</li>
|
|
83
|
-
<li>
|
|
84
|
-
Token xUDT args: {issuedTokenCell.cellOutput.type.args}{' '}
|
|
85
|
-
<strong>
|
|
86
|
-
{
|
|
87
|
-
'(Noticed that the xUDT args works like the unique id for your issued token, Think of it like an ERC20 contract address)'
|
|
88
|
-
}
|
|
89
|
-
</strong>
|
|
90
|
-
</li>
|
|
91
|
-
<li>
|
|
92
|
-
Token cell: <pre>{JSON.stringify(issuedTokenCell, null, 2)}</pre>
|
|
93
|
-
</li>
|
|
94
|
-
</>
|
|
95
|
-
)}
|
|
96
|
-
</div>
|
|
97
|
-
</>
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function ViewIssuedToken() {
|
|
102
|
-
const [xudtArgs, setXudtArgs] = useState<string>();
|
|
103
|
-
const [cells, setCells] = useState<Cell[]>([]);
|
|
104
|
-
return (
|
|
105
|
-
<>
|
|
106
|
-
<h2>View Custom Token</h2>
|
|
107
|
-
<div>
|
|
108
|
-
<label htmlFor="xudt-args">xDUT args: </label>
|
|
109
|
-
<input id="xudt-args" type="text" onChange={(e) => setXudtArgs(e.target.value)} />
|
|
110
|
-
</div>
|
|
111
|
-
|
|
112
|
-
<button disabled={!xudtArgs} onClick={() => queryIssuedTokenCells(xudtArgs).then(setCells).catch(alert)}>
|
|
113
|
-
Query Issued Token
|
|
114
|
-
</button>
|
|
115
|
-
{cells.length > 0 && <h3>Result: all the cells which hosted this issued token</h3>}
|
|
116
|
-
{cells.map((cell, index) => (
|
|
117
|
-
<div key={index}>
|
|
118
|
-
<p>Cell #{index}</p>
|
|
119
|
-
<li>Token amount: {readTokenAmount(cell.data).toNumber()}</li>
|
|
120
|
-
<li>Token xUDT args: {cell.cellOutput.type.args}</li>
|
|
121
|
-
<li>Token holder's lock script args: {cell.cellOutput.lock.args}</li>
|
|
122
|
-
<hr />
|
|
123
|
-
</div>
|
|
124
|
-
))}
|
|
125
|
-
</>
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function TransferIssuedToken() {
|
|
130
|
-
const [udtArgs, setUdtArgs] = useState<string>('');
|
|
131
|
-
const [senderPrivkey, setSenderPrivkey] = useState<string>('');
|
|
132
|
-
const [transferAmount, setTransferAmount] = useState('');
|
|
133
|
-
const [receiverAddress, setReceiverAddress] = useState('');
|
|
134
|
-
const [tx, setTx] = useState<Transaction>();
|
|
135
|
-
|
|
136
|
-
const onInputSenderPrivKey = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
137
|
-
const priv = e.target.value;
|
|
138
|
-
const privateKeyRegex = /^0x[0-9a-fA-F]{64}$/;
|
|
139
|
-
const isValid = privateKeyRegex.test(priv);
|
|
140
|
-
if (isValid) {
|
|
141
|
-
setSenderPrivkey(priv);
|
|
142
|
-
} else {
|
|
143
|
-
alert(
|
|
144
|
-
`Invalid private key: must start with 0x and 32 bytes length. Ensure you're using a valid private key from the offckb accounts list.`,
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const enabledCheck =
|
|
150
|
-
senderPrivkey.length > 0 && udtArgs.length > 0 && transferAmount.length > 0 && receiverAddress.length > 0;
|
|
151
|
-
|
|
152
|
-
return (
|
|
153
|
-
<>
|
|
154
|
-
<h2>Transfer Custom Token</h2>
|
|
155
|
-
<label htmlFor="sender-private-key">Private Key: </label>
|
|
156
|
-
<input id="sender-private-key" type="text" onChange={onInputSenderPrivKey} />
|
|
157
|
-
<br />
|
|
158
|
-
<label htmlFor="udt">xUDT args: </label>
|
|
159
|
-
|
|
160
|
-
<input id="udt" type="text" onChange={(e) => setUdtArgs(e.target.value)} />
|
|
161
|
-
<br />
|
|
162
|
-
<label htmlFor="transferAmount">Transfer Token Amount: </label>
|
|
163
|
-
|
|
164
|
-
<input id="transferAmount" type="number" onChange={(e) => setTransferAmount(e.target.value)} />
|
|
165
|
-
<br />
|
|
166
|
-
<label htmlFor="receiverAddress">Receiver Address: </label>
|
|
167
|
-
|
|
168
|
-
<input id="receiverAddress" type="text" onChange={(e) => setReceiverAddress(e.target.value)} />
|
|
169
|
-
<br />
|
|
170
|
-
<button
|
|
171
|
-
disabled={!enabledCheck}
|
|
172
|
-
onClick={() =>
|
|
173
|
-
transferTokenToAddress(udtArgs, senderPrivkey, transferAmount, receiverAddress)
|
|
174
|
-
.then((res) => setTx(res.tx))
|
|
175
|
-
.catch(alert)
|
|
176
|
-
}
|
|
177
|
-
>
|
|
178
|
-
Transfer Custom Token
|
|
179
|
-
</button>
|
|
180
|
-
<div>
|
|
181
|
-
{tx && (
|
|
182
|
-
<>
|
|
183
|
-
<h4>Result</h4>
|
|
184
|
-
<li>
|
|
185
|
-
Transaction: <pre>{JSON.stringify(tx, null, 2)}</pre>
|
|
186
|
-
</li>
|
|
187
|
-
</>
|
|
188
|
-
)}
|
|
189
|
-
</div>
|
|
190
|
-
</>
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function App() {
|
|
195
|
-
return (
|
|
196
|
-
<div>
|
|
197
|
-
<h1>
|
|
198
|
-
xUDT Scripts Dapp Example
|
|
199
|
-
<small>
|
|
200
|
-
<a href="https://github.com/XuJiandong/rfcs/blob/xudt/rfcs/0052-extensible-udt/0052-extensible-udt.md#xudt-witness">
|
|
201
|
-
{'(see xUDT specs)'}
|
|
202
|
-
</a>
|
|
203
|
-
</small>
|
|
204
|
-
</h1>
|
|
205
|
-
<IssuedToken />
|
|
206
|
-
<hr />
|
|
207
|
-
<ViewIssuedToken />
|
|
208
|
-
<hr />
|
|
209
|
-
<TransferIssuedToken />
|
|
210
|
-
</div>
|
|
211
|
-
);
|
|
212
|
-
}
|
package/templates/xudt/lib.ts
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { helpers, hd, config, Cell, commons, BI, utils } from '@ckb-lumos/lumos';
|
|
2
|
-
import { blockchain, HexString } from '@ckb-lumos/base';
|
|
3
|
-
import { indexer, lumosConfig, rpc } from './ckb';
|
|
4
|
-
import { bytes, number } from '@ckb-lumos/codec';
|
|
5
|
-
import { xudtWitnessType } from './scheme';
|
|
6
|
-
import { addCellDep, generateAccountFromPrivateKey } from './util';
|
|
7
|
-
|
|
8
|
-
config.initializeConfig(lumosConfig);
|
|
9
|
-
|
|
10
|
-
export async function issueToken(privKey: string, amount: string) {
|
|
11
|
-
const { lockScript } = generateAccountFromPrivateKey(privKey);
|
|
12
|
-
const xudtDeps = lumosConfig.SCRIPTS.XUDT;
|
|
13
|
-
const lockDeps = lumosConfig.SCRIPTS.SECP256K1_BLAKE160;
|
|
14
|
-
|
|
15
|
-
const xudtArgs = utils.computeScriptHash(lockScript) + '00000000';
|
|
16
|
-
const typeScript = {
|
|
17
|
-
codeHash: xudtDeps.CODE_HASH,
|
|
18
|
-
hashType: xudtDeps.HASH_TYPE,
|
|
19
|
-
args: xudtArgs,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
let txSkeleton = helpers.TransactionSkeleton();
|
|
23
|
-
txSkeleton = addCellDep(txSkeleton, {
|
|
24
|
-
outPoint: {
|
|
25
|
-
txHash: lockDeps.TX_HASH,
|
|
26
|
-
index: lockDeps.INDEX,
|
|
27
|
-
},
|
|
28
|
-
depType: lockDeps.DEP_TYPE,
|
|
29
|
-
});
|
|
30
|
-
txSkeleton = addCellDep(txSkeleton, {
|
|
31
|
-
outPoint: {
|
|
32
|
-
txHash: xudtDeps.TX_HASH,
|
|
33
|
-
index: xudtDeps.INDEX,
|
|
34
|
-
},
|
|
35
|
-
depType: xudtDeps.DEP_TYPE,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const targetOutput: Cell = {
|
|
39
|
-
cellOutput: {
|
|
40
|
-
capacity: '0x0',
|
|
41
|
-
lock: lockScript,
|
|
42
|
-
type: typeScript,
|
|
43
|
-
},
|
|
44
|
-
data: bytes.hexify(number.Uint128LE.pack(amount)),
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// additional 0.001 ckb for tx fee
|
|
48
|
-
// the tx fee could calculated by tx size
|
|
49
|
-
// this is just a simple example
|
|
50
|
-
const capacity = helpers.minimalCellCapacity(targetOutput);
|
|
51
|
-
targetOutput.cellOutput.capacity = '0x' + capacity.toString(16);
|
|
52
|
-
const neededCapacity = BI.from(capacity.toString(10)).add(100000);
|
|
53
|
-
let collectedSum = BI.from(0);
|
|
54
|
-
const collected: Cell[] = [];
|
|
55
|
-
const collector = indexer.collector({ lock: lockScript, type: 'empty' });
|
|
56
|
-
for await (const cell of collector.collect()) {
|
|
57
|
-
collectedSum = collectedSum.add(cell.cellOutput.capacity);
|
|
58
|
-
collected.push(cell);
|
|
59
|
-
if (collectedSum >= neededCapacity) break;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (collectedSum.lt(neededCapacity)) {
|
|
63
|
-
throw new Error(`Not enough CKB, ${collectedSum} < ${neededCapacity}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const changeOutput: Cell = {
|
|
67
|
-
cellOutput: {
|
|
68
|
-
capacity: collectedSum.sub(neededCapacity).toHexString(),
|
|
69
|
-
lock: lockScript,
|
|
70
|
-
},
|
|
71
|
-
data: '0x',
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...collected));
|
|
75
|
-
txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(targetOutput, changeOutput));
|
|
76
|
-
/* 65-byte zeros in hex */
|
|
77
|
-
const lockWitness =
|
|
78
|
-
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
|
|
79
|
-
const outputTypeWitness = xudtWitnessType.pack({ extension_data: [] });
|
|
80
|
-
const witnessArgs = blockchain.WitnessArgs.pack({ lock: lockWitness, outputType: outputTypeWitness });
|
|
81
|
-
const witness = bytes.hexify(witnessArgs);
|
|
82
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.set(0, witness));
|
|
83
|
-
|
|
84
|
-
// signing
|
|
85
|
-
txSkeleton = commons.common.prepareSigningEntries(txSkeleton);
|
|
86
|
-
const message = txSkeleton.get('signingEntries').get(0)?.message;
|
|
87
|
-
const Sig = hd.key.signRecoverable(message!, privKey);
|
|
88
|
-
const tx = helpers.sealTransaction(txSkeleton, [Sig]);
|
|
89
|
-
console.log(tx);
|
|
90
|
-
|
|
91
|
-
const hash = await rpc.sendTransaction(tx, 'passthrough');
|
|
92
|
-
console.log('The transaction hash is', hash);
|
|
93
|
-
return { hash, targetOutput };
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export async function queryIssuedTokenCells(xudtArgs: HexString) {
|
|
97
|
-
const xudtDeps = lumosConfig.SCRIPTS.XUDT;
|
|
98
|
-
const typeScript = {
|
|
99
|
-
codeHash: xudtDeps.CODE_HASH,
|
|
100
|
-
hashType: xudtDeps.HASH_TYPE,
|
|
101
|
-
args: xudtArgs,
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const collected: Cell[] = [];
|
|
105
|
-
const collector = indexer.collector({ type: typeScript });
|
|
106
|
-
for await (const cell of collector.collect()) {
|
|
107
|
-
collected.push(cell);
|
|
108
|
-
}
|
|
109
|
-
return collected;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export async function transferTokenToAddress(
|
|
113
|
-
udtIssuerArgs: string,
|
|
114
|
-
senderPrivKey: string,
|
|
115
|
-
amount: string,
|
|
116
|
-
receiverAddress: string,
|
|
117
|
-
) {
|
|
118
|
-
const { lockScript: senderLockScript } = generateAccountFromPrivateKey(senderPrivKey);
|
|
119
|
-
|
|
120
|
-
const receiverLockScript = helpers.parseAddress(receiverAddress);
|
|
121
|
-
|
|
122
|
-
const xudtDeps = lumosConfig.SCRIPTS.XUDT;
|
|
123
|
-
const lockDeps = lumosConfig.SCRIPTS.SECP256K1_BLAKE160;
|
|
124
|
-
|
|
125
|
-
const xudtArgs = udtIssuerArgs;
|
|
126
|
-
const typeScript = {
|
|
127
|
-
codeHash: xudtDeps.CODE_HASH,
|
|
128
|
-
hashType: xudtDeps.HASH_TYPE,
|
|
129
|
-
args: xudtArgs,
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
let txSkeleton = helpers.TransactionSkeleton();
|
|
133
|
-
txSkeleton = addCellDep(txSkeleton, {
|
|
134
|
-
outPoint: {
|
|
135
|
-
txHash: lockDeps.TX_HASH,
|
|
136
|
-
index: lockDeps.INDEX,
|
|
137
|
-
},
|
|
138
|
-
depType: lockDeps.DEP_TYPE,
|
|
139
|
-
});
|
|
140
|
-
txSkeleton = addCellDep(txSkeleton, {
|
|
141
|
-
outPoint: {
|
|
142
|
-
txHash: xudtDeps.TX_HASH,
|
|
143
|
-
index: xudtDeps.INDEX,
|
|
144
|
-
},
|
|
145
|
-
depType: xudtDeps.DEP_TYPE,
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const targetOutput: Cell = {
|
|
149
|
-
cellOutput: {
|
|
150
|
-
capacity: '0x0',
|
|
151
|
-
lock: receiverLockScript,
|
|
152
|
-
type: typeScript,
|
|
153
|
-
},
|
|
154
|
-
data: bytes.hexify(number.Uint128LE.pack(amount)),
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const capacity = helpers.minimalCellCapacity(targetOutput);
|
|
158
|
-
targetOutput.cellOutput.capacity = '0x' + capacity.toString(16);
|
|
159
|
-
// additional 0.001 ckb for tx fee
|
|
160
|
-
// the tx fee could calculated by tx size
|
|
161
|
-
// this is just a simple example
|
|
162
|
-
const neededCapacity = BI.from(capacity.toString(10)).add(100000);
|
|
163
|
-
let collectedSum = BI.from(0);
|
|
164
|
-
let collectedAmount = BI.from(0);
|
|
165
|
-
const collected: Cell[] = [];
|
|
166
|
-
const collector = indexer.collector({ lock: senderLockScript, type: typeScript });
|
|
167
|
-
for await (const cell of collector.collect()) {
|
|
168
|
-
collectedSum = collectedSum.add(cell.cellOutput.capacity);
|
|
169
|
-
collectedAmount = collectedAmount.add(number.Uint128LE.unpack(cell.data));
|
|
170
|
-
collected.push(cell);
|
|
171
|
-
if (collectedAmount >= BI.from(amount)) break;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
let changeOutputTokenAmount = BI.from(0);
|
|
175
|
-
if (collectedAmount.gt(BI.from(amount))) {
|
|
176
|
-
changeOutputTokenAmount = collectedAmount.sub(BI.from(amount));
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const changeOutput: Cell = {
|
|
180
|
-
cellOutput: {
|
|
181
|
-
capacity: '0x0',
|
|
182
|
-
lock: senderLockScript,
|
|
183
|
-
type: typeScript,
|
|
184
|
-
},
|
|
185
|
-
data: bytes.hexify(number.Uint128LE.pack(changeOutputTokenAmount.toString(10))),
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const changeOutputNeededCapacity = BI.from(helpers.minimalCellCapacity(changeOutput));
|
|
189
|
-
|
|
190
|
-
const extraNeededCapacity = collectedSum.lt(neededCapacity)
|
|
191
|
-
? neededCapacity.sub(collectedSum).add(changeOutputNeededCapacity)
|
|
192
|
-
: collectedSum.sub(neededCapacity).add(changeOutputNeededCapacity);
|
|
193
|
-
|
|
194
|
-
if (extraNeededCapacity.gt(0)) {
|
|
195
|
-
let extraCollectedSum = BI.from(0);
|
|
196
|
-
const extraCollectedCells: Cell[] = [];
|
|
197
|
-
const collector = indexer.collector({ lock: senderLockScript, type: 'empty' });
|
|
198
|
-
for await (const cell of collector.collect()) {
|
|
199
|
-
extraCollectedSum = extraCollectedSum.add(cell.cellOutput.capacity);
|
|
200
|
-
extraCollectedCells.push(cell);
|
|
201
|
-
if (extraCollectedSum >= extraNeededCapacity) break;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (extraCollectedSum.lt(extraNeededCapacity)) {
|
|
205
|
-
throw new Error(`Not enough CKB for change, ${extraCollectedSum} < ${extraNeededCapacity}`);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...extraCollectedCells));
|
|
209
|
-
|
|
210
|
-
const change2Capacity = extraCollectedSum.sub(changeOutputNeededCapacity);
|
|
211
|
-
if (change2Capacity.gt(61000000000)) {
|
|
212
|
-
changeOutput.cellOutput.capacity = changeOutputNeededCapacity.toHexString();
|
|
213
|
-
const changeOutput2: Cell = {
|
|
214
|
-
cellOutput: {
|
|
215
|
-
capacity: change2Capacity.toHexString(),
|
|
216
|
-
lock: senderLockScript,
|
|
217
|
-
},
|
|
218
|
-
data: '0x',
|
|
219
|
-
};
|
|
220
|
-
txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(changeOutput2));
|
|
221
|
-
} else {
|
|
222
|
-
changeOutput.cellOutput.capacity = extraCollectedSum.toHexString();
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...collected));
|
|
227
|
-
txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(targetOutput, changeOutput));
|
|
228
|
-
/* 65-byte zeros in hex */
|
|
229
|
-
const lockWitness =
|
|
230
|
-
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
|
|
231
|
-
|
|
232
|
-
const inputTypeWitness = xudtWitnessType.pack({ extension_data: [] });
|
|
233
|
-
const outputTypeWitness = xudtWitnessType.pack({ extension_data: [] });
|
|
234
|
-
const witnessArgs = blockchain.WitnessArgs.pack({
|
|
235
|
-
lock: lockWitness,
|
|
236
|
-
inputType: inputTypeWitness,
|
|
237
|
-
outputType: outputTypeWitness,
|
|
238
|
-
});
|
|
239
|
-
const witness = bytes.hexify(witnessArgs);
|
|
240
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.set(0, witness));
|
|
241
|
-
|
|
242
|
-
// signing
|
|
243
|
-
txSkeleton = commons.common.prepareSigningEntries(txSkeleton);
|
|
244
|
-
const message = txSkeleton.get('signingEntries').get(0)?.message;
|
|
245
|
-
const Sig = hd.key.signRecoverable(message!, senderPrivKey);
|
|
246
|
-
const tx = helpers.sealTransaction(txSkeleton, [Sig]);
|
|
247
|
-
console.log('tx: ', tx);
|
|
248
|
-
|
|
249
|
-
const txHash = await rpc.sendTransaction(tx, 'passthrough');
|
|
250
|
-
console.log('The transaction hash is', txHash);
|
|
251
|
-
return { txHash, tx };
|
|
252
|
-
}
|
|
@@ -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
|
-
);
|
package/templates/xudt/util.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { helpers, Address, Script, hd, BI, CellDep } from '@ckb-lumos/lumos';
|
|
2
|
-
import { values } from '@ckb-lumos/base';
|
|
3
|
-
import { indexer, lumosConfig } from './ckb';
|
|
4
|
-
import { TransactionSkeletonType } from '@ckb-lumos/helpers';
|
|
5
|
-
import { number } from '@ckb-lumos/codec';
|
|
6
|
-
|
|
7
|
-
type Account = {
|
|
8
|
-
lockScript: Script;
|
|
9
|
-
address: Address;
|
|
10
|
-
pubKey: string;
|
|
11
|
-
};
|
|
12
|
-
export const generateAccountFromPrivateKey = (privKey: string): Account => {
|
|
13
|
-
const pubKey = hd.key.privateToPublic(privKey);
|
|
14
|
-
const args = hd.key.publicKeyToBlake160(pubKey);
|
|
15
|
-
const template = lumosConfig.SCRIPTS['SECP256K1_BLAKE160']!;
|
|
16
|
-
const lockScript = {
|
|
17
|
-
codeHash: template.CODE_HASH,
|
|
18
|
-
hashType: template.HASH_TYPE,
|
|
19
|
-
args: args,
|
|
20
|
-
};
|
|
21
|
-
const address = helpers.encodeToAddress(lockScript, { config: lumosConfig });
|
|
22
|
-
return {
|
|
23
|
-
lockScript,
|
|
24
|
-
address,
|
|
25
|
-
pubKey,
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export async function capacityOf(address: string): Promise<BI> {
|
|
30
|
-
const collector = indexer.collector({
|
|
31
|
-
lock: helpers.parseAddress(address, { config: lumosConfig }),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
let balance = BI.from(0);
|
|
35
|
-
for await (const cell of collector.collect()) {
|
|
36
|
-
balance = balance.add(cell.cellOutput.capacity);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return balance;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function addCellDep(txSkeleton: TransactionSkeletonType, newCellDep: CellDep): TransactionSkeletonType {
|
|
43
|
-
const cellDep = txSkeleton.get('cellDeps').find((cellDep) => {
|
|
44
|
-
return (
|
|
45
|
-
cellDep.depType === newCellDep.depType &&
|
|
46
|
-
new values.OutPointValue(cellDep.outPoint, { validate: false }).equals(
|
|
47
|
-
new values.OutPointValue(newCellDep.outPoint, { validate: false }),
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
if (!cellDep) {
|
|
53
|
-
txSkeleton = txSkeleton.update('cellDeps', (cellDeps) => {
|
|
54
|
-
return cellDeps.push({
|
|
55
|
-
outPoint: newCellDep.outPoint,
|
|
56
|
-
depType: newCellDep.depType,
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return txSkeleton;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function readTokenAmount(amount: string) {
|
|
65
|
-
return number.Uint128LE.unpack(amount);
|
|
66
|
-
}
|