Skip to content

Commit 0659d3c

Browse files
authored
Merge pull request #277 from input-output-hk/lowhung/275-remove-address-network
Refactor `AddressNetwork` -> `NetworkId`
2 parents bb256b1 + a08bbdb commit 0659d3c

File tree

10 files changed

+109
-123
lines changed

10 files changed

+109
-123
lines changed

codec/src/map_parameters.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ use acropolis_common::{
1919
};
2020
use std::collections::{HashMap, HashSet};
2121

22-
/// Map Pallas Network to our AddressNetwork
23-
pub fn map_network(network: addresses::Network) -> Result<AddressNetwork> {
22+
/// Map Pallas Network to our NetworkId
23+
pub fn map_network(network: addresses::Network) -> Result<NetworkId> {
2424
match network {
25-
addresses::Network::Mainnet => Ok(AddressNetwork::Main),
26-
addresses::Network::Testnet => Ok(AddressNetwork::Test),
25+
addresses::Network::Mainnet => Ok(NetworkId::Mainnet),
26+
addresses::Network::Testnet => Ok(NetworkId::Testnet),
2727
_ => Err(anyhow!("Unknown network in address")),
2828
}
2929
}

common/src/address.rs

Lines changed: 38 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -83,43 +83,6 @@ impl ByronAddress {
8383
}
8484
}
8585

