This commit is contained in:
Overtorment 2019-06-30 11:59:10 +01:00
parent 2bb24b4f7a
commit 30c20b9e6c
3 changed files with 47 additions and 4 deletions

View file

@ -257,14 +257,15 @@ module.exports.multiGetHistoryByAddress = async function(addresses, batchsize) {
return ret;
};
module.exports.multiGetTransactionByTxid = async function(txids, batchsize) {
module.exports.multiGetTransactionByTxid = async function(txids, batchsize, verbose) {
batchsize = batchsize || 100;
verbose = verbose !== false;
if (!mainClient) throw new Error('Electrum client is not connected');
let ret = {};
let chunks = splitIntoChunks(txids, batchsize);
for (let chunk of chunks) {
let results = await mainClient.blockchainTransaction_getBatch(chunk, true);
let results = await mainClient.blockchainTransaction_getBatch(chunk, verbose);
for (let txdata of results) {
ret[txdata.param] = txdata.result;
@ -316,6 +317,11 @@ module.exports.broadcast = async function(hex) {
}
};
module.exports.broadcastV2 = async function(hex) {
if (!mainClient) throw new Error('Electrum client is not connected');
return mainClient.blockchainTransaction_broadcast(hex);
};
module.exports.forceDisconnect = () => {
mainClient.keepAlive = () => {}; // dirty hack to make it stop reconnecting
mainClient.reconnect = () => {}; // dirty hack to make it stop reconnecting

View file

@ -46,7 +46,7 @@ export class HDSegwitBech32Wallet extends AbstractHDWallet {
for (let bal of Object.values(this._balances_by_internal_index)) {
ret += bal.c;
}
return ret + this.getUnconfirmedBalance();
return ret + (this.getUnconfirmedBalance() < 0 ? this.getUnconfirmedBalance() : 0);
}
/**
@ -275,6 +275,15 @@ export class HDSegwitBech32Wallet extends AbstractHDWallet {
}
}
// now purge all unconfirmed txs from internal hashmaps, since some may be evicted from mempool because they became invalid
// or replaced. hashmaps are going to be re-populated anyways, since we fetched TXs for addresses with unconfirmed TXs
for (let c = 0; c < this.next_free_address_index + this.gap_limit; c++) {
this._txs_by_external_index[c] = this._txs_by_external_index[c].filter(tx => !!tx.confirmations);
}
for (let c = 0; c < this.next_free_change_address_index + this.gap_limit; c++) {
this._txs_by_internal_index[c] = this._txs_by_internal_index[c].filter(tx => !!tx.confirmations);
}
// now, we need to put transactions in all relevant `cells` of internal hashmaps: this._txs_by_internal_index && this._txs_by_external_index
for (let c = 0; c < this.next_free_address_index + this.gap_limit; c++) {
@ -747,4 +756,19 @@ export class HDSegwitBech32Wallet extends AbstractHDWallet {
}
return txs;
}
/**
* @param {String} txhex
* @returns {Promise<boolean>}
*/
async broadcastTx(txhex) {
try {
let broadcast = await BlueElectrum.broadcastV2(txhex);
if (broadcast.indexOf('successfully') !== -1) return true;
return broadcast.length === 64; // this means return string is txid (precise length), so it was broadcasted ok
} catch (error) {
console.warn(error);
return false;
}
}
}

View file

@ -158,7 +158,7 @@ describe('Electrum', () => {
assert.ok(Object.keys(histories).length === 4);
});
it('ElectrumClient can do multiGetHistoryByAddress()', async () => {
it('ElectrumClient can do multiGetTransactionByTxid()', async () => {
let txdatas = await BlueElectrum.multiGetTransactionByTxid(
[
'ad00a92409d8982a1d7f877056dbed0c4337d2ebab70b30463e2802279fb936d',
@ -184,4 +184,17 @@ describe('Electrum', () => {
assert.ok(txdatas['5e2fa84148a7389537434b3ad12fcae71ed43ce5fb0f016a7f154a9b99a973df'].blocktime);
assert.ok(Object.keys(txdatas).length === 4);
});
it('ElectrumClient can do multiGetHistoryByAddress() to obtain txhex', async () => {
let txdatas = await BlueElectrum.multiGetTransactionByTxid(
['881c54edd95cbdd1583d6b9148eb35128a47b64a2e67a5368a649d6be960f08e'],
3,
false,
);
assert.strictEqual(
txdatas['881c54edd95cbdd1583d6b9148eb35128a47b64a2e67a5368a649d6be960f08e'],
'02000000000102f1155666b534f7cb476a0523a45dc8731d38d56b5b08e877c968812423fbd7f3010000000000000000d8a2882a692ee759b43e6af48ac152dd3410cc4b7d25031e83b3396c16ffbc8900000000000000000002400d03000000000017a914e286d58e53f9247a4710e51232cce0686f16873c870695010000000000160014d3e2ecbf4d91321794e0297e0284c47527cf878b02483045022100d18dc865fb4d087004d021d480b983b8afb177a1934ce4cd11cf97b03e17944f02206d7310687a84aab5d4696d535bca69c2db4449b48feb55fff028aa004f2d1744012103af4b208608c75f38e78f6e5abfbcad9c360fb60d3e035193b2cd0cdc8fc0155c0247304402207556e859845df41d897fe442f59b6106c8fa39c74ba5b7b8e3268ab0aebf186f0220048a9f3742339c44a1e5c78b491822b96070bcfda3f64db9dc6434f8e8068475012102456e5223ed3884dc6b0e152067fd836e3eb1485422eda45558bf83f59c6ad09f00000000',
);
});
});