Skip to content
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
8 changes: 4 additions & 4 deletions codec/src/map_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use acropolis_common::{
};
use std::collections::{HashMap, HashSet};

/// Map Pallas Network to our AddressNetwork
pub fn map_network(network: addresses::Network) -> Result<AddressNetwork> {
/// Map Pallas Network to our NetworkId
pub fn map_network(network: addresses::Network) -> Result<NetworkId> {
match network {
addresses::Network::Mainnet => Ok(AddressNetwork::Main),
addresses::Network::Testnet => Ok(AddressNetwork::Test),
addresses::Network::Mainnet => Ok(NetworkId::Mainnet),
addresses::Network::Testnet => Ok(NetworkId::Testnet),
_ => Err(anyhow!("Unknown network in address")),
}
}
Expand Down
113 changes: 38 additions & 75 deletions common/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,43 +83,6 @@ impl ByronAddress {
}
}

/// Address network identifier
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Hash,
serde::Serialize,
serde::Deserialize,
minicbor::Encode,
minicbor::Decode,
)]
pub enum AddressNetwork {
/// Mainnet
#[n(0)]
Main,

/// Testnet
#[n(1)]
Test,
}

impl From<NetworkId> for AddressNetwork {
fn from(network: NetworkId) -> Self {
match network {
NetworkId::Mainnet => Self::Main,
NetworkId::Testnet => Self::Test,
}
}
}

impl Default for AddressNetwork {
fn default() -> Self {
Self::Main
}
}

