Merge pull request #6184 from BlueWallet/ref-_utxo

REF: unify  this.utxo and this._utxo
This commit is contained in:
GLaDOS 2024-02-25 21:19:32 +00:00 committed by GitHub
commit e6b0240153
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 21 additions and 24 deletions

View File

@ -912,17 +912,15 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
this._utxo = this._utxo.concat(arr);
}
// backward compatibility TODO: remove when we make sure `.utxo` is not used
this.utxo = this._utxo;
// this belongs in `.getUtxo()`
for (const u of this.utxo) {
for (const u of this._utxo) {
u.txid = u.txId;
u.amount = u.value;
u.wif = this._getWifForAddress(u.address);
if (!u.confirmations && u.height) u.confirmations = BlueElectrum.estimateCurrentBlockheight() - u.height;
}
this.utxo = this.utxo.sort((a, b) => Number(a.amount) - Number(b.amount));
this._utxo = this._utxo.sort((a, b) => Number(a.amount) - Number(b.amount));
// more consistent, so txhex in unit tests wont change
}

View File

@ -40,7 +40,7 @@ export class AbstractWallet {
balance: number;
unconfirmed_balance: number;
_address: string | false;
utxo: Utxo[];
_utxo: Utxo[];
_lastTxFetch: number;
_lastBalanceFetch: number;
preferredBalanceUnit: BitcoinUnit;
@ -63,7 +63,7 @@ export class AbstractWallet {
this.balance = 0;
this.unconfirmed_balance = 0;
this._address = false; // cache
this.utxo = [];
this._utxo = [];
this._lastTxFetch = 0;
this._lastBalanceFetch = 0;
this.preferredBalanceUnit = BitcoinUnit.BTC;

View File

@ -127,26 +127,26 @@ export class LegacyWallet extends AbstractWallet {
const address = this.getAddress();
if (!address) throw new Error('LegacyWallet: Invalid address');
const utxos = await BlueElectrum.multiGetUtxoByAddress([address]);
this.utxo = [];
this._utxo = [];
for (const arr of Object.values(utxos)) {
this.utxo = this.utxo.concat(arr);
this._utxo = this._utxo.concat(arr);
}
// now we need to fetch txhash for each input as required by PSBT
if (LegacyWallet.type !== this.type) return; // but only for LEGACY single-address wallets
const txhexes = await BlueElectrum.multiGetTransactionByTxid(
this.utxo.map(u => u.txId),
this._utxo.map(u => u.txId),
50,
false,
);
const newUtxos = [];
for (const u of this.utxo) {
for (const u of this._utxo) {
if (txhexes[u.txId]) u.txhex = txhexes[u.txId];
newUtxos.push(u);
}
this.utxo = newUtxos;
this._utxo = newUtxos;
} catch (error) {
console.warn(error);
}
@ -169,7 +169,7 @@ export class LegacyWallet extends AbstractWallet {
*/
getUtxo(respectFrozen = false): Utxo[] {
let ret: Utxo[] = [];
for (const u of this.utxo) {
for (const u of this._utxo) {
if (u.txId) u.txid = u.txId;
if (!u.confirmations && u.height) u.confirmations = BlueElectrum.estimateCurrentBlockheight() - u.height;
ret.push(u);

View File

@ -59,12 +59,12 @@ it('HD (BIP49) can create TX', async () => {
await hd.fetchBalance();
await hd.fetchUtxo();
assert.ok(typeof hd.utxo[0].confirmations === 'number');
assert.ok(hd.utxo[0].txid);
assert.ok(hd.utxo[0].vout !== undefined);
assert.ok(hd.utxo[0].amount);
assert.ok(hd.utxo[0].address);
assert.ok(hd.utxo[0].wif);
assert.ok(typeof hd._utxo[0].confirmations === 'number');
assert.ok(hd._utxo[0].txid);
assert.ok(hd._utxo[0].vout !== undefined);
assert.ok(hd._utxo[0].amount);
assert.ok(hd._utxo[0].address);
assert.ok(hd._utxo[0].wif);
let txNew = hd.createTransaction(
hd.getUtxo(),
@ -178,10 +178,10 @@ it('Segwit HD (BIP49) can fetch balance with many used addresses in hierarchy',
assert.strictEqual(hd.getBalance(), 51432);
await hd.fetchUtxo();
assert.ok(hd.utxo.length > 0);
assert.ok(hd.utxo[0].txid);
assert.ok(hd.utxo[0].vout === 0);
assert.ok(hd.utxo[0].amount);
assert.ok(hd._utxo.length > 0);
assert.ok(hd._utxo[0].txid);
assert.ok(hd._utxo[0].vout === 0);
assert.ok(hd._utxo[0].amount);
await hd.fetchTransactions();
assert.strictEqual(hd.getTransactions().length, 107);

View File

@ -99,7 +99,7 @@ describe('LegacyWallet', function () {
const w = new LegacyWallet();
w._address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX';
await w.fetchUtxo();
assert.ok(w.utxo.length > 0, 'unexpected empty UTXO');
assert.ok(w._utxo.length > 0, 'unexpected empty UTXO');
assert.ok(w.getUtxo().length > 0, 'unexpected empty UTXO');
assert.ok(w.getUtxo()[0].value);

View File

@ -1988,7 +1988,6 @@ describe('multisig-wallet (native segwit)', () => {
'UR:CRYPTO-OUTPUT/TAADMETAADMSOEADAOAOLSTAADDLOLAOWKAXHDCLAOCEBDFLNNTKJTIOJSFSURBNFXRPEEHKDLGYRTEMRPYTGYZOCASWENCYMKPAVWJKHYAAHDCXJEFTGSZOIMFEYNDYHYZEJTBAMSJEHLDSRDDIYLSRFYTSZTKNRNYLRNDPAMTLDPZCAHTAADEHOEADAEAOAEAMTAADDYOTADLOCSDYYKAEYKAEYKAOYKAOCYUOHFJPKOAXAAAYCYCSYASAVDTAADDLOLAOWKAXHDCLAXMSZTWZDIGERYDKFSFWTYDPFNDKLNAYSWTTMUHYZTOXHSETPEWSFXPEAYWLJSDEMTAAHDCXSPLTSTDPNTLESANSUTTLPRPFHNVSPFCNMHESOYGASTLRPYVAATNNDKFYHLQZPKLEAHTAADEHOEADAEAOAEAMTAADDYOTADLOCSDYYKAEYKAEYKAOYKAOCYWZFEPLETAXAAAYCYCPCKRENBTAADDLOLAOWKAXHDCLAOLSFWYKYLKTFHJLPYEMGLCEDPFNSNRDDSRFASEOZTGWIALFLUIYDNFXHGVESFEMMEAAHDCXHTZETLJNKPHHAYLSCXWPNDSWPSTPGTEOJKKGHDAELSKPNNBKBSYAWZJTFWNNBDKTAHTAADEHOEADAEAOAEAMTAADDYOTADLOCSDYYKAEYKAEYKAOYKAOCYSKTPJPMSAXAAAYCYCEBKWLAMTDWZGRZE\n';
const decoder = new BlueURDecoder();
decoder.receivePart(payload);
console.log(decoder.isComplete());
const data = decoder.toString();