Skip to content

Commit f00ccde

Browse files
authored
Merge pull request #5267 from BitGo/BTC-1450.refactor-abstract-utxo-wallet
refactor(abstract-utxo): AbstractUtxoCoinWallet
2 parents f6e5392 + c590792 commit f00ccde

File tree

6 files changed

+36
-47
lines changed

6 files changed

+36
-47
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,30 @@ import {
5757
VerifyAddressOptions as BaseVerifyAddressOptions,
5858
VerifyTransactionOptions as BaseVerifyTransactionOptions,
5959
Wallet,
60-
WalletData,
6160
} from '@bitgo/sdk-core';
6261
import { isReplayProtectionUnspent } from './replayProtection';
6362
import { signAndVerifyPsbt, signAndVerifyWalletTransaction } from './sign';
6463
import { supportedCrossChainRecoveries } from './config';
6564
import {
6665
assertValidTransactionRecipient,
6766
explainTx,
68-
parseTransaction,
69-
verifyTransaction,
7067
fromExtendedAddressFormat,
7168
isScriptRecipient,
69+
parseTransaction,
70+
verifyTransaction,
7271
} from './transaction';
7372
import { assertDescriptorWalletAddress, getDescriptorMapFromWallet, isDescriptorWallet } from './descriptor';
7473

7574
import { getChainFromNetwork, getFamilyFromNetwork, getFullNameFromNetwork } from './names';
7675
import { CustomChangeOptions } from './transaction/fixedScript';
7776
import { toBip32Triple, UtxoKeychain, UtxoNamedKeychains } from './keychains';
77+
import { verifyKeySignature, verifyUserPublicKey } from './verifyKey';
78+
import { getPolicyForEnv } from './descriptor/validatePolicy';
79+
import { UtxoWallet } from './wallet';
7880

7981
const debug = debugLib('bitgo:v2:utxo');
8082

8183
import ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;
82-
import { verifyKeySignature, verifyUserPublicKey } from './verifyKey';
83-
import { getPolicyForEnv } from './descriptor/validatePolicy';
8484

