@offckb/cli 0.3.0-canary-884fccd.0 → 0.3.0-canary-1bb6394.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/dist/cfg/setting.d.ts +11 -2
- package/dist/cli.js +6 -6
- package/dist/cmd/balance.js +4 -6
- package/dist/cmd/deposit.d.ts +1 -1
- package/dist/cmd/deposit.js +28 -30
- package/dist/cmd/transfer.d.ts +1 -1
- package/dist/cmd/transfer.js +7 -10
- package/dist/node/install.js +3 -4
- package/dist/sdk/ckb.d.ts +17 -3
- package/dist/sdk/ckb.js +78 -4
- package/dist/tools/moleculec-es.js +3 -4
- package/dist/util/request.d.ts +6 -6
- package/dist/util/request.js +15 -11
- package/package.json +4 -2
- package/dist/util/ckb.d.ts +0 -141
- package/dist/util/ckb.js +0 -312
package/dist/cfg/setting.d.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
import { AxiosProxyConfig } from 'axios';
|
|
2
1
|
export declare const configPath: string;
|
|
3
2
|
export declare const dataPath: string;
|
|
4
3
|
export declare const cachePath: string;
|
|
5
4
|
export declare const packageSrcPath: string;
|
|
6
5
|
export declare const packageRootPath: string;
|
|
6
|
+
export interface ProxyBasicCredentials {
|
|
7
|
+
username: string;
|
|
8
|
+
password: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ProxyConfig {
|
|
11
|
+
host: string;
|
|
12
|
+
port: number;
|
|
13
|
+
auth?: ProxyBasicCredentials;
|
|
14
|
+
protocol?: string;
|
|
15
|
+
}
|
|
7
16
|
export interface Settings {
|
|
8
|
-
proxy?:
|
|
17
|
+
proxy?: ProxyConfig;
|
|
9
18
|
rpc: {
|
|
10
19
|
proxyPort: number;
|
|
11
20
|
};
|
package/dist/cli.js
CHANGED
|
@@ -97,19 +97,19 @@ program
|
|
|
97
97
|
program.command('inject-config').description('Add offckb.config.ts to your frontend workspace').action(inject_config_1.injectConfig);
|
|
98
98
|
program.command('sync-scripts').description('Sync scripts json files in your frontend workspace').action(sync_scripts_1.syncScripts);
|
|
99
99
|
program
|
|
100
|
-
.command('deposit [toAddress] [
|
|
100
|
+
.command('deposit [toAddress] [amountInCKB]')
|
|
101
101
|
.description('Deposit CKB tokens to address, only devnet and testnet')
|
|
102
102
|
.option('--network <network>', 'Specify the network to deposit to', 'devnet')
|
|
103
|
-
.action((toAddress,
|
|
104
|
-
return (0, deposit_1.deposit)(toAddress,
|
|
103
|
+
.action((toAddress, amountInCKB, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
+
return (0, deposit_1.deposit)(toAddress, amountInCKB, options);
|
|
105
105
|
}));
|
|
106
106
|
program
|
|
107
|
-
.command('transfer [toAddress] [
|
|
107
|
+
.command('transfer [toAddress] [amountInCKB]')
|
|
108
108
|
.description('Transfer CKB tokens to address, only devnet and testnet')
|
|
109
109
|
.option('--network <network>', 'Specify the network to transfer to', 'devnet')
|
|
110
110
|
.option('--privkey <privkey>', 'Specify the private key to deploy scripts')
|
|
111
|
-
.action((toAddress,
|
|
112
|
-
return (0, transfer_1.transfer)(toAddress,
|
|
111
|
+
.action((toAddress, amountInCKB, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
112
|
+
return (0, transfer_1.transfer)(toAddress, amountInCKB, options);
|
|
113
113
|
}));
|
|
114
114
|
program
|
|
115
115
|
.command('balance [toAddress]')
|
package/dist/cmd/balance.js
CHANGED
|
@@ -10,19 +10,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.balanceOf = void 0;
|
|
13
|
-
const
|
|
14
|
-
const ckb_1 = require("../util/ckb");
|
|
13
|
+
const ckb_1 = require("../sdk/ckb");
|
|
15
14
|
const validator_1 = require("../util/validator");
|
|
16
15
|
const type_1 = require("../util/type");
|
|
17
16
|
function balanceOf(address, opt = { network: type_1.Network.devnet }) {
|
|
18
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
18
|
const network = opt.network;
|
|
20
19
|
(0, validator_1.validateNetworkOpt)(network);
|
|
21
|
-
const ckb = new ckb_1.CKB(network);
|
|
22
|
-
const
|
|
23
|
-
const balance = yield ckb.capacityOf(address, lumosConfig);
|
|
24
|
-
const balanceInCKB = balance.div(lumos_1.BI.from('100000000'));
|
|
20
|
+
const ckb = new ckb_1.CKB({ network });
|
|
21
|
+
const balanceInCKB = yield ckb.balance(address);
|
|
25
22
|
console.log(`Balance: ${balanceInCKB} CKB`);
|
|
23
|
+
process.exit(0);
|
|
26
24
|
});
|
|
27
25
|
}
|
|
28
26
|
exports.balanceOf = balanceOf;
|
package/dist/cmd/deposit.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { NetworkOption } from '../util/type';
|
|
2
2
|
export interface DepositOptions extends NetworkOption {
|
|
3
3
|
}
|
|
4
|
-
export declare function deposit(toAddress: string,
|
|
4
|
+
export declare function deposit(toAddress: string, amountInCKB: string, opt?: DepositOptions): Promise<void>;
|
package/dist/cmd/deposit.js
CHANGED
|
@@ -10,29 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.deposit = void 0;
|
|
13
|
-
const ckb_1 = require("../
|
|
13
|
+
const ckb_1 = require("../sdk/ckb");
|
|
14
14
|
const account_1 = require("../cfg/account");
|
|
15
15
|
const type_1 = require("../util/type");
|
|
16
16
|
const link_1 = require("../util/link");
|
|
17
17
|
const validator_1 = require("../util/validator");
|
|
18
18
|
const request_1 = require("../util/request");
|
|
19
|
-
function deposit(toAddress,
|
|
19
|
+
function deposit(toAddress, amountInCKB, opt = { network: type_1.Network.devnet }) {
|
|
20
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
21
21
|
const network = opt.network;
|
|
22
22
|
(0, validator_1.validateNetworkOpt)(network);
|
|
23
|
-
const ckb = new ckb_1.CKB(network);
|
|
24
|
-
const lumosConfig = ckb.getLumosConfig();
|
|
23
|
+
const ckb = new ckb_1.CKB({ network });
|
|
25
24
|
if (network === 'testnet') {
|
|
26
25
|
return yield depositFromTestnetFaucet(toAddress, ckb);
|
|
27
26
|
}
|
|
28
27
|
// deposit from devnet miner
|
|
29
|
-
const
|
|
28
|
+
const privateKey = account_1.ckbDevnetMinerAccount.privkey;
|
|
30
29
|
const txHash = yield ckb.transfer({
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}, lumosConfig);
|
|
30
|
+
toAddress,
|
|
31
|
+
privateKey,
|
|
32
|
+
amountInCKB,
|
|
33
|
+
});
|
|
36
34
|
console.log('tx hash: ', txHash);
|
|
37
35
|
});
|
|
38
36
|
}
|
|
@@ -40,29 +38,33 @@ exports.deposit = deposit;
|
|
|
40
38
|
function depositFromTestnetFaucet(ckbAddress, ckb) {
|
|
41
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
42
40
|
console.log('testnet faucet only supports fixed-amount claim: 10,000 CKB');
|
|
43
|
-
const lumosConfig = ckb.getLumosConfig();
|
|
44
41
|
const randomAccountPrivateKey = '0x' + generateHex(64);
|
|
45
|
-
const
|
|
46
|
-
console.log(`use random account to claim from faucet: \n\nprivate key: ${randomAccountPrivateKey}\n\n address: ${
|
|
42
|
+
const randomAccountAddress = yield ckb.buildSecp256k1Address(randomAccountPrivateKey);
|
|
43
|
+
console.log(`use random account to claim from faucet: \n\nprivate key: ${randomAccountPrivateKey}\n\n address: ${randomAccountAddress}`);
|
|
47
44
|
try {
|
|
48
|
-
yield sendClaimRequest(
|
|
49
|
-
console.log('Wait for
|
|
45
|
+
const claimResponse = yield sendClaimRequest(randomAccountAddress);
|
|
46
|
+
console.log('Wait for claim transaction confirmed to transfer all from random account to your account..');
|
|
50
47
|
console.log('You can transfer by yourself if it ends up fails..');
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
if (claimResponse.txHash != null) {
|
|
49
|
+
yield ckb.waitForTxConfirm(claimResponse.txHash);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
yield ckb.waitForBlocksBy(4); // wait 4 blocks
|
|
53
|
+
}
|
|
54
54
|
}
|
|
55
55
|
catch (error) {
|
|
56
56
|
console.log(error);
|
|
57
|
-
throw new Error('request failed.');
|
|
57
|
+
throw new Error('claim request failed.');
|
|
58
58
|
}
|
|
59
|
+
const txHash = yield ckb.transferAll({ privateKey: randomAccountPrivateKey, toAddress: ckbAddress });
|
|
60
|
+
console.log(`Done, check ${(0, link_1.buildTestnetTxLink)(txHash)} for details.`);
|
|
59
61
|
});
|
|
60
62
|
}
|
|
61
63
|
function sendClaimRequest(toAddress) {
|
|
62
64
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
65
|
const url = 'https://faucet-api.nervos.org/claim_events'; // Replace 'YOUR_API_ENDPOINT' with the actual API endpoint
|
|
64
66
|
const headers = {
|
|
65
|
-
'User-Agent': '
|
|
67
|
+
'User-Agent': 'node-fetch-requests/v2',
|
|
66
68
|
'Accept-Encoding': 'gzip, deflate',
|
|
67
69
|
Accept: '*/*',
|
|
68
70
|
Connection: 'keep-alive',
|
|
@@ -71,22 +73,18 @@ function sendClaimRequest(toAddress) {
|
|
|
71
73
|
const body = JSON.stringify({
|
|
72
74
|
claim_event: {
|
|
73
75
|
address_hash: toAddress,
|
|
74
|
-
amount: '10000',
|
|
76
|
+
amount: '10000', // unit: CKB
|
|
75
77
|
},
|
|
76
78
|
});
|
|
77
79
|
const config = {
|
|
78
80
|
method: 'post',
|
|
79
|
-
url: url,
|
|
80
81
|
headers: headers,
|
|
81
|
-
|
|
82
|
+
body,
|
|
82
83
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
catch (error) {
|
|
88
|
-
console.error('Error:', error);
|
|
89
|
-
}
|
|
84
|
+
const response = yield request_1.Request.send(url, config);
|
|
85
|
+
console.log('send claim request, status: ', response.status); // Handle the response data here
|
|
86
|
+
const jsonResponse = yield response.json();
|
|
87
|
+
return jsonResponse.data.attributes;
|
|
90
88
|
});
|
|
91
89
|
}
|
|
92
90
|
function generateHex(length) {
|
package/dist/cmd/transfer.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import { NetworkOption } from '../util/type';
|
|
|
2
2
|
export interface TransferOptions extends NetworkOption {
|
|
3
3
|
privkey?: string | null;
|
|
4
4
|
}
|
|
5
|
-
export declare function transfer(toAddress: string,
|
|
5
|
+
export declare function transfer(toAddress: string, amountInCKB: string, opt?: TransferOptions): Promise<void>;
|
package/dist/cmd/transfer.js
CHANGED
|
@@ -10,11 +10,11 @@ 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("../
|
|
13
|
+
const ckb_1 = require("../sdk/ckb");
|
|
14
14
|
const type_1 = require("../util/type");
|
|
15
15
|
const link_1 = require("../util/link");
|
|
16
16
|
const validator_1 = require("../util/validator");
|
|
17
|
-
function transfer(toAddress,
|
|
17
|
+
function transfer(toAddress, amountInCKB, opt = { network: type_1.Network.devnet }) {
|
|
18
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
19
|
const network = opt.network;
|
|
20
20
|
(0, validator_1.validateNetworkOpt)(network);
|
|
@@ -22,15 +22,12 @@ function transfer(toAddress, amount, opt = { network: type_1.Network.devnet }) {
|
|
|
22
22
|
throw new Error('--privkey is required!');
|
|
23
23
|
}
|
|
24
24
|
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);
|
|
25
|
+
const ckb = new ckb_1.CKB({ network });
|
|
28
26
|
const txHash = yield ckb.transfer({
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}, lumosConfig);
|
|
27
|
+
toAddress,
|
|
28
|
+
amountInCKB,
|
|
29
|
+
privateKey,
|
|
30
|
+
});
|
|
34
31
|
if (network === 'testnet') {
|
|
35
32
|
console.log(`Successfully transfer, check ${(0, link_1.buildTestnetTxLink)(txHash)} for details.`);
|
|
36
33
|
return;
|
package/dist/node/install.js
CHANGED
|
@@ -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.
|
|
101
|
-
|
|
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;
|
package/dist/sdk/ckb.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { ccc, Script } from '@ckb-ccc/core';
|
|
2
2
|
import { Network } from '../util/type';
|
|
3
|
-
import { HexString } from '@ckb-lumos/lumos';
|
|
3
|
+
import { HexNumber, HexString } from '@ckb-lumos/lumos';
|
|
4
4
|
export declare class CKBProps {
|
|
5
5
|
network?: Network;
|
|
6
|
+
feeRate?: number;
|
|
6
7
|
isEnableProxyRpc?: boolean;
|
|
7
8
|
}
|
|
8
9
|
export interface DeploymentResult {
|
|
@@ -12,12 +13,25 @@ export interface DeploymentResult {
|
|
|
12
13
|
isTypeId: boolean;
|
|
13
14
|
typeId?: Script;
|
|
14
15
|
}
|
|
16
|
+
export interface TransferOption {
|
|
17
|
+
privateKey: HexString;
|
|
18
|
+
toAddress: string;
|
|
19
|
+
amountInCKB: HexNumber;
|
|
20
|
+
}
|
|
21
|
+
export type TransferAllOption = Pick<TransferOption, 'privateKey' | 'toAddress'>;
|
|
15
22
|
export declare class CKB {
|
|
16
23
|
network: Network;
|
|
24
|
+
feeRate: number;
|
|
25
|
+
isEnableProxyRpc: boolean;
|
|
17
26
|
private client;
|
|
18
|
-
constructor({ network, isEnableProxyRpc }: CKBProps);
|
|
19
|
-
|
|
27
|
+
constructor({ network, feeRate, isEnableProxyRpc }: CKBProps);
|
|
28
|
+
buildSigner(privateKey: HexString): ccc.SignerCkbPrivateKey;
|
|
29
|
+
buildSecp256k1Address(privateKey: HexString): Promise<string>;
|
|
20
30
|
waitForTxConfirm(txHash: HexString, timeout?: number): Promise<void>;
|
|
31
|
+
waitForBlocksBy(interval: number): Promise<void>;
|
|
32
|
+
balance(address: string): Promise<string>;
|
|
33
|
+
transfer({ privateKey, toAddress, amountInCKB }: TransferOption): Promise<HexString>;
|
|
34
|
+
transferAll({ privateKey, toAddress }: TransferAllOption): Promise<HexString>;
|
|
21
35
|
deployScript(scriptBinBytes: Uint8Array, privateKey: string): Promise<DeploymentResult>;
|
|
22
36
|
deployNewTypeIDScript(scriptBinBytes: Uint8Array, privateKey: string): Promise<DeploymentResult>;
|
|
23
37
|
upgradeTypeIdScript(scriptName: string, newScriptBinBytes: Uint8Array, privateKey: HexString): Promise<DeploymentResult>;
|
package/dist/sdk/ckb.js
CHANGED
|
@@ -22,11 +22,13 @@ 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 = type_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,
|
|
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,
|
|
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,
|
|
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
|
});
|
|
@@ -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.
|
|
61
|
-
|
|
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) {
|
package/dist/util/request.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ProxyConfig } from '../cfg/setting';
|
|
2
|
+
import fetch, { RequestInit } from 'node-fetch';
|
|
2
3
|
export declare class Request {
|
|
3
|
-
static proxy:
|
|
4
|
-
static send(
|
|
5
|
-
static
|
|
6
|
-
static
|
|
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
|
}
|
package/dist/util/request.js
CHANGED
|
@@ -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(
|
|
20
|
+
static send(url, options = {}) {
|
|
20
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@offckb/cli",
|
|
3
|
-
"version": "0.3.0-canary-
|
|
3
|
+
"version": "0.3.0-canary-1bb6394.0",
|
|
4
4
|
"description": "ckb development network for your first try",
|
|
5
5
|
"author": "CKB EcoFund",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@types/adm-zip": "^0.5.5",
|
|
49
49
|
"@types/node": "^20.11.19",
|
|
50
|
+
"@types/node-fetch": "^2.6.11",
|
|
50
51
|
"@types/semver": "^7.5.7",
|
|
51
52
|
"@types/tar": "^6.1.11",
|
|
52
53
|
"@typescript-eslint/eslint-plugin": "^7.0.2",
|
|
@@ -65,11 +66,12 @@
|
|
|
65
66
|
"@inquirer/prompts": "^4.1.0",
|
|
66
67
|
"@types/http-proxy": "^1.17.15",
|
|
67
68
|
"adm-zip": "^0.5.10",
|
|
68
|
-
"axios": "^1.6.7",
|
|
69
69
|
"child_process": "^1.0.2",
|
|
70
70
|
"ckb-transaction-dumper": "^0.4.0",
|
|
71
71
|
"commander": "^12.0.0",
|
|
72
72
|
"http-proxy": "^1.18.1",
|
|
73
|
+
"https-proxy-agent": "^7.0.5",
|
|
74
|
+
"node-fetch": "2",
|
|
73
75
|
"semver": "^7.6.0",
|
|
74
76
|
"tar": "^6.2.1"
|
|
75
77
|
}
|
package/dist/util/ckb.d.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { Address, BI, Indexer, RPC, Script, config } from '@ckb-lumos/lumos';
|
|
2
|
-
import { Network } from './type';
|
|
3
|
-
export type Account = {
|
|
4
|
-
lockScript: Script;
|
|
5
|
-
address: Address;
|
|
6
|
-
pubKey: string;
|
|
7
|
-
privKey: string;
|
|
8
|
-
};
|
|
9
|
-
interface Options {
|
|
10
|
-
from: string;
|
|
11
|
-
to: string;
|
|
12
|
-
amount: string;
|
|
13
|
-
privKey: string;
|
|
14
|
-
}
|
|
15
|
-
export declare class CKB {
|
|
16
|
-
network: Network;
|
|
17
|
-
rpc_url: string;
|
|
18
|
-
rpc: RPC;
|
|
19
|
-
indexer: Indexer;
|
|
20
|
-
constructor(network?: Network);
|
|
21
|
-
static generateAccountFromPrivateKey(privKey: string, lumosConfig: config.Config): Account;
|
|
22
|
-
getLumosConfig(): config.Config | {
|
|
23
|
-
PREFIX: string;
|
|
24
|
-
SCRIPTS: {
|
|
25
|
-
SECP256K1_BLAKE160: {
|
|
26
|
-
CODE_HASH: string;
|
|
27
|
-
HASH_TYPE: "type";
|
|
28
|
-
TX_HASH: string;
|
|
29
|
-
INDEX: string;
|
|
30
|
-
DEP_TYPE: "depGroup";
|
|
31
|
-
SHORT_ID: number;
|
|
32
|
-
};
|
|
33
|
-
SECP256K1_BLAKE160_MULTISIG: {
|
|
34
|
-
CODE_HASH: string;
|
|
35
|
-
HASH_TYPE: "type";
|
|
36
|
-
TX_HASH: string;
|
|
37
|
-
INDEX: string;
|
|
38
|
-
DEP_TYPE: "depGroup";
|
|
39
|
-
SHORT_ID: number;
|
|
40
|
-
};
|
|
41
|
-
DAO: {
|
|
42
|
-
CODE_HASH: string;
|
|
43
|
-
HASH_TYPE: "type";
|
|
44
|
-
TX_HASH: string;
|
|
45
|
-
INDEX: string;
|
|
46
|
-
DEP_TYPE: "code";
|
|
47
|
-
};
|
|
48
|
-
SUDT: {
|
|
49
|
-
CODE_HASH: string;
|
|
50
|
-
HASH_TYPE: "type";
|
|
51
|
-
TX_HASH: string;
|
|
52
|
-
INDEX: string;
|
|
53
|
-
DEP_TYPE: "code";
|
|
54
|
-
};
|
|
55
|
-
ANYONE_CAN_PAY: {
|
|
56
|
-
CODE_HASH: string;
|
|
57
|
-
HASH_TYPE: "type";
|
|
58
|
-
TX_HASH: string;
|
|
59
|
-
INDEX: string;
|
|
60
|
-
DEP_TYPE: "depGroup";
|
|
61
|
-
SHORT_ID: number;
|
|
62
|
-
};
|
|
63
|
-
OMNILOCK: {
|
|
64
|
-
CODE_HASH: string;
|
|
65
|
-
HASH_TYPE: "type";
|
|
66
|
-
TX_HASH: string;
|
|
67
|
-
INDEX: string;
|
|
68
|
-
DEP_TYPE: "code";
|
|
69
|
-
};
|
|
70
|
-
XUDT: {
|
|
71
|
-
CODE_HASH: string;
|
|
72
|
-
HASH_TYPE: "type";
|
|
73
|
-
TX_HASH: string;
|
|
74
|
-
INDEX: string;
|
|
75
|
-
DEP_TYPE: "code";
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
} | {
|
|
79
|
-
PREFIX: string;
|
|
80
|
-
SCRIPTS: {
|
|
81
|
-
SECP256K1_BLAKE160: {
|
|
82
|
-
CODE_HASH: string;
|
|
83
|
-
HASH_TYPE: "type";
|
|
84
|
-
TX_HASH: string;
|
|
85
|
-
INDEX: string;
|
|
86
|
-
DEP_TYPE: "depGroup";
|
|
87
|
-
SHORT_ID: number;
|
|
88
|
-
};
|
|
89
|
-
SECP256K1_BLAKE160_MULTISIG: {
|
|
90
|
-
CODE_HASH: string;
|
|
91
|
-
HASH_TYPE: "type";
|
|
92
|
-
TX_HASH: string;
|
|
93
|
-
INDEX: string;
|
|
94
|
-
DEP_TYPE: "depGroup";
|
|
95
|
-
SHORT_ID: number;
|
|
96
|
-
};
|
|
97
|
-
DAO: {
|
|
98
|
-
CODE_HASH: string;
|
|
99
|
-
HASH_TYPE: "type";
|
|
100
|
-
TX_HASH: string;
|
|
101
|
-
INDEX: string;
|
|
102
|
-
DEP_TYPE: "code";
|
|
103
|
-
};
|
|
104
|
-
SUDT: {
|
|
105
|
-
CODE_HASH: string;
|
|
106
|
-
HASH_TYPE: "type";
|
|
107
|
-
TX_HASH: string;
|
|
108
|
-
INDEX: string;
|
|
109
|
-
DEP_TYPE: "code";
|
|
110
|
-
};
|
|
111
|
-
ANYONE_CAN_PAY: {
|
|
112
|
-
CODE_HASH: string;
|
|
113
|
-
HASH_TYPE: "type";
|
|
114
|
-
TX_HASH: string;
|
|
115
|
-
INDEX: string;
|
|
116
|
-
DEP_TYPE: "depGroup";
|
|
117
|
-
SHORT_ID: number;
|
|
118
|
-
};
|
|
119
|
-
OMNILOCK: {
|
|
120
|
-
CODE_HASH: string;
|
|
121
|
-
HASH_TYPE: "type";
|
|
122
|
-
TX_HASH: string;
|
|
123
|
-
INDEX: string;
|
|
124
|
-
DEP_TYPE: "code";
|
|
125
|
-
};
|
|
126
|
-
XUDT: {
|
|
127
|
-
CODE_HASH: string;
|
|
128
|
-
HASH_TYPE: "data1";
|
|
129
|
-
TX_HASH: string;
|
|
130
|
-
INDEX: string;
|
|
131
|
-
DEP_TYPE: "code";
|
|
132
|
-
};
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
initializedLumosConfig(): void;
|
|
136
|
-
capacityOf(address: string, lumosConfig: config.Config): Promise<BI>;
|
|
137
|
-
transfer(options: Options, lumosConfig: config.Config): Promise<string>;
|
|
138
|
-
transferAll(privateKey: string, toAddress: string, lumosConfig: config.Config): Promise<string>;
|
|
139
|
-
}
|
|
140
|
-
export declare function readPredefinedDevnetLumosConfig(): config.Config;
|
|
141
|
-
export {};
|
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;
|