forked from amanintech/amansharma.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc.ts
37 lines (32 loc) · 1002 Bytes
/
btc.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
// Based on https://stackoverflow.com/questions/45667415/how-to-receive-bitcoin-payments-using-bitcoinjs-lib-in-node-js
export function subscribePaymentAtAddress(
address: string,
onPayment,
onError = (err) => {}
) {
const btcWS = new WebSocket('wss://ws.blockchain.info/inv')
// NOTIFY ON ADDRESS UPDATE
btcWS.onopen = () => {
btcWS.send(JSON.stringify({ op: 'addr_sub', addr: address }))
console.log('[INFO] btc connection opened')
}
// WE GOT AN UPDATE
btcWS.onmessage = (msg) => {
const response = JSON.parse(msg.data)
const getOuts = response.x.out
// LET'S CHECK THE OUTPUTS
getOuts.map(function (out, i) {
if (address == out.addr) {
const amount = out.value
const calAmount = amount / 100000000
console.log('[INFO] amount received:', calAmount + ' BTC')
onPayment(calAmount)
}
})
}
btcWS.onerror = (error) => {
console.log('connection.onerror', error)
onError(error)
}
return btcWS
}