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 30231d10b0
commit 758d6e2119
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 22 deletions

View file

@ -28,6 +28,9 @@ pub enum Error {
#[error("invalid operation for Byron address")]
InvalidForByron,
#[error("invalid CBOR for Byron address")]
InvalidByronCbor,
#[error("unkown hrp for network {0:08b}")]
UnknownNetworkHrp(u8),
@ -231,9 +234,15 @@ pub enum StakePayload {
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct StakeAddress(Network, StakePayload);
/// Newtype representing a Byron address
/// New type wrapping a Byron address primitive
#[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
#[derive(Debug, Clone, PartialEq, PartialOrd)]
@ -325,7 +334,8 @@ parse_shelley_fn!(parse_type_7, script_hash);
// type 8 (1000) are Byron addresses
fn parse_type_8(header: u8, payload: &[u8]) -> Result<Address, Error> {
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
@ -378,7 +388,7 @@ impl ByronAddress {
}
fn to_vec(&self) -> Vec<u8> {
self.0.clone()
pallas_codec::minicbor::to_vec(&self.0).unwrap()
}
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 {
pub fn new(
network: Network,
@ -554,6 +558,11 @@ impl Address {
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
pub fn network(&self) -> Option<Network> {
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 {
fn from(addr: ByronAddress) -> Self {
Address::Byron(addr)

View file

@ -186,7 +186,7 @@ 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
/// struct.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, PartialOrd)]
pub struct OrderPreservingProperties<P>(Vec<P>);
impl<P> Deref for OrderPreservingProperties<P> {

View file

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

View file

@ -14,6 +14,7 @@ authors = [
[dependencies]
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-codec = { version = "0.11.0", path = "../pallas-codec" }
hex = "0.4.3"

View file

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