diff --git a/codec/src/map_parameters.rs b/codec/src/map_parameters.rs
index 4de54aff..b46d5c69 100644
--- a/codec/src/map_parameters.rs
+++ b/codec/src/map_parameters.rs
@@ -69,13 +69,9 @@ pub fn map_address(address: &addresses::Address) -> Result
{
addresses::Address::Stake(stake_address) => Ok(Address::Stake(StakeAddress {
network: map_network(stake_address.network())?,
- payload: match stake_address.payload() {
- addresses::StakePayload::Stake(hash) => {
- StakeAddressPayload::StakeKeyHash(hash.to_vec())
- }
- addresses::StakePayload::Script(hash) => {
- StakeAddressPayload::ScriptHash(hash.to_vec())
- }
+ credential: match stake_address.payload() {
+ addresses::StakePayload::Stake(hash) => StakeCredential::AddrKeyHash(hash.to_vec()),
+ addresses::StakePayload::Script(hash) => StakeCredential::ScriptHash(hash.to_vec()),
},
})),
}
@@ -97,10 +93,10 @@ pub fn map_stake_credential(cred: &PallasStakeCredential) -> StakeCredential {
pub fn map_stake_address(cred: &PallasStakeCredential, network_id: NetworkId) -> StakeAddress {
let payload = match cred {
PallasStakeCredential::AddrKeyhash(key_hash) => {
- StakeAddressPayload::StakeKeyHash(key_hash.to_vec())
+ StakeCredential::AddrKeyHash(key_hash.to_vec())
}
PallasStakeCredential::ScriptHash(script_hash) => {
- StakeAddressPayload::ScriptHash(script_hash.to_vec())
+ StakeCredential::ScriptHash(script_hash.to_vec())
}
};
@@ -265,7 +261,7 @@ pub fn map_certificate(
.iter()
.map(|v| {
StakeAddress::new(
- StakeAddressPayload::StakeKeyHash(v.to_vec()),
+ StakeCredential::AddrKeyHash(v.to_vec()),
network_id.clone().into(),
)
})
@@ -375,7 +371,7 @@ pub fn map_certificate(
.into_iter()
.map(|v| {
StakeAddress::new(
- StakeAddressPayload::StakeKeyHash(v.to_vec()),
+ StakeCredential::AddrKeyHash(v.to_vec()),
network_id.clone().into(),
)
})
diff --git a/common/src/address.rs b/common/src/address.rs
index 8992e253..7c5e6b3a 100644
--- a/common/src/address.rs
+++ b/common/src/address.rs
@@ -4,11 +4,10 @@
use crate::cip19::{VarIntDecoder, VarIntEncoder};
use crate::types::{KeyHash, ScriptHash};
-use crate::{Credential, NetworkId};
+use crate::{Credential, NetworkId, StakeCredential};
use anyhow::{anyhow, bail, Result};
use crc::{Crc, CRC_32_ISO_HDLC};
use minicbor::data::IanaTag;
-use serde_with::{hex::Hex, serde_as};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
@@ -159,7 +158,7 @@ pub enum ShelleyAddressDelegationPart {
#[n(1)]
StakeKeyHash(#[n(0)] Vec),
- /// Delegation to script key
+ /// Delegation to script key hash
#[n(2)]
ScriptHash(#[n(0)] ScriptHash),
@@ -357,55 +356,35 @@ impl ShelleyAddress {
}
}
-/// Payload of a stake address
-#[serde_as]
-#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Hash)]
-pub enum StakeAddressPayload {
- /// Stake key
- StakeKeyHash(#[serde_as(as = "Hex")] KeyHash),
-
- /// Script hash
- ScriptHash(#[serde_as(as = "Hex")] ScriptHash),
-}
-
-impl StakeAddressPayload {
- // Convert to string - note different encoding from when used as part of a StakeAddress
- pub fn to_string(&self) -> Result {
- let (hrp, data) = match &self {
- Self::StakeKeyHash(data) => (bech32::Hrp::parse("stake_vkh")?, data),
- Self::ScriptHash(data) => (bech32::Hrp::parse("script")?, data),
- };
-
- Ok(bech32::encode::(hrp, data)?)
- }
-}
-
/// A stake address
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
pub struct StakeAddress {
/// Network id
pub network: NetworkId,
- /// Payload
- pub payload: StakeAddressPayload,
+ /// Credential
+ pub credential: StakeCredential, // payload: StakePayload?
}
impl StakeAddress {
- pub fn new(payload: StakeAddressPayload, network: NetworkId) -> Self {
- StakeAddress { network, payload }
+ pub fn new(credential: StakeCredential, network: NetworkId) -> Self {
+ StakeAddress {
+ network,
+ credential,
+ }
}
pub fn get_hash(&self) -> &[u8] {
- match &self.payload {
- StakeAddressPayload::StakeKeyHash(hash) => hash,
- StakeAddressPayload::ScriptHash(hash) => hash,
+ match &self.credential {
+ StakeCredential::AddrKeyHash(hash) => hash,
+ StakeCredential::ScriptHash(hash) => hash,
}
}
pub fn get_credential(&self) -> Credential {
- match &self.payload {
- StakeAddressPayload::StakeKeyHash(hash) => Credential::AddrKeyHash(hash.clone()),
- StakeAddressPayload::ScriptHash(hash) => Credential::ScriptHash(hash.clone()),
+ match &self.credential {
+ StakeCredential::AddrKeyHash(hash) => Credential::AddrKeyHash(hash.clone()),
+ StakeCredential::ScriptHash(hash) => Credential::ScriptHash(hash.clone()),
}
}
@@ -429,13 +408,16 @@ impl StakeAddress {
false => NetworkId::Mainnet,
};
- let payload = match (header >> 4) & 0x0Fu8 {
- 0b1110 => StakeAddressPayload::StakeKeyHash(data[1..].to_vec()),
- 0b1111 => StakeAddressPayload::ScriptHash(data[1..].to_vec()),
+ let credential = match (header >> 4) & 0x0Fu8 {
+ 0b1110 => StakeCredential::AddrKeyHash(data[1..].to_vec()),
+ 0b1111 => StakeCredential::ScriptHash(data[1..].to_vec()),
_ => return Err(anyhow!("Unknown header {header} in stake address")),
};
- return Ok(StakeAddress { network, payload });
+ return Ok(StakeAddress {
+ network,
+ credential,
+ });
}
Err(anyhow!("Empty stake address data"))
@@ -448,9 +430,9 @@ impl StakeAddress {
NetworkId::Testnet => 0b0u8,
};
- let (stake_bits, stake_hash): (u8, &Vec) = match &self.payload {
- StakeAddressPayload::StakeKeyHash(data) => (0b1110, data),
- StakeAddressPayload::ScriptHash(data) => (0b1111, data),
+ let (stake_bits, stake_hash): (u8, &Vec) = match &self.credential {
+ StakeCredential::AddrKeyHash(data) => (0b1110, data),
+ StakeCredential::ScriptHash(data) => (0b1111, data),
};
let mut data = vec![network_bits | (stake_bits << 4)];
@@ -469,20 +451,23 @@ impl StakeAddress {
_ => NetworkId::Testnet,
};
- let payload = match (data[0] >> 4) & 0x0F {
- 0b1110 => StakeAddressPayload::StakeKeyHash(data[1..].to_vec()),
- 0b1111 => StakeAddressPayload::ScriptHash(data[1..].to_vec()),
+ let credential = match (data[0] >> 4) & 0x0F {
+ 0b1110 => StakeCredential::AddrKeyHash(data[1..].to_vec()),
+ 0b1111 => StakeCredential::ScriptHash(data[1..].to_vec()),
_ => bail!("Unknown header byte {:x} in stake address", data[0]),
};
- Ok(StakeAddress { network, payload })
+ Ok(StakeAddress {
+ network,
+ credential,
+ })
}
pub fn to_bytes_key(&self) -> Result> {
let mut out = Vec::new();
- let (bits, hash): (u8, &[u8]) = match &self.payload {
- StakeAddressPayload::StakeKeyHash(h) => (0b1110, h),
- StakeAddressPayload::ScriptHash(h) => (0b1111, h),
+ let (bits, hash): (u8, &[u8]) = match &self.credential {
+ StakeCredential::AddrKeyHash(h) => (0b1110, h),
+ StakeCredential::ScriptHash(h) => (0b1111, h),
};
let net_bit = match self.network {
@@ -529,7 +514,7 @@ impl Default for StakeAddress {
fn default() -> Self {
StakeAddress {
network: NetworkId::Mainnet,
- payload: StakeAddressPayload::StakeKeyHash(vec![0u8; 28]),
+ credential: StakeCredential::AddrKeyHash(vec![0u8; 28]),
}
}
}
@@ -611,9 +596,9 @@ impl Address {
ShelleyAddressPaymentPart::PaymentKeyHash(_) => false,
ShelleyAddressPaymentPart::ScriptHash(_) => true,
},
- Address::Stake(stake) => match stake.payload {
- StakeAddressPayload::StakeKeyHash(_) => false,
- StakeAddressPayload::ScriptHash(_) => true,
+ Address::Stake(stake) => match stake.credential {
+ StakeCredential::AddrKeyHash(_) => false,
+ StakeCredential::ScriptHash(_) => true,
},
Address::Byron(_) | Address::None => false,
}
@@ -828,7 +813,7 @@ mod tests {
fn shelley_type_14() {
let address = Address::Stake(StakeAddress {
network: NetworkId::Mainnet,
- payload: StakeAddressPayload::StakeKeyHash(test_stake_key_hash()),
+ credential: StakeCredential::AddrKeyHash(test_stake_key_hash()),
});
let text = address.to_string().unwrap();
@@ -845,7 +830,7 @@ mod tests {
fn shelley_type_15() {
let address = Address::Stake(StakeAddress {
network: NetworkId::Mainnet,
- payload: StakeAddressPayload::ScriptHash(test_script_hash()),
+ credential: StakeCredential::ScriptHash(test_script_hash()),
});
let text = address.to_string().unwrap();
@@ -890,8 +875,8 @@ mod tests {
let sa = StakeAddress::from_binary(&binary).unwrap();
assert_eq!(sa.network, NetworkId::Mainnet);
assert_eq!(
- match sa.payload {
- StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
+ match sa.credential {
+ StakeCredential::AddrKeyHash(key) => hex::encode(&key),
_ => "SCRIPT".to_string(),
},
"558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001"
@@ -906,8 +891,8 @@ mod tests {
let sa = StakeAddress::from_binary(&binary).unwrap();
assert_eq!(sa.network, NetworkId::Mainnet);
assert_eq!(
- match sa.payload {
- StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
+ match sa.credential {
+ StakeCredential::ScriptHash(key) => hex::encode(&key),
_ => "STAKE".to_string(),
},
"558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001"
@@ -922,8 +907,8 @@ mod tests {
let sa = StakeAddress::from_binary(&binary).unwrap();
assert_eq!(sa.network, NetworkId::Testnet);
assert_eq!(
- match sa.payload {
- StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
+ match sa.credential {
+ StakeCredential::AddrKeyHash(key) => hex::encode(&key),
_ => "SCRIPT".to_string(),
},
"558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001"
@@ -975,8 +960,8 @@ mod tests {
assert_eq!(decoded.network, NetworkId::Mainnet);
assert_eq!(
- match decoded.payload {
- StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key),
+ match decoded.credential {
+ StakeCredential::AddrKeyHash(key) => hex::encode(&key),
_ => "STAKE".to_string(),
},
"558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001"
@@ -998,8 +983,8 @@ mod tests {
assert_eq!(decoded.network, NetworkId::Mainnet);
assert_eq!(
- match decoded.payload {
- StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
+ match decoded.credential {
+ StakeCredential::ScriptHash(key) => hex::encode(&key),
_ => "STAKE".to_string(),
},
"558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001"
@@ -1019,8 +1004,8 @@ mod tests {
assert_eq!(decoded.network, NetworkId::Testnet);
assert_eq!(
- match decoded.payload {
- StakeAddressPayload::ScriptHash(key) => hex::encode(&key),
+ match decoded.credential {
+ StakeCredential::ScriptHash(key) => hex::encode(&key),
_ => "SCRIPT".to_string(),
},
"558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001"
diff --git a/common/src/stake_addresses.rs b/common/src/stake_addresses.rs
index b2a18883..b4daf076 100644
--- a/common/src/stake_addresses.rs
+++ b/common/src/stake_addresses.rs
@@ -544,7 +544,7 @@ impl StakeAddressMap {
#[cfg(test)]
mod tests {
- use crate::{NetworkId, StakeAddress, StakeAddressPayload};
+ use crate::{NetworkId, StakeAddress, StakeCredential};
use super::*;
@@ -558,9 +558,7 @@ mod tests {
fn create_stake_address(hash: &[u8]) -> StakeAddress {
StakeAddress::new(
- StakeAddressPayload::StakeKeyHash(
- hash.to_vec().try_into().expect("Invalid hash length"),
- ),
+ StakeCredential::AddrKeyHash(hash.to_vec().try_into().expect("Invalid hash length")),
NetworkId::Mainnet,
)
}
diff --git a/common/src/types.rs b/common/src/types.rs
index fc9985ea..5030fcbd 100644
--- a/common/src/types.rs
+++ b/common/src/types.rs
@@ -625,15 +625,16 @@ pub struct PotDelta {
pub delta: LovelaceDelta,
}
+#[serde_as]
#[derive(
Debug, Clone, Ord, Eq, PartialEq, PartialOrd, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum Credential {
/// Script hash. NOTE: Order matters when parsing Haskell Node Snapshot data.
- ScriptHash(KeyHash),
+ ScriptHash(#[serde_as(as = "Hex")] KeyHash),
/// Address key hash
- AddrKeyHash(KeyHash),
+ AddrKeyHash(#[serde_as(as = "Hex")] KeyHash),
}
impl Credential {
@@ -735,6 +736,17 @@ impl Credential {
pub type StakeCredential = Credential;
+impl StakeCredential {
+ pub fn to_string(&self) -> Result {
+ let (hrp, data) = match &self {
+ Self::AddrKeyHash(data) => (Hrp::parse("stake_vkh")?, data),
+ Self::ScriptHash(data) => (Hrp::parse("script")?, data),
+ };
+
+ Ok(bech32::encode::(hrp, data)?)
+ }
+}
+
/// Relay single host address
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
pub struct SingleHostAddr {
diff --git a/modules/accounts_state/src/snapshot.rs b/modules/accounts_state/src/snapshot.rs
index 3983a1d6..8d1a9d3d 100644
--- a/modules/accounts_state/src/snapshot.rs
+++ b/modules/accounts_state/src/snapshot.rs
@@ -187,7 +187,7 @@ mod tests {
use super::*;
use acropolis_common::stake_addresses::StakeAddressState;
use acropolis_common::NetworkId::Mainnet;
- use acropolis_common::{StakeAddress, StakeAddressPayload};
+ use acropolis_common::{StakeAddress, StakeCredential};
// Helper function to create stake addresses for testing
fn create_test_stake_address(id: u8) -> StakeAddress {
@@ -195,9 +195,7 @@ mod tests {
hash[0] = id;
StakeAddress {
network: Mainnet,
- payload: StakeAddressPayload::StakeKeyHash(
- hash.try_into().expect("Invalid hash length"),
- ),
+ credential: StakeCredential::AddrKeyHash(hash.try_into().expect("Invalid hash length")),
}
}
diff --git a/modules/accounts_state/src/state.rs b/modules/accounts_state/src/state.rs
index 28fb74a9..c59be443 100644
--- a/modules/accounts_state/src/state.rs
+++ b/modules/accounts_state/src/state.rs
@@ -993,8 +993,8 @@ mod tests {
use acropolis_common::{
protocol_params::ConwayParams, rational_number::RationalNumber, Anchor, Committee,
Constitution, CostModel, DRepVotingThresholds, NetworkId, PoolVotingThresholds, Pot,
- PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeAddressPayload,
- StakeAndVoteDelegation, StakeRegistrationAndStakeAndVoteDelegation,
+ PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeAndVoteDelegation,
+ StakeCredential, StakeRegistrationAndStakeAndVoteDelegation,
StakeRegistrationAndVoteDelegation, VoteDelegation, Withdrawal,
};
@@ -1004,7 +1004,7 @@ mod tests {
full_hash[..hash.len().min(28)].copy_from_slice(&hash[..hash.len().min(28)]);
StakeAddress {
network: NetworkId::Mainnet,
- payload: StakeAddressPayload::StakeKeyHash(full_hash),
+ credential: StakeCredential::AddrKeyHash(full_hash),
}
}
diff --git a/modules/rest_blockfrost/src/handlers/epochs.rs b/modules/rest_blockfrost/src/handlers/epochs.rs
index 7df97108..94b4a30d 100644
--- a/modules/rest_blockfrost/src/handlers/epochs.rs
+++ b/modules/rest_blockfrost/src/handlers/epochs.rs
@@ -15,7 +15,7 @@ use acropolis_common::{
utils::query_state,
},
serialization::Bech32WithHrp,
- NetworkId, StakeAddress, StakeAddressPayload,
+ NetworkId, StakeAddress, StakeCredential,
};
use anyhow::{anyhow, Result};
use caryatid_sdk::Context;
@@ -508,7 +508,7 @@ pub async fn handle_epoch_total_stakes_blockfrost(
.map(|(pool_id, stake_key_hash, amount)| {
let stake_address = StakeAddress {
network: network.clone(),
- payload: StakeAddressPayload::StakeKeyHash(stake_key_hash),
+ credential: StakeCredential::AddrKeyHash(stake_key_hash),
}
.to_string()
.map_err(|e| anyhow::anyhow!("Failed to convert stake address to string: {e}"))?;
@@ -642,10 +642,10 @@ pub async fn handle_epoch_pool_stakes_blockfrost(
.await?;
let spdd_response = spdd
.into_iter()
- .map(|(stake_key_hash, amount)| {
+ .map(|(key_hash, amount)| {
let stake_address = StakeAddress {
network: network.clone(),
- payload: StakeAddressPayload::StakeKeyHash(stake_key_hash),
+ credential: StakeCredential::AddrKeyHash(key_hash),
}
.to_string()
.map_err(|e| anyhow::anyhow!("Failed to convert stake address to string: {e}"))?;
diff --git a/modules/stake_delta_filter/src/predefined.rs b/modules/stake_delta_filter/src/predefined.rs
index 36debe29..f1a2ef92 100644
--- a/modules/stake_delta_filter/src/predefined.rs
+++ b/modules/stake_delta_filter/src/predefined.rs
@@ -59,8 +59,8 @@ pub const POINTER_CACHE: [(&str, &str); 1] = [(
},
{
"network": "Mainnet",
- "payload": {
- "StakeKeyHash": "bc1597ad71c55d2d009a9274b3831ded155118dd769f5376decc1369"
+ "credential": {
+ "AddrKeyHash": "bc1597ad71c55d2d009a9274b3831ded155118dd769f5376decc1369"
}
}
],
@@ -80,8 +80,8 @@ pub const POINTER_CACHE: [(&str, &str); 1] = [(
},
{
"network": "Mainnet",
- "payload": {
- "StakeKeyHash": "e46c33afa9ca60cfeb3b7452a415c271772020b3f57ac90c496a6127"
+ "credential": {
+ "AddrKeyHash": "e46c33afa9ca60cfeb3b7452a415c271772020b3f57ac90c496a6127"
}
}
],
@@ -117,8 +117,8 @@ pub const POINTER_CACHE: [(&str, &str); 1] = [(
},
{
"network": "Mainnet",
- "payload": {
- "StakeKeyHash": "1332d859dd71f5b1089052a049690d81f7367eac9fafaef80b4da395"
+ "credential": {
+ "AddrKeyHash": "1332d859dd71f5b1089052a049690d81f7367eac9fafaef80b4da395"
}
}
],
diff --git a/modules/stake_delta_filter/src/utils.rs b/modules/stake_delta_filter/src/utils.rs
index 5a349c25..41431f1b 100644
--- a/modules/stake_delta_filter/src/utils.rs
+++ b/modules/stake_delta_filter/src/utils.rs
@@ -1,7 +1,7 @@
use acropolis_common::{
messages::{AddressDeltasMessage, StakeAddressDeltasMessage},
Address, AddressDelta, BlockInfo, Era, ShelleyAddressDelegationPart, ShelleyAddressPointer,
- StakeAddress, StakeAddressDelta, StakeAddressPayload,
+ StakeAddress, StakeAddressDelta, StakeCredential,
};
use anyhow::{anyhow, Result};
use serde_with::serde_as;
@@ -345,12 +345,12 @@ pub fn process_message(
// Base addresses (stake delegated to itself)
ShelleyAddressDelegationPart::StakeKeyHash(keyhash) => StakeAddress {
network: shelley.network.clone(),
- payload: StakeAddressPayload::StakeKeyHash(keyhash.clone()),
+ credential: StakeCredential::AddrKeyHash(keyhash.clone()),
},
ShelleyAddressDelegationPart::ScriptHash(scripthash) => StakeAddress {
network: shelley.network.clone(),
- payload: StakeAddressPayload::ScriptHash(scripthash.clone()),
+ credential: StakeCredential::ScriptHash(scripthash.clone()),
},
// Shelley addresses (stake delegated to some different address)
@@ -403,9 +403,8 @@ mod test {
use crate::*;
use acropolis_common::{
messages::AddressDeltasMessage, Address, AddressDelta, BlockHash, BlockInfo, BlockStatus,
- ByronAddress, Era, NetworkId, ShelleyAddress, ShelleyAddressDelegationPart,
- ShelleyAddressPaymentPart, ShelleyAddressPointer, StakeAddress, StakeAddressPayload,
- UTxOIdentifier, ValueDelta,
+ ByronAddress, Era, ShelleyAddress, ShelleyAddressDelegationPart, ShelleyAddressPaymentPart,
+ ShelleyAddressPointer, StakeAddress, StakeCredential, UTxOIdentifier, ValueDelta,
};
use bech32::{Bech32, Hrp};
@@ -470,12 +469,12 @@ mod test {
addresses::Address::Stake(stake_address) => Ok(Address::Stake(StakeAddress {
network: map_network(stake_address.network())?,
- payload: match stake_address.payload() {
+ credential: match stake_address.payload() {
addresses::StakePayload::Stake(hash) => {
- StakeAddressPayload::StakeKeyHash(hash.to_vec())
+ StakeCredential::AddrKeyHash(hash.to_vec())
}
addresses::StakePayload::Script(hash) => {
- StakeAddressPayload::ScriptHash(hash.to_vec())
+ StakeCredential::ScriptHash(hash.to_vec())
}
},
})),
@@ -586,11 +585,11 @@ mod test {
// additional check: payload conversion correctness
assert_eq!(
- stake_delta.deltas.get(0).unwrap().address.payload.to_string().unwrap(),
+ stake_delta.deltas.get(0).unwrap().address.credential.to_string().unwrap(),
stake_key_hash
);
assert_eq!(
- stake_delta.deltas.get(2).unwrap().address.payload.to_string().unwrap(),
+ stake_delta.deltas.get(2).unwrap().address.credential.to_string().unwrap(),
script_hash
);