-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.js
132 lines (103 loc) · 3.07 KB
/
start.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
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
global.fetch = require('node-fetch')
const zmq = require('zeromq')
const {createCall} = require('bitcoind-client')
const bitcoin = require('bitcoinjs-lib')
const {pushtx} = require('pushtx')
const randomBytes = require('randombytes')
const keys = require('./keys')
const config = require('./config')
const args = process.argv.slice(2)
const testing = args.length && args[0] === 'test'
const call = createCall(config.jsonrpc)
function connect() {
const sock = zmq.socket('sub')
sock.connect(config.zeromq.address)
sock.subscribe('rawtx')
console.log('Connected to %s\n', config.zeromq.address)
sock.on('message', (topic, message) => {
onTransaction(message.toString('hex'))
})
}
function toCoins(satoshi) {
return (satoshi / 1e8).toFixed(8)
}
function logStatus(status) {
process.stdout.write("\033[1A\033[K")
process.stdout.write(status + "\n")
}
function onTransaction(hex) {
const tx = bitcoin.Transaction.fromHex(hex)
const {outs} = tx
outs.forEach((o, n) => {
const {script, value} = o
let addr
try {
addr = bitcoin.address.fromOutputScript(script)
logStatus(toCoins(value) + ' => ' + addr)
} catch (e) {
return
}
if (addr in keys) {
const hash = tx.getId().toString('hex')
const pair = keys[addr]
console.log('Sweeping %s (%s)...', addr, pair.privateKey.toString('hex'))
if ('original' in pair.privateKey) {
console.log('Original: %s', pair.privateKey.original ? pair.privateKey.original : '(empty)')
}
spend({hash, n, value, pair})
}
})
}
function spend(data) {
const {hash, n, value, pair} = data
const txb = new bitcoin.TransactionBuilder()
let fee = 0
if (value >= 10000000) {
fee = 100000
} else if (value >= 100000) {
fee = Math.floor(value / 100)
} else if (value >= 1000) {
fee = 1000
} else {
fee = 0
}
txb.setVersion(1)
txb.addInput(hash, n)
txb.addOutput(config.address, value - fee)
txb.sign(0, pair)
const tx = txb.build()
const hex = tx.toHex()
console.log('Prev TX: %s:%d', hash, n)
console.log('Value: %s', toCoins(value))
console.log('Fee: %s', toCoins(fee))
console.log('Output: %s', config.address)
console.log('TXID: %s', tx.getId().toString('hex'))
console.log('Raw Hex: %s', hex)
sendTx(hex)
.then((result) => console.log('sendTx result: ', result))
.catch((err) => console.log('sendTx err: ', err))
}
function sendTx(hex) {
return Promise.all([
call('sendrawtransaction', hex),
pushtx(hex)
].map(p => p.catch(err => {
return err
})))
}
function test() {
const key = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
const pair = bitcoin.ECPair.fromPrivateKey(key)
const {address} = bitcoin.payments.p2pkh({pubkey: pair.publicKey})
const txb = new bitcoin.TransactionBuilder()
txb.setVersion(1)
txb.addInput(randomBytes(32).toString('hex'), 0)
txb.addOutput(address, 1000000)
txb.sign(0, pair)
onTransaction(txb.build().toHex())
}
console.log('Collecting coins to: %s', config.address)
connect()
if (testing) {
test()
}