-
Notifications
You must be signed in to change notification settings - Fork 29
/
sendTransactionWithMultiplePrivateKey.js
84 lines (69 loc) · 2.49 KB
/
sendTransactionWithMultiplePrivateKey.js
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
/* eslint-disable */
const path = require('path')
const os = require('os')
const { Indexer, CellCollector } = require('@ckb-lumos/indexer')
const CKB = require('../lib').default
const CKB_URL = process.env.CKB_URL || 'http://localhost:8114'
const LUMOS_DB = path.join(os.tmpdir(), 'lumos_db')
/**
* lumos indexer
*/
const indexer = new Indexer(CKB_URL, LUMOS_DB)
indexer.startForever()
/**
* sdk
*/
const ckb = new CKB(CKB_URL)
const sk1 = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // exmaple private key
const sk2 = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' // example private key
const loadCells = async () => {
await ckb.loadDeps()
const lockScript1 = {
codeHash: ckb.config.secp256k1Dep.codeHash,
hashType: ckb.config.secp256k1Dep.hashType,
args: `0x${ckb.utils.blake160(ckb.utils.privateKeyToPublicKey(sk1), 'hex')}`
}
const lockScript2 = {
codeHash: ckb.config.secp256k1Dep.codeHash,
hashType: ckb.config.secp256k1Dep.hashType,
args: `0x${ckb.utils.blake160(ckb.utils.privateKeyToPublicKey(sk2), 'hex')}`
}
const lockHash1 = ckb.utils.scriptToHash(lockScript1)
const lockHash2 = ckb.utils.scriptToHash(lockScript2)
/**
* load cells from lumos as `examples/sendTransactionWithLumosCollector.js` shows
*/
await ckb.loadCells({ indexer, CellCollector, lock: lockScript1, save: true })
await ckb.loadCells({ indexer, CellCollector, lock: lockScript2, save: true })
const cell1 = ckb.cells.get(lockHash1)
const cell2 = ckb.cells.get(lockHash2)
}
const generateTransaction = async () => {
await loadCells()
const addr1 = ckb.utils.privateKeyToAddress(sk1)
const addr2 = ckb.utils.privateKeyToAddress(sk2)
await ckb.loadDeps()
const rawTransaction = ckb.generateRawTransaction({
fromAddresses: [addr1, addr2],
receivePairs: [{
address: addr2,
capacity: BigInt(30621362931463)
}],
fee: BigInt(10000),
deps: ckb.config.secp256k1Dep,
})
const keys = new Map([sk1, sk2].map(sk => ([
ckb.generateLockHash(`0x${ckb.utils.blake160(ckb.utils.privateKeyToPublicKey(sk), 'hex')}`), sk
])))
const cells = [...ckb.cells.values()].flat()
const signedTransaction = ckb.signTransaction(keys)(rawTransaction, cells)
return signedTransaction
}
const sendTransaction = async () => {
const signedTx = await generateTransaction()
const txHash = await ckb.rpc.sendTransaction(signedTx)
console.log(`tx hash: ${txHash}`)
}
// loadCells()
// generateTransaction()
sendTransaction()