-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SilentPaymentAddress and Network to utils
These structs are not meant to be used for the default sending and receiving flow, but can still be quite useful. Therefore the utils module is probably the best place for them.
- Loading branch information
Showing
9 changed files
with
143 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use core::fmt; | ||
|
||
use crate::Error; | ||
use crate::Result; | ||
use bech32::{FromBase32, ToBase32}; | ||
use secp256k1::PublicKey; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// The network format used for this silent payment address. | ||
/// | ||
/// There are three network types: Mainnet (`sp1..`), Testnet (`tsp1..`), and Regtest (`sprt1..`). | ||
/// Signet uses the same network type as Testnet. | ||
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Deserialize, Serialize)] | ||
pub enum Network { | ||
Mainnet, | ||
Testnet, | ||
Regtest, | ||
} | ||
|
||
/// A silent payment address struct that can be used to deserialize a silent payment address string. | ||
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] | ||
pub struct SilentPaymentAddress { | ||
version: u8, | ||
scan_pubkey: PublicKey, | ||
m_pubkey: PublicKey, | ||
network: Network, | ||
} | ||
|
||
impl SilentPaymentAddress { | ||
pub fn new( | ||
scan_pubkey: PublicKey, | ||
m_pubkey: PublicKey, | ||
network: Network, | ||
version: u8, | ||
) -> Result<Self> { | ||
if version != 0 { | ||
return Err(Error::GenericError( | ||
"Can't have other version than 0 for now".to_owned(), | ||
)); | ||
} | ||
|
||
Ok(SilentPaymentAddress { | ||
scan_pubkey, | ||
m_pubkey, | ||
network, | ||
version, | ||
}) | ||
} | ||
|
||
pub fn get_scan_key(&self) -> PublicKey { | ||
self.scan_pubkey | ||
} | ||
|
||
pub fn get_spend_key(&self) -> PublicKey { | ||
self.m_pubkey | ||
} | ||
|
||
pub fn get_network(&self) -> Network { | ||
self.network | ||
} | ||
} | ||
|
||
impl fmt::Display for SilentPaymentAddress { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", <SilentPaymentAddress as Into<String>>::into(*self)) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for SilentPaymentAddress { | ||
type Error = Error; | ||
|
||
fn try_from(addr: &str) -> Result<Self> { | ||
let (hrp, data, _variant) = bech32::decode(addr)?; | ||
|
||
if data.len() != 107 { | ||
return Err(Error::GenericError("Address length is wrong".to_owned())); | ||
} | ||
|
||
let version = data[0].to_u8(); | ||
|
||
let network = match hrp.as_str() { | ||
"sp" => Network::Mainnet, | ||
"tsp" => Network::Testnet, | ||
"sprt" => Network::Regtest, | ||
_ => { | ||
return Err(Error::InvalidAddress(format!( | ||
"Wrong prefix, expected \"sp\", \"tsp\", or \"sprt\", got \"{}\"", | ||
&hrp | ||
))) | ||
} | ||
}; | ||
|
||
let data = Vec::<u8>::from_base32(&data[1..])?; | ||
|
||
let scan_pubkey = PublicKey::from_slice(&data[..33])?; | ||
let m_pubkey = PublicKey::from_slice(&data[33..])?; | ||
|
||
SilentPaymentAddress::new(scan_pubkey, m_pubkey, network, version) | ||
} | ||
} | ||
|
||
impl TryFrom<String> for SilentPaymentAddress { | ||
type Error = Error; | ||
|
||
fn try_from(addr: String) -> Result<Self> { | ||
addr.as_str().try_into() | ||
} | ||
} | ||
|
||
impl From<SilentPaymentAddress> for String { | ||
fn from(val: SilentPaymentAddress) -> Self { | ||
let hrp = match val.network { | ||
Network::Testnet => "tsp", | ||
Network::Regtest => "sprt", | ||
Network::Mainnet => "sp", | ||
}; | ||
|
||
let version = bech32::u5::try_from_u8(val.version).unwrap(); | ||
|
||
let B_scan_bytes = val.scan_pubkey.serialize(); | ||
let B_m_bytes = val.m_pubkey.serialize(); | ||
|
||
let mut data = [B_scan_bytes, B_m_bytes].concat().to_base32(); | ||
|
||
data.insert(0, version); | ||
|
||
bech32::encode(hrp, data, bech32::Variant::Bech32m).unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters