diff --git a/pages/menu/wallet/index.vue b/pages/menu/wallet/index.vue index abe72e8..e606738 100644 --- a/pages/menu/wallet/index.vue +++ b/pages/menu/wallet/index.vue @@ -119,9 +119,9 @@ }, onShow() { - this.timer = setInterval(() => { - this.updateBalance(); - }, 5000) + // this.timer = setInterval(() => { + // this.updateBalance(); + // }, 5000) }, onHide() { if (this.timer) { @@ -131,7 +131,9 @@ } }, onLoad() { - + this.$BtcUtil.sendTransaction('n2YtuKz8JvKeWqfhmNPP8XMJenaShqZCTs', + 'mrHdfpDqetHCJXT7ZP1anAFXsXvXL8h11i', + '0.001','cSgoXHGhU5o3Xhv6FFvAERsaMqUD964uYhZik5hizu6QjUdcxJ2c') let currency = uni.getStorageSync('currency'); if (currency) { this.currency = currency; diff --git a/utils/BtcUtil.js b/utils/BtcUtil.js index 3dc84b2..375b4f8 100644 --- a/utils/BtcUtil.js +++ b/utils/BtcUtil.js @@ -25,31 +25,62 @@ let BtcUtil = { } return balance; }, + btcToSatoshi:function(btcAmount){ + return Math.ceil(btcAmount * 1e8); + }, sendTransaction:async function(fromAddress, toAddress, value, privateKey){ + const utxo = await fetch(systemConfiguration.constant.btcUtxo+fromAddress); + const utxoData=await utxo.json(); + const txs=utxoData.data.txs; + let txsList=[]; + let remainingValue=value; + let balanceSatoshis=0; + + for(let i=0;iremainingValue){ + break; + } + remainingValue=remainingValue-txs[i].value; + } + if(balanceSatoshis<=value){ + throw new Error("Insufficient balance"); + } + console.log(balanceSatoshis,value) + const feeSatoshis=this.btcToSatoshi(0.0001); - let bob = new bitcoin.ECPair.fromWIF(privateKey); + balanceSatoshis=this.btcToSatoshi(balanceSatoshis); + value=this.btcToSatoshi(value); + let change = balanceSatoshis - feeSatoshis - value; - const txb = new bitcoin.TransactionBuilder(systemConfiguration.constant.btcNetwork); + let bob = new bitcoin.ECPair.fromWIF(privateKey,systemConfiguration.constant.btcNetwork); + let txb = new bitcoin.TransactionBuilder(systemConfiguration.constant.btcNetwork); + for(let i=0;i