Skip to content

Commit

Permalink
Use Network enum in receiving
Browse files Browse the repository at this point in the history
Make the Network enum used in sending public, and replace `is_testnet`
for the Receiver struct with this Network struct.
Also move the Network enum to `common.rs`
  • Loading branch information
cygnet3 committed May 8, 2024
1 parent d715914 commit a9cc996
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
8 changes: 8 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretKey> {
let hash = SharedSecretHash::from_ecdh_and_k(ecdh_shared_secret, k).to_byte_array();
Expand All @@ -17,3 +18,10 @@ pub(crate) fn calculate_P_n(B_spend: &PublicKey, t_n: Scalar) -> Result<PublicKe

Ok(P_n)
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub enum Network {
Mainnet,
Testnet,
Regtest,
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ pub use secp256k1;
pub use crate::error::Error;

pub type Result<T> = std::result::Result<T, Error>;
pub type Network = common::Network;
23 changes: 12 additions & 11 deletions src/receiving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Label, PublicKey>,
pub is_testnet: bool,
pub network: Network,
}

struct SerializablePubkey([u8; 33]);
Expand Down Expand Up @@ -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()),
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -255,7 +255,7 @@ impl Receiver {
scan_pubkey: PublicKey,
spend_pubkey: PublicKey,
change_label: Label,
is_testnet: bool,
network: Network,
) -> Result<Self> {
let labels: BiMap<Label, PublicKey> = BiMap::new();

Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 1 addition & 8 deletions src/sending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a9cc996

Please sign in to comment.