Skip to content

Commit 7b1dc9f

Browse files
Merge pull request #8105 from BitGo/BTC-2650.eliminate-utxolib-in-recovery
feat(abstract-utxo): simplify mock recovery provider
2 parents 909aaab + fb8feb7 commit 7b1dc9f

1 file changed

Lines changed: 3 additions & 71 deletions

File tree

  • modules/abstract-utxo/test/unit/recovery

modules/abstract-utxo/test/unit/recovery/mock.ts

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { bitgo } from '@bitgo/utxo-lib';
21
import { AddressInfo, TransactionIO } from '@bitgo/blockapis';
3-
import * as utxolib from '@bitgo/utxo-lib';
42
import { address as wasmAddress, AddressFormat } from '@bitgo/wasm-utxo';
53

64
import { AbstractUtxoCoin, RecoveryProvider } from '../../../src';
75
import { Bch } from '../../../src/impl/bch';
86
import { Bsv } from '../../../src/impl/bsv';
9-
import type { Unspent, UnspentWithPrevTx, WalletUnspent } from '../../../src/unspent';
7+
import { parseOutputId, type Unspent, type UnspentWithPrevTx, type WalletUnspent } from '../../../src/unspent';
108
export class MockRecoveryProvider implements RecoveryProvider {
119
public unspents: Unspent<bigint>[];
1210
private prevTxCache: Record<string, string> = {};
1311
constructor(unspents: Unspent<bigint>[]) {
1412
this.unspents = unspents;
1513
this.unspents.forEach((u) => {
16-
if (utxolib.bitgo.isUnspentWithPrevTx(u)) {
17-
const { txid } = bitgo.parseOutputId(u.id);
14+
if ('prevTx' in u) {
15+
const { txid } = parseOutputId(u.id);
1816
this.prevTxCache[txid] = (u as UnspentWithPrevTx<bigint>).prevTx.toString('hex');
1917
}
2018
});
@@ -49,72 +47,6 @@ export class MockRecoveryProvider implements RecoveryProvider {
4947
throw new Error(`not implemented`);
5048
}
5149
}
52-
export class MockCrossChainRecoveryProvider<TNumber extends number | bigint> implements RecoveryProvider {
53-
private addressVersion: 'cashaddr' | 'base58';
54-
private addressFormat: AddressFormat;
55-
constructor(
56-
public coin: AbstractUtxoCoin,
57-
public unspents: Unspent<TNumber>[],
58-
public tx: utxolib.bitgo.UtxoTransaction<TNumber>
59-
) {
60-
// this is how blockchair will return the data, as a cashaddr for BCH like coins
61-
// BSV supports cashaddr, but at the time of writing the SDK does not support cashaddr for bsv
62-
this.addressFormat = this.coin instanceof Bch && !(this.coin instanceof Bsv) ? 'cashaddr' : 'default';
63-
this.addressVersion = this.coin instanceof Bch && !(this.coin instanceof Bsv) ? 'cashaddr' : 'base58';
64-
}
65-
66-
async getUnspentsForAddresses(addresses: string[]): Promise<Unspent[]> {
67-
return this.tx.outs.map((o, vout: number) => {
68-
let address = wasmAddress.fromOutputScriptWithCoin(o.script, this.coin.name, this.addressFormat);
69-
if (address.includes(':')) {
70-
[, address] = address.split(':');
71-
}
72-
return {
73-
id: `${this.tx?.getId()}:${vout}`,
74-
address,
75-
value: Number(o.value),
76-
...(this.coin.amountType === 'bigint' ? { valueString: o.value.toString() } : {}),
77-
};
78-
});
79-
}
80-
81-
async getTransactionIO(txid: string): Promise<TransactionIO> {
82-
const payload: TransactionIO = {
83-
inputs: this.unspents.map((u) => {
84-
// imitate how blockchair returns data
85-
let address = this.coin.canonicalAddress(u.address, this.addressVersion);
86-
if (address.includes(':')) {
87-
[, address] = address.split(':');
88-
}
89-
return {
90-
address,
91-
};
92-
}),
93-
outputs: this.tx.outs.map((o) => {
94-
let address = wasmAddress.fromOutputScriptWithCoin(o.script, this.coin.name, this.addressFormat);
95-
if (address.includes(':')) {
96-
[, address] = address.split(':');
97-
}
98-
return {
99-
address,
100-
};
101-
}),
102-
};
103-
return payload;
104-
}
105-
106-
async getAddressInfo(address: string): Promise<AddressInfo> {
107-
throw new Error(`not implemented`);
108-
}
109-
110-
async getTransactionHex(txid: string): Promise<string> {
111-
throw new Error(`not implemented`);
112-
}
113-
114-
getTransactionInputs(txid: string): Promise<Unspent<TNumber>[]> {
115-
throw new Error(`not implemented`);
116-
}
117-
}
11850

11951
/**
12052
* Cross-chain recovery mock provider using wasm-utxo only (no utxolib dependency).

0 commit comments

Comments
 (0)