-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathexplainTransaction.ts
More file actions
74 lines (70 loc) · 2.75 KB
/
explainTransaction.ts
File metadata and controls
74 lines (70 loc) · 2.75 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
import * as utxolib from '@bitgo/utxo-lib';
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
import { isTriple, IWallet, Triple } from '@bitgo/sdk-core';
import { getDescriptorMapFromWallet, isDescriptorWallet } from '../descriptor';
import { toBip32Triple } from '../keychains';
import { getPolicyForEnv } from '../descriptor/validatePolicy';
import { UtxoCoinName } from '../names';
import type { Unspent } from '../unspent';
import { getReplayProtectionPubkeys } from './fixedScript/replayProtection';
import type {
TransactionExplanationUtxolibLegacy,
TransactionExplanationUtxolibPsbt,
TransactionExplanationWasm,
} from './fixedScript/explainTransaction';
import * as fixedScript from './fixedScript';
import * as descriptor from './descriptor';
/**
* Decompose a raw transaction into useful information, such as the total amounts,
* change amounts, and transaction outputs.
*/
export function explainTx<TNumber extends number | bigint>(
tx: utxolib.bitgo.UtxoTransaction<TNumber> | utxolib.bitgo.UtxoPsbt | fixedScriptWallet.BitGoPsbt,
params: {
wallet?: IWallet;
pubs?: string[];
txInfo?: { unspents?: Unspent<TNumber>[] };
changeInfo?: fixedScript.ChangeAddressInfo[];
},
coinName: UtxoCoinName
): TransactionExplanationUtxolibLegacy | TransactionExplanationUtxolibPsbt | TransactionExplanationWasm {
if (params.wallet && isDescriptorWallet(params.wallet)) {
if (tx instanceof utxolib.bitgo.UtxoPsbt) {
if (!params.pubs || !isTriple(params.pubs)) {
throw new Error('pub triple is required for descriptor wallets');
}
const walletKeys = toBip32Triple(params.pubs);
const descriptors = getDescriptorMapFromWallet(
params.wallet,
walletKeys,
getPolicyForEnv(params.wallet.bitgo.env)
);
return descriptor.explainPsbt(tx, descriptors);
}
throw new Error('legacy transactions are not supported for descriptor wallets');
}
if (tx instanceof utxolib.bitgo.UtxoPsbt) {
return fixedScript.explainPsbt(tx, params, coinName);
} else if (tx instanceof fixedScriptWallet.BitGoPsbt) {
const pubs = params.pubs;
if (!pubs) {
throw new Error('pub triple is required');
}
const walletXpubs: Triple<string> | undefined =
pubs instanceof utxolib.bitgo.RootWalletKeys
? (pubs.triple.map((k) => k.neutered().toBase58()) as Triple<string>)
: isTriple(pubs)
? (pubs as Triple<string>)
: undefined;
if (!walletXpubs) {
throw new Error('pub triple must be valid triple or RootWalletKeys');
}
return fixedScript.explainPsbtWasm(tx, walletXpubs, {
replayProtection: {
publicKeys: getReplayProtectionPubkeys(coinName),
},
});
} else {
return fixedScript.explainLegacyTx(tx, params, coinName);
}
}