8585
type UtxoCustomSigningFunction<TNumber extends number | bigint> = {
8686
(params: {
@@ -198,27 +198,10 @@ export interface TransactionParams extends BaseTransactionParams {
198198
rbfTxIds?: string[];
199199
}
200200

201-
// parseTransactions' return type makes use of WalletData's type but with customChangeKeySignatures as required.
202-
export interface AbstractUtxoCoinWalletData extends WalletData {
203-
customChangeKeySignatures: {
204-
user: string;
205-
backup: string;
206-
bitgo: string;
207-
};
208-
}
209-
210-
export class AbstractUtxoCoinWallet extends Wallet {
211-
public _wallet: AbstractUtxoCoinWalletData;
212-
213-
constructor(bitgo: BitGoBase, baseCoin: IBaseCoin, walletData: any) {
214-
super(bitgo, baseCoin, walletData);
215-
}
216-
}
217-
218201
export interface ParseTransactionOptions<TNumber extends number | bigint = number> extends BaseParseTransactionOptions {
219202
txParams: TransactionParams;
220203
txPrebuild: TransactionPrebuild<TNumber>;
221-
wallet: AbstractUtxoCoinWallet;
204+
wallet: UtxoWallet;
222205
verification?: VerificationOptions;
223206
reqId?: IRequestTracer;
224207
}
@@ -346,7 +329,7 @@ export interface VerifyTransactionOptions<TNumber extends number | bigint = numb
346329
extends BaseVerifyTransactionOptions {
347330
txPrebuild: TransactionPrebuild<TNumber>;
348331
txParams: TransactionParams;
349-
wallet: AbstractUtxoCoinWallet;
332+
wallet: UtxoWallet;
350333
}
351334

352335
export interface SignPsbtRequest {

modules/abstract-utxo/src/descriptor/descriptorWallet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { IWallet, WalletCoinSpecific } from '@bitgo/sdk-core';
33

44
import { NamedDescriptor } from './NamedDescriptor';
55
import { DescriptorMap } from '../core/descriptor';
6-
import { AbstractUtxoCoinWalletData } from '../abstractUtxoCoin';
76
import { DescriptorValidationPolicy, KeyTriple, toDescriptorMapValidate } from './validatePolicy';
7+
import { UtxoWalletData } from '../wallet';
88

99
type DescriptorWalletCoinSpecific = {
1010
descriptors: NamedDescriptor[];
@@ -16,15 +16,15 @@ function isDescriptorWalletCoinSpecific(obj: unknown): obj is DescriptorWalletCo
1616
);
1717
}
1818

19-
type DescriptorWalletData = AbstractUtxoCoinWalletData & {
19+
type DescriptorWalletData = UtxoWalletData & {
2020
coinSpecific: DescriptorWalletCoinSpecific;
2121
};
2222

2323
interface IDescriptorWallet extends IWallet {
2424
coinSpecific(): WalletCoinSpecific & DescriptorWalletCoinSpecific;
2525
}
2626

27-
export function isDescriptorWalletData(obj: AbstractUtxoCoinWalletData): obj is DescriptorWalletData {
27+
export function isDescriptorWalletData(obj: UtxoWalletData): obj is DescriptorWalletData {
2828
return isDescriptorWalletCoinSpecific(obj.coinSpecific);
2929
}
3030

modules/abstract-utxo/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './recovery';
44
export * from './replayProtection';
55
export * from './sign';
66

7+
export { UtxoWallet } from './wallet';
78
export * as descriptor from './descriptor';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Wallet, WalletData } from '@bitgo/sdk-core';
2+
3+
// parseTransactions' return type makes use of WalletData's type but with customChangeKeySignatures as required.
4+
export interface UtxoWalletData extends WalletData {
5+
customChangeKeySignatures: {
6+
user: string;
7+
backup: string;
8+
bitgo: string;
9+
};
10+
}
11+
12+
export interface UtxoWallet extends Wallet {
13+
_wallet: UtxoWalletData;
14+
}

modules/abstract-utxo/test/descriptor/descriptorWallet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assert from 'assert';
22
import { getDescriptorMapFromWallet, isDescriptorWallet } from '../../src/descriptor';
3-
import { AbstractUtxoCoinWallet } from '../../src';
3+
import { UtxoWallet } from '../../src/wallet';
44
import { getDefaultXPubs, getDescriptorMap } from '../core/descriptor/descriptor.utils';
55
import { toBip32Triple } from '../../src/keychains';
66

77
describe('isDescriptorWalletData', function () {
88
const descriptorMap = getDescriptorMap('Wsh2Of3');
99
it('should return true for valid DescriptorWalletData', function () {
10-
const wallet: AbstractUtxoCoinWallet = {
10+
const wallet: UtxoWallet = {
1111
coinSpecific() {
1212
return {
1313
descriptors: [...descriptorMap.entries()].map(([name, descriptor]) => ({
@@ -16,7 +16,7 @@ describe('isDescriptorWalletData', function () {
1616
})),
1717
};
1818
},
19-
} as unknown as AbstractUtxoCoinWallet;
19+
} as unknown as UtxoWallet;
2020

2121
assert(isDescriptorWallet(wallet));
2222
assert.strictEqual(

modules/bitgo/test/v2/unit/coins/abstractUtxoCoin.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import * as utxolib from '@bitgo/utxo-lib';
22
import * as should from 'should';
33
import * as sinon from 'sinon';
4-
import { UnexpectedAddressError, VerificationOptions } from '@bitgo/sdk-core';
4+
import { Wallet, UnexpectedAddressError, VerificationOptions } from '@bitgo/sdk-core';
55
import { TestBitGo } from '@bitgo/sdk-test';
66
import { BitGo } from '../../../../src/bitgo';
7-
import {
8-
AbstractUtxoCoin,
9-
AbstractUtxoCoinWallet,
10-
Output,
11-
TransactionExplanation,
12-
TransactionParams,
13-
} from '@bitgo/abstract-utxo';
7+
import { AbstractUtxoCoin, UtxoWallet, Output, TransactionExplanation, TransactionParams } from '@bitgo/abstract-utxo';
148

159
describe('Abstract UTXO Coin:', () => {
1610
describe('Parse Transaction:', () => {
@@ -31,7 +25,7 @@ describe('Abstract UTXO Coin:', () => {
3125
},
3226
};
3327

34-
const wallet = sinon.createStubInstance(AbstractUtxoCoinWallet, {
28+
const wallet = sinon.createStubInstance(Wallet, {
3529
migratedFrom: 'v1_wallet_base_address',
3630
});
3731

@@ -60,7 +54,7 @@ describe('Abstract UTXO Coin:', () => {
6054
const parsedTransaction = await coin.parseTransaction({
6155
txParams,
6256
txPrebuild: { txHex: '' },
63-
wallet: wallet as any,
57+
wallet: wallet as unknown as UtxoWallet,
6458
verification,
6559
});
6660

@@ -177,8 +171,8 @@ describe('Abstract UTXO Coin:', () => {
177171
});
178172

179173
it('should consider addresses derived from the custom change keys as internal spends', async () => {
180-
const signedSendingWallet = sinon.createStubInstance(AbstractUtxoCoinWallet, stubData.signedSendingWallet as any);
181-
const changeWallet = sinon.createStubInstance(AbstractUtxoCoinWallet, stubData.changeWallet as any);
174+
const signedSendingWallet = sinon.createStubInstance(Wallet, stubData.signedSendingWallet as any);
175+
const changeWallet = sinon.createStubInstance(Wallet, stubData.changeWallet as any);
182176

183177
sinon.stub(coin, 'keychains').returns({
184178
get: sinon.stub().callsFake(({ id }) => {
@@ -319,14 +313,11 @@ describe('Abstract UTXO Coin:', () => {
319313
},
320314
};
321315

322-
const unsignedSendingWallet = sinon.createStubInstance(
323-
AbstractUtxoCoinWallet,
324-
stubData.unsignedSendingWallet as any
325-
);
316+
const unsignedSendingWallet = sinon.createStubInstance(Wallet, stubData.unsignedSendingWallet as any);
326317

327318
it('should fail if the user private key cannot be verified to match the user public key', async () => {
328319
sinon.stub(coin, 'parseTransaction').resolves(stubData.parseTransactionData.badKey as any);
329-
const verifyWallet = sinon.createStubInstance(AbstractUtxoCoinWallet, {});
320+
const verifyWallet = sinon.createStubInstance(Wallet, {});
330321

331322
await coin
332323
.verifyTransaction({

0 commit comments

Comments
 (0)