@offckb/cli 0.3.0-rc4 → 0.3.0-rc6

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.
Files changed (49) hide show
  1. package/README.md +22 -21
  2. package/dist/cfg/setting.d.ts +11 -2
  3. package/dist/cfg/setting.js +2 -2
  4. package/dist/cli.js +19 -6
  5. package/dist/cmd/balance.d.ts +1 -1
  6. package/dist/cmd/balance.js +6 -8
  7. package/dist/cmd/debug.d.ts +1 -1
  8. package/dist/cmd/debug.js +5 -5
  9. package/dist/cmd/deploy.d.ts +2 -1
  10. package/dist/cmd/deploy.js +4 -3
  11. package/dist/cmd/deposit.d.ts +3 -2
  12. package/dist/cmd/deposit.js +30 -31
  13. package/dist/cmd/list-hashes.d.ts +1 -1
  14. package/dist/cmd/my-scripts.d.ts +1 -1
  15. package/dist/cmd/my-scripts.js +2 -2
  16. package/dist/cmd/node.js +2 -2
  17. package/dist/cmd/proxy-rpc.d.ts +1 -1
  18. package/dist/cmd/proxy-rpc.js +4 -4
  19. package/dist/cmd/system-scripts.d.ts +70 -2
  20. package/dist/cmd/system-scripts.js +4 -0
  21. package/dist/cmd/transfer-all.d.ts +6 -0
  22. package/dist/cmd/transfer-all.js +38 -0
  23. package/dist/cmd/transfer.d.ts +3 -2
  24. package/dist/cmd/transfer.js +9 -11
  25. package/dist/deploy/index.d.ts +2 -2
  26. package/dist/deploy/index.js +3 -3
  27. package/dist/deploy/migration.d.ts +2 -2
  28. package/dist/deploy/migration.js +5 -5
  29. package/dist/deploy/toml.d.ts +7 -3
  30. package/dist/deploy/util.d.ts +1 -1
  31. package/dist/deploy/util.js +4 -4
  32. package/dist/node/install.js +3 -4
  33. package/dist/scripts/gen.js +4 -4
  34. package/dist/scripts/util.d.ts +1 -1
  35. package/dist/sdk/ckb.d.ts +17 -4
  36. package/dist/sdk/ckb.js +79 -5
  37. package/dist/template/option.js +17 -5
  38. package/dist/tools/moleculec-es.js +3 -4
  39. package/dist/tools/rpc-proxy.d.ts +1 -2
  40. package/dist/tools/rpc-proxy.js +32 -45
  41. package/dist/{util/type.d.ts → type/base.d.ts} +2 -0
  42. package/dist/util/request.d.ts +6 -6
  43. package/dist/util/request.js +15 -11
  44. package/dist/util/validator.js +2 -2
  45. package/package.json +5 -4
  46. package/dist/template/option.json +0 -16
  47. package/dist/util/ckb.d.ts +0 -141
  48. package/dist/util/ckb.js +0 -312
  49. /package/dist/{util/type.js → type/base.js} +0 -0
@@ -10,27 +10,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.transfer = void 0;
13
- const ckb_1 = require("../util/ckb");
14
- const type_1 = require("../util/type");
13
+ const ckb_1 = require("../sdk/ckb");
14
+ const base_1 = require("../type/base");
15
15
  const link_1 = require("../util/link");
16
16
  const validator_1 = require("../util/validator");
