-
Notifications
You must be signed in to change notification settings - Fork 19
Quality of life features + txid in error cases + Crate updates. #32
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
Changes from all commits
ee6bb11
06035d4
2d918a1
2e4f74a
2c3af63
a9beb2e
5a520f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [package] | ||
| name = "deep_space" | ||
| version = "2.30.0" | ||
| version = "2.31.0" | ||
| authors = ["Justin Kilpatrick <[email protected]>", "Michał Papierski <[email protected]>"] | ||
| repository = "https://github.com/althea-net/deep_space" | ||
| description = "A highly portable, batteries included, transaction generation and key management library for CosmosSDK blockchains" | ||
|
|
@@ -22,23 +22,23 @@ prost-types = "0.13" | |
| prost = "0.13" | ||
| pbkdf2 = {version = "0.12"} | ||
| hmac = {version = "0.12"} | ||
| rand = {version = "0.8"} | ||
| rust_decimal = "1.36" | ||
| secp256k1 = {version = "0.30", features = ["global-context"]} | ||
| rand = {version = "0.9"} | ||
| rust_decimal = "1.40" | ||
| secp256k1 = {version = "0.31", features = ["global-context"]} | ||
| tonic = {version = "0.12", features = ["gzip"]} | ||
| bytes = "1.8" | ||
| bytes = "1.11" | ||
| log = "0.4" | ||
| tokio = {version = "1", features=["time"]} | ||
| clarity = {version = "1.5", optional = true} | ||
| sha3 = {version = "0.10", optional = true} | ||
|
|
||
| cosmos-sdk-proto = {package = "cosmos-sdk-proto-althea", version = "0.20"} | ||
| althea_proto = {version="0.10", optional=true} | ||
| althea_proto = {version="0.9", optional=true} | ||
|
|
||
| [dev-dependencies] | ||
| rand = "0.8" | ||
| rand = "0.9" | ||
| env_logger = "0.11" | ||
| actix-rt = "2.10" | ||
| actix-rt = "2.11" | ||
|
|
||
|
|
||
| [features] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,6 +144,20 @@ impl Address { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Changes the `prefix` field to modify the resulting Bech32 `hrp`, returns the updated Address | ||
| pub fn re_prefix<T: Into<String>>(&self, prefix: T) -> Result<Address, AddressError> { | ||
| match self { | ||
| Address::Base(base_address) => Ok(Address::Base(BaseAddress { | ||
| bytes: base_address.bytes, | ||
| prefix: ArrayString::new(&prefix.into())?, | ||
| })), | ||
| Address::Derived(derived_address) => Ok(Address::Derived(DerivedAddress { | ||
| bytes: derived_address.bytes, | ||
| prefix: ArrayString::new(&prefix.into())?, | ||
| })), | ||
| } | ||
| } | ||
jkilpatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Returns the underlying `bytes` buffer as a slice | ||
| pub fn get_bytes(&self) -> &[u8] { | ||
| match self { | ||
|
|
@@ -167,6 +181,36 @@ impl Address { | |
| } | ||
| } | ||
|
|
||
| // Ethermint address conversion, note there's no way to determine from an address how the signers wallet is configured, if you convert Cosmos addressed | ||
| // used with a non-eth signing scheme using this method that public key goes to a completely different address and the user will never be able to retrieve | ||
| // funds sent to that address, so use caution when making assumptions. | ||
| #[cfg(feature = "ethermint")] | ||
| impl TryInto<EthAddress> for &Address { | ||
| type Error = clarity::error::Error; | ||
|
|
||
| fn try_into(self) -> Result<EthAddress, Self::Error> { | ||
| EthAddress::from_slice(self.get_bytes()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "ethermint")] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we even have this implemented if it's just going to use the default prefix? It seems like a footgun to me.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should probably just be a function with a prefix argument. Remembering to re-prefix when using it will be a pain. |
||
| impl From<EthAddress> for Address { | ||
| fn from(value: EthAddress) -> Self { | ||
| Address::from_slice(value.as_bytes(), DEFAULT_PREFIX).unwrap() | ||
jkilpatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "ethermint")] | ||
| impl Address { | ||
| /// From EthAddress with prefix | ||
| pub fn from_eth_address_with_prefix<T: Into<String>>( | ||
| address: EthAddress, | ||
| prefix: T, | ||
| ) -> Result<Self, AddressError> { | ||
| Address::from_slice(address.as_bytes(), prefix) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for Address { | ||
| type Err = AddressError; | ||
|
|
||
|
|
@@ -296,3 +340,42 @@ fn test_address_conversion() { | |
| let eth_address = cosmos_address_to_eth_address(test).unwrap(); | ||
| let _cosmos_address = eth_address_to_cosmos_address(eth_address, None).unwrap(); | ||
| } | ||
|
|
||
| #[cfg(feature = "ethermint")] | ||
| #[test] | ||
| fn test_trait_conversions() { | ||
| use std::convert::TryInto; | ||
|
|
||
| // Test TryInto<EthAddress> for &Address | ||
| let test: Address = "cosmos1vlms2r8f6x7yxjh3ynyzc7ckarqd8a96ckjvrp" | ||
| .parse() | ||
| .unwrap(); | ||
| let eth_address: EthAddress = (&test).try_into().unwrap(); | ||
|
|
||
| // Test From<EthAddress> for Address | ||
| let cosmos_address: Address = eth_address.into(); | ||
|
|
||
| // Verify the roundtrip works | ||
| assert_eq!(test.get_bytes(), cosmos_address.get_bytes()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_re_prefix() { | ||
| let test: Address = "cosmos1vlms2r8f6x7yxjh3ynyzc7ckarqd8a96ckjvrp" | ||
| .parse() | ||
| .unwrap(); | ||
|
|
||
| // Test re_prefix with a different prefix | ||
| let osmosis_address = test.re_prefix("osmo").unwrap(); | ||
| assert_eq!( | ||
| osmosis_address.to_string(), | ||
| "osmo1vlms2r8f6x7yxjh3ynyzc7ckarqd8a96sdpu4n" | ||
| ); | ||
|
|
||
| // Verify the underlying bytes are the same | ||
| assert_eq!(test.get_bytes(), osmosis_address.get_bytes()); | ||
|
|
||
| // Test re_prefix with the same prefix | ||
| let cosmos_address = test.re_prefix("cosmos").unwrap(); | ||
| assert_eq!(test.to_string(), cosmos_address.to_string()); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.