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")]
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)