UNPKG

bitcoin-utility-belt

Version:

Bitcoin Utility Belt is a client-side Bitcoin JavaScript library

100 lines (88 loc) 2.41 kB
// import libs let bitcoin = require("bitcoinjs-lib"); let wallet = require("./wallet"); let fetch = require("node-fetch"); let request = require('request'); let create = (txInfo = [], testnet = false) => { // check network: bitcoin or testnet let network; if(testnet){ network = bitcoin.networks.testnet; }else{ network = bitcoin.networks.bitcoin; } var txb = new bitcoin.TransactionBuilder(network); //txb.setVersion(1); let i; let inputsTx = []; for(i = 0; i < txInfo.length; i++){ let keyPair = bitcoin.ECPair.fromWIF(txInfo[i].privateKey); // add all inputs to an array let j; for(j = 0; j < txInfo[i].inputs.length; j++){ inputsTx.push({ hash: txInfo[i].inputs[j].inputHash, vout: txInfo[i].inputs[j].vout, keyPair: keyPair }); } // add outputs let m; for(m = 0; m < txInfo[i].outputs.length; m++){ txb.addOutput(txInfo[i].outputs[m].address, txInfo[i].outputs[m].value); } } // adding all inputs at once for(i = 0; i < inputsTx.length; i++){ txb.addInput(inputsTx[i].hash, inputsTx[i].vout); } // sign all inputs with their own key pairs for(i = 0; i < inputsTx.length; i++){ txb.sign(i, inputsTx[i].keyPair); } return txb.build().toHex(); }; let txInfo = [ { privateKey: 'L1Knwj9W3qK3qMKdTvmg3VfzUs3ij2LETTFhxza9LfD5dngnoLG1', inputs: [ { inputHash: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c", vout: 6 } ], outputs: [ { address: "1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb", value: 180000 } ] }, { privateKey: 'KwcN2pT3wnRAurhy7qMczzbkpY5nXMW2ubh696UBc1bcwctTx26z', inputs: [ { inputHash: "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730", vout: 0 } ], outputs: [ { address: "1JtK9CQw1syfWj1WtFMWomrYdV3W2tWBF9", value: 170000 } ] } ]; //console.log(create(txInfo)); let faucet = (address, amount, token) => { var data = {address: address, amount: amount}; request.post( { url:'https://api.blockcypher.com/v1/btc/test3/faucet?token='+token, form: JSON.stringify(data) }, function(err,httpResponse,body){ console.log(body); } ); };