17
- function transfer(toAddress, amount, opt = { network: type_1.Network.devnet }) {
17
+ function transfer(toAddress, amountInCKB, opt = { network: base_1.Network.devnet, proxyRpc: false }) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
19
  const network = opt.network;
20
20
  (0, validator_1.validateNetworkOpt)(network);
21
21
  if (opt.privkey == null) {
22
22
  throw new Error('--privkey is required!');
23
23
  }
24
+ const isEnableProxyRpc = opt.proxyRpc;
24
25
  const privateKey = opt.privkey;
25
- const ckb = new ckb_1.CKB(network);
26
- const lumosConfig = ckb.getLumosConfig();
27
- const from = ckb_1.CKB.generateAccountFromPrivateKey(privateKey, lumosConfig);
26
+ const ckb = new ckb_1.CKB({ network, isEnableProxyRpc });
28
27
  const txHash = yield ckb.transfer({
29
- from: from.address,
30
- to: toAddress,
31
- amount: amount,
32
- privKey: privateKey,
33
- }, lumosConfig);
28
+ toAddress,
29
+ amountInCKB,
30
+ privateKey,
31
+ });
34
32
  if (network === 'testnet') {
35
33
  console.log(`Successfully transfer, check ${(0, link_1.buildTestnetTxLink)(txHash)} for details.`);
36
34
  return;
@@ -1,8 +1,8 @@
1
1
  import { DeploymentOptions } from '../deploy/toml';
2
2
  import { DeploymentRecipe } from '../deploy/migration';
3
- import { HexString } from '@ckb-lumos/lumos';
4
- import { Network } from '../util/type';
3
+ import { Network } from '../type/base';
5
4
  import { CKB } from '../sdk/ckb';
5
+ import { HexString } from '../type/base';
6
6
  export type DeployBinaryReturnType = ReturnType<typeof deployBinary>;
7
7
  export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
8
8
  export type DeployedInterfaceType = UnwrapPromise<DeployBinaryReturnType>;
@@ -15,12 +15,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.deployBinary = exports.deployBinaries = exports.recordDeployResult = exports.getToDeployBinsPath = void 0;
16
16
  const toml_1 = require("../deploy/toml");
17
17
  const migration_1 = require("../deploy/migration");
18
- const utils_1 = require("@ckb-lumos/lumos/utils");
19
18
  const gen_1 = require("../scripts/gen");
20
19
  const offckb_config_1 = require("../template/offckb-config");
21
20
  const fs_1 = require("../util/fs");
22
21
  const path_1 = __importDefault(require("path"));
23
22
  const fs_2 = __importDefault(require("fs"));
23
+ const core_1 = require("@ckb-ccc/core");
24
24
  function getToDeployBinsPath() {
25
25
  const userOffCKBConfigPath = path_1.default.resolve(process.cwd(), 'offckb.config.ts');
26
26
  const fileContent = fs_2.default.readFileSync(userOffCKBConfigPath, 'utf-8');
@@ -98,7 +98,7 @@ function deployBinary(binPath, privateKey, enableTypeId, ckb) {
98
98
  if (enableTypeId && typeIdScript == null) {
99
99
  throw new Error('type id script is null while enableTypeId is true.');
100
100
  }
101
- const typeIdScriptHash = enableTypeId ? (0, utils_1.computeScriptHash)(typeIdScript) : undefined;
101
+ const typeIdScriptHash = enableTypeId ? core_1.ccc.Script.from(typeIdScript).hash() : undefined;
102
102
  // todo: handle multiple cell recipes?
103
103
  return {
104
104
  deploymentOptions: {
@@ -114,7 +114,7 @@ function deployBinary(binPath, privateKey, enableTypeId, ckb) {
114
114
  txHash,
115
115
  index: '0x' + index.toString(16),
116
116
  occupiedCapacity,
117
- dataHash: (0, utils_1.ckbHash)(tx.outputsData[+index]),
117
+ dataHash: core_1.ccc.hashCkb(tx.outputsData[+index]),
118
118
  typeId: typeIdScriptHash,
119
119
  },
120
120
  ],
@@ -1,5 +1,5 @@
1
- import { HexNumber } from '@ckb-lumos/lumos';
2
- import { Network } from '../util/type';
1
+ import { Network } from '../type/base';
2
+ import { HexNumber } from '../type/base';
3
3
  export interface CellRecipe {
4
4
  name: string;
5
5
  txHash: string;
@@ -27,25 +27,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.deploymentRecipeFromJson = exports.deploymentRecipeToJson = exports.getNewestMigrationFile = exports.getMigrationFolderPath = exports.getFormattedMigrationDate = exports.readDeploymentMigrationFile = exports.generateDeploymentMigrationFile = exports.Migration = void 0;
30
- const type_1 = require("../util/type");
30
+ const base_1 = require("../type/base");
31
31
  const path_1 = __importStar(require("path"));
32
32
  const fs_1 = __importDefault(require("fs"));
33
33
  const util_1 = require("./util");
34
34
  class Migration {
35
- static find(scriptName, network = type_1.Network.devnet) {
35
+ static find(scriptName, network = base_1.Network.devnet) {
36
36
  const filePath = getMigrationFolderPath(scriptName, network);
37
37
  const migrationFile = getNewestMigrationFile(filePath);
38
38
  if (migrationFile == null)
39
39
  return null;
40
40
  return readDeploymentMigrationFile(migrationFile);
41
41
  }
42
- static isDeployed(scriptName, network = type_1.Network.devnet) {
42
+ static isDeployed(scriptName, network = base_1.Network.devnet) {
43
43
  const deploymentReceipt = Migration.find(scriptName, network);
44
44
  if (deploymentReceipt == null)
45
45
  return false;
46
46
  return true;
47
47
  }
48
- static isDeployedWithTypeId(scriptName, network = type_1.Network.devnet) {
48
+ static isDeployedWithTypeId(scriptName, network = base_1.Network.devnet) {
49
49
  const isDeployed = this.isDeployed(scriptName, network);
50
50
  if (isDeployed === false)
51
51
  return false;
@@ -57,7 +57,7 @@ class Migration {
57
57
  }
58
58
  }
59
59
  exports.Migration = Migration;
60
- function generateDeploymentMigrationFile(name, deploymentRecipe, network = type_1.Network.devnet) {
60
+ function generateDeploymentMigrationFile(name, deploymentRecipe, network = base_1.Network.devnet) {
61
61
  const cellRecipes = deploymentRecipe.cellRecipes;
62
62
  const depGroupRecipes = deploymentRecipe.depGroupRecipes;
63
63
  const jsonString = JSON.stringify(deploymentRecipeToJson({ cellRecipes, depGroupRecipes }), null, 2);
@@ -1,11 +1,15 @@
1
- import { Script } from '@ckb-lumos/lumos';
2
- import { Network } from '../util/type';
1
+ import { Network } from '../type/base';
3
2
  import { HashType } from '@ckb-ccc/core';
3
+ import { HexString } from '../type/base';
4
4
  export interface DeploymentOptions {
5
5
  name: string;
6
6
  binFilePath: string;
7
7
  enableTypeId: boolean;
8
- lockScript: Script;
8
+ lockScript: {
9
+ codeHash: HexString;
10
+ hashType: HashType;
11
+ args: HexString;
12
+ };
9
13
  }
10
14
  export interface DeploymentToml {
11
15
  cells: {
@@ -1,2 +1,2 @@
1
- import { Network } from '../util/type';
1
+ import { Network } from '../type/base';
2
2
  export declare function getContractsPath(network: Network): string;
@@ -2,16 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getContractsPath = void 0;
4
4
  const setting_1 = require("../cfg/setting");
5
- const type_1 = require("../util/type");
5
+ const base_1 = require("../type/base");
6
6
  function getContractsPath(network) {
7
7
  const settings = (0, setting_1.readSettings)();
8
- if (network === type_1.Network.devnet) {
8
+ if (network === base_1.Network.devnet) {
9
9
  return settings.devnet.contractsPath;
10
10
  }
11
- if (network === type_1.Network.testnet) {
11
+ if (network === base_1.Network.testnet) {
12
12
  return settings.testnet.contractsPath;
13
13
  }
14
- if (network === type_1.Network.mainnet) {
14
+ if (network === base_1.Network.mainnet) {
15
15
  return settings.mainnet.contractsPath;
16
16
  }
17
17
  throw new Error(`invalid network ${network}`);
@@ -97,10 +97,9 @@ exports.downloadCKBBinaryAndUnzip = downloadCKBBinaryAndUnzip;
97
97
  function downloadAndSaveCKBBinary(version, tempFilePath) {
98
98
  return __awaiter(this, void 0, void 0, function* () {
99
99
  const downloadURL = buildDownloadUrl(version);
100
- const response = yield request_1.Request.get(downloadURL, {
101
- responseType: 'arraybuffer',
102
- });
103
- fs.writeFileSync(tempFilePath, response.data);
100
+ const response = yield request_1.Request.send(downloadURL);
101
+ const arrayBuffer = yield response.arrayBuffer();
102
+ fs.writeFileSync(tempFilePath, Buffer.from(arrayBuffer));
104
103
  });
105
104
  }
106
105
  exports.downloadAndSaveCKBBinary = downloadAndSaveCKBBinary;
@@ -31,7 +31,7 @@ const fs = __importStar(require("fs"));
31
31
  const public_1 = __importDefault(require("./public"));
32
32
  const system_scripts_1 = require("../cmd/system-scripts");
33
33
  const path_1 = __importDefault(require("path"));
34
- const type_1 = require("../util/type");
34
+ const base_1 = require("../type/base");
35
35
  const util_1 = require("./util");
36
36
  function genSystemScripts() {
37
37
  const devnetScripts = (0, system_scripts_1.getSystemScriptsFromListHashes)();
@@ -54,9 +54,9 @@ function genSystemScriptsJsonFile(filePath) {
54
54
  exports.genSystemScriptsJsonFile = genSystemScriptsJsonFile;
55
55
  function genMyScripts() {
56
56
  const networkMyScripts = {
57
- devnet: (0, util_1.readUserDeployedScriptsInfo)(type_1.Network.devnet),
58
- testnet: (0, util_1.readUserDeployedScriptsInfo)(type_1.Network.testnet),
59
- mainnet: (0, util_1.readUserDeployedScriptsInfo)(type_1.Network.mainnet),
57
+ devnet: (0, util_1.readUserDeployedScriptsInfo)(base_1.Network.devnet),
58
+ testnet: (0, util_1.readUserDeployedScriptsInfo)(base_1.Network.testnet),
59
+ mainnet: (0, util_1.readUserDeployedScriptsInfo)(base_1.Network.mainnet),
60
60
  };
61
61
  return networkMyScripts;
62
62
  }
@@ -1,3 +1,3 @@
1
1
  import { MyScriptsRecord } from '../scripts/type';
2
- import { Network } from '../util/type';
2
+ import { Network } from '../type/base';
3
3
  export declare function readUserDeployedScriptsInfo(network: Network): MyScriptsRecord;
package/dist/sdk/ckb.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ccc, Script } from '@ckb-ccc/core';
2
- import { Network } from '../util/type';
3
- import { HexString } from '@ckb-lumos/lumos';
2
+ import { Network, HexNumber, HexString } from '../type/base';
4
3
  export declare class CKBProps {
5
4
  network?: Network;
5
+ feeRate?: number;
6
6
  isEnableProxyRpc?: boolean;
7
7
  }
8
8
  export interface DeploymentResult {
@@ -12,12 +12,25 @@ export interface DeploymentResult {
12
12
  isTypeId: boolean;
13
13
  typeId?: Script;
14
14
  }
15
+ export interface TransferOption {
16
+ privateKey: HexString;
17
+ toAddress: string;
18
+ amountInCKB: HexNumber;
19
+ }
20
+ export type TransferAllOption = Pick<TransferOption, 'privateKey' | 'toAddress'>;
15
21
  export declare class CKB {
16
22
  network: Network;
23
+ feeRate: number;
24
+ isEnableProxyRpc: boolean;
17
25
  private client;
18
- constructor({ network, isEnableProxyRpc }: CKBProps);
19
- private buildSigner;
26
+ constructor({ network, feeRate, isEnableProxyRpc }: CKBProps);
27
+ buildSigner(privateKey: HexString): ccc.SignerCkbPrivateKey;
28
+ buildSecp256k1Address(privateKey: HexString): Promise<string>;
20
29
  waitForTxConfirm(txHash: HexString, timeout?: number): Promise<void>;
30
+ waitForBlocksBy(interval: number): Promise<void>;
31
+ balance(address: string): Promise<string>;
32
+ transfer({ privateKey, toAddress, amountInCKB }: TransferOption): Promise<HexString>;
33
+ transferAll({ privateKey, toAddress }: TransferAllOption): Promise<HexString>;
21
34
  deployScript(scriptBinBytes: Uint8Array, privateKey: string): Promise<DeploymentResult>;
22
35
  deployNewTypeIDScript(scriptBinBytes: Uint8Array, privateKey: string): Promise<DeploymentResult>;
23
36
  upgradeTypeIdScript(scriptName: string, newScriptBinBytes: Uint8Array, privateKey: HexString): Promise<DeploymentResult>;
package/dist/sdk/ckb.js CHANGED
@@ -13,20 +13,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.CKB = exports.CKBProps = void 0;
15
15
  const core_1 = require("@ckb-ccc/core");
16
- const type_1 = require("../util/type");
17
16
  const validator_1 = require("../util/validator");
18
17
  const network_1 = require("./network");
19
18
  const private_1 = require("../scripts/private");
20
19
  const migration_1 = require("../deploy/migration");
20
+ const base_1 = require("../type/base");
21
21
  class CKBProps {
22
22
  }
23
23
  exports.CKBProps = CKBProps;
24
24
  class CKB {
25
- constructor({ network = type_1.Network.devnet, isEnableProxyRpc = false }) {
25
+ constructor({ network = base_1.Network.devnet, feeRate = 1000, isEnableProxyRpc = false }) {
26
26
  if (!(0, validator_1.isValidNetworkString)(network)) {
27
27
  throw new Error('invalid network option');
28
28
  }
29
29
  this.network = network;
30
+ this.feeRate = feeRate;
31
+ this.isEnableProxyRpc = isEnableProxyRpc;
30
32
  if (isEnableProxyRpc === true) {
31
33
  this.client =
32
34
  network === 'mainnet'
@@ -54,6 +56,13 @@ class CKB {
54
56
  const signer = new core_1.ccc.SignerCkbPrivateKey(this.client, privateKey);
55
57
  return signer;
56
58
  }
59
+ buildSecp256k1Address(privateKey) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const signer = this.buildSigner(privateKey);
62
+ const address = yield signer.getAddressObjSecp256k1();
63
+ return address.toString();
64
+ });
65
+ }
57
66
  waitForTxConfirm(txHash, timeout = 60000) {
58
67
  return __awaiter(this, void 0, void 0, function* () {
59
68
  const query = () => __awaiter(this, void 0, void 0, function* () {
@@ -68,6 +77,71 @@ class CKB {
68
77
  return waitFor(query, timeout, 5000);
69
78
  });
70
79
  }
80
+ waitForBlocksBy(interval) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ if (interval < 0)
83
+ throw new Error('interval must be number >= 0');
84
+ const timeout = interval * 50000; // block interval is 18 secs, we set limit to 30s
85
+ const tip = yield this.client.getTip();
86
+ const blockNum = tip + BigInt(interval);
87
+ const query = () => __awaiter(this, void 0, void 0, function* () {
88
+ const res = yield this.client.getBlockByNumber(blockNum);
89
+ if (res) {
90
+ return true;
91
+ }
92
+ else {
93
+ return false;
94
+ }
95
+ });
96
+ return waitFor(query, timeout, 5000);
97
+ });
98
+ }
99
+ balance(address) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ const lock = (yield core_1.ccc.Address.fromString(address, this.client)).script;
102
+ const balanceInShannon = yield this.client.getBalanceSingle(lock);
103
+ const balanceInCKB = core_1.ccc.fixedPointToString(balanceInShannon);
104
+ return balanceInCKB;
105
+ });
106
+ }
107
+ transfer({ privateKey, toAddress, amountInCKB }) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ const signer = this.buildSigner(privateKey);
110
+ const to = yield core_1.ccc.Address.fromString(toAddress, this.client);
111
+ const tx = core_1.ccc.Transaction.from({
112
+ outputs: [
113
+ {
114
+ capacity: core_1.ccc.fixedPointFrom(amountInCKB),
115
+ lock: to.script,
116
+ },
117
+ ],
118
+ });
119
+ yield tx.completeInputsByCapacity(signer);
120
+ yield tx.completeFeeBy(signer, this.feeRate);
121
+ const txHash = yield signer.sendTransaction(tx);
122
+ return txHash;
123
+ });
124
+ }
125
+ transferAll({ privateKey, toAddress }) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const signer = this.buildSigner(privateKey);
128
+ const to = yield core_1.ccc.Address.fromString(toAddress, this.client);
129
+ const balanceInCKB = yield this.balance((yield signer.getRecommendedAddressObj()).toString());
130
+ // leave 0.001 ckb for tx fee
131
+ const amountInCKB = core_1.ccc.fixedPointFrom(balanceInCKB) - core_1.ccc.fixedPointFrom(0.001);
132
+ const tx = core_1.ccc.Transaction.from({
133
+ outputs: [
134
+ {
135
+ capacity: core_1.ccc.fixedPointFrom(amountInCKB),
136
+ lock: to.script,
137
+ },
138
+ ],
139
+ });
140
+ yield tx.completeInputsByCapacity(signer);
141
+ const txHash = yield signer.sendTransaction(tx);
142
+ return txHash;
143
+ });
144
+ }
71
145
  deployScript(scriptBinBytes, privateKey) {
72
146
  return __awaiter(this, void 0, void 0, function* () {
73
147
  const signer = this.buildSigner(privateKey);
@@ -81,7 +155,7 @@ class CKB {
81
155
  outputsData: [scriptBinBytes],
82
156
  });
83
157
  yield tx.completeInputsByCapacity(signer);
84
- yield tx.completeFeeBy(signer, 1000);
158
+ yield tx.completeFeeBy(signer, this.feeRate);
85
159
  const txHash = yield signer.sendTransaction(tx);
86
160
  return { txHash, tx, scriptOutputCellIndex: 0, isTypeId: false };
87
161
  });
@@ -104,7 +178,7 @@ class CKB {
104
178
  throw new Error('Unexpected disappeared output');
105
179
  }
106
180
  typeIdTx.outputs[0].type.args = core_1.ccc.hashTypeId(typeIdTx.inputs[0], 0);
107
- yield typeIdTx.completeFeeBy(signer, 1000);
181
+ yield typeIdTx.completeFeeBy(signer, this.feeRate);
108
182
  const txHash = yield signer.sendTransaction(typeIdTx);
109
183
  return { txHash, tx: typeIdTx, scriptOutputCellIndex: 0, isTypeId: true, typeId: typeIdTx.outputs[0].type };
110
184
  });
@@ -152,7 +226,7 @@ class CKB {
152
226
  throw new Error('Unexpected disappeared output');
153
227
  }
154
228
  typeIdTx.outputs[0].type.args = typeIdArgs;
155
- yield typeIdTx.completeFeeBy(signer, 1000);
229
+ yield typeIdTx.completeFeeBy(signer, this.feeRate);
156
230
  const txHash = yield signer.sendTransaction(typeIdTx);
157
231
  return { txHash, tx: typeIdTx, scriptOutputCellIndex: 0, isTypeId: true, typeId: typeIdTx.outputs[0].type };
158
232
  });
@@ -1,11 +1,23 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.loadBareTemplateOpts = void 0;
7
- const option_json_1 = __importDefault(require("./option.json"));
4
+ const templates = [
5
+ {
6
+ name: 'Remix-Vite Bare Templates',
7
+ value: 'remix-vite-template',
8
+ description: 'A full-stack template with Remix-vite and ckb-script-templates',
9
+ tag: ['remix', 'vite', 'tailwindcss', 'ckb-script-templates', 'typescript', 'rust'],
10
+ author: 'retric@cryptape.com',
11
+ },
12
+ {
13
+ name: 'Next.js Bare Templates',
14
+ value: 'next-js-template',
15
+ description: 'A full-stack template with Next.js framework and ckb-script-templates',
16
+ tag: ['next.js', 'tailwindcss', 'ckb-script-templates', 'typescript', 'rust'],
17
+ author: 'retric@cryptape.com',
18
+ },
19
+ ];
8
20
  function loadBareTemplateOpts() {
9
- return option_json_1.default;
21
+ return templates;
10
22
  }
11
23
  exports.loadBareTemplateOpts = loadBareTemplateOpts;
@@ -57,10 +57,9 @@ class MoleculecES {
57
57
  static downloadAndSaveMoleculeES(version, tempFilePath) {
58
58
  return __awaiter(this, void 0, void 0, function* () {
59
59
  const downloadURL = MoleculecES.buildDownloadUrl(version);
60
- const response = yield request_1.Request.get(downloadURL, {
61
- responseType: 'arraybuffer',
62
- });
63
- fs_1.default.writeFileSync(tempFilePath, response.data);
60
+ const response = yield request_1.Request.send(downloadURL);
61
+ const arrayBuffer = yield response.arrayBuffer();
62
+ fs_1.default.writeFileSync(tempFilePath, Buffer.from(arrayBuffer));
64
63
  });
65
64
  }
66
65
  static buildDownloadUrl(version) {
@@ -1,8 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import http from 'http';
3
- import { Network } from '../util/type';
3
+ import { Network } from '../type/base';
4
4
  export declare function createRPCProxy(network: Network, targetRpcUrl: string, port: number): {
5
- server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
6
5
  network: Network;
7
6
  start: () => http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
8
7
  stop: () => http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createRPCProxy = void 0;
7
7
  const http_proxy_1 = __importDefault(require("http-proxy"));
8
8
  const http_1 = __importDefault(require("http"));
9
- const type_1 = require("../util/type");
9
+ const base_1 = require("../type/base");
10
10
  const fs_1 = __importDefault(require("fs"));
11
11
  const setting_1 = require("../cfg/setting");
12
12
  const path_1 = __importDefault(require("path"));
@@ -16,6 +16,37 @@ function createRPCProxy(network, targetRpcUrl, port) {
16
16
  const proxy = http_proxy_1.default.createProxyServer({
17
17
  target: targetRpcUrl, // Target RPC server
18
18
  });
19
+ proxy.on('proxyReq', (_, req) => {
20
+ let reqData = '';
21
+ req.on('data', (chunk) => {
22
+ reqData += chunk;
23
+ });
24
+ req.on('end', () => {
25
+ try {
26
+ const jsonRpcContent = JSON.parse(reqData);
27
+ const method = jsonRpcContent.method;
28
+ const params = jsonRpcContent.params;
29
+ console.debug('RPC Req: ', method);
30
+ if (method === 'send_transaction') {
31
+ const tx = params[0];
32
+ // todo: record tx
33
+ if (network === base_1.Network.devnet) {
34
+ const cccTx = cccA.JsonRpcTransformers.transactionTo(tx);
35
+ const txHash = cccTx.hash();
36
+ const settings = (0, setting_1.readSettings)();
37
+ if (!fs_1.default.existsSync(settings.devnet.transactionsPath)) {
38
+ fs_1.default.mkdirSync(settings.devnet.transactionsPath);
39
+ }
40
+ const txFile = path_1.default.resolve(settings.devnet.transactionsPath, `${txHash}.json`);
41
+ fs_1.default.writeFileSync(txFile, JSON.stringify(tx, null, 2));
42
+ }
43
+ }
44
+ }
45
+ catch (err) {
46
+ console.error('Error parsing JSON-RPC content:', err);
47
+ }
48
+ });
49
+ });
19
50
  const server = http_1.default.createServer((req, res) => {
20
51
  proxy.web(req, res, {}, (err) => {
21
52
  if (err) {
@@ -24,52 +55,8 @@ function createRPCProxy(network, targetRpcUrl, port) {
24
55
  res.end('Proxy error');
25
56
  }
26
57
  });
27
- proxy.on('proxyReq', (_, req) => {
28
- let reqData = '';
29
- req.on('data', (chunk) => {
30
- reqData += chunk;
31
- });
32
- req.on('end', () => {
33
- try {
34
- const jsonRpcContent = JSON.parse(reqData);
35
- console.debug('Incoming Request: ', jsonRpcContent);
36
- const method = jsonRpcContent.method;
37
- const params = jsonRpcContent.params;
38
- if (method === 'send_transaction') {
39
- const tx = params[0];
40
- // todo: record tx
41
- if (network === type_1.Network.devnet) {
42
- const cccTx = cccA.JsonRpcTransformers.transactionTo(tx);
43
- const txHash = cccTx.hash();
44
- const settings = (0, setting_1.readSettings)();
45
- console.log('txHash: ', txHash, settings, settings.devnet.transactionsPath);
46
- if (!fs_1.default.existsSync(settings.devnet.transactionsPath)) {
47
- fs_1.default.mkdirSync(settings.devnet.transactionsPath);
48
- }
49
- const txFile = path_1.default.resolve(settings.devnet.transactionsPath, `${txHash}.json`);
50
- fs_1.default.writeFileSync(txFile, JSON.stringify(tx, null, 2));
51
- }
52
- }
53
- }
54
- catch (err) {
55
- console.error('Error parsing JSON-RPC content:', err);
56
- }
57
- });
58
- });
59
- // Capture the content from the response (or request)
60
- proxy.on('proxyRes', (proxyRes) => {
61
- let data = '';
62
- proxyRes.on('data', (chunk) => {
63
- data += chunk;
64
- });
65
- proxyRes.on('end', () => {
66
- console.log('Captured content:', data);
67
- // Do something with the captured content
68
- });
69
- });
70
58
  });
71
59
  return {
72
- server,
73
60
  network,
74
61
  start: () => {
75
62
  return server.listen(port, () => {
@@ -7,3 +7,5 @@ export interface NetworkOption {
7
7
  network: Network;
8
8
  }
9
9
  export type H256 = string;
10
+ export type HexString = string;
11
+ export type HexNumber = string;
@@ -1,8 +1,8 @@
1
- import axios, { AxiosProxyConfig, AxiosRequestConfig } from 'axios';
1
+ import { ProxyConfig } from '../cfg/setting';
2
+ import fetch, { RequestInit } from 'node-fetch';
2
3
  export declare class Request {
3
- static proxy: axios.AxiosProxyConfig | undefined;
4
- static send(_config: AxiosRequestConfig): Promise<axios.AxiosResponse<any, any>>;
5
- static get(url: string, _config?: AxiosRequestConfig): Promise<axios.AxiosResponse<any, any>>;
6
- static parseProxyUrl(url: string): AxiosProxyConfig;
7
- static proxyConfigToUrl(proxy: AxiosProxyConfig): string;
4
+ static proxy: ProxyConfig | undefined;
5
+ static send(url: string, options?: RequestInit): Promise<fetch.Response>;
6
+ static parseProxyUrl(url: string): ProxyConfig;
7
+ static proxyConfigToUrl(proxy: ProxyConfig): string;
8
8
  }
@@ -13,20 +13,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Request = void 0;
16
- const axios_1 = __importDefault(require("axios"));
17
16
  const setting_1 = require("../cfg/setting");
17
+ const https_proxy_agent_1 = require("https-proxy-agent");
18
+ const node_fetch_1 = __importDefault(require("node-fetch"));
18
19
  class Request {
19
- static send(_config) {
20
+ static send(url, options = {}) {
20
21
  return __awaiter(this, void 0, void 0, function* () {
21
- const config = this.proxy ? Object.assign({ proxy: this.proxy }, _config) : _config;
22
- return yield (0, axios_1.default)(config);
23
- });
24
- }
25
- static get(url, _config) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- const config = this.proxy ? Object.assign({ proxy: this.proxy }, _config) : _config;
28
- console.log(config);
29
- return yield axios_1.default.get(url, config);
22
+ const agent = this.proxy ? new https_proxy_agent_1.HttpsProxyAgent(this.proxyConfigToUrl(this.proxy)) : undefined;
23
+ const opt = Object.assign({ agent }, options);
24
+ try {
25
+ const response = yield (0, node_fetch_1.default)(url, opt);
26
+ if (!response.ok) {
27
+ throw new Error(`HTTP error! Status: ${response.status}, URL: ${response.url}`);
28
+ }
29
+ return yield response;
30
+ }
31
+ catch (error) {
32
+ throw new Error(`fetch error! ${error.message}`);
33
+ }
30
34
  });
31
35
  }
32
36
  static parseProxyUrl(url) {
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.isValidVersion = exports.validateNetworkOpt = exports.isValidNetworkString = exports.validateExecDappEnvironment = exports.validateTypescriptWorkspace = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const fs_1 = __importDefault(require("fs"));
9
- const type_1 = require("./type");
9
+ const base_1 = require("../type/base");
10
10
  function validateTypescriptWorkspace() {
11
11
  const cwd = process.cwd();
12
12
  // Check if package.json exists
@@ -50,7 +50,7 @@ function validateNetworkOpt(network) {
50
50
  if (!isValidNetworkString(network)) {
51
51
  throw new Error('invalid network option, ' + network);
52
52
  }
53
- if (network === type_1.Network.mainnet) {
53
+ if (network === base_1.Network.mainnet) {
54
54
  console.log('Mainnet not support yet. Please use CKB-CLI to operate on mainnet for better security. Check https://github.com/nervosnetwork/ckb-cli');
55
55
  process.exit(1);
56
56
  }