如何使用节点js中的bitcoinjs-lib接收比特币支付

时间:2017-08-14 04:18:57

标签: javascript node.js payment bitcoin bitcoinj

基本上我一直试图弄清楚如何使用bitcoinjs-lib来拥有一个快速简便的钱包(在节点js中)。这个程序不是一个完整的钱包,它只需要能够判断比特币地址何时收到付款以及收到多少钱。

1 个答案:

答案 0 :(得分:2)

您可以使用Blockchain.info的WebSocket服务获取地址更新,然后计算通过输出收到的总金额:

var WebSocket = require('ws');
var btcWS = new WebSocket("wss://ws.blockchain.info/inv");
var BTC_ADDR = "1FoxBitjXcBeZUS4eDzPZ7b124q3N7QJK7";

// NOTIFY ON ADDRESS UPDATE
btcWS.onopen = function(){ btcWS.send(JSON.stringify({ "op": "addr_sub", "addr" : BTC_ADDR })); };

// WE GOT AN UPDATE
btcWS.onmessage = function(msg){

    var response  = JSON.parse(msg.data);
    var getOuts   = response.x.out;

    // LET'S CHECK THE OUTPUTS
    getOuts.map(function(out,i){

        if ( BTC_ADDR == out.addr ){

            var amount = out.value;
            var calAmount = amount / 100000000;
            console.log(calAmount + " BTC");    // <-- The total amount just received

        }

    });

};

btcWS.onerror = function (error){ console.log('connection.onerror', error); };