diff --git a/examples/create_wallet.rs b/examples/create_wallet.rs index a82a756..a049b9d 100644 --- a/examples/create_wallet.rs +++ b/examples/create_wallet.rs @@ -5,6 +5,7 @@ use bitcoin::bip32::{DerivationPath, Xpriv}; use bitcoin::secp256k1::Secp256k1; use silentpayments::receiving::{Label, Receiver}; +use silentpayments::Network; fn main() -> Result<(), Box> { let secp = Secp256k1::new(); @@ -28,7 +29,7 @@ fn main() -> Result<(), Box> { scan_privkey.public_key(&secp), spend_privkey.public_key(&secp), change_label, - true, + Network::Testnet, )?; println!("Receiving address: {}", receiver.get_receiving_address()); diff --git a/examples/find_output.rs b/examples/find_output.rs index fbbadd0..18f27ee 100644 --- a/examples/find_output.rs +++ b/examples/find_output.rs @@ -43,7 +43,7 @@ fn main() -> Result<(), Box> { scan_privkey.public_key(&secp), spend_privkey.public_key(&secp), change_label, - true, + silentpayments::Network::Testnet, )?; let outpoints: Vec<(String, u32)> = tx diff --git a/src/common.rs b/src/common.rs index 0a8882e..6fc565e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2,6 +2,7 @@ use crate::utils::hash::SharedSecretHash; use crate::Result; use bitcoin_hashes::Hash; use secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey}; +use serde::{Deserialize, Serialize}; pub(crate) fn calculate_t_n(ecdh_shared_secret: &PublicKey, k: u32) -> Result { let hash = SharedSecretHash::from_ecdh_and_k(ecdh_shared_secret, k).to_byte_array(); @@ -17,3 +18,10 @@ pub(crate) fn calculate_P_n(B_spend: &PublicKey, t_n: Scalar) -> Result = std::result::Result; +pub type Network = common::Network; diff --git a/src/receiving.rs b/src/receiving.rs index bdd670c..e4046f8 100644 --- a/src/receiving.rs +++ b/src/receiving.rs @@ -18,7 +18,7 @@ use std::{ use crate::{ common::{calculate_P_n, calculate_t_n}, utils::hash::LabelHash, - Error, Result, + Error, Network, Result, }; use bech32::ToBase32; use bimap::BiMap; @@ -113,7 +113,7 @@ pub struct Receiver { spend_pubkey: PublicKey, change_label: Label, // To be able to tell which label is the change labels: BiMap, - pub is_testnet: bool, + pub network: Network, } struct SerializablePubkey([u8; 33]); @@ -207,7 +207,7 @@ impl Serialize for Receiver { { let mut state = serializer.serialize_struct("Receiver", 5)?; state.serialize_field("version", &self.version)?; - state.serialize_field("is_testnet", &self.is_testnet)?; + state.serialize_field("network", &self.network)?; state.serialize_field( "scan_pubkey", &SerializablePubkey(self.scan_pubkey.serialize()), @@ -225,7 +225,7 @@ impl Serialize for Receiver { #[derive(Deserialize)] struct ReceiverHelper { version: u8, - is_testnet: bool, + network: Network, scan_pubkey: SerializablePubkey, spend_pubkey: SerializablePubkey, change_label: String, @@ -240,7 +240,7 @@ impl<'de> Deserialize<'de> for Receiver { let helper = ReceiverHelper::deserialize(deserializer)?; Ok(Receiver { version: helper.version, - is_testnet: helper.is_testnet, + network: helper.network, scan_pubkey: PublicKey::from_slice(&helper.scan_pubkey.0).unwrap(), spend_pubkey: PublicKey::from_slice(&helper.spend_pubkey.0).unwrap(), change_label: Label::try_from(helper.change_label).unwrap(), @@ -255,7 +255,7 @@ impl Receiver { scan_pubkey: PublicKey, spend_pubkey: PublicKey, change_label: Label, - is_testnet: bool, + network: Network, ) -> Result { let labels: BiMap = BiMap::new(); @@ -272,10 +272,10 @@ impl Receiver { spend_pubkey, change_label: change_label.clone(), labels, - is_testnet, + network, }; - // This check that the change_label produces a valid key at each step + // This checks that the change_label produces a valid key at each step receiver.add_label(change_label)?; Ok(receiver) @@ -462,9 +462,10 @@ impl Receiver { } fn encode_silent_payment_address(&self, m_pubkey: PublicKey) -> String { - let hrp = match self.is_testnet { - false => "sp", - true => "tsp", + let hrp = match self.network { + Network::Mainnet => "sp", + Network::Testnet => "tsp", + Network::Regtest => "sprt", }; let version = bech32::u5::try_from_u8(self.version).unwrap(); diff --git a/src/sending.rs b/src/sending.rs index a38e50e..92e99f2 100644 --- a/src/sending.rs +++ b/src/sending.rs @@ -14,14 +14,7 @@ use core::fmt; use secp256k1::{PublicKey, Secp256k1, SecretKey, XOnlyPublicKey}; use std::collections::HashMap; -use crate::{common::calculate_t_n, error::Error, Result}; - -#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] -pub enum Network { - Mainnet, - Testnet, - Regtest, -} +use crate::{common::calculate_t_n, error::Error, Network, Result}; #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub struct SilentPaymentAddress { diff --git a/tests/vector_tests.rs b/tests/vector_tests.rs index 81ef481..12222d1 100644 --- a/tests/vector_tests.rs +++ b/tests/vector_tests.rs @@ -11,6 +11,7 @@ mod tests { }, sending::calculate_partial_secret, }, + Network, }; use std::{collections::HashSet, io::Cursor, str::FromStr}; @@ -26,7 +27,7 @@ mod tests { }, }; - const IS_TESTNET: bool = false; + const NETWORK: Network = Network::Mainnet; #[test] fn test_with_test_vectors() { @@ -103,8 +104,7 @@ mod tests { let B_scan = b_scan.public_key(&secp); let change_label = Label::new(b_scan, 0); - let mut sp_receiver = - Receiver::new(0, B_scan, B_spend, change_label, IS_TESTNET).unwrap(); + let mut sp_receiver = Receiver::new(0, B_scan, B_spend, change_label, NETWORK).unwrap(); let outputs_to_check = decode_outputs_to_check(&given.outputs);