-
Notifications
You must be signed in to change notification settings - Fork 1
feat(general): More types #217
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
Merged
stanislav-tkach
merged 9 commits into
main
from
rename-transaction-hash-and-add-stake-address-type
Feb 20, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39b2b72
Rename TransactionHash to TransactionId
stanislav-tkach db72b38
fix
stanislav-tkach e701205
Bump versions
stanislav-tkach 61f3c95
Update versions
stanislav-tkach 1e9685b
fix
stanislav-tkach eca1555
Implement Display for StakeAddress
stanislav-tkach a15202c
Fix spellcheck
stanislav-tkach 798a2d1
Use tag instead of branch
stanislav-tkach a3056dc
Remove unnecessary comment
stanislav-tkach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| //! A stake address. | ||
|
|
||
| // cspell: words Scripthash, Keyhash | ||
|
|
||
| use std::fmt::{Display, Formatter}; | ||
|
|
||
| use anyhow::{anyhow, Context}; | ||
| use pallas::{ | ||
| crypto::hash::Hash, | ||
| ledger::{ | ||
| addresses::{ | ||
| ShelleyAddress, ShelleyDelegationPart, ShelleyPaymentPart, | ||
| StakeAddress as PallasStakeAddress, | ||
| }, | ||
| primitives::conway, | ||
| }, | ||
| }; | ||
|
|
||
| use crate::Network; | ||
|
|
||
| /// A stake address. | ||
| #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)] | ||
| pub struct StakeAddress(PallasStakeAddress); | ||
|
|
||
| impl StakeAddress { | ||
| /// Creates a new instance from the given parameters. | ||
| #[allow(clippy::expect_used, clippy::missing_panics_doc)] | ||
| #[must_use] | ||
| pub fn new(network: Network, is_script: bool, hash: Hash<28>) -> Self { | ||
| let network = network.into(); | ||
| // `pallas::StakeAddress` can only be constructed from `ShelleyAddress`, so we are forced | ||
| // to create a dummy shelley address. The input hash parameter is used to construct both | ||
| // payment and delegation parts, but the payment part isn't used in the stake address | ||
| // construction, so it doesn't matter. | ||
| let payment = ShelleyPaymentPart::Key(hash); | ||
| let delegation = if is_script { | ||
| ShelleyDelegationPart::Script(hash) | ||
| } else { | ||
| ShelleyDelegationPart::Key(hash) | ||
| }; | ||
| let address = ShelleyAddress::new(network, payment, delegation); | ||
| // This conversion can only fail if the delegation part isn't key or script, but we know | ||
| // it is valid because we construct it just above. | ||
| let address = address.try_into().expect("Unexpected delegation part"); | ||
| Self(address) | ||
| } | ||
|
|
||
| /// Creates `StakeAddress` from `StakeCredential`. | ||
| #[must_use] | ||
| pub fn from_stake_cred(network: Network, cred: &conway::StakeCredential) -> Self { | ||
| match cred { | ||
| conway::StakeCredential::Scripthash(h) => Self::new(network, true, *h), | ||
| conway::StakeCredential::AddrKeyhash(h) => Self::new(network, false, *h), | ||
| } | ||
| } | ||
|
|
||
| /// Returns true if it is a script address. | ||
| #[must_use] | ||
| pub fn is_script(&self) -> bool { | ||
| self.0.is_script() | ||
| } | ||
| } | ||
|
|
||
| impl From<PallasStakeAddress> for StakeAddress { | ||
| fn from(value: PallasStakeAddress) -> Self { | ||
| Self(value) | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<ShelleyAddress> for StakeAddress { | ||
| type Error = anyhow::Error; | ||
|
|
||
| fn try_from(value: ShelleyAddress) -> Result<Self, Self::Error> { | ||
| let address = PallasStakeAddress::try_from(value.clone()) | ||
| .with_context(|| format!("Unable to get stake address from {value:?}"))?; | ||
| Ok(Self(address)) | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<&[u8]> for StakeAddress { | ||
| type Error = anyhow::Error; | ||
|
|
||
| fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { | ||
| /// A stake address length in bytes. | ||
| const ADDRESS_LENGTH: usize = 29; | ||
| /// A hash length in bytes. | ||
| const HASH_LENGTH: usize = 28; | ||
|
|
||
| let (header, hash) = match bytes { | ||
| [header, hash @ ..] if hash.len() == HASH_LENGTH => (header, Hash::<28>::from(hash)), | ||
| _ => { | ||
| return Err(anyhow!( | ||
| "Invalid bytes length: {}, expected {ADDRESS_LENGTH}", | ||
| bytes.len() | ||
| )); | ||
| }, | ||
| }; | ||
|
|
||
| // The network part stored in the last four bits of the header. | ||
| let network = match header & 0b0000_1111 { | ||
| 0 => Network::Preprod, | ||
| 1 => Network::Mainnet, | ||
| v => return Err(anyhow!("Unexpected network value: {v}, header = {header}")), | ||
| }; | ||
|
|
||
| // The 'type' (stake or script) is stored in the first four bits of the header. | ||
| let type_ = header >> 4; | ||
| let is_script = match type_ { | ||
| 0b1110 => false, | ||
| 0b1111 => true, | ||
| v => return Err(anyhow!("Unexpected type value: {v}, header = {header}")), | ||
| }; | ||
|
|
||
| Ok(Self::new(network, is_script, hash)) | ||
| } | ||
| } | ||
|
|
||
| /// This conversion returns a 29 bytes value that includes both header and hash. | ||
| impl From<StakeAddress> for Vec<u8> { | ||
| fn from(value: StakeAddress) -> Self { | ||
| value.0.to_vec() | ||
| } | ||
| } | ||
|
|
||
| impl Display for StakeAddress { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| // The `to_bech32` implementation returns an error if the network isn't equal to testnet | ||
| // or mainnet. We don't allow other networks, so it is safe to unwrap, but just in case | ||
| // return a debug representation. | ||
| let bech32 = self | ||
| .0 | ||
| .to_bech32() | ||
| .unwrap_or_else(|_| format!("{:?}", self.0)); | ||
| write!(f, "{bech32}") | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[allow(clippy::indexing_slicing)] | ||
| #[test] | ||
| fn roundtrip() { | ||
| let hash: Hash<28> = "276fd18711931e2c0e21430192dbeac0e458093cd9d1fcd7210f64b3" | ||
| .parse() | ||
| .unwrap(); | ||
| let test_data = [ | ||
| (Network::Mainnet, true, hash, 0b1111_0001), | ||
| (Network::Mainnet, false, hash, 0b1110_0001), | ||
| (Network::Preprod, true, hash, 0b1111_0000), | ||
| (Network::Preprod, false, hash, 0b1110_0000), | ||
| (Network::Preview, true, hash, 0b1111_0000), | ||
| (Network::Preview, false, hash, 0b1110_0000), | ||
| ]; | ||
|
|
||
| for (network, is_script, hash, expected_header) in test_data { | ||
| let stake_address = StakeAddress::new(network, is_script, hash); | ||
| assert_eq!(stake_address.is_script(), is_script); | ||
|
|
||
| // Check that conversion to bytes includes the expected header value. | ||
| let bytes: Vec<_> = stake_address.clone().into(); | ||
| assert_eq!(29, bytes.len(), "Invalid length for {network} {is_script}"); | ||
| assert_eq!( | ||
| &bytes[1..], | ||
| hash.as_ref(), | ||
| "Invalid hash for {network} {is_script}" | ||
| ); | ||
| assert_eq!( | ||
| expected_header, | ||
| *bytes.first().unwrap(), | ||
| "Invalid header for {network} {is_script}" | ||
| ); | ||
|
|
||
| // Check that it is possible to create an address from the bytes. | ||
| let from_bytes = StakeAddress::try_from(bytes.as_slice()).unwrap(); | ||
| assert_eq!(from_bytes.is_script(), is_script); | ||
| assert_eq!(from_bytes, stake_address); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn display() { | ||
| let hash: Hash<28> = "276fd18711931e2c0e21430192dbeac0e458093cd9d1fcd7210f64b3" | ||
| .parse() | ||
| .unwrap(); | ||
|
|
||
| // cSpell:disable | ||
| let test_data = [ | ||
| ( | ||
| Network::Mainnet, | ||
| true, | ||
| hash, | ||
| "stake17ynkl5v8zxf3utqwy9psrykmatqwgkqf8nvarlxhyy8kfvcpxcgqv", | ||
| ), | ||
| ( | ||
| Network::Mainnet, | ||
| false, | ||
| hash, | ||
| "stake1uynkl5v8zxf3utqwy9psrykmatqwgkqf8nvarlxhyy8kfvcgwyghv", | ||
| ), | ||
| ( | ||
| Network::Preprod, | ||
| true, | ||
| hash, | ||
| "stake_test17qnkl5v8zxf3utqwy9psrykmatqwgkqf8nvarlxhyy8kfvcxvj2y3", | ||
| ), | ||
| ( | ||
| Network::Preprod, | ||
| false, | ||
| hash, | ||
| "stake_test1uqnkl5v8zxf3utqwy9psrykmatqwgkqf8nvarlxhyy8kfvc0yw2n3", | ||
| ), | ||
| ( | ||
| Network::Preview, | ||
| true, | ||
| hash, | ||
| "stake_test17qnkl5v8zxf3utqwy9psrykmatqwgkqf8nvarlxhyy8kfvcxvj2y3", | ||
| ), | ||
| ( | ||
| Network::Preview, | ||
| false, | ||
| hash, | ||
| "stake_test1uqnkl5v8zxf3utqwy9psrykmatqwgkqf8nvarlxhyy8kfvc0yw2n3", | ||
| ), | ||
| ]; | ||
| // cSpell:enable | ||
|
|
||
| for (network, is_script, hash, expected) in test_data { | ||
| let address = StakeAddress::new(network, is_script, hash); | ||
| assert_eq!(expected, format!("{address}")); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.