feat(addresses): Add helper for shelley into stake address (#208)
This commit is contained in:
parent
f5c096a8ae
commit
323402eb54
1 changed files with 39 additions and 0 deletions
|
|
@ -242,6 +242,14 @@ impl StakePayload {
|
|||
pub fn is_script(&self) -> bool {
|
||||
matches!(self, StakePayload::Script(_))
|
||||
}
|
||||
|
||||
/// Get a reference to the inner hash of this address part
|
||||
pub fn as_hash(&self) -> &Hash<28> {
|
||||
match self {
|
||||
StakePayload::Stake(x) => x,
|
||||
StakePayload::Script(x) => x,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The network tag of an address
|
||||
|
|
@ -494,6 +502,20 @@ impl ShelleyAddress {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<ShelleyAddress> for StakeAddress {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: ShelleyAddress) -> Result<Self, Self::Error> {
|
||||
let payload = match value.delegation() {
|
||||
ShelleyDelegationPart::Key(h) => StakePayload::Stake(*h),
|
||||
ShelleyDelegationPart::Script(h) => StakePayload::Script(*h),
|
||||
_ => return Err(Error::InvalidForContent),
|
||||
};
|
||||
|
||||
Ok(StakeAddress(value.network(), payload))
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for StakePayload {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
match self {
|
||||
|
|
@ -860,4 +882,21 @@ mod tests {
|
|||
let addr = Address::from_hex("40C19D7D05E90EEB6394B53313FE79D47077DE33068C6B813BBE5C9D5681FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7F81FFFFFFFFFFFFFFFF7F81FFFFFFFFFFFFFFFF7F");
|
||||
assert!(matches!(addr, Ok(Address::Shelley(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shelley_into_stake() {
|
||||
let addr = Address::from_bech32("addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x").unwrap();
|
||||
|
||||
match addr {
|
||||
Address::Shelley(shelley_addr) => {
|
||||
let stake_addr: StakeAddress = shelley_addr.clone().try_into().unwrap();
|
||||
assert_eq!(stake_addr.network(), shelley_addr.network());
|
||||
|
||||
let stake_hash = stake_addr.payload().as_hash();
|
||||
let shelley_hash = shelley_addr.delegation().as_hash().unwrap();
|
||||
assert_eq!(stake_hash, shelley_hash);
|
||||
}
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue