|
| 1 | +import { BitGoPsbt as WasmBitGoPsbt } from "../wasm/wasm_utxo.js"; |
| 2 | +import { type WalletKeysArg, RootWalletKeys } from "./RootWalletKeys.js"; |
| 3 | +import { BitGoPsbt, type CreateEmptyOptions } from "./BitGoPsbt.js"; |
| 4 | + |
| 5 | +/** Zcash network names */ |
| 6 | +export type ZcashNetworkName = "zcash" | "zcashTest" | "zec" | "tzec"; |
| 7 | + |
| 8 | +/** Options for creating an empty Zcash PSBT (preferred method using block height) */ |
| 9 | +export type CreateEmptyZcashOptions = CreateEmptyOptions & { |
| 10 | + /** Block height to determine consensus branch ID automatically */ |
| 11 | + blockHeight: number; |
| 12 | + /** Zcash version group ID (defaults to Sapling: 0x892F2085) */ |
| 13 | + versionGroupId?: number; |
| 14 | + /** Zcash transaction expiry height */ |
| 15 | + expiryHeight?: number; |
| 16 | +}; |
| 17 | + |
| 18 | +/** Options for creating an empty Zcash PSBT with explicit consensus branch ID (advanced use) */ |
| 19 | +export type CreateEmptyZcashWithConsensusBranchIdOptions = CreateEmptyOptions & { |
| 20 | + /** Zcash consensus branch ID (required, e.g., 0xC2D6D0B4 for NU5, 0x76B809BB for Sapling) */ |
| 21 | + consensusBranchId: number; |
| 22 | + /** Zcash version group ID (defaults to Sapling: 0x892F2085) */ |
| 23 | + versionGroupId?: number; |
| 24 | + /** Zcash transaction expiry height */ |
| 25 | + expiryHeight?: number; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Zcash-specific PSBT implementation |
| 30 | + * |
| 31 | + * This class extends BitGoPsbt with Zcash-specific functionality: |
| 32 | + * - Required consensus branch ID for sighash computation |
| 33 | + * - Version group ID for Zcash transaction format |
| 34 | + * - Expiry height for transaction validity |
| 35 | + * |
| 36 | + * All Zcash-specific getters return non-optional types since they're |
| 37 | + * guaranteed to be present for Zcash PSBTs. |
| 38 | + * |
| 39 | + * @example |
| 40 | + * ```typescript |
| 41 | + * // Create a new Zcash PSBT |
| 42 | + * const psbt = ZcashBitGoPsbt.createEmpty("zcash", walletKeys, { |
| 43 | + * consensusBranchId: 0x76B809BB, // Sapling |
| 44 | + * }); |
| 45 | + * |
| 46 | + * // Deserialize from bytes |
| 47 | + * const psbt = ZcashBitGoPsbt.fromBytes(bytes, "zcash"); |
| 48 | + * ``` |
| 49 | + */ |
| 50 | +export class ZcashBitGoPsbt extends BitGoPsbt { |
| 51 | + /** |
| 52 | + * Create an empty Zcash PSBT with consensus branch ID determined from block height |
| 53 | + * |
| 54 | + * **This is the preferred method for creating Zcash PSBTs.** It automatically determines |
| 55 | + * the correct consensus branch ID based on the network and block height using Zcash |
| 56 | + * network upgrade activation heights, eliminating the need to manually look up branch IDs. |
| 57 | + * |
| 58 | + * @param network - Zcash network name ("zcash", "zcashTest", "zec", "tzec") |
| 59 | + * @param walletKeys - The wallet's root keys (sets global xpubs in the PSBT) |
| 60 | + * @param options - Options including blockHeight to determine consensus rules |
| 61 | + * @returns A new ZcashBitGoPsbt instance |
| 62 | + * @throws Error if block height is before Overwinter activation |
| 63 | + * |
| 64 | + * @example |
| 65 | + * ```typescript |
| 66 | + * // Create PSBT for a specific block height (recommended) |
| 67 | + * const psbt = ZcashBitGoPsbt.createEmpty("zcash", walletKeys, { |
| 68 | + * blockHeight: 1687104, // Automatically uses Nu5 branch ID |
| 69 | + * }); |
| 70 | + * |
| 71 | + * // Create PSBT for current block height |
| 72 | + * const currentHeight = await getBlockHeight(); |
| 73 | + * const psbt = ZcashBitGoPsbt.createEmpty("zcash", walletKeys, { |
| 74 | + * blockHeight: currentHeight, |
| 75 | + * }); |
| 76 | + * ``` |
| 77 | + */ |
| 78 | + static override createEmpty( |
| 79 | + network: ZcashNetworkName, |
| 80 | + walletKeys: WalletKeysArg, |
| 81 | + options: CreateEmptyZcashOptions, |
| 82 | + ): ZcashBitGoPsbt { |
| 83 | + const keys = RootWalletKeys.from(walletKeys); |
| 84 | + const wasm = WasmBitGoPsbt.create_empty_zcash_at_height( |
| 85 | + network, |
| 86 | + keys.wasm, |
| 87 | + options.blockHeight, |
| 88 | + options.version, |
| 89 | + options.lockTime, |
| 90 | + options.versionGroupId, |
| 91 | + options.expiryHeight, |
| 92 | + ); |
| 93 | + return new ZcashBitGoPsbt(wasm); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Create an empty Zcash PSBT with explicit consensus branch ID |
| 98 | + * |
| 99 | + * **Advanced use only.** This method requires manually specifying the consensus branch ID. |
| 100 | + * In most cases, you should use `createEmpty()` instead, which automatically determines |
| 101 | + * the correct branch ID from the block height. |
| 102 | + * |
| 103 | + * @param network - Zcash network name ("zcash", "zcashTest", "zec", "tzec") |
| 104 | + * @param walletKeys - The wallet's root keys (sets global xpubs in the PSBT) |
| 105 | + * @param options - Zcash-specific options including required consensusBranchId |
| 106 | + * @returns A new ZcashBitGoPsbt instance |
| 107 | + * |
| 108 | + * @example |
| 109 | + * ```typescript |
| 110 | + * // Only use this if you need explicit control over the branch ID |
| 111 | + * const psbt = ZcashBitGoPsbt.createEmptyWithConsensusBranchId("zcash", walletKeys, { |
| 112 | + * consensusBranchId: 0xC2D6D0B4, // Nu5 branch ID |
| 113 | + * }); |
| 114 | + * ``` |
| 115 | + */ |
| 116 | + static createEmptyWithConsensusBranchId( |
| 117 | + network: ZcashNetworkName, |
| 118 | + walletKeys: WalletKeysArg, |
| 119 | + options: CreateEmptyZcashWithConsensusBranchIdOptions, |
| 120 | + ): ZcashBitGoPsbt { |
| 121 | + const keys = RootWalletKeys.from(walletKeys); |
| 122 | + const wasm = WasmBitGoPsbt.create_empty_zcash( |
| 123 | + network, |
| 124 | + keys.wasm, |
| 125 | + options.consensusBranchId, |
| 126 | + options.version, |
| 127 | + options.lockTime, |
| 128 | + options.versionGroupId, |
| 129 | + options.expiryHeight, |
| 130 | + ); |
| 131 | + return new ZcashBitGoPsbt(wasm); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Deserialize a Zcash PSBT from bytes |
| 136 | + * |
| 137 | + * @param bytes - The PSBT bytes |
| 138 | + * @param network - Zcash network name ("zcash", "zcashTest", "zec", "tzec") |
| 139 | + * @returns A ZcashBitGoPsbt instance |
| 140 | + */ |
| 141 | + static override fromBytes(bytes: Uint8Array, network: ZcashNetworkName): ZcashBitGoPsbt { |
| 142 | + const wasm = WasmBitGoPsbt.from_bytes(bytes, network); |
| 143 | + return new ZcashBitGoPsbt(wasm); |
| 144 | + } |
| 145 | + |
| 146 | + // --- Zcash-specific getters --- |
| 147 | + |
| 148 | + /** |
| 149 | + * Get the Zcash version group ID |
| 150 | + * @returns The version group ID (e.g., 0x892F2085 for Sapling) |
| 151 | + */ |
| 152 | + get versionGroupId(): number { |
| 153 | + return this.wasm.version_group_id(); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get the Zcash expiry height |
| 158 | + * @returns The expiry height (0 if not set) |
| 159 | + */ |
| 160 | + get expiryHeight(): number { |
| 161 | + return this.wasm.expiry_height(); |
| 162 | + } |
| 163 | +} |
0 commit comments