REF: convert wallet class to typescript

This commit is contained in:
overtorment 2022-09-11 11:59:00 +01:00
parent 58944c0f3e
commit 34f6ece97b
5 changed files with 23 additions and 13 deletions

View File

@ -22,6 +22,7 @@ export type ElectrumTransaction = {
txinwitness: string[];
sequence: number;
addresses?: string[];
value?: number;
}[];
vout: {
value: number;
@ -64,6 +65,14 @@ export function multiGetTransactionByTxid(
): Promise<Record<string, ElectrumTransaction>>;
export function multiGetTransactionByTxid(txIds: string[], batchsize: number, verbose: false): Promise<Record<string, string>>;
export type MultiGetBalanceResponse = {
balance: number;
unconfirmed_balance: number; // eslint-disable-line camelcase
addresses: Record<string, { confirmed: number; unconfirmed: number }>;
};
export function multiGetBalanceByAddress(addresses: string[], batchsize?: number): Promise<MultiGetBalanceResponse>;
export function getTransactionsByAddress(address: string): ElectrumTransaction[];
export function getMempoolTransactionsByAddress(address: string): Promise<MempoolTransaction[]>;

View File

@ -9,14 +9,15 @@ import { randomBytes } from '../rng';
import { AbstractHDWallet } from './abstract-hd-wallet';
import { ECPairFactory } from 'ecpair';
import { CreateTransactionResult, CreateTransactionUtxo, Transaction, Utxo } from './types';
import { ElectrumHistory, ElectrumTransaction } from '../../blue_modules/BlueElectrum';
import { ElectrumHistory } from '../../blue_modules/BlueElectrum';
import type BlueElectrumNs from '../../blue_modules/BlueElectrum';
import { ECPairInterface } from 'ecpair/src/ecpair';
import { Psbt, Transaction as BTransaction } from 'bitcoinjs-lib';
import { CoinselectReturnInput, CoinSelectTarget } from 'coinselect';
import { CoinSelectReturnInput, CoinSelectTarget } from 'coinselect';
const ECPair = ECPairFactory(ecc);
const bitcoin = require('bitcoinjs-lib');
const BlueElectrum = require('../../blue_modules/BlueElectrum');
const BlueElectrum: typeof BlueElectrumNs = require('../../blue_modules/BlueElectrum');
const reverse = require('buffer-reverse');
const bip32 = BIP32Factory(ecc);
@ -280,7 +281,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
// now, tricky part. we collect all transactions from inputs (vin), and batch fetch them too.
// then we combine all this data (we need inputs to see source addresses and amounts)
const vinTxids = [];
for (const txdata of Object.values(txdatas) as ElectrumTransaction[]) {
for (const txdata of Object.values(txdatas)) {
for (const vin of txdata.vin) {
vinTxids.push(vin.txid);
}
@ -315,7 +316,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
// 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++) {
for (const tx of Object.values(txdatas) as ElectrumTransaction[]) {
for (const tx of Object.values(txdatas)) {
for (const vin of tx.vin) {
if (vin.addresses && vin.addresses.indexOf(this._getExternalAddressByIndex(c)) !== -1) {
// this TX is related to our address
@ -356,7 +357,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
}
for (let c = 0; c < this.next_free_change_address_index + this.gap_limit; c++) {
for (const tx of Object.values(txdatas) as ElectrumTransaction[]) {
for (const tx of Object.values(txdatas)) {
for (const vin of tx.vin) {
if (vin.addresses && vin.addresses.indexOf(this._getInternalAddressByIndex(c)) !== -1) {
// this TX is related to our address
@ -993,7 +994,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
return { tx, inputs, outputs, fee, psbt };
}
_addPsbtInput(psbt: Psbt, input: CoinselectReturnInput, sequence: number, masterFingerprintBuffer: Buffer) {
_addPsbtInput(psbt: Psbt, input: CoinSelectReturnInput, sequence: number, masterFingerprintBuffer: Buffer) {
if (!input.address) {
throw new Error('Internal error: no address on Utxo during _addPsbtInput()');
}

View File

@ -5,7 +5,7 @@ import { AbstractWallet } from './abstract-wallet';
import { HDSegwitBech32Wallet } from '..';
import * as bitcoin from 'bitcoinjs-lib';
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
import coinSelect, { CoinSelectOutput, CoinselectReturnInput, CoinSelectTarget, CoinSelectUtxo } from 'coinselect';
import coinSelect, { CoinSelectOutput, CoinSelectReturnInput, CoinSelectTarget, CoinSelectUtxo } from 'coinselect';
import coinSelectSplit from 'coinselect/split';
import { CreateTransactionResult, CreateTransactionUtxo, Transaction, Utxo } from './types';
import { ECPairAPI, ECPairFactory, Signer } from 'ecpair';
@ -378,7 +378,7 @@ export class LegacyWallet extends AbstractWallet {
feeRate: number,
changeAddress: string,
): {
inputs: CoinselectReturnInput[];
inputs: CoinSelectReturnInput[];
outputs: CoinSelectOutput[];
fee: number;
} {

View File

@ -1,5 +1,5 @@
import bitcoin from 'bitcoinjs-lib';
import { CoinSelectOutput, CoinselectReturnInput } from 'coinselect';
import { CoinSelectOutput, CoinSelectReturnInput } from 'coinselect';
export type Utxo = {
// Returned by BlueElectrum
@ -34,7 +34,7 @@ export type CreateTransactionUtxo = {
export type CreateTransactionResult = {
tx?: bitcoin.Transaction;
inputs: CoinselectReturnInput[];
inputs: CoinSelectReturnInput[];
outputs: CoinSelectOutput[];
fee: number;
psbt: bitcoin.Psbt;

View File

@ -19,7 +19,7 @@ declare module 'coinselect' {
};
};
export type CoinselectReturnInput = {
export type CoinSelectReturnInput = {
vout: number;
value: number;
txid: string;
@ -42,7 +42,7 @@ declare module 'coinselect' {
feeRate: number,
changeAddress?: string,
): {
inputs: CoinselectReturnInput[];
inputs: CoinSelectReturnInput[];
outputs: CoinSelectOutput[];
fee: number;
};