/// A Shelley-era address - payment part
#[derive(
Debug,
Expand Down Expand Up @@ -227,7 +190,7 @@ impl Default for ShelleyAddressDelegationPart {
pub struct ShelleyAddress {
/// Network id
#[n(0)]
pub network: AddressNetwork,
pub network: NetworkId,

/// Payment part
#[n(1)]
Expand All @@ -244,8 +207,8 @@ impl ShelleyAddress {
let (hrp, data) = bech32::decode(text)?;
if let Some(header) = data.first() {
let network = match hrp.as_str().contains("test") {
true => AddressNetwork::Test,
false => AddressNetwork::Main,
true => NetworkId::Testnet,
false => NetworkId::Mainnet,
};

let header = *header;
Expand Down Expand Up @@ -288,8 +251,8 @@ impl ShelleyAddress {
/// Convert to addr1xxx form
pub fn to_string(&self) -> Result<String> {
let (hrp, network_bits) = match self.network {
AddressNetwork::Main => (bech32::Hrp::parse("addr")?, 1u8),
AddressNetwork::Test => (bech32::Hrp::parse("addr_test")?, 0u8),
NetworkId::Mainnet => (bech32::Hrp::parse("addr")?, 1u8),
NetworkId::Testnet => (bech32::Hrp::parse("addr_test")?, 0u8),
};

let (payment_hash, payment_bits): (&Vec<u8>, u8) = match &self.payment {
Expand Down Expand Up @@ -318,8 +281,8 @@ impl ShelleyAddress {

pub fn to_bytes_key(&self) -> Result<Vec<u8>> {
let network_bits = match self.network {
AddressNetwork::Main => 1u8,
AddressNetwork::Test => 0u8,
NetworkId::Mainnet => 1u8,
NetworkId::Testnet => 0u8,
};

let (payment_hash, payment_bits): (&Vec<u8>, u8) = match &self.payment {
Expand Down Expand Up @@ -368,8 +331,8 @@ impl ShelleyAddress {

pub fn stake_address_string(&self) -> Result<Option<String>> {
let network_bit = match self.network {
AddressNetwork::Main => 1,
AddressNetwork::Test => 0,
NetworkId::Mainnet => 1,
NetworkId::Testnet => 0,
};

match &self.delegation {
Expand Down Expand Up @@ -421,14 +384,14 @@ impl StakeAddressPayload {
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
pub struct StakeAddress {
/// Network id
pub network: AddressNetwork,
pub network: NetworkId,

/// Payload
pub payload: StakeAddressPayload,
}

impl StakeAddress {
pub fn new(payload: StakeAddressPayload, network: AddressNetwork) -> Self {
pub fn new(payload: StakeAddressPayload, network: NetworkId) -> Self {
StakeAddress { network, payload }
}

Expand All @@ -449,8 +412,8 @@ impl StakeAddress {
/// Convert to string stake1xxx format
pub fn to_string(&self) -> Result<String> {
let hrp = match self.network {
AddressNetwork::Main => bech32::Hrp::parse("stake")?,
AddressNetwork::Test => bech32::Hrp::parse("stake_test")?,
NetworkId::Mainnet => bech32::Hrp::parse("stake")?,
NetworkId::Testnet => bech32::Hrp::parse("stake_test")?,
};

let data = self.to_binary();
Expand All @@ -462,8 +425,8 @@ impl StakeAddress {
let (hrp, data) = bech32::decode(text)?;
if let Some(header) = data.first() {
let network = match hrp.as_str().contains("test") {
true => AddressNetwork::Test,
false => AddressNetwork::Main,
true => NetworkId::Testnet,
false => NetworkId::Mainnet,
};

let payload = match (header >> 4) & 0x0Fu8 {
Expand All @@ -481,8 +444,8 @@ impl StakeAddress {
/// Convert to binary format (29 bytes)
pub fn to_binary(&self) -> Vec<u8> {
let network_bits = match self.network {
AddressNetwork::Main => 0b1u8,
AddressNetwork::Test => 0b0u8,
NetworkId::Mainnet => 0b1u8,
NetworkId::Testnet => 0b0u8,
};

let (stake_bits, stake_hash): (u8, &Vec<u8>) = match &self.payload {
Expand All @@ -502,8 +465,8 @@ impl StakeAddress {
}

let network = match data[0] & 0x01 {
0b1 => AddressNetwork::Main,
_ => AddressNetwork::Test,
0b1 => NetworkId::Mainnet,
_ => NetworkId::Testnet,
};

let payload = match (data[0] >> 4) & 0x0F {
Expand All @@ -523,8 +486,8 @@ impl StakeAddress {
};

let net_bit = match self.network {
AddressNetwork::Main => 1,
AddressNetwork::Test => 0,
NetworkId::Mainnet => 1,
NetworkId::Testnet => 0,
};

let header = net_bit | (bits << 4);
Expand Down Expand Up @@ -565,7 +528,7 @@ impl<'b, C> minicbor::Decode<'b, C> for StakeAddress {
impl Default for StakeAddress {
fn default() -> Self {
StakeAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payload: StakeAddressPayload::StakeKeyHash(vec![0u8; 28]),
}
}
Expand Down Expand Up @@ -732,7 +695,7 @@ mod tests {
#[test]
fn shelley_type_0() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
delegation: ShelleyAddressDelegationPart::StakeKeyHash(test_stake_key_hash()),
});
Expand All @@ -747,7 +710,7 @@ mod tests {
#[test]
fn shelley_type_1() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
delegation: ShelleyAddressDelegationPart::StakeKeyHash(test_stake_key_hash()),
});
Expand All @@ -762,7 +725,7 @@ mod tests {
#[test]
fn shelley_type_2() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
delegation: ShelleyAddressDelegationPart::ScriptHash(test_script_hash()),
});
Expand All @@ -777,7 +740,7 @@ mod tests {
#[test]
fn shelley_type_3() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
delegation: ShelleyAddressDelegationPart::ScriptHash(test_script_hash()),
});
Expand All @@ -792,7 +755,7 @@ mod tests {
#[test]
fn shelley_type_4() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
delegation: ShelleyAddressDelegationPart::Pointer(test_pointer()),
});
Expand All @@ -810,7 +773,7 @@ mod tests {
#[test]
fn shelley_type_5() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
delegation: ShelleyAddressDelegationPart::Pointer(test_pointer()),
});
Expand All @@ -828,7 +791,7 @@ mod tests {
#[test]
fn shelley_type_6() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
delegation: ShelleyAddressDelegationPart::None,
});
Expand All @@ -846,7 +809,7 @@ mod tests {
#[test]
fn shelley_type_7() {
let address = Address::Shelley(ShelleyAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
delegation: ShelleyAddressDelegationPart::None,
});
Expand All @@ -864,7 +827,7 @@ mod tests {
#[test]
fn shelley_type_14() {
let address = Address::Stake(StakeAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payload: StakeAddressPayload::StakeKeyHash(test_stake_key_hash()),
});

Expand All @@ -881,7 +844,7 @@ mod tests {
#[test]
fn shelley_type_15() {
let address = Address::Stake(StakeAddress {
network: AddressNetwork::Main,
network: NetworkId::Mainnet,
payload: StakeAddressPayload::ScriptHash(test_script_hash()),
});

Expand Down Expand Up @@ -925,7 +888,7 @@ mod tests {
let binary =
hex::decode("e1558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
let sa = StakeAddress::from_binary(&binary).unwrap();
assert_eq!(sa.network, AddressNetwork::Main);
assert_eq!(sa.network, NetworkId::Mainnet);
assert_eq!(
match sa.payload {
StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
Expand All @@ -941,7 +904,7 @@ mod tests {
let binary =
hex::decode("f1558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
let sa = StakeAddress::from_binary(&binary).unwrap();
assert_eq!(sa.network, AddressNetwork::Main);
assert_eq!(sa.network, NetworkId::Mainnet);
assert_eq!(
match sa.payload {
StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
Expand All @@ -957,7 +920,7 @@ mod tests {
let binary =
hex::decode("e0558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
let sa = StakeAddress::from_binary(&binary).unwrap();
assert_eq!(sa.network, AddressNetwork::Test);
assert_eq!(sa.network, NetworkId::Testnet);
assert_eq!(
match sa.payload {
StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
Expand Down Expand Up @@ -1010,7 +973,7 @@ mod tests {
let mut decoder = minicbor::Decoder::new(&binary);
let decoded = StakeAddress::decode(&mut decoder, &mut ()).unwrap();

assert_eq!(decoded.network, AddressNetwork::Main);
assert_eq!(decoded.network, NetworkId::Mainnet);
assert_eq!(
match decoded.payload {
StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
Expand All @@ -1033,7 +996,7 @@ mod tests {
let mut decoder = minicbor::Decoder::new(&encoded);
let decoded = StakeAddress::decode(&mut decoder, &mut ()).unwrap();

assert_eq!(decoded.network, AddressNetwork::Main);
assert_eq!(decoded.network, NetworkId::Mainnet);
assert_eq!(
match decoded.payload {
StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
Expand All @@ -1054,7 +1017,7 @@ mod tests {
let mut decoder = minicbor::Decoder::new(&encoded);
let decoded = StakeAddress::decode(&mut decoder, &mut ()).unwrap();

assert_eq!(decoded.network, AddressNetwork::Test);
assert_eq!(decoded.network, NetworkId::Testnet);
assert_eq!(
match decoded.payload {
StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
Expand Down
4 changes: 2 additions & 2 deletions common/src/stake_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl StakeAddressMap {

#[cfg(test)]
mod tests {
use crate::{AddressNetwork, StakeAddress, StakeAddressPayload};
use crate::{NetworkId, StakeAddress, StakeAddressPayload};

use super::*;

Expand All @@ -561,7 +561,7 @@ mod tests {
StakeAddressPayload::StakeKeyHash(
hash.to_vec().try_into().expect("Invalid hash length"),
),
AddressNetwork::Main,
NetworkId::Mainnet,
)
}

Expand Down
21 changes: 19 additions & 2 deletions common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@ use std::fmt::{Display, Formatter};
use std::ops::{AddAssign, Neg};
use std::{cmp::Ordering, fmt};

#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// Network identifier
#[derive(
Debug,
Clone,
Default,
PartialEq,
Eq,
Hash,
serde::Serialize,
serde::Deserialize,
minicbor::Encode,
minicbor::Decode,
)]
pub enum NetworkId {
Testnet,
/// Main
#[n(0)]
#[default]
Mainnet,

/// Test
#[n(1)]
Testnet,
}

impl From<String> for NetworkId {
Expand Down
Loading