forked from makevoid/bitcore-lib-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (32 loc) · 1.22 KB
/
index.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
const { readFileSync } = require('fs')
const bitcoin = require('bitcore-lib')
const { PrivateKey, Transaction } = bitcoin
const pvtKeyString = readFileSync('./private-key.txt').toString().trim()
const pvtKey = new PrivateKey(pvtKeyString)
const address = pvtKey.toAddress().toString()
console.log("private key:", pvtKey.toString())
console.log("address:", address)
const getUTXOs = require('./blockchain-api/get-utxos')
const pushTx = require('./blockchain-api/push-tx')
;(async () => {
try {
let utxos = await getUTXOs({ address })
// TODO: replace with actual input selection or disable (uncomment)
// NOTE: the current code selects always the first spendable input (usually the latest transaction, as it's using the blockchain.info `/unspent` endpoint)
utxos = [ utxos[0] ]
console.log("utxos:", utxos)
const amount = 10000 // 10k sats (0.1mbtc)
const tx = new Transaction()
.from(utxos)
.to(address, amount)
.change(address)
.fee(1000)
.sign(pvtKey)
const txHex = tx.serialize()
console.log("tx:", txHex)
const { success, txHash } = pushTx(txHex)
console.log("success:", success, "txHash:", txHash)
} catch (err) {
console.error(err)
}
})()