import systemConfiguration from 'utils/SystemConfiguration.js' const bitcoin = require('bitcoinjs-lib') let BtcUtil = { getBalance:async function(address,success) { let balance=0; try{ let res= await uni.request({ url: 'http://scan.weirui0755.com/btc/api/address/balancetrend/btc/' + address, //请求接口 header: { 'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息 } }); if (res.data.code === 1) { if(res.data.data.length>0){ var data=res.data.data[0]; console.log(data,11111) for(let b in data){ balance=data[b]; } } } }catch(e){ //TODO handle the exception } return balance; }, sendTransaction:async function(fromAddress, toAddress, value, privateKey){ let bob = new bitcoin.ECPair.fromWIF(privateKey); const txb = new bitcoin.TransactionBuilder(systemConfiguration.constant.btcNetwork); txb.setVersion(1); // 在这个交易中, bob在第0个位置,上图所示 txb.addInput("5799a647d6b89a9f73122d75faee6f5a0210bd3cb22c48a70d35eac33ce5d426", 0); txb.addOutput(toAddress, 2000000); console.log(txb,1111111111) // 签名交易,0代表索引,输入排序,这里只有一个输入,所以是第0位。 txb.sign(0, bob); // 序列化成一串字符 const tx = txb.build().toHex(); console.log(tx); const result = await fetch('https://api.blockcypher.com/v1/btc/test3/txs/push',{ method:'post', headers:{'Content-Type':'application/json'}, body:JSON.stringify({tx}) }); // 打印结果 console.log(result); } } export default BtcUtil