Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(traverse): Integrate address library #149

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions pallas-addresses/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pallas-codec/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
10 changes: 5 additions & 5 deletions pallas-primitives/src/byron/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub type Attributes = EmptyMap;

// Addresses

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrDistr {
Variant0(StakeholderId),
Variant1,
Expand Down Expand Up @@ -96,7 +96,7 @@ impl minicbor::Encode<()> for AddrDistr {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrType {
PubKey,
Script,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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>,
Expand Down
1 change: 1 addition & 0 deletions pallas-traverse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 8 additions & 6 deletions pallas-traverse/src/output.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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()),
}
}

Expand Down