Skip to content

Commit 1923280

Browse files
Merge pull request #7539 from BitGo/BTC-2732.test-parsePsbt
feat(abstract-utxo): improve transaction parsing and structure
2 parents 218b063 + 2d36d4e commit 1923280

File tree

19 files changed

+322
-104
lines changed

19 files changed

+322
-104
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ import {
7474
import { assertDescriptorWalletAddress, getDescriptorMapFromWallet, isDescriptorWallet } from './descriptor';
7575
import { getChainFromNetwork, getFamilyFromNetwork, getFullNameFromNetwork } from './names';
7676
import { assertFixedScriptWalletAddress } from './address/fixedScript';
77-
import { CustomChangeOptions } from './transaction/fixedScript';
78-
import { toBip32Triple, UtxoKeychain, UtxoNamedKeychains } from './keychains';
77+
import { ParsedTransaction } from './transaction/types';
78+
import { toBip32Triple, UtxoKeychain } from './keychains';
7979
import { verifyKeySignature, verifyUserPublicKey } from './verifyKey';
8080
import { getPolicyForEnv } from './descriptor/validatePolicy';
8181
import { signTransaction } from './transaction/signTransaction';
@@ -186,33 +186,11 @@ export interface VerifyAddressOptions<TCoinSpecific extends UtxoCoinSpecific> ex
186186
coinSpecific?: TCoinSpecific;
187187
}
188188

189-
export interface BaseOutput<TAmount = string | number> {
190-
address: string;
191-
amount: TAmount;
192-
// Even though this external flag is redundant with the chain property, it is necessary for backwards compatibility
193-
// with legacy transaction format.
194-
external?: boolean;
195-
}
196-
197-
export interface FixedScriptWalletOutput<TAmount = string | number> extends BaseOutput<TAmount> {
198-
needsCustomChangeKeySignatureVerification?: boolean;
199-
chain: number;
200-
index: number;
201-
}
202-
203-
export type Output<TAmount = string | number> = BaseOutput<TAmount> | FixedScriptWalletOutput<TAmount>;
204-
205189
export type Bip322Message = {
206190
address: string;
207191
message: string;
208192
};
209193

210-
export function isWalletOutput(output: Output): output is FixedScriptWalletOutput {
211-
return (
212-
(output as FixedScriptWalletOutput).chain !== undefined && (output as FixedScriptWalletOutput).index !== undefined
213-
);
214-
}
215-
216194
export interface TransactionInfo<TNumber extends number | bigint = number> {
217195
/** Maps txid to txhex. Required for offline signing. */
218196
txHexes?: Record<string, string>;
@@ -255,42 +233,6 @@ export interface ParseTransactionOptions<TNumber extends number | bigint = numbe
255233
reqId?: IRequestTracer;
256234
}
257235

258-
export type BaseParsedTransactionOutputs<TNumber extends number | bigint, TOutput> = {
259-
/** all transaction outputs */
260-
outputs: TOutput[];
261-
/** transaction outputs that were specified as recipients but are missing from the transaction */
262-
missingOutputs: TOutput[];
263-
/** transaction outputs that were specified as recipients and are present in the transaction */
264-
explicitExternalOutputs: TOutput[];
265-
/** transaction outputs that were not specified as recipients but are present in the transaction */
266-
implicitExternalOutputs: TOutput[];
267-
/** transaction outputs that are change outputs */
268-
changeOutputs: TOutput[];
269-
/** sum of all explicit external outputs */
270-
explicitExternalSpendAmount: TNumber;
271-
/** sum of all implicit external outputs */
272-
implicitExternalSpendAmount: TNumber;
273-
};
274-
275-
export type BaseParsedTransaction<TNumber extends number | bigint, TOutput> = BaseParsedTransactionOutputs<
276-
TNumber,
277-
TOutput
278-
> /** Some extra properties that have nothing to do with an individual transaction */ & {
279-
keychains: UtxoNamedKeychains;
280-
keySignatures: {
281-
backupPub?: string;
282-
bitgoPub?: string;
283-
};
284-
needsCustomChangeKeySignatureVerification: boolean;
285-
customChange?: CustomChangeOptions;
286-
};
287-
288-
/**
289-
* This type is a bit silly because it allows the type for the aggregate amounts to be different from the type of
290-
* individual amounts.
291-
*/
292-
export type ParsedTransaction<TNumber extends number | bigint = number> = BaseParsedTransaction<TNumber, Output>;
293-
294236
export interface GenerateAddressOptions {
295237
addressType?: ScriptType2Of3;
296238
threshold?: number;

modules/abstract-utxo/src/impl/bch/bch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BitGoBase } from '@bitgo/sdk-core';
22
import * as utxolib from '@bitgo/utxo-lib';
33

44
import { AbstractUtxoCoin, UtxoNetwork } from '../../abstractUtxoCoin';
5+
import { isScriptRecipient } from '../../transaction';
56

