From 50f13d3e2e2802ebbc6efceccb03eafe352b1e77 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Mon, 13 Feb 2023 17:55:45 +0100 Subject: [PATCH] signer: allow to the set the network for extended keys encoding --- src/signer.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/signer.rs b/src/signer.rs index 7a027b36..6d415392 100644 --- a/src/signer.rs +++ b/src/signer.rs @@ -287,6 +287,13 @@ impl HotSigner { Ok(psbt) } + + /// Change the network of generated extended keys. Note this value only has to do with the + /// BIP32 encoding of those keys (xpubs, tpubs, ..) but does not affect any data (whether it is + /// the keys or the mnemonics). + pub fn set_network(&mut self, network: bitcoin::Network) { + self.master_xpriv.network = network; + } } #[cfg(test)] @@ -569,4 +576,30 @@ mod tests { assert!(psbt.inputs[0].partial_sigs.is_empty()); assert_eq!(psbt.inputs[1].partial_sigs.len(), 2); } + + #[test] + fn signer_set_net() { + let secp = secp256k1::Secp256k1::signing_only(); + let mut signer = HotSigner::from_str( + bitcoin::Network::Bitcoin, + "burger ball theme dog light account produce chest warrior swarm flip equip", + ) + .unwrap(); + assert_eq!(signer.xpub_at(&bip32::DerivationPath::master(), &secp).to_string(), "xpub661MyMwAqRbcGKvR8dChsA92AHfJS6fJMR41jAASu5S79v65dac244iBd7PwqnfMQ9jWsmg8SqnNz3MjkwYF8Edzr2ttxt171Cr5RyJrvF2"); + + let tpub = "tpubD6NzVbkrYhZ4Y87GapBo55UPVQkxRVAMu3eK5iDbEzBzuCknhoT7CWP1s9UjNHcbC4GRVMBzywcRgDrM9oPV1g6HudeCeQfLbASVBxpNJV3"; + for net in &[ + bitcoin::Network::Testnet, + bitcoin::Network::Signet, + bitcoin::Network::Regtest, + ] { + signer.set_network(*net); + assert_eq!( + signer + .xpub_at(&bip32::DerivationPath::master(), &secp) + .to_string(), + tpub + ); + } + } }