forked from cryptape/ckb-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
182 lines (159 loc) · 5.31 KB
/
helper.ts
File metadata and controls
182 lines (159 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
175
176
177
178
179
180
181
182
import { blockchain } from "@ckb-lumos/base";
import { bytes } from "@ckb-lumos/codec";
import {
Address,
BI,
Cell,
HexString,
Indexer,
Script,
Transaction,
WitnessArgs,
config,
hd,
helpers as lumosHelpers,
} from "@ckb-lumos/lumos";
import { Account } from "./type";
export {
payFeeByFeeRate,
prepareSigningEntries,
} from "@ckb-lumos/common-scripts/lib/common";
export const CKB_TESTNET_EXPLORER = "https://pudge.explorer.nervos.org";
export const CKB_TESTNET_RPC = "https://testnet.ckb.dev/rpc";
export const ckbIndexer = new Indexer(CKB_TESTNET_RPC);
// This tutorial uses CKB testnet.
// CKB Testnet Explorer: https://pudge.explorer.nervos.org
config.initializeConfig(config.predefined.AGGRON4);
export const TESTNET_SCRIPTS = config.predefined.AGGRON4.SCRIPTS;
// get the address of CKB testnet from the private key
export const getAddressByPrivateKey = (privateKey: HexString): Address => {
const args = hd.key.privateKeyToBlake160(privateKey);
// rome-ignore lint: SECP256K1_BLAKE160 script should exist
const template = TESTNET_SCRIPTS["SECP256K1_BLAKE160"]!;
const lockScript = {
codeHash: template.CODE_HASH,
hashType: template.HASH_TYPE,
args: args,
};
return lumosHelpers.encodeToAddress(lockScript);
};
// generate an Account from the private key
export const generateAccountFromPrivateKey = (privateKey: string): Account => {
const pubKey = hd.key.privateToPublic(privateKey);
const args = hd.key.publicKeyToBlake160(pubKey);
// rome-ignore lint: SECP256K1_BLAKE160 script should exist
const template = TESTNET_SCRIPTS["SECP256K1_BLAKE160"]!;
const lockScript = {
codeHash: template.CODE_HASH,
hashType: template.HASH_TYPE,
args: args,
};
const address = lumosHelpers.encodeToAddress(lockScript);
return {
lockScript,
address,
pubKey,
};
};
/**
* Get CKB balance of an address
*
* In CKB, the CKB balance is the sum of all capacity field of the Cells owned
* by the address
* @param address
* @returns
*/
export async function getCapacities(address: string): Promise<BI> {
const collector = ckbIndexer.collector({
lock: lumosHelpers.parseAddress(address),
});
let capacities = BI.from(0);
for await (const cell of collector.collect()) {
capacities = capacities.add(cell.cellOutput.capacity);
}
return capacities;
}
export async function capacityOf(lock: Script): Promise<BI> {
const collector = ckbIndexer.collector({ lock });
let balance: BI = BI.from(0);
for await (const cell of collector.collect()) {
balance = balance.add(cell.cellOutput.capacity);
}
return balance;
}
/**
* collect input cells with empty output data
* @param lock The lock script protects the input cells
* @param requiredCapacity The required capacity sum of the input cells
*/
export async function collectInputCells(
lock: Script,
requiredCapacity: bigint,
): Promise<Cell[]> {
const collector = ckbIndexer.collector({
lock,
// filter cells by output data len range, [inclusive, exclusive)
// data length range: [0, 1), which means the data length is 0
outputDataLenRange: ["0x0", "0x1"],
});
let _needCapacity = requiredCapacity;
const collected: Cell[] = [];
for await (const inputCell of collector.collect()) {
collected.push(inputCell);
_needCapacity -= BigInt(inputCell.cellOutput.capacity);
if (_needCapacity <= 0) break;
}
return collected;
}
/**
* add the first witness for the fromAddress script,
* which has a WitnessArgs constructed with 65-byte zero filled values
*/
export function addWitness(
txSkeleton: lumosHelpers.TransactionSkeletonType,
): lumosHelpers.TransactionSkeletonType {
const firstLockInputIndex = 0;
/* 65-byte zeros in hex */
const SECP_SIGNATURE_PLACEHOLDER =
"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
const newWitnessArgs: WitnessArgs = { lock: SECP_SIGNATURE_PLACEHOLDER };
const witness = bytes.hexify(blockchain.WitnessArgs.pack(newWitnessArgs));
return txSkeleton.update("witnesses", (witnesses) =>
witnesses.set(firstLockInputIndex, witness),
);
}
/**
* Calculate transaction fee
*
* @param txSkeleton {@link lumosHelpers.TransactionSkeletonType}
* @param feeRate how many shannons per KB charge
* @returns fee, unit: shannons
*
* See https://github.com/nervosnetwork/ckb/wiki/Transaction-%C2%BB-Transaction-Fee
*/
export function calculateTxFee(
txSkeleton: lumosHelpers.TransactionSkeletonType,
feeRate: bigint,
): bigint {
const tx: Transaction =
lumosHelpers.createTransactionFromSkeleton(txSkeleton);
const serializedTx = blockchain.Transaction.pack(tx);
// 4 is serialized offset bytesize
const txSize = BigInt(serializedTx.byteLength + 4);
const ratio = 1000n;
const base = txSize * feeRate;
const fee = base / ratio;
return fee * ratio < base ? fee + 1n : fee;
}
export function encodeStringToHex(str: string): HexString {
return `0x${Buffer.from(str).toString("hex")}`;
}
/**
* Get faucet from https://github.com/Flouse/nervos-functions#faucet
*/
export async function getFaucet() {
// TODO: one line code to get faucet from https://faucet.nervos.org or https://github.com/Flouse/nervos-functions#faucet
throw new Error(
"TODO: get faucet from https://faucet.nervos.org or https://github.com/Flouse/nervos-functions#faucet",
);
}