|
|
@ -1,3 +1,6 @@ |
|
|
|
import systemConfiguration from 'utils/SystemConfiguration.js' |
|
|
|
const bitcoin = require('bitcoinjs-lib') |
|
|
|
|
|
|
|
let BtcUtil = { |
|
|
|
getBalance:async function(address,success) { |
|
|
|
let balance=0; |
|
|
@ -21,6 +24,32 @@ let BtcUtil = { |
|
|
|
//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 |