|
| 1 | +//! Utility functions for CIP-0134 address. |
| 2 | +
|
| 3 | +// Ignore URIs that are used in tests and doc-examples. |
| 4 | +// cSpell:ignoreRegExp web\+cardano:.+ |
| 5 | + |
| 6 | +use std::fmt::{Display, Formatter}; |
| 7 | + |
| 8 | +use anyhow::{anyhow, Context, Result}; |
| 9 | +use pallas::ledger::addresses::Address; |
| 10 | + |
| 11 | +/// An URI in the CIP-0134 format. |
| 12 | +/// |
| 13 | +/// See the [proposal] for more details. |
| 14 | +/// |
| 15 | +/// [proposal]: https://github.com/cardano-foundation/CIPs/pull/888 |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct Cip0134Uri { |
| 18 | + /// A URI string. |
| 19 | + uri: String, |
| 20 | + /// An address parsed from the URI. |
| 21 | + address: Address, |
| 22 | +} |
| 23 | + |
| 24 | +impl Cip0134Uri { |
| 25 | + /// Creates a new `Cip0134Uri` instance by parsing the given URI. |
| 26 | + /// |
| 27 | + /// # Errors |
| 28 | + /// - Invalid URI. |
| 29 | + /// |
| 30 | + /// # Examples |
| 31 | + /// |
| 32 | + /// ``` |
| 33 | + /// use rbac_registration::cardano::cip509::utils::Cip0134Uri; |
| 34 | + /// |
| 35 | + /// let uri = "web+cardano://addr/stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw"; |
| 36 | + /// let cip0134_uri = Cip0134Uri::parse(uri).unwrap(); |
| 37 | + /// ``` |
| 38 | + pub fn parse(uri: &str) -> Result<Self> { |
| 39 | + let bech32 = uri |
| 40 | + .strip_prefix("web+cardano://addr/") |
| 41 | + .ok_or_else(|| anyhow!("Missing schema part of URI"))?; |
| 42 | + let address = Address::from_bech32(bech32).context("Unable to parse bech32 part of URI")?; |
| 43 | + |
| 44 | + Ok(Self { |
| 45 | + uri: uri.to_owned(), |
| 46 | + address, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + /// Returns a URI string. |
| 51 | + /// |
| 52 | + /// # Examples |
| 53 | + /// |
| 54 | + /// ``` |
| 55 | + /// use rbac_registration::cardano::cip509::utils::Cip0134Uri; |
| 56 | + /// |
| 57 | + /// let uri = "web+cardano://addr/stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw"; |
| 58 | + /// let cip0134_uri = Cip0134Uri::parse(uri).unwrap(); |
| 59 | + /// assert_eq!(cip0134_uri.uri(), uri); |
| 60 | + #[must_use] |
| 61 | + pub fn uri(&self) -> &str { |
| 62 | + &self.uri |
| 63 | + } |
| 64 | + |
| 65 | + /// Returns a URI string. |
| 66 | + /// |
| 67 | + /// # Examples |
| 68 | + /// |
| 69 | + /// ``` |
| 70 | + /// use pallas::ledger::addresses::{Address, Network}; |
| 71 | + /// use rbac_registration::cardano::cip509::utils::Cip0134Uri; |
| 72 | + /// |
| 73 | + /// let uri = "web+cardano://addr/stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw"; |
| 74 | + /// let cip0134_uri = Cip0134Uri::parse(uri).unwrap(); |
| 75 | + /// let Address::Stake(address) = cip0134_uri.address() else { |
| 76 | + /// panic!("Unexpected address type"); |
| 77 | + /// }; |
| 78 | + /// assert_eq!(address.network(), Network::Mainnet); |
| 79 | + #[must_use] |
| 80 | + pub fn address(&self) -> &Address { |
| 81 | + &self.address |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl Display for Cip0134Uri { |
| 86 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 87 | + write!(f, "{}", self.uri()) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use pallas::ledger::addresses::{Address, Network}; |
| 94 | + |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn invalid_prefix() { |
| 99 | + // cSpell:disable |
| 100 | + let test_uris = [ |
| 101 | + "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x", |
| 102 | + "//addr/addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x", |
| 103 | + "web+cardano:/addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x", |
| 104 | + "somthing+unexpected://addr/addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x", |
| 105 | + ]; |
| 106 | + // cSpell:enable |
| 107 | + |
| 108 | + for uri in test_uris { |
| 109 | + let err = format!("{:?}", Cip0134Uri::parse(uri).expect_err(uri)); |
| 110 | + assert!(err.starts_with("Missing schema part of URI")); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn invalid_bech32() { |
| 116 | + let uri = "web+cardano://addr/adr1qx2fxv2umyh"; |
| 117 | + let err = format!("{:?}", Cip0134Uri::parse(uri).unwrap_err()); |
| 118 | + assert!(err.starts_with("Unable to parse bech32 part of URI")); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn stake_address() { |
| 123 | + let test_data = [ |
| 124 | + ( |
| 125 | + "web+cardano://addr/stake_test1uqehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gssrtvn", |
| 126 | + Network::Testnet, |
| 127 | + "337b62cfff6403a06a3acbc34f8c46003c69fe79a3628cefa9c47251", |
| 128 | + ), |
| 129 | + ( |
| 130 | + "web+cardano://addr/stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw", |
| 131 | + Network::Mainnet, |
| 132 | + "337b62cfff6403a06a3acbc34f8c46003c69fe79a3628cefa9c47251", |
| 133 | + ), |
| 134 | + ( |
| 135 | + "web+cardano://addr/drep_vk17axh4sc9zwkpsft3tlgpjemfwc0u5mnld80r85zw7zdqcst6w54sdv4a4e", |
| 136 | + Network::Other(7), |
| 137 | + "4d7ac30513ac1825715fd0196769761fca6e7f69de33d04ef09a0c41", |
| 138 | + ) |
| 139 | + ]; |
| 140 | + |
| 141 | + for (uri, network, payload) in test_data { |
| 142 | + let cip0134_uri = Cip0134Uri::parse(uri).expect(uri); |
| 143 | + let Address::Stake(address) = cip0134_uri.address() else { |
| 144 | + panic!("Unexpected address type ({uri})"); |
| 145 | + }; |
| 146 | + assert_eq!(network, address.network()); |
| 147 | + assert_eq!(payload, address.payload().as_hash().to_string()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn shelley_address() { |
| 153 | + let test_data = [ |
| 154 | + ( |
| 155 | + "web+cardano://addr/addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x", |
| 156 | + Network::Mainnet, |
| 157 | + ), |
| 158 | + ( |
| 159 | + "web+cardano://addr/addr_test1gz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer5pnz75xxcrdw5vky", |
| 160 | + Network::Testnet, |
| 161 | + ), |
| 162 | + ( |
| 163 | + "web+cardano://addr/cc_hot_vk10y48lq72hypxraew74lwjjn9e2dscuwphckglh2nrrpkgweqk5hschnzv5", |
| 164 | + Network::Other(9), |
| 165 | + ) |
| 166 | + ]; |
| 167 | + |
| 168 | + for (uri, network) in test_data { |
| 169 | + let cip0134_uri = Cip0134Uri::parse(uri).expect(uri); |
| 170 | + let Address::Shelley(address) = cip0134_uri.address() else { |
| 171 | + panic!("Unexpected address type ({uri})"); |
| 172 | + }; |
| 173 | + assert_eq!(network, address.network()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // The Display should return the original URI. |
| 178 | + #[test] |
| 179 | + fn display() { |
| 180 | + let uri = "web+cardano://addr/stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw"; |
| 181 | + let cip0134_uri = Cip0134Uri::parse(uri).expect(uri); |
| 182 | + assert_eq!(uri, cip0134_uri.to_string()); |
| 183 | + } |
| 184 | +} |
0 commit comments