From daf0c2425575332c054a0818024017f867e3bac9 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Tue, 28 Sep 2021 17:55:48 +0100 Subject: [PATCH] FIX: support for electrum backed by bitcoin code 22+ (closes #3877) --- blue_modules/BlueElectrum.js | 13 +++++++++++++ tests/integration/BlueElectrum.test.js | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/blue_modules/BlueElectrum.js b/blue_modules/BlueElectrum.js index 9341de608..2ea4bb740 100644 --- a/blue_modules/BlueElectrum.js +++ b/blue_modules/BlueElectrum.js @@ -392,11 +392,17 @@ module.exports.getTransactionsFullByAddress = async function (address) { if (prevTxForVin.vout[input.vout].scriptPubKey && prevTxForVin.vout[input.vout].scriptPubKey.addresses) { input.addresses = prevTxForVin.vout[input.vout].scriptPubKey.addresses; } + // in bitcoin core 22.0.0+ they removed `.addresses` and replaced it with plain `.address`: + if (prevTxForVin.vout[input.vout]?.scriptPubKey?.address) { + input.addresses = [prevTxForVin.vout[input.vout].scriptPubKey.address]; + } } } for (const output of full.vout) { if (output.scriptPubKey && output.scriptPubKey.addresses) output.addresses = output.scriptPubKey.addresses; + // in bitcoin core 22.0.0+ they removed `.addresses` and replaced it with plain `.address`: + if (output?.scriptPubKey?.address) output.addresses = [output.scriptPubKey.address]; } full.inputs = full.vin; full.outputs = full.vout; @@ -647,6 +653,13 @@ module.exports.multiGetTransactionByTxid = async function (txids, batchsize, ver } } + // in bitcoin core 22.0.0+ they removed `.addresses` and replaced it with plain `.address`: + for (const txid of Object.keys(ret) ?? []) { + for (const vout of ret[txid].vout ?? []) { + if (vout?.scriptPubKey?.address) vout.scriptPubKey.addresses = [vout.scriptPubKey.address]; + } + } + // saving cache: realm.write(() => { for (const txid of Object.keys(ret)) { diff --git a/tests/integration/BlueElectrum.test.js b/tests/integration/BlueElectrum.test.js index 39001579b..c5e3808f3 100644 --- a/tests/integration/BlueElectrum.test.js +++ b/tests/integration/BlueElectrum.test.js @@ -158,6 +158,7 @@ describe('BlueElectrum', () => { assert.ok(!tx.vin); assert.ok(!tx.vout); assert.ok(tx.inputs); + assert.strictEqual(tx.inputs[0]?.addresses[0], 'bc1q7td49wcxfad9v42kmvg5refn9wcnvnru4395qw'); assert.ok(tx.inputs[0].addresses.length > 0); assert.ok(tx.inputs[0].value > 0); assert.ok(tx.outputs); @@ -263,6 +264,11 @@ describe('BlueElectrum', () => { assert.ok(txdatas['5e2fa84148a7389537434b3ad12fcae71ed43ce5fb0f016a7f154a9b99a973df'].vin); assert.ok(txdatas['5e2fa84148a7389537434b3ad12fcae71ed43ce5fb0f016a7f154a9b99a973df'].vout); assert.ok(txdatas['5e2fa84148a7389537434b3ad12fcae71ed43ce5fb0f016a7f154a9b99a973df'].blocktime); + assert.strictEqual( + txdatas['5e2fa84148a7389537434b3ad12fcae71ed43ce5fb0f016a7f154a9b99a973df']?.vout[0]?.scriptPubKey?.addresses[0], + 'bc1qp09gdem9xepasp4zxa2fxyvr8wazhms0wvtds9', + ); + assert.ok(Object.keys(txdatas).length === 4); if (disableBatching) BlueElectrum.setBatchingEnabled(); });