@offckb/cli 0.3.0-rc4 → 0.3.0-rc5
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 +21 -20
- package/dist/cfg/setting.d.ts +11 -2
- package/dist/cli.js +15 -6
- package/dist/cmd/balance.d.ts +1 -1
- package/dist/cmd/balance.js +6 -8
- package/dist/cmd/debug.d.ts +1 -1
- package/dist/cmd/debug.js +5 -5
- package/dist/cmd/deploy.d.ts +1 -1
- package/dist/cmd/deploy.js +2 -2
- package/dist/cmd/deposit.d.ts +2 -2
- package/dist/cmd/deposit.js +29 -31
- package/dist/cmd/list-hashes.d.ts +1 -1
- package/dist/cmd/my-scripts.d.ts +1 -1
- package/dist/cmd/my-scripts.js +2 -2
- package/dist/cmd/node.js +2 -2
- package/dist/cmd/proxy-rpc.d.ts +1 -1
- package/dist/cmd/proxy-rpc.js +4 -4
- package/dist/cmd/system-scripts.d.ts +70 -2
- package/dist/cmd/system-scripts.js +4 -0
- package/dist/cmd/transfer-all.d.ts +5 -0
- package/dist/cmd/transfer-all.js +37 -0
- package/dist/cmd/transfer.d.ts +2 -2
- package/dist/cmd/transfer.js +8 -11
- package/dist/deploy/index.d.ts +2 -2
- package/dist/deploy/index.js +3 -3
- package/dist/deploy/migration.d.ts +2 -2
- package/dist/deploy/migration.js +5 -5
- package/dist/deploy/toml.d.ts +7 -3
- package/dist/deploy/util.d.ts +1 -1
- package/dist/deploy/util.js +4 -4
- package/dist/node/install.js +3 -4
- package/dist/scripts/gen.js +4 -4
- package/dist/scripts/util.d.ts +1 -1
- package/dist/sdk/ckb.d.ts +17 -4
- package/dist/sdk/ckb.js +79 -5
- package/dist/tools/moleculec-es.js +3 -4
- package/dist/tools/rpc-proxy.d.ts +1 -1
- package/dist/tools/rpc-proxy.js +2 -2
- package/dist/{util/type.d.ts → type/base.d.ts} +2 -0
- package/dist/util/request.d.ts +6 -6
- package/dist/util/request.js +15 -11
- package/dist/util/validator.js +2 -2
- package/package.json +4 -3
- package/dist/util/ckb.d.ts +0 -141
- package/dist/util/ckb.js +0 -312
- /package/dist/{util/type.js → type/base.js} +0 -0
package/dist/util/ckb.js
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
-
var m = o[Symbol.asyncIterator], i;
|
|
14
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
-
};
|
|
18
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.readPredefinedDevnetLumosConfig = exports.CKB = void 0;
|
|
20
|
-
const lumos_1 = require("@ckb-lumos/lumos");
|
|
21
|
-
const base_1 = require("@ckb-lumos/base");
|
|
22
|
-
const codec_1 = require("@ckb-lumos/codec");
|
|
23
|
-
const validator_1 = require("./validator");
|
|
24
|
-
const type_1 = require("./type");
|
|
25
|
-
const system_scripts_1 = require("../cmd/system-scripts");
|
|
26
|
-
const { ScriptValue } = base_1.values;
|
|
27
|
-
const networks = {
|
|
28
|
-
devnet: {
|
|
29
|
-
rpc_url: 'http://127.0.0.1:8114',
|
|
30
|
-
rpc: new lumos_1.RPC('http://127.0.0.1:8114'),
|
|
31
|
-
indexer: new lumos_1.Indexer('http://127.0.0.1:8114'),
|
|
32
|
-
},
|
|
33
|
-
testnet: {
|
|
34
|
-
rpc_url: 'https://testnet.ckb.dev/rpc',
|
|
35
|
-
rpc: new lumos_1.RPC('https://testnet.ckb.dev/rpc'),
|
|
36
|
-
indexer: new lumos_1.Indexer('https://testnet.ckb.dev/rpc'),
|
|
37
|
-
},
|
|
38
|
-
mainnet: {
|
|
39
|
-
rpc_url: 'https://mainnet.ckb.dev/rpc',
|
|
40
|
-
rpc: new lumos_1.RPC('https://mainnet.ckb.dev/rpc'),
|
|
41
|
-
indexer: new lumos_1.Indexer('https://mainnet.ckb.dev/rpc'),
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
class CKB {
|
|
45
|
-
constructor(network = type_1.Network.devnet) {
|
|
46
|
-
if (!(0, validator_1.isValidNetworkString)(network)) {
|
|
47
|
-
throw new Error('invalid network option');
|
|
48
|
-
}
|
|
49
|
-
this.network = network;
|
|
50
|
-
this.rpc_url = networks[network].rpc_url;
|
|
51
|
-
this.rpc = networks[network].rpc;
|
|
52
|
-
this.indexer = networks[network].indexer;
|
|
53
|
-
}
|
|
54
|
-
static generateAccountFromPrivateKey(privKey, lumosConfig) {
|
|
55
|
-
const pubKey = lumos_1.hd.key.privateToPublic(privKey);
|
|
56
|
-
const args = lumos_1.hd.key.publicKeyToBlake160(pubKey);
|
|
57
|
-
const template = lumosConfig.SCRIPTS['SECP256K1_BLAKE160'];
|
|
58
|
-
const lockScript = {
|
|
59
|
-
codeHash: template.CODE_HASH,
|
|
60
|
-
hashType: template.HASH_TYPE,
|
|
61
|
-
args: args,
|
|
62
|
-
};
|
|
63
|
-
const address = lumos_1.helpers.encodeToAddress(lockScript, { config: lumosConfig });
|
|
64
|
-
return {
|
|
65
|
-
lockScript,
|
|
66
|
-
address,
|
|
67
|
-
pubKey,
|
|
68
|
-
privKey,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
getLumosConfig() {
|
|
72
|
-
if (this.network === type_1.Network.devnet) {
|
|
73
|
-
return readPredefinedDevnetLumosConfig();
|
|
74
|
-
}
|
|
75
|
-
if (this.network === type_1.Network.testnet) {
|
|
76
|
-
return lumos_1.config.predefined.AGGRON4;
|
|
77
|
-
}
|
|
78
|
-
return lumos_1.config.predefined.LINA;
|
|
79
|
-
}
|
|
80
|
-
initializedLumosConfig() {
|
|
81
|
-
return lumos_1.config.initializeConfig(this.getLumosConfig());
|
|
82
|
-
}
|
|
83
|
-
capacityOf(address, lumosConfig) {
|
|
84
|
-
var _a, e_1, _b, _c;
|
|
85
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
const collector = this.indexer.collector({
|
|
87
|
-
lock: lumos_1.helpers.parseAddress(address, { config: lumosConfig }),
|
|
88
|
-
});
|
|
89
|
-
let balance = lumos_1.BI.from(0);
|
|
90
|
-
try {
|
|
91
|
-
for (var _d = true, _e = __asyncValues(collector.collect()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
92
|
-
_c = _f.value;
|
|
93
|
-
_d = false;
|
|
94
|
-
const cell = _c;
|
|
95
|
-
balance = balance.add(cell.cellOutput.capacity);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
99
|
-
finally {
|
|
100
|
-
try {
|
|
101
|
-
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
102
|
-
}
|
|
103
|
-
finally { if (e_1) throw e_1.error; }
|
|
104
|
-
}
|
|
105
|
-
return balance;
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
transfer(options, lumosConfig) {
|
|
109
|
-
var _a, e_2, _b, _c;
|
|
110
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
-
let txSkeleton = lumos_1.helpers.TransactionSkeleton({});
|
|
112
|
-
const fromScript = lumos_1.helpers.parseAddress(options.from, {
|
|
113
|
-
config: lumosConfig,
|
|
114
|
-
});
|
|
115
|
-
const toScript = lumos_1.helpers.parseAddress(options.to, { config: lumosConfig });
|
|
116
|
-
if (lumos_1.BI.from(options.amount).lt(lumos_1.BI.from('6100000000'))) {
|
|
117
|
-
throw new Error(`every cell's capacity must be at least 61 CKB, see https://medium.com/nervosnetwork/understanding-the-nervos-dao-and-cell-model-d68f38272c24`);
|
|
118
|
-
}
|
|
119
|
-
// additional 0.001 ckb for tx fee
|
|
120
|
-
// the tx fee could calculated by tx size
|
|
121
|
-
// this is just a simple example
|
|
122
|
-
const neededCapacity = lumos_1.BI.from(options.amount).add(100000);
|
|
123
|
-
let collectedSum = lumos_1.BI.from(0);
|
|
124
|
-
const collected = [];
|
|
125
|
-
const collector = this.indexer.collector({ lock: fromScript, type: 'empty' });
|
|
126
|
-
try {
|
|
127
|
-
for (var _d = true, _e = __asyncValues(collector.collect()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
128
|
-
_c = _f.value;
|
|
129
|
-
_d = false;
|
|
130
|
-
const cell = _c;
|
|
131
|
-
collectedSum = collectedSum.add(cell.cellOutput.capacity);
|
|
132
|
-
collected.push(cell);
|
|
133
|
-
if (collectedSum >= neededCapacity)
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
138
|
-
finally {
|
|
139
|
-
try {
|
|
140
|
-
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
141
|
-
}
|
|
142
|
-
finally { if (e_2) throw e_2.error; }
|
|
143
|
-
}
|
|
144
|
-
if (collectedSum.lt(neededCapacity)) {
|
|
145
|
-
throw new Error(`Not enough CKB, ${collectedSum} < ${neededCapacity}`);
|
|
146
|
-
}
|
|
147
|
-
const transferOutput = {
|
|
148
|
-
cellOutput: {
|
|
149
|
-
capacity: lumos_1.BI.from(options.amount).toHexString(),
|
|
150
|
-
lock: toScript,
|
|
151
|
-
},
|
|
152
|
-
data: '0x',
|
|
153
|
-
};
|
|
154
|
-
const changeOutput = {
|
|
155
|
-
cellOutput: {
|
|
156
|
-
capacity: collectedSum.sub(neededCapacity).toHexString(),
|
|
157
|
-
lock: fromScript,
|
|
158
|
-
},
|
|
159
|
-
data: '0x',
|
|
160
|
-
};
|
|
161
|
-
txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...collected));
|
|
162
|
-
txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(transferOutput, changeOutput));
|
|
163
|
-
txSkeleton = txSkeleton.update('cellDeps', (cellDeps) => cellDeps.push({
|
|
164
|
-
outPoint: {
|
|
165
|
-
txHash: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.TX_HASH,
|
|
166
|
-
index: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.INDEX,
|
|
167
|
-
},
|
|
168
|
-
depType: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.DEP_TYPE,
|
|
169
|
-
}));
|
|
170
|
-
const firstIndex = txSkeleton
|
|
171
|
-
.get('inputs')
|
|
172
|
-
.findIndex((input) => new ScriptValue(input.cellOutput.lock, { validate: false }).equals(new ScriptValue(fromScript, { validate: false })));
|
|
173
|
-
if (firstIndex !== -1) {
|
|
174
|
-
while (firstIndex >= txSkeleton.get('witnesses').size) {
|
|
175
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.push('0x'));
|
|
176
|
-
}
|
|
177
|
-
let witness = txSkeleton.get('witnesses').get(firstIndex);
|
|
178
|
-
const newWitnessArgs = {
|
|
179
|
-
/* 65-byte zeros in hex */
|
|
180
|
-
lock: '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
|
|
181
|
-
};
|
|
182
|
-
if (witness !== '0x') {
|
|
183
|
-
const witnessArgs = base_1.blockchain.WitnessArgs.unpack(codec_1.bytes.bytify(witness));
|
|
184
|
-
const lock = witnessArgs.lock;
|
|
185
|
-
if (!!lock && !!newWitnessArgs.lock && !codec_1.bytes.equal(lock, newWitnessArgs.lock)) {
|
|
186
|
-
throw new Error('Lock field in first witness is set aside for signature!');
|
|
187
|
-
}
|
|
188
|
-
const inputType = witnessArgs.inputType;
|
|
189
|
-
if (inputType) {
|
|
190
|
-
newWitnessArgs.inputType = inputType;
|
|
191
|
-
}
|
|
192
|
-
const outputType = witnessArgs.outputType;
|
|
193
|
-
if (outputType) {
|
|
194
|
-
newWitnessArgs.outputType = outputType;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
witness = codec_1.bytes.hexify(base_1.blockchain.WitnessArgs.pack(newWitnessArgs));
|
|
198
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.set(firstIndex, witness));
|
|
199
|
-
}
|
|
200
|
-
txSkeleton = lumos_1.commons.common.prepareSigningEntries(txSkeleton);
|
|
201
|
-
const message = txSkeleton.get('signingEntries').get(0).message;
|
|
202
|
-
const Sig = lumos_1.hd.key.signRecoverable(message, options.privKey);
|
|
203
|
-
const tx = lumos_1.helpers.sealTransaction(txSkeleton, [Sig]);
|
|
204
|
-
const hash = yield this.rpc.sendTransaction(tx, 'passthrough');
|
|
205
|
-
return hash;
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
transferAll(privateKey, toAddress, lumosConfig) {
|
|
209
|
-
var _a, e_3, _b, _c;
|
|
210
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
let txSkeleton = lumos_1.helpers.TransactionSkeleton({});
|
|
212
|
-
const from = CKB.generateAccountFromPrivateKey(privateKey, lumosConfig);
|
|
213
|
-
const fromScript = from.lockScript;
|
|
214
|
-
const toScript = lumos_1.helpers.parseAddress(toAddress, { config: lumosConfig });
|
|
215
|
-
const balance = yield this.capacityOf(from.address, lumosConfig);
|
|
216
|
-
// additional 0.001 ckb for tx fee
|
|
217
|
-
// the tx fee could calculated by tx size
|
|
218
|
-
// this is just a simple example
|
|
219
|
-
const neededCapacity = balance.sub(100000);
|
|
220
|
-
let collectedSum = lumos_1.BI.from(0);
|
|
221
|
-
const collected = [];
|
|
222
|
-
const collector = this.indexer.collector({ lock: fromScript, type: 'empty' });
|
|
223
|
-
try {
|
|
224
|
-
for (var _d = true, _e = __asyncValues(collector.collect()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
225
|
-
_c = _f.value;
|
|
226
|
-
_d = false;
|
|
227
|
-
const cell = _c;
|
|
228
|
-
collectedSum = collectedSum.add(cell.cellOutput.capacity);
|
|
229
|
-
collected.push(cell);
|
|
230
|
-
if (collectedSum >= neededCapacity)
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
235
|
-
finally {
|
|
236
|
-
try {
|
|
237
|
-
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
238
|
-
}
|
|
239
|
-
finally { if (e_3) throw e_3.error; }
|
|
240
|
-
}
|
|
241
|
-
if (collectedSum.lt(neededCapacity)) {
|
|
242
|
-
throw new Error(`Not enough CKB, ${collectedSum} < ${neededCapacity}`);
|
|
243
|
-
}
|
|
244
|
-
const transferOutput = {
|
|
245
|
-
cellOutput: {
|
|
246
|
-
capacity: neededCapacity.toHexString(),
|
|
247
|
-
lock: toScript,
|
|
248
|
-
},
|
|
249
|
-
data: '0x',
|
|
250
|
-
};
|
|
251
|
-
txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...collected));
|
|
252
|
-
txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(transferOutput));
|
|
253
|
-
txSkeleton = txSkeleton.update('cellDeps', (cellDeps) => cellDeps.push({
|
|
254
|
-
outPoint: {
|
|
255
|
-
txHash: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.TX_HASH,
|
|
256
|
-
index: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.INDEX,
|
|
257
|
-
},
|
|
258
|
-
depType: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.DEP_TYPE,
|
|
259
|
-
}));
|
|
260
|
-
const firstIndex = txSkeleton
|
|
261
|
-
.get('inputs')
|
|
262
|
-
.findIndex((input) => new ScriptValue(input.cellOutput.lock, { validate: false }).equals(new ScriptValue(fromScript, { validate: false })));
|
|
263
|
-
if (firstIndex !== -1) {
|
|
264
|
-
while (firstIndex >= txSkeleton.get('witnesses').size) {
|
|
265
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.push('0x'));
|
|
266
|
-
}
|
|
267
|
-
let witness = txSkeleton.get('witnesses').get(firstIndex);
|
|
268
|
-
const newWitnessArgs = {
|
|
269
|
-
/* 65-byte zeros in hex */
|
|
270
|
-
lock: '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
|
|
271
|
-
};
|
|
272
|
-
if (witness !== '0x') {
|
|
273
|
-
const witnessArgs = base_1.blockchain.WitnessArgs.unpack(codec_1.bytes.bytify(witness));
|
|
274
|
-
const lock = witnessArgs.lock;
|
|
275
|
-
if (!!lock && !!newWitnessArgs.lock && !codec_1.bytes.equal(lock, newWitnessArgs.lock)) {
|
|
276
|
-
throw new Error('Lock field in first witness is set aside for signature!');
|
|
277
|
-
}
|
|
278
|
-
const inputType = witnessArgs.inputType;
|
|
279
|
-
if (inputType) {
|
|
280
|
-
newWitnessArgs.inputType = inputType;
|
|
281
|
-
}
|
|
282
|
-
const outputType = witnessArgs.outputType;
|
|
283
|
-
if (outputType) {
|
|
284
|
-
newWitnessArgs.outputType = outputType;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
witness = codec_1.bytes.hexify(base_1.blockchain.WitnessArgs.pack(newWitnessArgs));
|
|
288
|
-
txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.set(firstIndex, witness));
|
|
289
|
-
}
|
|
290
|
-
txSkeleton = lumos_1.commons.common.prepareSigningEntries(txSkeleton);
|
|
291
|
-
const message = txSkeleton.get('signingEntries').get(0).message;
|
|
292
|
-
const Sig = lumos_1.hd.key.signRecoverable(message, privateKey);
|
|
293
|
-
const tx = lumos_1.helpers.sealTransaction(txSkeleton, [Sig]);
|
|
294
|
-
const hash = yield this.rpc.sendTransaction(tx, 'passthrough');
|
|
295
|
-
return hash;
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
exports.CKB = CKB;
|
|
300
|
-
function readPredefinedDevnetLumosConfig() {
|
|
301
|
-
try {
|
|
302
|
-
const systemScripts = (0, system_scripts_1.getSystemScriptsFromListHashes)();
|
|
303
|
-
if (systemScripts) {
|
|
304
|
-
return (0, system_scripts_1.toLumosConfig)(systemScripts);
|
|
305
|
-
}
|
|
306
|
-
throw new Error('systemScripts not found!');
|
|
307
|
-
}
|
|
308
|
-
catch (error) {
|
|
309
|
-
throw new Error('getSystemScriptsFromListHashes error' + error.message);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
exports.readPredefinedDevnetLumosConfig = readPredefinedDevnetLumosConfig;
|
|
File without changes
|