-
Notifications
You must be signed in to change notification settings - Fork 4
/
fund.ts
132 lines (103 loc) · 3.65 KB
/
fund.ts
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
// Usage
// > ./node_modules/.bin/ts-node fund.ts
// prompt: contract: ppn2nh44nkuazpweh2fz7pdhw5xrh0wk0st2g245gz
// prompt: amount: 3000
// success: https://explorer.bitcoin.com/tbch/tx/3cc6dd2dda36588904ff9bb689c6b9c02dfa07395971a32b79dcb3f8eeeb7607#o=1
// MIT License https://en.wikipedia.org/wiki/MIT_License
// imports
import { BITBOX } from "bitbox-sdk"
import { AddressUtxoResult } from "bitcoin-com-rest"
import { ECPair, HDNode } from "bitcoincashjs-lib"
import { config } from "./config"
// consts
const prompt: any = require("prompt")
// Interfaces
interface Fund {
contract: string
amount: number
}
run()
export async function run(): Promise<void> {
try {
// BITBOX Instance
const bitbox: BITBOX = new BITBOX({
restURL: "https://trest.bitcoin.com/v2/"
})
// start the prompt to get user input
prompt.start()
// ask for contract and amount
prompt.get(
["contract", "amount"],
async (err: any, result: Fund): Promise<any> => {
// contract
const contract: string = result.contract
// amount
const amount: number = +result.amount
// root seed buffer from mnemonic
const rootSeed: Buffer = bitbox.Mnemonic.toSeed(config.mnemonic)
// master HDNode
let masterHDNode: HDNode = bitbox.HDNode.fromSeed(rootSeed, "testnet")
// HDNode of BIP44 account
let account: HDNode = bitbox.HDNode.derivePath(
masterHDNode,
"m/44'/145'/1'"
)
// derive the first external change address HDNode which is going to spend utxo
let change: HDNode = bitbox.HDNode.derivePath(account, "0/0")
// get the cash address
let cashAddress: string = bitbox.HDNode.toCashAddress(change)
// fetch utxo for cash addr
let utxos = (await bitbox.Address.utxo(
cashAddress
)) as AddressUtxoResult
// BITBOX Transaction Builder
let transactionBuilder: any = new bitbox.TransactionBuilder("testnet")
// original amount of satoshis in vin
let originalAmount: number = utxos.utxos[0].satoshis
// index of vout
let vout: number = utxos.utxos[0].vout
// txid of vout
let txid: string = utxos.utxos[0].txid
// add input with txid and index of vout
transactionBuilder.addInput(txid, vout)
// get byte count to calculate fee. paying 1 sat/byte
let byteCount: number = bitbox.BitcoinCash.getByteCount(
{ P2PKH: 1 },
{ P2PKH: 2 }
)
// amount to send to receiver. It's the original amount - 1 sat/byte for tx size
let sendAmount: number = originalAmount - byteCount
// Send most of the tBCH back to self
transactionBuilder.addOutput(cashAddress, sendAmount - amount)
// send top off amount
transactionBuilder.addOutput(contract, amount)
// keypair
let keyPair: ECPair = bitbox.HDNode.toKeyPair(change)
// sign w/ HDNode
let redeemScript: undefined
transactionBuilder.sign(
0,
keyPair,
redeemScript,
transactionBuilder.hashTypes.SIGHASH_ALL,
originalAmount
)
// build tx
let tx: any = transactionBuilder.build()
// output rawhex
let hex: string = tx.toHex()
// console.log(hex)
// sendRawTransaction to running BCH node
let success: string = await bitbox.RawTransactions.sendRawTransaction(
hex
)
// success!
console.log(
`success: https://explorer.bitcoin.com/tbch/tx/${success}#o=1`
)
}
)
} catch (error) {
console.log(error)
}
}