-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathparseTransaction.ts
More file actions
308 lines (271 loc) · 11.3 KB
/
parseTransaction.ts
File metadata and controls
308 lines (271 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import assert from 'assert';
import _ from 'lodash';
import { ITransactionRecipient, KeyIndices, Triple, VerificationOptions, Wallet } from '@bitgo/sdk-core';
import type { AbstractUtxoCoin, ParseTransactionOptions } from '../../abstractUtxoCoin';
import type { FixedScriptWalletOutput, Output, ParsedTransaction } from '../types';
import {
fetchKeychains,
getKeySignatures,
toKeychainTriple,
toXpubTriple,
UtxoKeychain,
UtxoNamedKeychains,
} from '../../keychains';
import { verifyKeySignature } from '../../verifyKey';
import {
assertValidTransactionRecipient,
fromExtendedAddressFormatToScript,
isScriptRecipient,
toExtendedAddressFormat,
toOutputScript,
} from '../recipient';
import { ComparableOutput, ExpectedOutput, outputDifference } from '../outputDifference';
import { toTNumber } from '../../tnumber';
import type { TransactionExplanation } from './explainTransaction';
import { CustomChangeOptions, parseOutput } from './parseOutput';
export type ComparableOutputWithExternal<TValue> = (ComparableOutput<TValue> | ExpectedOutput) & {
external: boolean | undefined;
};
function toCanonicalTransactionRecipient(
coin: AbstractUtxoCoin,
output: { valueString: string; address?: string }
): {
amount: bigint;
address: string;
} {
const amount = BigInt(output.valueString);
assertValidTransactionRecipient({ amount, address: output.address });
assert(output.address, 'address is required');
if (isScriptRecipient(output.address)) {
return { amount, address: output.address };
}
return { amount, address: coin.canonicalAddress(output.address) };
}
async function parseRbfTransaction<TNumber extends bigint | number>(
coin: AbstractUtxoCoin,
params: ParseTransactionOptions<TNumber>
): Promise<ParsedTransaction<TNumber>> {
const { txParams, wallet } = params;
assert(txParams.rbfTxIds);
assert(txParams.rbfTxIds.length === 1);
const txToBeReplaced = await wallet.getTransaction({ txHash: txParams.rbfTxIds[0], includeRbf: true });
const recipients = txToBeReplaced.outputs.flatMap(
(output: { valueString: string; address?: string; wallet?: string }) => {
// For self-sends, the walletId will be the same as the wallet's id
if (output.wallet === wallet.id()) {
return [];
}
return [toCanonicalTransactionRecipient(coin, output)];
}
);
// Recurse into parseTransaction with the derived recipients and without rbfTxIds
return parseTransaction(coin, {
...params,
txParams: {
...txParams,
recipients,
rbfTxIds: undefined,
},
});
}
function toExpectedOutputs(
coin: AbstractUtxoCoin,
txParams: {
recipients?: ITransactionRecipient[];
allowExternalChangeAddress?: boolean;
changeAddress?: string;
}
): ExpectedOutput[] {
// verify that each recipient from txParams has their own output
const expectedOutputs: ExpectedOutput[] = (txParams.recipients ?? []).flatMap((output) => {
if (output.address === undefined) {
assert('script' in output, 'script is required for non-encodeable scriptPubkeys');
if (output.amount.toString() !== '0') {
throw new Error(`Only zero amounts allowed for non-encodeable scriptPubkeys: ${output}`);
}
return [
{
script: toOutputScript(output, coin.name),
value: output.amount === 'max' ? 'max' : BigInt(output.amount),
},
];
}
return [
{
script: fromExtendedAddressFormatToScript(output.address, coin.name),
value: output.amount === 'max' ? 'max' : BigInt(output.amount),
},
];
});
if (txParams.allowExternalChangeAddress && txParams.changeAddress) {
expectedOutputs.push({
script: toOutputScript(txParams.changeAddress, coin.name),
// When an external change address is explicitly specified, count all outputs going towards that
// address in the expected outputs (regardless of the output amount)
value: 'max',
// Note that the change output is not required to exist, so we mark it as optional.
optional: true,
});
}
return expectedOutputs;
}
function verifyCustomChangeKeys(userKeychain: UtxoKeychain, customChange: CustomChangeOptions): void {
for (const keyIndex of [KeyIndices.USER, KeyIndices.BACKUP, KeyIndices.BITGO]) {
if (
!verifyKeySignature({
userKeychain,
keychainToVerify: customChange.keys[keyIndex],
keySignature: customChange.signatures[keyIndex],
})
) {
throw new Error(`failed to verify custom change ${KeyIndices[keyIndex].toLowerCase()} key signature`);
}
}
}
export async function parseTransaction<TNumber extends bigint | number>(
coin: AbstractUtxoCoin,
params: ParseTransactionOptions<TNumber>
): Promise<ParsedTransaction<TNumber>> {
const { txParams, txPrebuild, wallet, verification = {}, reqId } = params;
// Branch off early for RBF transactions
if (txParams.rbfTxIds) {
return parseRbfTransaction(coin, params);
}
if (!_.isUndefined(verification.disableNetworking) && !_.isBoolean(verification.disableNetworking)) {
throw new Error('verification.disableNetworking must be a boolean');
}
const disableNetworking = verification.disableNetworking;
// obtain the keychains and key signatures
let keychains: UtxoNamedKeychains | VerificationOptions['keychains'] | undefined = verification.keychains;
if (!keychains) {
if (disableNetworking) {
throw new Error('cannot fetch keychains without networking');
}
keychains = await fetchKeychains(coin, wallet, reqId);
}
if (!UtxoNamedKeychains.is(keychains)) {
throw new Error('invalid keychains');
}
const keychainArray: Triple<UtxoKeychain> = toKeychainTriple(keychains);
if (_.isUndefined(txPrebuild.txHex)) {
throw new Error('missing required txPrebuild property txHex');
}
const expectedOutputs = toExpectedOutputs(coin, txParams);
// get the keychains from the custom change wallet if needed
let customChange: CustomChangeOptions | undefined;
const { customChangeWalletId = undefined } = wallet.coinSpecific() || {};
if (customChangeWalletId) {
// fetch keychains from custom change wallet for deriving addresses.
// These keychains should be signed and this should be verified in verifyTransaction
const customChangeKeySignatures = wallet._wallet.customChangeKeySignatures;
const customChangeWallet: Wallet = await coin.wallets().get({ id: customChangeWalletId });
const customChangeKeys = await fetchKeychains(coin, customChangeWallet, reqId);
if (!customChangeKeys) {
throw new Error('failed to fetch keychains for custom change wallet');
}
if (customChangeKeys.user && customChangeKeys.backup && customChangeKeys.bitgo && customChangeWallet) {
const customChangeKeychains: Triple<UtxoKeychain> = [
customChangeKeys.user,
customChangeKeys.backup,
customChangeKeys.bitgo,
];
customChange = {
keys: customChangeKeychains,
signatures: [customChangeKeySignatures.user, customChangeKeySignatures.backup, customChangeKeySignatures.bitgo],
};
}
}
let customChangeXpubs: Triple<string> | undefined;
if (customChange) {
verifyCustomChangeKeys(keychainArray[KeyIndices.USER], customChange);
customChangeXpubs = toXpubTriple(customChange.keys);
}
// obtain all outputs
const explanation: TransactionExplanation = await coin.explainTransaction<TNumber>({
txHex: txPrebuild.txHex,
txInfo: txPrebuild.txInfo,
pubs: keychainArray.map((k) => k.pub) as Triple<string>,
customChangeXpubs,
});
const allOutputs = [...explanation.outputs, ...explanation.changeOutputs];
/**
* Loop through all the outputs and classify each of them as either internal spends
* or external spends by setting the "external" property to true or false on the output object.
*/
const allOutputDetails: Output[] = await Promise.all(
allOutputs.map((currentOutput) => {
return parseOutput({
currentOutput,
coin,
txPrebuild,
verification,
keychainArray: toKeychainTriple(keychains),
wallet,
txParams: {
recipients: txParams.recipients ?? [],
changeAddress: txParams.changeAddress,
},
customChange,
reqId,
});
})
);
const needsCustomChangeKeySignatureVerification = allOutputDetails.some(
(output) => (output as FixedScriptWalletOutput)?.needsCustomChangeKeySignatureVerification
);
const changeOutputs = _.filter(allOutputDetails, { external: false });
function toComparableOutputsWithExternal(outputs: Output[]): ComparableOutputWithExternal<bigint | 'max'>[] {
return outputs.map((output) => ({
script: fromExtendedAddressFormatToScript(output.address, coin.name),
value: output.amount === 'max' ? 'max' : (BigInt(output.amount) as bigint | 'max'),
external: output.external,
}));
}
const missingOutputs = outputDifference(expectedOutputs, toComparableOutputsWithExternal(allOutputs));
const implicitOutputs = outputDifference(toComparableOutputsWithExternal(allOutputDetails), expectedOutputs);
const explicitOutputs = outputDifference(toComparableOutputsWithExternal(allOutputDetails), implicitOutputs);
// these are all the non-wallet outputs that had been originally explicitly specified in recipients
const explicitExternalOutputs = explicitOutputs.filter((output) => output.external);
// this is the sum of all the originally explicitly specified non-wallet output values
const explicitExternalSpendAmount = toTNumber(
explicitExternalOutputs.reduce((sum: bigint, o) => sum + BigInt(o.value), BigInt(0)),
coin.amountType
) as TNumber;
/**
* The calculation of the implicit external spend amount pertains to verifying the pay-as-you-go-fee BitGo
* automatically applied to transactions sending money out of the wallet. The logic is fairly straightforward
* in that we compare the external spend amount that was specified explicitly by the user to the portion
* that was specified implicitly. To protect customers from people tampering with the transaction outputs, we
* define a threshold for the maximum percentage of the implicit external spend in relation to the explicit
* external spend.
*
* This has become obsolete with the intoduction of `utxocore.paygo.verifyPayGoAddressProof()`.
*/
// make sure that all the extra addresses are change addresses
// get all the additional external outputs the server added and calculate their values
const implicitExternalOutputs = implicitOutputs.filter((output) => output.external);
const implicitExternalSpendAmount = toTNumber(
implicitExternalOutputs.reduce((sum: bigint, o) => sum + BigInt(o.value), BigInt(0)),
coin.amountType
) as TNumber;
function toOutputs(outputs: ExpectedOutput[] | ComparableOutputWithExternal<bigint | 'max'>[]): Output[] {
return outputs.map((output) => ({
address: toExtendedAddressFormat(output.script, coin.name),
amount: output.value.toString(),
external: output.external,
}));
}
return {
keychains,
keySignatures: getKeySignatures(wallet) ?? {},
outputs: allOutputDetails,
missingOutputs: toOutputs(missingOutputs),
explicitExternalOutputs: toOutputs(explicitExternalOutputs),
implicitExternalOutputs: toOutputs(implicitExternalOutputs),
changeOutputs,
explicitExternalSpendAmount,
implicitExternalSpendAmount,
needsCustomChangeKeySignatureVerification,
customChange,
};
}