Skip to content

Commit 1d5845f

Browse files
Merge pull request #296 from BitGo/otto/T1-3519-zec-branch-id-apis
feat(wasm-utxo): expose ZEC branch ID APIs on ZcashBitGoPsbt
2 parents 8fd682d + e9371ee commit 1d5845f

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

packages/wasm-utxo/js/fixedScriptWallet/ZcashBitGoPsbt.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BitGoPsbt as WasmBitGoPsbt } from "../wasm/wasm_utxo.js";
1+
import { BitGoPsbt as WasmBitGoPsbt, zcash_branch_id_for_height } from "../wasm/wasm_utxo.js";
22
import { type WalletKeysArg, RootWalletKeys } from "./RootWalletKeys.js";
33
import { BitGoPsbt, type CreateEmptyOptions, type HydrationUnspent } from "./BitGoPsbt.js";
44
import { ZcashTransaction, type ITransaction } from "../transaction.js";
@@ -264,6 +264,22 @@ export class ZcashBitGoPsbt extends BitGoPsbt {
264264
return this.wasm.expiry_height();
265265
}
266266

267+
/**
268+
* Get the Zcash consensus branch ID stored in the PSBT proprietary map.
269+
* Returns undefined for v5 PSBTs or PSBTs without the key.
270+
*/
271+
get consensusBranchId(): number | undefined {
272+
return this.wasm.consensus_branch_id();
273+
}
274+
275+
/**
276+
* Return the Zcash consensus branch ID active at `height` on `network`.
277+
* Returns undefined if `height` is before Overwinter activation.
278+
*/
279+
static branchIdForHeight(network: ZcashNetworkName, height: number): number | undefined {
280+
return zcash_branch_id_for_height(network, height);
281+
}
282+
267283
/**
268284
* Extract the final Zcash transaction from a finalized PSBT
269285
*

packages/wasm-utxo/src/wasm/fixed_script_wallet/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,17 @@ impl BitGoPsbt {
961961
}
962962
}
963963

964+
/// Get the Zcash consensus branch ID from the PSBT proprietary map (returns None for non-Zcash PSBTs)
965+
pub fn consensus_branch_id(&self) -> Option<u32> {
966+
use crate::fixed_script_wallet::bitgo_psbt::{
967+
propkv::get_zec_consensus_branch_id, BitGoPsbt as InnerBitGoPsbt,
968+
};
969+
match &self.psbt {
970+
InnerBitGoPsbt::Zcash(z, _) => get_zec_consensus_branch_id(&z.psbt),
971+
_ => None,
972+
}
973+
}
974+
964975
pub fn get_outputs_with_address(&self) -> Result<JsValue, WasmUtxoError> {
965976
crate::wasm::psbt::get_outputs_with_address_from_psbt(self.psbt.psbt(), self.psbt.network())
966977
}
@@ -1943,3 +1954,23 @@ impl BitGoPsbt {
19431954
}
19441955

19451956
impl_wasm_psbt_ops!(BitGoPsbt, psbt);
1957+
1958+
/// Return the Zcash consensus branch ID active at `height` on `network`.
1959+
///
1960+
/// `network`: "zcash" / "zec" for mainnet, "zcashTest" / "tzec" for testnet.
1961+
/// Returns `None` if `height` is before Overwinter activation.
1962+
/// Throws if `network` is not a recognised Zcash network name.
1963+
#[wasm_bindgen]
1964+
pub fn zcash_branch_id_for_height(network: &str, height: u32) -> Result<Option<u32>, JsValue> {
1965+
let is_mainnet = match network {
1966+
"zcash" | "zec" => true,
1967+
"zcashTest" | "tzec" => false,
1968+
_ => {
1969+
return Err(JsValue::from_str(&format!(
1970+
"unknown Zcash network {:?}: expected \"zcash\", \"zec\", \"zcashTest\", or \"tzec\"",
1971+
network
1972+
)))
1973+
}
1974+
};
1975+
Ok(crate::zcash::branch_id_for_height(height, is_mainnet))
1976+
}

0 commit comments

Comments
 (0)