Skip to content

Commit

Permalink
ADD: Broadcast TX using Electrum
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosrdz authored and Overtorment committed Feb 28, 2019
1 parent 13443fe commit a1bf0f8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
11 changes: 11 additions & 0 deletions BlueElectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 6 additions & 26 deletions class/legacy-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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....."
Expand Down Expand Up @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions models/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
37 changes: 21 additions & 16 deletions screen/send/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down

0 comments on commit a1bf0f8

Please sign in to comment.