mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-13 11:09:20 +01:00
ADD: Broadcast TX using Electrum
This commit is contained in:
parent
13443fe608
commit
a1bf0f8276
4 changed files with 40 additions and 44 deletions
|
@ -166,11 +166,22 @@ async function estimateFees() {
|
|||
return { fast, medium, slow };
|
||||
}
|
||||
|
||||
async function broadcast(hex) {
|
||||
if (!mainClient) throw new Error('Electrum client is not connected');
|
||||
try {
|
||||
const broadcast = await mainClient.blockchainTransaction_broadcast(hex);
|
||||
return broadcast;
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.getBalanceByAddress = getBalanceByAddress;
|
||||
module.exports.getTransactionsByAddress = getTransactionsByAddress;
|
||||
module.exports.multiGetBalanceByAddress = multiGetBalanceByAddress;
|
||||
module.exports.waitTillConnected = waitTillConnected;
|
||||
module.exports.estimateFees = estimateFees;
|
||||
module.exports.broadcast = broadcast;
|
||||
|
||||
module.exports.forceDisconnect = () => {
|
||||
mainClient.keepAlive = () => {}; // dirty hack to make it stop reconnecting
|
||||
|
|
|
@ -7,6 +7,7 @@ const { RNRandomBytes } = NativeModules;
|
|||
const BigNumber = require('bignumber.js');
|
||||
const bitcoin = require('bitcoinjs-lib');
|
||||
const signer = require('../models/signer');
|
||||
const BlueElectrum = require('../BlueElectrum');
|
||||
|
||||
/**
|
||||
* Has private key and single address like "1ABCD....."
|
||||
|
@ -356,32 +357,11 @@ export class LegacyWallet extends AbstractWallet {
|
|||
}
|
||||
|
||||
async broadcastTx(txhex) {
|
||||
let chainso = await this._broadcastTxChainso(txhex);
|
||||
console.log('chainso = ', chainso);
|
||||
|
||||
if ((chainso && chainso.status && chainso.status === 'fail') || !chainso) {
|
||||
console.log('fallback to blockcypher');
|
||||
let blockcypher = await this._broadcastTxBlockcypher(txhex); // fallback
|
||||
console.log('blockcypher = ', blockcypher);
|
||||
|
||||
if (Object.keys(blockcypher).length === 0 || blockcypher.error) {
|
||||
// error
|
||||
console.log('blockcypher error, fallback to smartbit');
|
||||
let smartbit = await this._broadcastTxSmartbit(txhex);
|
||||
console.log('smartbit = ', smartbit);
|
||||
return smartbit;
|
||||
|
||||
// let btczen = await this._broadcastTxBtczen(txhex);
|
||||
// console.log(btczen);
|
||||
// return btczen;
|
||||
}
|
||||
return blockcypher;
|
||||
} else {
|
||||
console.log('success');
|
||||
// success
|
||||
return {
|
||||
result: chainso.data.txid,
|
||||
};
|
||||
try {
|
||||
const broadcast = await BlueElectrum.broadcast(txhex);
|
||||
return broadcast;
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ exports.createHDTransaction = function(utxos, toAddress, amount, fixedFee, chang
|
|||
let ourOutputs = {};
|
||||
let outputNum = 0;
|
||||
for (const unspent of utxos) {
|
||||
if (unspent.confirmations < 2) {
|
||||
if (unspent.confirmations < 1) {
|
||||
// using only confirmed outputs
|
||||
continue;
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ exports.createHDSegwitTransaction = function(utxos, toAddress, amount, fixedFee,
|
|||
let ourOutputs = {};
|
||||
let outputNum = 0;
|
||||
for (const unspent of utxos) {
|
||||
if (unspent.confirmations < 2) {
|
||||
if (unspent.confirmations < 1) {
|
||||
// using only confirmed outputs
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -40,22 +40,27 @@ export default class Confirm extends Component {
|
|||
|
||||
broadcast() {
|
||||
this.setState({ isLoading: true }, async () => {
|
||||
let result = await this.state.fromWallet.broadcastTx(this.state.tx);
|
||||
console.log('broadcast result = ', result);
|
||||
if (typeof result === 'string') {
|
||||
result = JSON.parse(result);
|
||||
}
|
||||
this.setState({ isLoading: false });
|
||||
if (result && result.error) {
|
||||
alert(JSON.stringify(result.error));
|
||||
} else {
|
||||
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
|
||||
this.props.navigation.navigate('Success', {
|
||||
fee: Number(this.state.fee),
|
||||
amount: this.state.amount,
|
||||
address: this.state.address,
|
||||
dismissModal: () => this.props.navigation.dismiss(),
|
||||
});
|
||||
try {
|
||||
let result = await this.state.fromWallet.broadcastTx(this.state.tx);
|
||||
if (result && result.code) {
|
||||
if (result.code === 1) {
|
||||
const message = result.message.split('\n');
|
||||
console.warn(message);
|
||||
throw new Error(`${message[0]}: ${message[2]}`);
|
||||
}
|
||||
} else {
|
||||
console.log('broadcast result = ', result);
|
||||
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
|
||||
this.props.navigation.navigate('Success', {
|
||||
fee: Number(this.state.fee),
|
||||
amount: this.state.amount,
|
||||
address: this.state.address,
|
||||
dismissModal: () => this.props.navigation.dismiss(),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
this.setState({ isLoading: false });
|
||||
alert(error.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue