@offckb/cli 0.1.1 → 0.1.2-canary-a369f24.0
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/account/ckb-miner-and-faucet.key +1 -0
- package/ckb/devnet/ckb.toml +3 -0
- package/dist/cfg/account.d.ts +55 -0
- package/dist/cfg/account.js +66 -0
- package/dist/cfg/const.d.ts +6 -1
- package/dist/cfg/const.js +7 -2
- package/dist/cli.js +69 -21
- package/dist/cmd/balance.d.ts +4 -0
- package/dist/cmd/balance.js +28 -0
- package/dist/cmd/clean.js +3 -2
- package/dist/cmd/create.d.ts +3 -0
- package/dist/cmd/create.js +56 -0
- package/dist/cmd/deploy.d.ts +6 -0
- package/dist/cmd/deploy.js +137 -0
- package/dist/cmd/deposit.d.ts +4 -0
- package/dist/cmd/deposit.js +95 -0
- package/dist/cmd/develop/build-account.d.ts +1 -0
- package/dist/cmd/develop/build-account.js +9 -0
- package/dist/cmd/{genkey.d.ts → develop/genkey.d.ts} +1 -0
- package/dist/cmd/{genkey.js → develop/genkey.js} +3 -2
- package/dist/cmd/{init-chain.js → develop/init-chain.js} +4 -4
- package/dist/cmd/{install.js → develop/install.js} +1 -1
- package/dist/cmd/develop/lumos-config.d.ts +4 -0
- package/dist/cmd/{build-lumos-config.js → develop/lumos-config.js} +25 -13
- package/dist/cmd/init.d.ts +3 -2
- package/dist/cmd/init.js +17 -10
- package/dist/cmd/inject-config.d.ts +4 -0
- package/dist/cmd/inject-config.js +99 -0
- package/dist/cmd/list-hashes.js +2 -2
- package/dist/cmd/node.js +2 -2
- package/dist/cmd/transfer.d.ts +5 -0
- package/dist/cmd/transfer.js +41 -0
- package/dist/cmd/update-config.d.ts +1 -0
- package/dist/cmd/update-config.js +19 -0
- package/dist/util/ckb.d.ts +77 -0
- package/dist/util/ckb.js +299 -0
- package/dist/util/config.d.ts +8 -0
- package/dist/util/config.js +127 -0
- package/dist/util/fs.d.ts +12 -0
- package/dist/{util.js → util/fs.js} +86 -65
- package/dist/util/git.d.ts +2 -0
- package/dist/util/git.js +85 -0
- package/dist/util/link.d.ts +1 -0
- package/dist/util/link.js +7 -0
- package/dist/util/template.d.ts +14 -0
- package/dist/util/template.js +43 -0
- package/dist/util/type.d.ts +8 -0
- package/dist/util/type.js +10 -0
- package/dist/util/validator.d.ts +4 -0
- package/dist/util/validator.js +59 -0
- package/package.json +1 -1
- package/templates/offckb.config.ts +193 -0
- package/dist/cmd/build-lumos-config.d.ts +0 -99
- package/dist/util.d.ts +0 -13
- /package/dist/cmd/{init-chain.d.ts → develop/init-chain.d.ts} +0 -0
- /package/dist/cmd/{install.d.ts → develop/install.d.ts} +0 -0
- /package/dist/{encoding.d.ts → util/encoding.d.ts} +0 -0
- /package/dist/{encoding.js → util/encoding.js} +0 -0
package/dist/util/ckb.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
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.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 config_1 = require("./config");
|
|
24
|
+
const validator_1 = require("./validator");
|
|
25
|
+
const type_1 = require("./type");
|
|
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 (0, config_1.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;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { config } from '@ckb-lumos/lumos';
|
|
2
|
+
import { Network } from './type';
|
|
3
|
+
export declare function updateScriptInfoInOffCKBConfigTs(newConfig: config.Config, filePath: string, network: Network): void;
|
|
4
|
+
export declare function readUserDeployedScriptsInfo(network: Network): Record<string, config.ScriptConfig>;
|
|
5
|
+
export declare function readPredefinedDevnetLumosConfig(): config.Config;
|
|
6
|
+
export declare function readPredefinedMainnetLumosConfig(): config.Config;
|
|
7
|
+
export declare function readPredefinedTestnetLumosConfig(): config.Config;
|
|
8
|
+
export declare function buildFullLumosConfig(network: Network): any;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.buildFullLumosConfig = exports.readPredefinedTestnetLumosConfig = exports.readPredefinedMainnetLumosConfig = exports.readPredefinedDevnetLumosConfig = exports.readUserDeployedScriptsInfo = exports.updateScriptInfoInOffCKBConfigTs = void 0;
|
|
27
|
+
const fs = __importStar(require("fs"));
|
|
28
|
+
const path = __importStar(require("path"));
|
|
29
|
+
const const_1 = require("../cfg/const");
|
|
30
|
+
const lumos_1 = require("@ckb-lumos/lumos");
|
|
31
|
+
const type_1 = require("./type");
|
|
32
|
+
function updateScriptInfoInOffCKBConfigTs(newConfig, filePath, network) {
|
|
33
|
+
// Read the content of the offckb.config.ts file
|
|
34
|
+
let fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
35
|
+
if (network === type_1.Network.devnet) {
|
|
36
|
+
/// Define the regular expression pattern to match the JSON content
|
|
37
|
+
const regexPattern = /\/\/ ---devnet lumos config---([\s\S]*?)\/\/ ---end of devnet lumos config---/;
|
|
38
|
+
// Replace the old JSON content with the new JSON content using the regular expression
|
|
39
|
+
fileContent = fileContent.replace(regexPattern, `// ---devnet lumos config---\nconst lumosConfig: config.Config = ${JSON.stringify(newConfig, null, 2)} as config.Config;\n// ---end of devnet lumos config---`);
|
|
40
|
+
// Write the updated content back to the file
|
|
41
|
+
fs.writeFileSync(filePath, fileContent, 'utf-8');
|
|
42
|
+
}
|
|
43
|
+
if (network === type_1.Network.testnet) {
|
|
44
|
+
/// Define the regular expression pattern to match the JSON content
|
|
45
|
+
const regexPattern = /\/\/ ---testnet lumos config---([\s\S]*?)\/\/ ---end of testnet lumos config---/;
|
|
46
|
+
// Replace the old JSON content with the new JSON content using the regular expression
|
|
47
|
+
fileContent = fileContent.replace(regexPattern, `// ---testnet lumos config---\nconst testnetLumosConfig: config.Config = ${JSON.stringify(newConfig, null, 2)} as config.Config;\n// ---end of testnet lumos config---`);
|
|
48
|
+
// Write the updated content back to the file
|
|
49
|
+
fs.writeFileSync(filePath, fileContent, 'utf-8');
|
|
50
|
+
}
|
|
51
|
+
if (network === type_1.Network.mainnet) {
|
|
52
|
+
/// Define the regular expression pattern to match the JSON content
|
|
53
|
+
const regexPattern = /\/\/ ---mainnet lumos config---([\s\S]*?)\/\/ ---end of mainnet lumos config---/;
|
|
54
|
+
// Replace the old JSON content with the new JSON content using the regular expression
|
|
55
|
+
fileContent = fileContent.replace(regexPattern, `// ---mainnet lumos config---\nconst mainnetLumosConfig: config.Config = ${JSON.stringify(newConfig, null, 2)} as config.Config;\n// ---end of mainnet lumos config---`);
|
|
56
|
+
// Write the updated content back to the file
|
|
57
|
+
fs.writeFileSync(filePath, fileContent, 'utf-8');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.updateScriptInfoInOffCKBConfigTs = updateScriptInfoInOffCKBConfigTs;
|
|
61
|
+
function readUserDeployedScriptsInfo(network) {
|
|
62
|
+
const deployedScriptsInfo = {};
|
|
63
|
+
// Read all files in the folder
|
|
64
|
+
const folder = path.resolve(const_1.deployedContractInfoFolderPath, network);
|
|
65
|
+
if (!fs.existsSync(folder)) {
|
|
66
|
+
return deployedScriptsInfo;
|
|
67
|
+
}
|
|
68
|
+
const files = fs.readdirSync(folder);
|
|
69
|
+
// Iterate through each file
|
|
70
|
+
files.forEach((fileName) => {
|
|
71
|
+
// Construct the full file path
|
|
72
|
+
const filePath = path.join(folder, fileName);
|
|
73
|
+
// Check if the file is a JSON file
|
|
74
|
+
if (fileName.endsWith('.json')) {
|
|
75
|
+
try {
|
|
76
|
+
// Read the file content
|
|
77
|
+
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
78
|
+
// Parse the JSON content
|
|
79
|
+
const scriptContent = JSON.parse(fileContent);
|
|
80
|
+
const rawFileName = path.parse(fileName).name.replace(/-/g, '_');
|
|
81
|
+
// Add the file content to the result object with the file name as the key
|
|
82
|
+
deployedScriptsInfo[rawFileName] = scriptContent;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.error(`Error reading or parsing file '${fileName}':`, error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return deployedScriptsInfo;
|
|
90
|
+
}
|
|
91
|
+
exports.readUserDeployedScriptsInfo = readUserDeployedScriptsInfo;
|
|
92
|
+
function readPredefinedDevnetLumosConfig() {
|
|
93
|
+
const filePath = path.resolve(const_1.dappTemplatePath, 'config.json');
|
|
94
|
+
try {
|
|
95
|
+
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
96
|
+
const jsonData = JSON.parse(fileContent);
|
|
97
|
+
return jsonData;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
throw new Error('Error reading the json file:' + error.message);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.readPredefinedDevnetLumosConfig = readPredefinedDevnetLumosConfig;
|
|
104
|
+
function readPredefinedMainnetLumosConfig() {
|
|
105
|
+
const predefined = lumos_1.config.predefined.LINA;
|
|
106
|
+
// add more example like spore;
|
|
107
|
+
return predefined;
|
|
108
|
+
}
|
|
109
|
+
exports.readPredefinedMainnetLumosConfig = readPredefinedMainnetLumosConfig;
|
|
110
|
+
function readPredefinedTestnetLumosConfig() {
|
|
111
|
+
const predefined = lumos_1.config.predefined.AGGRON4;
|
|
112
|
+
// add more example like spore;
|
|
113
|
+
return predefined;
|
|
114
|
+
}
|
|
115
|
+
exports.readPredefinedTestnetLumosConfig = readPredefinedTestnetLumosConfig;
|
|
116
|
+
function buildFullLumosConfig(network) {
|
|
117
|
+
const config = network === type_1.Network.devnet
|
|
118
|
+
? readPredefinedDevnetLumosConfig()
|
|
119
|
+
: network === type_1.Network.testnet
|
|
120
|
+
? readPredefinedTestnetLumosConfig()
|
|
121
|
+
: readPredefinedMainnetLumosConfig();
|
|
122
|
+
const userDeployedScripts = readUserDeployedScriptsInfo(network);
|
|
123
|
+
const conf = JSON.parse(JSON.stringify(config));
|
|
124
|
+
conf.SCRIPTS = Object.assign(Object.assign({}, config.SCRIPTS), userDeployedScripts);
|
|
125
|
+
return conf;
|
|
126
|
+
}
|
|
127
|
+
exports.buildFullLumosConfig = buildFullLumosConfig;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function isFolderExists(folderPath: string): boolean;
|
|
2
|
+
export declare function copyFolderSync(source: string, destination: string): void;
|
|
3
|
+
export declare function copyFileSync(source: string, target: string): void;
|
|
4
|
+
export declare function copyFilesWithExclusion(sourceDir: string, destinationDir: string, excludedFolders: string[]): Promise<void>;
|
|
5
|
+
export declare function copyRecursive(source: string, destination: string, excludedFolders: string[]): Promise<void>;
|
|
6
|
+
export declare function updateVersionInTSFile(newVersion: string, filePath: string): void;
|
|
7
|
+
export declare function readFileToUint8Array(filePath: string): Promise<Uint8Array>;
|
|
8
|
+
export declare function convertFilenameToUppercase(filePath: string): string;
|
|
9
|
+
export declare function listBinaryFilesInFolder(folderPath: string): string[];
|
|
10
|
+
export declare function isBinaryFile(filePath: string): boolean;
|
|
11
|
+
export declare function isAbsolutePath(filePath: string): boolean;
|
|
12
|
+
export declare function findFileInFolder(folderPath: string, fileName: string): string | null;
|
|
@@ -31,17 +31,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
31
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
-
};
|
|
37
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.
|
|
39
|
-
const child_process_1 = require("child_process");
|
|
35
|
+
exports.findFileInFolder = exports.isAbsolutePath = exports.isBinaryFile = exports.listBinaryFilesInFolder = exports.convertFilenameToUppercase = exports.readFileToUint8Array = exports.updateVersionInTSFile = exports.copyRecursive = exports.copyFilesWithExclusion = exports.copyFileSync = exports.copyFolderSync = exports.isFolderExists = void 0;
|
|
40
36
|
const fs = __importStar(require("fs"));
|
|
41
37
|
const path = __importStar(require("path"));
|
|
42
|
-
const const_1 = require("./cfg/const");
|
|
43
|
-
const axios_1 = __importDefault(require("axios"));
|
|
44
|
-
const const_2 = require("./cfg/const");
|
|
45
38
|
function isFolderExists(folderPath) {
|
|
46
39
|
try {
|
|
47
40
|
// Check if the path exists
|
|
@@ -128,70 +121,98 @@ function copyRecursive(source, destination, excludedFolders) {
|
|
|
128
121
|
});
|
|
129
122
|
}
|
|
130
123
|
exports.copyRecursive = copyRecursive;
|
|
131
|
-
function
|
|
124
|
+
function updateVersionInTSFile(newVersion, filePath) {
|
|
132
125
|
try {
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
// Read the contents of the TS file
|
|
127
|
+
const fileContents = fs.readFileSync(filePath, 'utf8');
|
|
128
|
+
// Use a regular expression to find the version value
|
|
129
|
+
const regex = /const\s+offCKBConfig:\s+OffCKBConfig\s*=\s*\{\s*version:\s*'([^']+)'/;
|
|
130
|
+
const match = fileContents.match(regex);
|
|
131
|
+
if (match) {
|
|
132
|
+
// Replace the version value with the new value
|
|
133
|
+
const updatedContents = fileContents.replace(regex, `const offCKBConfig: OffCKBConfig = {\n version: '${newVersion}'`);
|
|
134
|
+
// Write the updated contents back to the file
|
|
135
|
+
fs.writeFileSync(filePath, updatedContents, 'utf8');
|
|
136
|
+
console.log(`Version updated to '${newVersion}' in ${filePath}`);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
console.error(`Could not find version value in ${filePath}`);
|
|
140
|
+
}
|
|
135
141
|
}
|
|
136
142
|
catch (error) {
|
|
137
|
-
|
|
143
|
+
console.error(`Error updating version in ${filePath}: ${error}`);
|
|
138
144
|
}
|
|
139
145
|
}
|
|
140
|
-
exports.
|
|
141
|
-
function
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
fs.
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
146
|
+
exports.updateVersionInTSFile = updateVersionInTSFile;
|
|
147
|
+
function readFileToUint8Array(filePath) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
fs.readFile(filePath, (err, data) => {
|
|
151
|
+
if (err) {
|
|
152
|
+
reject(err);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
resolve(new Uint8Array(data));
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
exports.readFileToUint8Array = readFileToUint8Array;
|
|
161
|
+
function convertFilenameToUppercase(filePath) {
|
|
162
|
+
// Extract the filename from the file path
|
|
163
|
+
const filename = path.basename(filePath);
|
|
164
|
+
// Convert the filename to uppercase
|
|
165
|
+
const uppercaseFilename = filename.toUpperCase();
|
|
166
|
+
return uppercaseFilename;
|
|
167
|
+
}
|
|
168
|
+
exports.convertFilenameToUppercase = convertFilenameToUppercase;
|
|
169
|
+
function listBinaryFilesInFolder(folderPath) {
|
|
170
|
+
// Check if the provided path is a directory
|
|
171
|
+
if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
|
|
172
|
+
throw new Error(`${folderPath} is not a valid directory.`);
|
|
173
|
+
}
|
|
174
|
+
// Read the contents of the directory
|
|
175
|
+
const files = fs.readdirSync(folderPath);
|
|
176
|
+
// Filter out only the binary files (assuming they have extensions like .exe, .bin, .dll, etc.)
|
|
177
|
+
const binaryFiles = files.filter((file) => {
|
|
178
|
+
const filePath = path.join(folderPath, file);
|
|
179
|
+
// Check if the file is a regular file and not a directory
|
|
180
|
+
return fs.statSync(filePath).isFile() && isBinaryFile(filePath);
|
|
181
|
+
});
|
|
182
|
+
return binaryFiles;
|
|
183
|
+
}
|
|
184
|
+
exports.listBinaryFilesInFolder = listBinaryFilesInFolder;
|
|
185
|
+
// Function to check if a file is binary
|
|
186
|
+
function isBinaryFile(filePath) {
|
|
187
|
+
const buffer = fs.readFileSync(filePath);
|
|
188
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
189
|
+
// If any byte has a value greater than 127, it's likely a binary file
|
|
190
|
+
if (buffer[i] > 127) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
181
193
|
}
|
|
182
|
-
|
|
194
|
+
return false;
|
|
183
195
|
}
|
|
184
|
-
exports.
|
|
185
|
-
function
|
|
186
|
-
return
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
196
|
+
exports.isBinaryFile = isBinaryFile;
|
|
197
|
+
function isAbsolutePath(filePath) {
|
|
198
|
+
return path.isAbsolute(filePath);
|
|
199
|
+
}
|
|
200
|
+
exports.isAbsolutePath = isAbsolutePath;
|
|
201
|
+
function findFileInFolder(folderPath, fileName) {
|
|
202
|
+
const files = fs.readdirSync(folderPath);
|
|
203
|
+
for (const file of files) {
|
|
204
|
+
const filePath = path.join(folderPath, file);
|
|
205
|
+
const stats = fs.statSync(filePath);
|
|
206
|
+
if (stats.isDirectory()) {
|
|
207
|
+
const foundFilePath = findFileInFolder(filePath, fileName);
|
|
208
|
+
if (foundFilePath) {
|
|
209
|
+
return foundFilePath;
|
|
210
|
+
}
|
|
191
211
|
}
|
|
192
|
-
|
|
193
|
-
|
|
212
|
+
else if (file === fileName) {
|
|
213
|
+
return filePath;
|
|
194
214
|
}
|
|
195
|
-
}
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
196
217
|
}
|
|
197
|
-
exports.
|
|
218
|
+
exports.findFileInFolder = findFileInFolder;
|