-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-addr.js
52 lines (45 loc) · 1.51 KB
/
watch-addr.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
const btcd = require('btcd')
, iferr = require('iferr')
, { tx: TX, coin: Coin } = require('bcoin')
, { EventEmitter } = require('events')
const NETWORK = process.env.NETWORK || 'testnet'
, { BTCD_URI, BTCD_CERT } = process.env
const hub = new EventEmitter
if (BTCD_URI) {
const btcd = require('btcd')
module.exports = (addr, cb) => {
btcdClient().notifyreceived([ addr ], iferr(cb))
hub.once(addr, cb)
}
const btcdClient = (client => _ => {
if (!client) {
client = btcd(process.env.BTCD_URI, process.env.BTCD_CERT)
client.on('recvtx', (rawtx, block) => {
const tx = TX.fromRaw(rawtx, 'hex')
tx.outputs.forEach((out, outv) => {
const addr = out.getAddress()
addr && hub.emit(addr.toBase58(NETWORK), null, Coin.fromTX(tx, outv, block ? block.height : -1), tx, block)
})
})
}
return client
})()
} else {
const inquirer = require('inquirer')
module.exports = (addr, cb) => {
inquirer.prompt([
{ name: 'rawtx', message: 'the lock tx in raw hex format' }
//, { name: 'txid', message: 'txid' }
//, { name: 'vout', message: 'vout' }
//, { name: 'value', message: 'value' }
]).then(answers => {
const tx = TX.fromRaw(answers.rawtx.replace(/\s+/g, ''), 'hex')
tx.outputs.some((out, outv) => {
if (out.getAddress().toBase58(NETWORK) == addr) {
cb(null, Coin.fromTX(tx, outv, -1), tx)
return true
}
}) || cb(new Error('invalid tx'))
})
}
}