Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Network enum in receiving #90

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/create_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
let secp = Secp256k1::new();
Expand All @@ -28,7 +29,7 @@ fn main() -> Result<(), Box<dyn Error>> {
scan_privkey.public_key(&secp),
spend_privkey.public_key(&secp),
change_label,
true,
Network::Testnet,
)?;

println!("Receiving address: {}", receiver.get_receiving_address());
Expand Down
2 changes: 1 addition & 1 deletion examples/find_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() -> Result<(), Box<dyn Error>> {
scan_privkey.public_key(&secp),
spend_privkey.public_key(&secp),
change_label,
true,
silentpayments::Network::Testnet,
)?;

let outpoints: Vec<(String, u32)> = tx
Expand Down
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
6 changes: 3 additions & 3 deletions tests/vector_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod tests {
},
sending::calculate_partial_secret,
},
Network,
};
use std::{collections::HashSet, io::Cursor, str::FromStr};

Expand All @@ -26,7 +27,7 @@ mod tests {
},
};

const IS_TESTNET: bool = false;
const NETWORK: Network = Network::Mainnet;

#[test]
fn test_with_test_vectors() {
Expand Down Expand Up @@ -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);

Expand Down
Loading