86-
/// Address network identifier
87-
#[derive(
88-
Debug,
89-
Clone,
90-
PartialEq,
91-
Eq,
92-
Hash,
93-
serde::Serialize,
94-
serde::Deserialize,
95-
minicbor::Encode,
96-
minicbor::Decode,
97-
)]
98-
pub enum AddressNetwork {
99-
/// Mainnet
100-
#[n(0)]
101-
Main,
102-
103-
/// Testnet
104-
#[n(1)]
105-
Test,
106-
}
107-
108-
impl From<NetworkId> for AddressNetwork {
109-
fn from(network: NetworkId) -> Self {
110-
match network {
111-
NetworkId::Mainnet => Self::Main,
112-
NetworkId::Testnet => Self::Test,
113-
}
114-
}
115-
}
116-
117-
impl Default for AddressNetwork {
118-
fn default() -> Self {
119-
Self::Main
120-
}
121-
}
122-
12386
/// A Shelley-era address - payment part
12487
#[derive(
12588
Debug,
@@ -227,7 +190,7 @@ impl Default for ShelleyAddressDelegationPart {
227190
pub struct ShelleyAddress {
228191
/// Network id
229192
#[n(0)]
230-
pub network: AddressNetwork,
193+
pub network: NetworkId,
231194

232195
/// Payment part
233196
#[n(1)]
@@ -244,8 +207,8 @@ impl ShelleyAddress {
244207
let (hrp, data) = bech32::decode(text)?;
245208
if let Some(header) = data.first() {
246209
let network = match hrp.as_str().contains("test") {
247-
true => AddressNetwork::Test,
248-
false => AddressNetwork::Main,
210+
true => NetworkId::Testnet,
211+
false => NetworkId::Mainnet,
249212
};
250213

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

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

319282
pub fn to_bytes_key(&self) -> Result<Vec<u8>> {
320283
let network_bits = match self.network {
321-
AddressNetwork::Main => 1u8,
322-
AddressNetwork::Test => 0u8,
284+
NetworkId::Mainnet => 1u8,
285+
NetworkId::Testnet => 0u8,
323286
};
324287

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

369332
pub fn stake_address_string(&self) -> Result<Option<String>> {
370333
let network_bit = match self.network {
371-
AddressNetwork::Main => 1,
372-
AddressNetwork::Test => 0,
334+
NetworkId::Mainnet => 1,
335+
NetworkId::Testnet => 0,
373336
};
374337

375338
match &self.delegation {
@@ -421,14 +384,14 @@ impl StakeAddressPayload {
421384
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
422385
pub struct StakeAddress {
423386
/// Network id
424-
pub network: AddressNetwork,
387+
pub network: NetworkId,
425388

426389
/// Payload
427390
pub payload: StakeAddressPayload,
428391
}
429392

430393
impl StakeAddress {
431-
pub fn new(payload: StakeAddressPayload, network: AddressNetwork) -> Self {
394+
pub fn new(payload: StakeAddressPayload, network: NetworkId) -> Self {
432395
StakeAddress { network, payload }
433396
}
434397

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

456419
let data = self.to_binary();
@@ -462,8 +425,8 @@ impl StakeAddress {
462425
let (hrp, data) = bech32::decode(text)?;
463426
if let Some(header) = data.first() {
464427
let network = match hrp.as_str().contains("test") {
465-
true => AddressNetwork::Test,
466-
false => AddressNetwork::Main,
428+
true => NetworkId::Testnet,
429+
false => NetworkId::Mainnet,
467430
};
468431

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

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

504467
let network = match data[0] & 0x01 {
505-
0b1 => AddressNetwork::Main,
506-
_ => AddressNetwork::Test,
468+
0b1 => NetworkId::Mainnet,
469+
_ => NetworkId::Testnet,
507470
};
508471

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

525488
let net_bit = match self.network {
526-
AddressNetwork::Main => 1,
527-
AddressNetwork::Test => 0,
489+
NetworkId::Mainnet => 1,
490+
NetworkId::Testnet => 0,
528491
};
529492

530493
let header = net_bit | (bits << 4);
@@ -565,7 +528,7 @@ impl<'b, C> minicbor::Decode<'b, C> for StakeAddress {
565528
impl Default for StakeAddress {
566529
fn default() -> Self {
567530
StakeAddress {
568-
network: AddressNetwork::Main,
531+
network: NetworkId::Mainnet,
569532
payload: StakeAddressPayload::StakeKeyHash(vec![0u8; 28]),
570533
}
571534
}
@@ -732,7 +695,7 @@ mod tests {
732695
#[test]
733696
fn shelley_type_0() {
734697
let address = Address::Shelley(ShelleyAddress {
735-
network: AddressNetwork::Main,
698+
network: NetworkId::Mainnet,
736699
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
737700
delegation: ShelleyAddressDelegationPart::StakeKeyHash(test_stake_key_hash()),
738701
});
@@ -747,7 +710,7 @@ mod tests {
747710
#[test]
748711
fn shelley_type_1() {
749712
let address = Address::Shelley(ShelleyAddress {
750-
network: AddressNetwork::Main,
713+
network: NetworkId::Mainnet,
751714
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
752715
delegation: ShelleyAddressDelegationPart::StakeKeyHash(test_stake_key_hash()),
753716
});
@@ -762,7 +725,7 @@ mod tests {
762725
#[test]
763726
fn shelley_type_2() {
764727
let address = Address::Shelley(ShelleyAddress {
765-
network: AddressNetwork::Main,
728+
network: NetworkId::Mainnet,
766729
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
767730
delegation: ShelleyAddressDelegationPart::ScriptHash(test_script_hash()),
768731
});
@@ -777,7 +740,7 @@ mod tests {
777740
#[test]
778741
fn shelley_type_3() {
779742
let address = Address::Shelley(ShelleyAddress {
780-
network: AddressNetwork::Main,
743+
network: NetworkId::Mainnet,
781744
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
782745
delegation: ShelleyAddressDelegationPart::ScriptHash(test_script_hash()),
783746
});
@@ -792,7 +755,7 @@ mod tests {
792755
#[test]
793756
fn shelley_type_4() {
794757
let address = Address::Shelley(ShelleyAddress {
795-
network: AddressNetwork::Main,
758+
network: NetworkId::Mainnet,
796759
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
797760
delegation: ShelleyAddressDelegationPart::Pointer(test_pointer()),
798761
});
@@ -810,7 +773,7 @@ mod tests {
810773
#[test]
811774
fn shelley_type_5() {
812775
let address = Address::Shelley(ShelleyAddress {
813-
network: AddressNetwork::Main,
776+
network: NetworkId::Mainnet,
814777
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
815778
delegation: ShelleyAddressDelegationPart::Pointer(test_pointer()),
816779
});
@@ -828,7 +791,7 @@ mod tests {
828791
#[test]
829792
fn shelley_type_6() {
830793
let address = Address::Shelley(ShelleyAddress {
831-
network: AddressNetwork::Main,
794+
network: NetworkId::Mainnet,
832795
payment: ShelleyAddressPaymentPart::PaymentKeyHash(test_payment_key_hash()),
833796
delegation: ShelleyAddressDelegationPart::None,
834797
});
@@ -846,7 +809,7 @@ mod tests {
846809
#[test]
847810
fn shelley_type_7() {
848811
let address = Address::Shelley(ShelleyAddress {
849-
network: AddressNetwork::Main,
812+
network: NetworkId::Mainnet,
850813
payment: ShelleyAddressPaymentPart::ScriptHash(test_script_hash()),
851814
delegation: ShelleyAddressDelegationPart::None,
852815
});
@@ -864,7 +827,7 @@ mod tests {
864827
#[test]
865828
fn shelley_type_14() {
866829
let address = Address::Stake(StakeAddress {
867-
network: AddressNetwork::Main,
830+
network: NetworkId::Mainnet,
868831
payload: StakeAddressPayload::StakeKeyHash(test_stake_key_hash()),
869832
});
870833

@@ -881,7 +844,7 @@ mod tests {
881844
#[test]
882845
fn shelley_type_15() {
883846
let address = Address::Stake(StakeAddress {
884-
network: AddressNetwork::Main,
847+
network: NetworkId::Mainnet,
885848
payload: StakeAddressPayload::ScriptHash(test_script_hash()),
886849
});
887850

@@ -925,7 +888,7 @@ mod tests {
925888
let binary =
926889
hex::decode("e1558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
927890
let sa = StakeAddress::from_binary(&binary).unwrap();
928-
assert_eq!(sa.network, AddressNetwork::Main);
891+
assert_eq!(sa.network, NetworkId::Mainnet);
929892
assert_eq!(
930893
match sa.payload {
931894
StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
@@ -941,7 +904,7 @@ mod tests {
941904
let binary =
942905
hex::decode("f1558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
943906
let sa = StakeAddress::from_binary(&binary).unwrap();
944-
assert_eq!(sa.network, AddressNetwork::Main);
907+
assert_eq!(sa.network, NetworkId::Mainnet);
945908
assert_eq!(
946909
match sa.payload {
947910
StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
@@ -957,7 +920,7 @@ mod tests {
957920
let binary =
958921
hex::decode("e0558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
959922
let sa = StakeAddress::from_binary(&binary).unwrap();
960-
assert_eq!(sa.network, AddressNetwork::Test);
923+
assert_eq!(sa.network, NetworkId::Testnet);
961924
assert_eq!(
962925
match sa.payload {
963926
StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
@@ -1010,7 +973,7 @@ mod tests {
1010973
let mut decoder = minicbor::Decoder::new(&binary);
1011974
let decoded = StakeAddress::decode(&mut decoder, &mut ()).unwrap();
1012975

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

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

1057-
assert_eq!(decoded.network, AddressNetwork::Test);
1020+
assert_eq!(decoded.network, NetworkId::Testnet);
10581021
assert_eq!(
10591022
match decoded.payload {
10601023
StakeAddressPayload::ScriptHash(key) => hex::encode(&key),

common/src/stake_addresses.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ impl StakeAddressMap {
544544

545545
#[cfg(test)]
546546
mod tests {
547-
use crate::{AddressNetwork, StakeAddress, StakeAddressPayload};
547+
use crate::{NetworkId, StakeAddress, StakeAddressPayload};
548548

549549
use super::*;
550550

@@ -561,7 +561,7 @@ mod tests {
561561
StakeAddressPayload::StakeKeyHash(
562562
hash.to_vec().try_into().expect("Invalid hash length"),
563563
),
564-
AddressNetwork::Main,
564+
NetworkId::Mainnet,
565565
)
566566
}
567567

common/src/types.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,28 @@ use std::fmt::{Display, Formatter};
1919
use std::ops::{AddAssign, Neg};
2020
use std::{cmp::Ordering, fmt};
2121

22-
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
22+
/// Network identifier
23+
#[derive(
24+
Debug,
25+
Clone,
26+
Default,
27+
PartialEq,
28+
Eq,
29+
Hash,
30+
serde::Serialize,
31+
serde::Deserialize,
32+
minicbor::Encode,
33+
minicbor::Decode,
34+
)]
2335
pub enum NetworkId {
24-
Testnet,
36+
/// Main
37+
#[n(0)]
2538
#[default]
2639
Mainnet,
40+
41+
/// Test
42+
#[n(1)]
43+
Testnet,
2744
}
2845

2946
impl From<String> for NetworkId {

0 commit comments

Comments
 (0)