feat(traverse): Integrate address library (#149)

This commit is contained in:
Santiago Carmuega 2022-07-04 18:06:14 -03:00 committed by GitHub
parent a26773a94c
commit 9e22c842cd
5 changed files with 42 additions and 22 deletions

View file

@ -28,6 +28,9 @@ pub enum Error {
#[error("invalid operation for Byron address")] #[error("invalid operation for Byron address")]
InvalidForByron, InvalidForByron,
#[error("invalid CBOR for Byron address")]
InvalidByronCbor,
#[error("unkown hrp for network {0:08b}")] #[error("unkown hrp for network {0:08b}")]
UnknownNetworkHrp(u8), UnknownNetworkHrp(u8),
@ -231,9 +234,15 @@ pub enum StakePayload {
#[derive(Debug, Clone, PartialEq, PartialOrd)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct StakeAddress(Network, StakePayload); pub struct StakeAddress(Network, StakePayload);
/// Newtype representing a Byron address /// New type wrapping a Byron address primitive
#[derive(Debug, Clone, PartialEq, PartialOrd)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct ByronAddress(Vec<u8>); pub struct ByronAddress(pallas_primitives::byron::Address);
impl ByronAddress {
pub fn new(primitive: pallas_primitives::byron::Address) -> Self {
Self(primitive)
}
}
/// A decoded Cardano address of any type /// A decoded Cardano address of any type
#[derive(Debug, Clone, PartialEq, PartialOrd)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
@ -325,7 +334,8 @@ parse_shelley_fn!(parse_type_7, script_hash);
// type 8 (1000) are Byron addresses // type 8 (1000) are Byron addresses
fn parse_type_8(header: u8, payload: &[u8]) -> Result<Address, Error> { fn parse_type_8(header: u8, payload: &[u8]) -> Result<Address, Error> {
let vec = [&[header], payload].concat(); let vec = [&[header], payload].concat();
Ok(Address::Byron(ByronAddress(vec))) let prim = pallas_codec::minicbor::decode(&vec).map_err(|_| Error::InvalidByronCbor)?;
Ok(Address::Byron(ByronAddress(prim)))
} }
// types 14-15 are Stake addresses // types 14-15 are Stake addresses
@ -378,7 +388,7 @@ impl ByronAddress {
} }
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
self.0.clone() pallas_codec::minicbor::to_vec(&self.0).unwrap()
} }
pub fn to_hex(&self) -> String { pub fn to_hex(&self) -> String {
@ -387,12 +397,6 @@ impl ByronAddress {
} }
} }
impl AsRef<[u8]> for ByronAddress {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl ShelleyAddress { impl ShelleyAddress {
pub fn new( pub fn new(
network: Network, network: Network,
@ -554,6 +558,11 @@ impl Address {
bech32_to_address(bech32) bech32_to_address(bech32)
} }
// Tries to decode the raw bytes of an address
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
bytes_to_address(bytes)
}
/// Gets the network assoaciated with this address /// Gets the network assoaciated with this address
pub fn network(&self) -> Option<Network> { pub fn network(&self) -> Option<Network> {
match self { match self {
@ -615,6 +624,14 @@ impl Address {
} }
} }
impl TryFrom<&[u8]> for Address {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
bytes_to_address(value)
}
}
impl From<ByronAddress> for Address { impl From<ByronAddress> for Address {
fn from(addr: ByronAddress) -> Self { fn from(addr: ByronAddress) -> Self {
Address::Byron(addr) Address::Byron(addr)

View file

@ -186,7 +186,7 @@ where
/// transform key-value structures into an orderer vec of `properties`, where /// transform key-value structures into an orderer vec of `properties`, where
/// each entry represents a a cbor-encodable variant of an attribute of the /// each entry represents a a cbor-encodable variant of an attribute of the
/// struct. /// struct.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone, PartialOrd)]
pub struct OrderPreservingProperties<P>(Vec<P>); pub struct OrderPreservingProperties<P>(Vec<P>);
impl<P> Deref for OrderPreservingProperties<P> { impl<P> Deref for OrderPreservingProperties<P> {

View file

@ -51,7 +51,7 @@ pub type Attributes = EmptyMap;
// Addresses // Addresses
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrDistr { pub enum AddrDistr {
Variant0(StakeholderId), Variant0(StakeholderId),
Variant1, Variant1,
@ -96,7 +96,7 @@ impl minicbor::Encode<()> for AddrDistr {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrType { pub enum AddrType {
PubKey, PubKey,
Script, Script,
@ -137,7 +137,7 @@ impl<C> minicbor::Encode<C> for AddrType {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrAttrProperty { pub enum AddrAttrProperty {
AddrDistr(AddrDistr), AddrDistr(AddrDistr),
Bytes(ByteVec), Bytes(ByteVec),
@ -187,7 +187,7 @@ impl<C> minicbor::Encode<C> for AddrAttrProperty {
pub type AddrAttr = OrderPreservingProperties<AddrAttrProperty>; pub type AddrAttr = OrderPreservingProperties<AddrAttrProperty>;
#[derive(Debug, Encode, Decode, Clone)] #[derive(Debug, Encode, Decode, Clone, PartialEq, PartialOrd)]
pub struct AddressPayload { pub struct AddressPayload {
#[n(0)] #[n(0)]
pub root: AddressId, pub root: AddressId,
@ -200,7 +200,7 @@ pub struct AddressPayload {
} }
// address = [ #6.24(bytes .cbor ([addressid, addrattr, addrtype])), u64 ] // address = [ #6.24(bytes .cbor ([addressid, addrattr, addrtype])), u64 ]
#[derive(Debug, Encode, Decode, Clone)] #[derive(Debug, Encode, Decode, Clone, PartialEq, PartialOrd)]
pub struct Address { pub struct Address {
#[n(0)] #[n(0)]
pub payload: CborWrap<AddressPayload>, pub payload: CborWrap<AddressPayload>,

View file

@ -14,6 +14,7 @@ authors = [
[dependencies] [dependencies]
pallas-primitives = { version = "0.11.0", path = "../pallas-primitives" } pallas-primitives = { version = "0.11.0", path = "../pallas-primitives" }
pallas-addresses = { version = "0.11.0", path = "../pallas-addresses" }
pallas-crypto = { version = "0.11.0", path = "../pallas-crypto" } pallas-crypto = { version = "0.11.0", path = "../pallas-crypto" }
pallas-codec = { version = "0.11.0", path = "../pallas-codec" } pallas-codec = { version = "0.11.0", path = "../pallas-codec" }
hex = "0.4.3" hex = "0.4.3"

View file

@ -1,5 +1,6 @@
use std::{borrow::Cow, ops::Deref}; use std::{borrow::Cow, ops::Deref};
use pallas_addresses::{Address, ByronAddress, Error as AddressError};
use pallas_codec::minicbor; use pallas_codec::minicbor;
use pallas_primitives::{alonzo, babbage, byron}; use pallas_primitives::{alonzo, babbage, byron};
@ -18,13 +19,14 @@ impl<'b> MultiEraOutput<'b> {
Self::Babbage(Box::new(Cow::Borrowed(output))) Self::Babbage(Box::new(Cow::Borrowed(output)))
} }
pub fn address(&self, hrp: &str) -> String { pub fn to_address(&self) -> Result<Address, AddressError> {
match self { match self {
MultiEraOutput::AlonzoCompatible(x) => { MultiEraOutput::AlonzoCompatible(x) => Address::from_bytes(&x.address),
x.to_bech32_address(hrp).expect("invalid address value") MultiEraOutput::Babbage(x) => match x.deref().deref() {
} babbage::TransactionOutput::Legacy(x) => Address::from_bytes(&x.address),
MultiEraOutput::Babbage(x) => x.to_bech32_address(hrp).expect("invalid address value"), babbage::TransactionOutput::PostAlonzo(x) => Address::from_bytes(&x.address),
MultiEraOutput::Byron(x) => x.address.to_addr_string().expect("invalid address value"), },
MultiEraOutput::Byron(x) => Ok(ByronAddress::new(x.address.clone()).into()),
} }
} }