@offckb/cli 0.1.0-rc5 → 0.1.0-rc7

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.
@@ -1,153 +0,0 @@
1
- import { bytes } from '@ckb-lumos/codec';
2
- import { helpers, Address, Script, hd, config, Cell, commons, WitnessArgs, BI } from '@ckb-lumos/lumos';
3
- import { values, blockchain } from '@ckb-lumos/base';
4
- import { indexer, lumosConfig, rpc } from './ckb';
5
- const { ScriptValue } = values;
6
-
7
- config.initializeConfig(lumosConfig);
8
-
9
- type Account = {
10
- lockScript: Script;
11
- address: Address;
12
- pubKey: string;
13
- };
14
- export const generateAccountFromPrivateKey = (privKey: string): Account => {
15
- const pubKey = hd.key.privateToPublic(privKey);
16
- const args = hd.key.publicKeyToBlake160(pubKey);
17
- const template = lumosConfig.SCRIPTS['SECP256K1_BLAKE160']!;
18
- const lockScript = {
19
- codeHash: template.CODE_HASH,
20
- hashType: template.HASH_TYPE,
21
- args: args,
22
- };
23
- const address = helpers.encodeToAddress(lockScript, { config: lumosConfig });
24
- return {
25
- lockScript,
26
- address,
27
- pubKey,
28
- };
29
- };
30
-
31
- export async function capacityOf(address: string): Promise<BI> {
32
- const collector = indexer.collector({
33
- lock: helpers.parseAddress(address, { config: lumosConfig }),
34
- });
35
-
36
- let balance = BI.from(0);
37
- for await (const cell of collector.collect()) {
38
- balance = balance.add(cell.cellOutput.capacity);
39
- }
40
-
41
- return balance;
42
- }
43
-
44
- interface Options {
45
- from: string;
46
- to: string;
47
- amount: string;
48
- privKey: string;
49
- }
50
-
51
- export async function transfer(options: Options): Promise<string> {
52
- let txSkeleton = helpers.TransactionSkeleton({});
53
- const fromScript = helpers.parseAddress(options.from, {
54
- config: lumosConfig,
55
- });
56
- const toScript = helpers.parseAddress(options.to, { config: lumosConfig });
57
-
58
- if (BI.from(options.amount).lt(BI.from('6100000000'))) {
59
- throw new Error(
60
- `every cell's capacity must be at least 61 CKB, see https://medium.com/nervosnetwork/understanding-the-nervos-dao-and-cell-model-d68f38272c24`,
61
- );
62
- }
63
-
64
- // additional 0.001 ckb for tx fee
65
- // the tx fee could calculated by tx size
66
- // this is just a simple example
67
- const neededCapacity = BI.from(options.amount).add(100000);
68
- let collectedSum = BI.from(0);
69
- const collected: Cell[] = [];
70
- const collector = indexer.collector({ lock: fromScript, type: 'empty' });
71
- for await (const cell of collector.collect()) {
72
- collectedSum = collectedSum.add(cell.cellOutput.capacity);
73
- collected.push(cell);
74
- if (collectedSum >= neededCapacity) break;
75
- }
76
-
77
- if (collectedSum.lt(neededCapacity)) {
78
- throw new Error(`Not enough CKB, ${collectedSum} < ${neededCapacity}`);
79
- }
80
-
81
- const transferOutput: Cell = {
82
- cellOutput: {
83
- capacity: BI.from(options.amount).toHexString(),
84
- lock: toScript,
85
- },
86
- data: '0x',
87
- };
88
-
89
- const changeOutput: Cell = {
90
- cellOutput: {
91
- capacity: collectedSum.sub(neededCapacity).toHexString(),
92
- lock: fromScript,
93
- },
94
- data: '0x',
95
- };
96
-
97
- txSkeleton = txSkeleton.update('inputs', (inputs) => inputs.push(...collected));
98
- txSkeleton = txSkeleton.update('outputs', (outputs) => outputs.push(transferOutput, changeOutput));
99
- txSkeleton = txSkeleton.update('cellDeps', (cellDeps) =>
100
- cellDeps.push({
101
- outPoint: {
102
- txHash: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.TX_HASH,
103
- index: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.INDEX,
104
- },
105
- depType: lumosConfig.SCRIPTS.SECP256K1_BLAKE160.DEP_TYPE,
106
- }),
107
- );
108
-
109
- const firstIndex = txSkeleton
110
- .get('inputs')
111
- .findIndex((input) =>
112
- new ScriptValue(input.cellOutput.lock, { validate: false }).equals(
113
- new ScriptValue(fromScript, { validate: false }),
114
- ),
115
- );
116
- if (firstIndex !== -1) {
117
- while (firstIndex >= txSkeleton.get('witnesses').size) {
118
- txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.push('0x'));
119
- }
120
- let witness: string = txSkeleton.get('witnesses').get(firstIndex)!;
121
- const newWitnessArgs: WitnessArgs = {
122
- /* 65-byte zeros in hex */
123
- lock: '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
124
- };
125
- if (witness !== '0x') {
126
- const witnessArgs = blockchain.WitnessArgs.unpack(bytes.bytify(witness));
127
- const lock = witnessArgs.lock;
128
- if (!!lock && !!newWitnessArgs.lock && !bytes.equal(lock, newWitnessArgs.lock)) {
129
- throw new Error('Lock field in first witness is set aside for signature!');
130
- }
131
- const inputType = witnessArgs.inputType;
132
- if (inputType) {
133
- newWitnessArgs.inputType = inputType;
134
- }
135
- const outputType = witnessArgs.outputType;
136
- if (outputType) {
137
- newWitnessArgs.outputType = outputType;
138
- }
139
- }
140
- witness = bytes.hexify(blockchain.WitnessArgs.pack(newWitnessArgs));
141
- txSkeleton = txSkeleton.update('witnesses', (witnesses) => witnesses.set(firstIndex, witness));
142
- }
143
-
144
- txSkeleton = commons.common.prepareSigningEntries(txSkeleton);
145
- const message = txSkeleton.get('signingEntries').get(0)?.message;
146
- const Sig = hd.key.signRecoverable(message!, options.privKey);
147
- const tx = helpers.sealTransaction(txSkeleton, [Sig]);
148
- const hash = await rpc.sendTransaction(tx, 'passthrough');
149
- console.log('The transaction hash is', hash);
150
- alert(`The transaction hash is ${hash}`);
151
-
152
- return hash;
153
- }
@@ -1,29 +0,0 @@
1
- {
2
- "name": "@offckb/lumos-scaffold",
3
- "version": "0.1.0",
4
- "description": "",
5
- "main": "index.js",
6
- "scripts": {
7
- "start": "parcel index.html",
8
- "lint": "tsc --noEmit"
9
- },
10
- "keywords": [],
11
- "author": "",
12
- "license": "MIT",
13
- "dependencies": {
14
- "@ckb-lumos/base": "0.21.1",
15
- "@ckb-lumos/codec": "0.21.1",
16
- "@ckb-lumos/lumos": "0.21.1",
17
- "@types/react": "^18.0.25",
18
- "@types/react-dom": "^18.0.9",
19
- "react": "^18.2.0",
20
- "react-dom": "^18.2.0"
21
- },
22
- "devDependencies": {
23
- "crypto-browserify": "^3.12.0",
24
- "parcel": "^2.9.3",
25
- "path-browserify": "^1.0.0",
26
- "process": "^0.11.10",
27
- "stream-browserify": "^3.0.0"
28
- }
29
- }
@@ -1,10 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "lib": ["dom"],
4
- "jsx": "react",
5
- "module": "CommonJS",
6
- "skipLibCheck": true,
7
- "esModuleInterop": true,
8
- "resolveJsonModule": true
9
- }
10
- }