Skip to content

Commit

Permalink
fix: txId || txid
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains authored and Overtorment committed Mar 24, 2024
1 parent cb91dbf commit 575331d
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 138 deletions.
6 changes: 3 additions & 3 deletions blue_modules/BlueElectrum.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type Utxo = {
height: number;
value: number;
address: string;
txId: string;
txid: string;
vout: number;
wif?: string;
};
Expand Down Expand Up @@ -59,11 +59,11 @@ export function multiGetUtxoByAddress(addresses: string[]): Promise<Record<strin

// TODO: this function returns different results based on the value of `verbose`, consider splitting it into two
export function multiGetTransactionByTxid(
txIds: string[],
txids: string[],
batchsize: number = 45,
verbose: true = true,
): Promise<Record<string, ElectrumTransaction>>;
export function multiGetTransactionByTxid(txIds: string[], batchsize: number, verbose: false): Promise<Record<string, string>>;
export function multiGetTransactionByTxid(txids: string[], batchsize: number, verbose: false): Promise<Record<string, string>>;

export type MultiGetBalanceResponse = {
balance: number;
Expand Down
2 changes: 1 addition & 1 deletion blue_modules/BlueElectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ module.exports.multiGetUtxoByAddress = async function (addresses, batchsize) {
ret[scripthash2addr[utxos.param]] = utxos.result;
for (const utxo of ret[scripthash2addr[utxos.param]]) {
utxo.address = scripthash2addr[utxos.param];
utxo.txId = utxo.tx_hash;
utxo.txid = utxo.tx_hash;
utxo.vout = utxo.tx_pos;
delete utxo.tx_pos;
delete utxo.tx_hash;
Expand Down
4 changes: 2 additions & 2 deletions class/hd-segwit-bech32-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class HDSegwitBech32Transaction {
value = new BigNumber(value).multipliedBy(100000000).toNumber();
wentIn += value;
const address = SegwitBech32Wallet.witnessToAddress(inp.witness[inp.witness.length - 1]);
utxos.push({ vout: inp.index, value, txId: reversedHash, address });
utxos.push({ vout: inp.index, value, txid: reversedHash, address });
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@ export class HDSegwitBech32Transaction {
unconfirmedUtxos.push({
vout: outp.n,
value,
txId: this._txid || this._txDecoded.getId(),
txid: this._txid || this._txDecoded.getId(),
address,
});
}
Expand Down
13 changes: 3 additions & 10 deletions class/wallets/abstract-hd-electrum-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,11 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {

// this belongs in `.getUtxo()`
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.value) - Number(b.value));
// more consistent, so txhex in unit tests wont change
}

Expand All @@ -932,10 +930,8 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
* [ { height: 0,
* value: 666,
* address: 'string',
* txId: 'string',
* vout: 1,
* txid: 'string',
* amount: 666,
* wif: 'string',
* confirmations: 0 } ]
*
Expand Down Expand Up @@ -984,11 +980,9 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
const value = new BigNumber(output.value).multipliedBy(100000000).toNumber();
utxos.push({
txid: tx.txid,
txId: tx.txid,
vout: output.n,
address: String(address),
value,
amount: value,
confirmations: tx.confirmations,
wif: false,
height: BlueElectrum.estimateCurrentBlockheight() - (tx.confirmations ?? 0),
Expand Down Expand Up @@ -1104,7 +1098,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {

/**
*
* @param utxos {Array.<{vout: Number, value: Number, txId: String, address: String}>} List of spendable utxos
* @param utxos {Array.<{vout: Number, value: Number, txid: String, address: String}>} List of spendable utxos
* @param targets {Array.<{value: Number, address: String}>} Where coins are going. If theres only 1 target and that target has no value - this will send MAX to that address (respecting fee rate)
* @param feeRate {Number} satoshi per byte
* @param changeAddress {String} Excessive coins will go back to that address
Expand Down Expand Up @@ -1247,8 +1241,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
}

psbt.addInput({
// @ts-ignore
hash: input.txid || input.txId,
hash: input.txid,
index: input.vout,
sequence,
bip32Derivation: [
Expand Down
2 changes: 1 addition & 1 deletion class/wallets/abstract-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export class AbstractWallet {

/**
*
* @param utxos {Array.<{vout: Number, value: Number, txId: String, address: String}>} List of spendable utxos
* @param utxos {Array.<{vout: Number, value: Number, txid: String, address: String}>} List of spendable utxos
* @param targets {Array.<{value: Number, address: String}>} Where coins are going. If theres only 1 target and that target has no value - this will send MAX to that address (respecting fee rate)
* @param feeRate {Number} satoshi per byte
* @param changeAddress {String} Excessive coins will go back to that address
Expand Down
11 changes: 3 additions & 8 deletions class/wallets/legacy-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ export class LegacyWallet extends AbstractWallet {
// 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) {
if (txhexes[u.txId]) u.txhex = txhexes[u.txId];
if (txhexes[u.txid]) u.txhex = txhexes[u.txid];
newUtxos.push(u);
}

Expand All @@ -161,10 +161,8 @@ export class LegacyWallet extends AbstractWallet {
* [ { height: 0,
* value: 666,
* address: 'string',
* txId: 'string',
* vout: 1,
* txid: 'string',
* amount: 666,
* wif: 'string',
* confirmations: 0 } ]
*
Expand All @@ -174,7 +172,6 @@ export class LegacyWallet extends AbstractWallet {
getUtxo(respectFrozen = false): Utxo[] {
let ret: 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);
}
Expand Down Expand Up @@ -211,11 +208,9 @@ export class LegacyWallet extends AbstractWallet {
const value = new BigNumber(output.value).multipliedBy(100000000).toNumber();
utxos.push({
txid: tx.txid,
txId: tx.txid,
vout: output.n,
address,
value,
amount: value,
confirmations: tx.confirmations,
wif: false,
height: BlueElectrum.estimateCurrentBlockheight() - (tx.confirmations ?? 0),
Expand Down Expand Up @@ -404,7 +399,7 @@ export class LegacyWallet extends AbstractWallet {

/**
*
* @param utxos {Array.<{vout: Number, value: Number, txId: String, address: String, txhex: String, }>} List of spendable utxos
* @param utxos {Array.<{vout: Number, value: Number, txid: String, address: String, txhex: String, }>} List of spendable utxos
* @param targets {Array.<{value: Number, address: String}>} Where coins are going. If theres only 1 target and that target has no value - this will send MAX to that address (respecting fee rate)
* @param feeRate {Number} satoshi per byte
* @param changeAddress {String} Excessive coins will go back to that address
Expand Down
52 changes: 26 additions & 26 deletions class/wallets/lightning-custodian-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ export class LightningCustodianWallet extends LegacyWallet {
headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' },
});
const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + (json.message ? json.message : json.error) + ' (code ' + json.code + ')');
}

Expand Down Expand Up @@ -143,11 +143,11 @@ export class LightningCustodianWallet extends LegacyWallet {
}

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand All @@ -171,11 +171,11 @@ export class LightningCustodianWallet extends LegacyWallet {
},
});
const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand Down Expand Up @@ -234,11 +234,11 @@ export class LightningCustodianWallet extends LegacyWallet {
},
});
const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand Down Expand Up @@ -271,11 +271,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand Down Expand Up @@ -319,11 +319,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand All @@ -348,11 +348,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand Down Expand Up @@ -430,11 +430,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand All @@ -459,11 +459,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand Down Expand Up @@ -492,11 +492,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
if (json.code * 1 === 1 && !noRetry) {
await this.authorize();
return this.fetchBalance(true);
Expand Down Expand Up @@ -581,11 +581,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand All @@ -605,11 +605,11 @@ export class LightningCustodianWallet extends LegacyWallet {
},
});
const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.code && json.code !== 1) {
if (json.code && json.code !== 1) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}
return true;
Expand Down Expand Up @@ -652,11 +652,11 @@ export class LightningCustodianWallet extends LegacyWallet {
});

const json = response.body;
if (typeof json === 'undefined') {
if (!json) {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.body));
}

if (json && json.error) {
if (json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}

Expand Down
3 changes: 1 addition & 2 deletions class/wallets/multisig-hd-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet {
const witnessScript = p2wsh.redeem.output;

psbt.addInput({
// @ts-ignore: fix me txid || txId issue
hash: input.txid || input.txId,
hash: input.txid,
index: input.vout,
sequence,
bip32Derivation,
Expand Down
2 changes: 1 addition & 1 deletion class/wallets/segwit-p2sh-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class SegwitP2SHWallet extends LegacyWallet {

/**
*
* @param utxos {Array.<{vout: Number, value: Number, txId: String, address: String, txhex: String, }>} List of spendable utxos
* @param utxos {Array.<{vout: Number, value: Number, txid: String, address: String, txhex: String, }>} List of spendable utxos
* @param targets {Array.<{value: Number, address: String}>} Where coins are going. If theres only 1 target and that target has no value - this will send MAX to that address (respecting fee rate)
* @param feeRate {Number} satoshi per byte
* @param changeAddress {String} Excessive coins will go back to that address
Expand Down
Loading

0 comments on commit 575331d

Please sign in to comment.