-
Notifications
You must be signed in to change notification settings - Fork 3
/
btctr.js
77 lines (59 loc) · 3.14 KB
/
btctr.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
// Import libraries
const bitcore = require("bitcore-lib");
const axios = require("axios");
// 1. Set up test wallets (generate on http://bip32.org)
let walletA = {
addr: "mzSPa2N1rqNeFpivoszw7ejb1oagc35pYJ",
privateKey: "cNESzuThJn24iYUziZeMxkbey1hhpKgy64x9wvsJMvTvcGzWJRNh"
}
let walletB = {
addr: "mjSLAYMgvwQr8ctXkuEuHckp92wcNhjA9f",
privateKey: "cSaoKeJv2g2C6yJckZQepP8PRxzjzcHfx9KV6EPSKh3Eps9ycALX"
}
// Before you can send a transaction, you need to send BTC to the sender wallet.
// You can use Coinfaucet.eu for that: https://coinfaucet.eu/en/btc-testnet/
function sendBTC(fromAddress, toAddress, privateKey, amount) {
// 2. Connect to a node
const network = "BTCTEST"
// Get the unspent transaction outputs from the sender wallet, that will be used as input for the transaction
axios.get(`https://sochain.com/api/v2/get_tx_unspent/${network}/${fromAddress}`).then(firstResponse => {
let inputs = [];
let utxos = firstResponse.data.data.txs;
let totalAmountAvailable = 0; // To evaluate, if we have enough funds to send the transaction
let inputCount = 0; // To later calculate the transaction size
for (const element of utxos) {
let utxo = {} // Generate utxo object to specify input for transaction
utxo.satoshis = Math.floor(Number(element.value) * 100000000) // 100 million satoshi = 1 Bitcoin
utxo.script = element.script_hex // Script contains basic instructions for the transaction
utxo.address = firstResponse.data.data.address // Address of the sender wallet
utxo.txid = element.txid // Transaction ID of the transaction behind the utxo
utxo.outputIndex = element.output_no // To identify the utxo
totalAmountAvailable += utxo.satoshis // increase the available funds by the amount within the utxo
inputCount += 1
inputs.push(utxo);
}
// 2. Generate transaction
const transaction = new bitcore.Transaction()
const satoshiToSend = amount * 100000000 // 100 million satoshi = 1 Bitcoin
let outputCount = 2 // one for recipient, one for change
// calculate fee
const transactionSize = inputCount * 180 + outputCount * 34 + 10 - inputCount
let fee = transactionSize * 33 // 33 satoshi per byte
if (totalAmountAvailable - satoshiToSend - fee < 0) { // Check, if funds are sufficient to send transaction
throw new Error("Insufficient funds")
}
// Specify transaction
transaction.from(inputs)
transaction.to(toAddress, satoshiToSend)
transaction.change(fromAddress)
transaction.fee(Math.round(fee))
transaction.sign(privateKey)
const serializedTransaction = transaction.serialize()
// broadcast transaction
axios({method: "POST", url: `https://sochain.com/api/v2/send_tx/${network}`, data: {tx_hex: serializedTransaction},})
.then(result => {
console.log(result.data.data) // log the result
})
})
}
sendBTC(fromAddress = walletA.addr, toAddress = walletB.addr, privateKey = walletA.privateKey, amount = 0.001)