|
| 1 | +/** |
| 2 | + * @prettier |
| 3 | + */ |
| 4 | +import { fixedScriptWallet } from '@bitgo/wasm-utxo'; |
| 5 | +import { Triple } from '@bitgo/sdk-core'; |
| 6 | + |
| 7 | +import { getReplayProtectionPubkeys } from '../../transaction/fixedScript/replayProtection'; |
| 8 | + |
| 9 | +/** |
| 10 | + * How a recipient parsed from a Zcash PSBT is spent. |
| 11 | + * |
| 12 | + * The decode-side counterpart of utxo-core's `buildTransaction/zcash.ts` `ZcashDestination` on |
| 13 | + * the build side: a shielded recipient is an Orchard/Ironwood output stored in the v6 (Ironwood) |
| 14 | + * PSBT's orchard PCZT, and everything else is an ordinary transparent output. A transparent |
| 15 | + * output resolved from a Unified Address carries that original UA (`zcashUnifiedTransparent`), a |
| 16 | + * plain address does not. |
| 17 | + */ |
| 18 | +export type PsbtRecipientDestination = |
| 19 | + | { |
| 20 | + kind: 'zcashShielded'; |
| 21 | + /** |
| 22 | + * The Unified Address the output was addressed to — the original multi-receiver UA the |
| 23 | + * client passed when the PSBT stores one verbatim, otherwise a re-encoded single-receiver |
| 24 | + * Orchard UA. |
| 25 | + */ |
| 26 | + unifiedAddress: string; |
| 27 | + } |
| 28 | + | { |
| 29 | + kind: 'zcashUnifiedTransparent'; |
| 30 | + /** The original Unified Address the transparent receiver was resolved from. */ |
| 31 | + unifiedAddress: string; |
| 32 | + } |
| 33 | + | { kind: 'transparent' }; |
| 34 | + |
| 35 | +/** A recipient resolved from a decoded Zcash PSBT's external outputs. */ |
| 36 | +export interface PsbtRecipient { |
| 37 | + /** Amount in satoshis. */ |
| 38 | + amount: bigint; |
| 39 | + /** |
| 40 | + * The recipient address. For a shielded output this is the Unified Address the output was |
| 41 | + * addressed to — the original multi-receiver UA when the PSBT stores one verbatim, otherwise a |
| 42 | + * re-encoded single-receiver Orchard UA. For a transparent output it is the original Unified |
| 43 | + * Address when one was stored, else the decoded transparent address. |
| 44 | + */ |
| 45 | + address: string; |
| 46 | + /** |
| 47 | + * Raw receiver bytes: the 43-byte Orchard/Ironwood receiver for a shielded output, the |
| 48 | + * scriptPubKey for a transparent one. |
| 49 | + */ |
| 50 | + script: Uint8Array; |
| 51 | + /** |
| 52 | + * The original Unified Address the client supplied for this recipient, when the PSBT stores |
| 53 | + * one: the v6 (Ironwood) PCZT for a shielded output, the transparent-output proprietary |
| 54 | + * key-value map for a v4 transparent output. `undefined` when the recipient was built from a |
| 55 | + * plain address (or the single-receiver UA re-encoding is byte-identical for a shielded |
| 56 | + * output). |
| 57 | + */ |
| 58 | + unifiedAddress?: string; |
| 59 | + destination: PsbtRecipientDestination; |
| 60 | +} |
| 61 | + |
| 62 | +export type ResolvePsbtRecipientsOptions = { |
| 63 | + /** |
| 64 | + * Custom change wallet xpubs, when the transaction spends to a custom change wallet. Outputs |
| 65 | + * matching these keys are classified as change, not recipients — matching how |
| 66 | + * `explainPsbtWasm` treats them. |
| 67 | + */ |
| 68 | + customChangeXpubs?: Triple<string>; |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Resolve the recipient list of a decoded Zcash PSBT (v4 Sapling-shaped or v6 Ironwood). |
| 73 | + * |
| 74 | + * Mirrors the recipient resolution of wallet-platform's utxo-core `buildTransaction` in the |
| 75 | + * decode direction: every non-wallet, non-custom-change output with a resolvable address is a |
| 76 | + * recipient. A shielded output parses with `isShielded: true`, its `script` being the raw |
| 77 | + * 43-byte receiver; when the build stored the client's original Unified Address (the v6 PCZT for |
| 78 | + * shielded outputs, the transparent-output proprietary key-value map for v4), both the parsed |
| 79 | + * address and `unifiedAddress` report it verbatim. Opaque outputs with no address (e.g. |
| 80 | + * OP_RETURN) are skipped, as they carry no recipient. |
| 81 | + */ |
| 82 | +export function resolvePsbtRecipients( |
| 83 | + psbt: fixedScriptWallet.ZcashBitGoPsbt, |
| 84 | + walletKeys: fixedScriptWallet.RootWalletKeys, |
| 85 | + opts: ResolvePsbtRecipientsOptions = {} |
| 86 | +): PsbtRecipient[] { |
| 87 | + const parsed = psbt.parseTransactionWithWalletKeys(walletKeys, { |
| 88 | + replayProtection: { publicKeys: getReplayProtectionPubkeys('zec') }, |
| 89 | + }); |
| 90 | + const customChangeOutputs = opts.customChangeXpubs |
| 91 | + ? psbt.parseOutputsWithWalletKeys(opts.customChangeXpubs) |
| 92 | + : undefined; |
| 93 | + |
| 94 | + const recipients: PsbtRecipient[] = []; |
| 95 | + parsed.outputs.forEach((output, i) => { |
| 96 | + // Wallet-owned (change) outputs. |
| 97 | + if (output.scriptId !== null) { |
| 98 | + return; |
| 99 | + } |
| 100 | + // Outputs owned by the custom change wallet, if one was supplied. |
| 101 | + if (customChangeOutputs?.[i]?.scriptId != null) { |
| 102 | + return; |
| 103 | + } |
| 104 | + // Opaque outputs (e.g. OP_RETURN) carry no recipient address. |
| 105 | + if (output.address === null) { |
| 106 | + return; |
| 107 | + } |
| 108 | + // The original client-passed Unified Address, stored verbatim in the PSBT's key-value |
| 109 | + // pairs: the orchard PCZT for a shielded output (parsed `address` reports it in full), the |
| 110 | + // transparent-output proprietary map for a v4 transparent output. |
| 111 | + const unifiedAddress = output.isShielded ? output.address : psbt.transparentOutputUnifiedAddress(i) ?? undefined; |
| 112 | + recipients.push({ |
| 113 | + amount: output.value, |
| 114 | + address: output.address, |
| 115 | + script: output.script, |
| 116 | + unifiedAddress, |
| 117 | + destination: output.isShielded |
| 118 | + ? { kind: 'zcashShielded', unifiedAddress: output.address } |
| 119 | + : unifiedAddress |
| 120 | + ? { kind: 'zcashUnifiedTransparent', unifiedAddress } |
| 121 | + : { kind: 'transparent' }, |
| 122 | + }); |
| 123 | + }); |
| 124 | + return recipients; |
| 125 | +} |
0 commit comments