feat(primitives): Enable serde of ledger structs (#169)

This commit is contained in:
Santiago Carmuega 2022-08-12 19:43:56 -03:00 committed by GitHub
parent 1f0eb3bfc0
commit 9845fc4040
22 changed files with 584 additions and 270 deletions

View file

@ -12,7 +12,7 @@ pub type Blake2b224 = Hash<28>;
pub type AddressId = Blake2b224;
pub type StakeholderId = Blake2b224;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AddrDistr {
Variant0(StakeholderId),
Variant1,
@ -57,7 +57,7 @@ impl minicbor::Encode<()> for AddrDistr {
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AddrType {
PubKey,
Script,
@ -98,7 +98,7 @@ impl<C> minicbor::Encode<C> for AddrType {
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AddrAttrProperty {
AddrDistr(AddrDistr),
Bytes(ByteVec),
@ -148,7 +148,7 @@ impl<C> minicbor::Encode<C> for AddrAttrProperty {
pub type AddrAttr = OrderPreservingProperties<AddrAttrProperty>;
#[derive(Debug, Encode, Decode, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, PartialOrd)]
pub struct AddressPayload {
#[n(0)]
pub root: AddressId,
@ -161,7 +161,7 @@ pub struct AddressPayload {
}
/// New type wrapping a Byron address primitive
#[derive(Debug, Encode, Decode, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ByronAddress {
#[n(0)]
payload: TagWrap<ByteVec, 24>,

View file

@ -63,7 +63,7 @@ pub type TxIdx = u64;
pub type CertIdx = u64;
/// An on-chain pointer to a stake key
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Pointer(Slot, TxIdx, CertIdx);
fn slice_to_hash(slice: &[u8]) -> Result<Hash<28>, Error> {
@ -113,7 +113,7 @@ impl Pointer {
}
/// The payment part of a Shelley address
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub enum ShelleyPaymentPart {
Key(PaymentKeyHash),
Script(ScriptHash),
@ -151,7 +151,7 @@ impl ShelleyPaymentPart {
}
/// The delegation part of a Shelley address
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub enum ShelleyDelegationPart {
Key(StakeKeyHash),
Script(ScriptHash),
@ -212,7 +212,7 @@ impl StakePayload {
}
/// The network tag of an address
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub enum Network {
Testnet,
Mainnet,
@ -230,24 +230,24 @@ impl From<u8> for Network {
}
/// A decoded Shelley address
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub struct ShelleyAddress(Network, ShelleyPaymentPart, ShelleyDelegationPart);
/// The payload of a Stake address
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub enum StakePayload {
Stake(StakeKeyHash),
Script(ScriptHash),
}
/// A decoded Stake address
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub struct StakeAddress(Network, StakePayload);
pub use byron::ByronAddress;
/// A decoded Cardano address of any type
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub enum Address {
Byron(ByronAddress),
Shelley(ShelleyAddress),
@ -345,7 +345,7 @@ parse_stake_fn!(parse_type_14, stake_key);
parse_stake_fn!(parse_type_15, script);
fn bytes_to_address(bytes: &[u8]) -> Result<Address, Error> {
let header = *bytes.get(0).ok_or(Error::MissingHeader)?;
let header = *bytes.first().ok_or(Error::MissingHeader)?;
let payload = &bytes[1..];
match header & 0b1111_0000 {