Browse Source

ADD: Broadcast TX using Electrum

transactionselectrum
Marcos Rodriguez Vélez 6 years ago
committed by Igor Korsakov
parent
commit
a1bf0f8276
  1. 11
      BlueElectrum.js
  2. 32
      class/legacy-wallet.js
  3. 4
      models/signer.js
  4. 17
      screen/send/confirm.js

11
BlueElectrum.js

@ -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

32
class/legacy-wallet.js

@ -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;
}
}

4
models/signer.js

@ -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;
}

17
screen/send/confirm.js

@ -40,15 +40,16 @@ export default class Confirm extends Component {
broadcast() {
this.setState({ isLoading: true }, async () => {
try {
let result = await this.state.fromWallet.broadcastTx(this.state.tx);
console.log('broadcast result = ', result);
if (typeof result === 'string') {
result = JSON.parse(result);
if (result && result.code) {
if (result.code === 1) {
const message = result.message.split('\n');
console.warn(message);
throw new Error(`${message[0]}: ${message[2]}`);
}
this.setState({ isLoading: false });
if (result && result.error) {
alert(JSON.stringify(result.error));
} 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),
@ -57,6 +58,10 @@ export default class Confirm extends Component {
dismissModal: () => this.props.navigation.dismiss(),
});
}
} catch (error) {
this.setState({ isLoading: false });
alert(error.message);
}
});
}

Loading…
Cancel
Save