67
export class Bch extends AbstractUtxoCoin {
78
protected constructor(bitgo: BitGoBase, network?: UtxoNetwork) {
@@ -25,6 +26,10 @@ export class Bch extends AbstractUtxoCoin {
2526
* @returns {*} address string
2627
*/
2728
canonicalAddress(address: string, version: unknown = 'base58'): string {
29+
if (isScriptRecipient(address)) {
30+
return address;
31+
}
32+
2833
if (version === 'base58') {
2934
return utxolib.addressFormat.toCanonicalFormat(address, this.network);
3035
}

modules/abstract-utxo/src/impl/bcha/bcha.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,4 @@ export class Bcha extends Bch {
1212
static createInstance(bitgo: BitGoBase): Bcha {
1313
return new Bcha(bitgo);
1414
}
15-
16-
canonicalAddress(address: string, version: unknown = 'base58'): string {
17-
if (version === 'base58') {
18-
return utxolib.addressFormat.toCanonicalFormat(address, this.network);
19-
}
20-
21-
if (version === 'cashaddr') {
22-
const script = utxolib.addressFormat.toOutputScriptTryFormats(address, this.network);
23-
return utxolib.addressFormat.fromOutputScriptWithFormat(script, version, this.network);
24-
}
25-
26-
throw new Error(`invalid version ${version}`);
27-
}
2815
}

modules/abstract-utxo/src/impl/doge/doge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
SignTransactionOptions,
88
ExplainTransactionOptions,
99
ParseTransactionOptions,
10-
ParsedTransaction,
1110
VerifyTransactionOptions,
1211
RecoverFromWrongChainOptions,
1312
TransactionInfo,
1413
TransactionPrebuild,
1514
} from '../../abstractUtxoCoin';
15+
import { ParsedTransaction } from '../../transaction/types';
1616
import type { TransactionExplanation } from '../../transaction/fixedScript/explainTransaction';
1717
import type { CrossChainRecoverySigned, CrossChainRecoveryUnsigned } from '../../recovery/crossChainRecovery';
1818

modules/abstract-utxo/src/transaction/descriptor/parse.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ import * as utxolib from '@bitgo/utxo-lib';
22
import { ITransactionRecipient } from '@bitgo/sdk-core';
33
import * as coreDescriptors from '@bitgo/utxo-core/descriptor';
44

5-
import {
6-
AbstractUtxoCoin,
7-
BaseOutput,
8-
BaseParsedTransaction,
9-
BaseParsedTransactionOutputs,
10-
ParseTransactionOptions,
11-
} from '../../abstractUtxoCoin';
5+
import { AbstractUtxoCoin, ParseTransactionOptions } from '../../abstractUtxoCoin';
6+
import { BaseOutput, BaseParsedTransaction, BaseParsedTransactionOutputs } from '../types';
127
import { getKeySignatures, toBip32Triple, UtxoNamedKeychains } from '../../keychains';
138
import { getDescriptorMapFromWallet, getPolicyForEnv } from '../../descriptor';
149
import { IDescriptorWallet } from '../../descriptor/descriptorWallet';

modules/abstract-utxo/src/transaction/descriptor/parseToAmountType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AbstractUtxoCoin, BaseOutput, BaseParsedTransaction, ParseTransactionOptions } from '../../abstractUtxoCoin';
1+
import { AbstractUtxoCoin, ParseTransactionOptions } from '../../abstractUtxoCoin';
2+
import { BaseOutput, BaseParsedTransaction } from '../types';
23
import { IDescriptorWallet } from '../../descriptor/descriptorWallet';
34

45
import { parse, ParsedDescriptorTransaction } from './parse';

modules/abstract-utxo/src/transaction/descriptor/verifyTransaction.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ import * as utxolib from '@bitgo/utxo-lib';
22
import { ITransactionRecipient, TxIntentMismatchError } from '@bitgo/sdk-core';
33
import { DescriptorMap } from '@bitgo/utxo-core/descriptor';
44

5-
import {
6-
AbstractUtxoCoin,
7-
BaseOutput,
8-
BaseParsedTransactionOutputs,
9-
VerifyTransactionOptions,
10-
} from '../../abstractUtxoCoin';
5+
import { AbstractUtxoCoin, VerifyTransactionOptions } from '../../abstractUtxoCoin';
6+
import { BaseOutput, BaseParsedTransactionOutputs } from '../types';
117

128
import { toBaseParsedTransactionOutputsFromPsbt } from './parse';
139

modules/abstract-utxo/src/transaction/fixedScript/explainPsbtWasm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
22
import { Triple } from '@bitgo/sdk-core';
33

4-
import type { Output, FixedScriptWalletOutput } from '../../abstractUtxoCoin';
4+
import type { FixedScriptWalletOutput, Output } from '../types';
55

66
import type { TransactionExplanationWasm } from './explainTransaction';
77

modules/abstract-utxo/src/transaction/fixedScript/explainTransaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { bitgo } from '@bitgo/utxo-lib';
55
import { ITransactionExplanation as BaseTransactionExplanation, Triple } from '@bitgo/sdk-core';
66
import * as utxocore from '@bitgo/utxo-core';
77

8-
import type { Output, Bip322Message, FixedScriptWalletOutput } from '../../abstractUtxoCoin';
8+
import type { Bip322Message } from '../../abstractUtxoCoin';
9+
import type { Output, FixedScriptWalletOutput } from '../types';
910
import { toExtendedAddressFormat } from '../recipient';
1011
import { getPayGoVerificationPubkey } from '../getPayGoVerificationPubkey';
1112

modules/abstract-utxo/src/transaction/fixedScript/parseOutput.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ import {
1212
Triple,
1313
} from '@bitgo/sdk-core';
1414

15-
import { AbstractUtxoCoin, Output, isWalletOutput } from '../../abstractUtxoCoin';
15+
import { AbstractUtxoCoin } from '../../abstractUtxoCoin';
16+
import { Output, FixedScriptWalletOutput } from '../types';
1617

1718
const debug = debugLib('bitgo:v2:parseoutput');
1819

20+
export function isWalletOutput(output: Output): output is FixedScriptWalletOutput {
21+
return (
22+
(output as FixedScriptWalletOutput).chain !== undefined && (output as FixedScriptWalletOutput).index !== undefined
23+
);
24+
}
25+
1926
interface HandleVerifyAddressErrorResponse {
2027
external: boolean;
2128
needsCustomChangeKeySignatureVerification?: boolean;

0 commit comments

Comments
 (0)