From 50a8245edbb945affe7ac8e0fd354b83cb4890e6 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Wed, 18 Sep 2024 23:02:19 +1000 Subject: [PATCH] Add Address::from_str --- soroban-sdk/src/address.rs | 12 ++++++++++++ soroban-sdk/src/tests/address.rs | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/soroban-sdk/src/address.rs b/soroban-sdk/src/address.rs index 46196c7f5..dfed7f87a 100644 --- a/soroban-sdk/src/address.rs +++ b/soroban-sdk/src/address.rs @@ -223,6 +223,18 @@ impl Address { self.env.require_auth(self); } + /// Creates an `Address` corresponding to the provided Stellar strkey. + /// + /// The only supported strkey types are account keys (`G...`) and contract keys (`C...`). Any + /// other valid or invalid strkey will cause this to panic. + /// + /// Prefer using the `Address` directly as input or output argument. Only + /// use this in special cases when addresses need to be shared between + /// different environments (e.g. different chains). + pub fn from_str(env: &Env, strkey: &str) -> Address { + Address::from_string(&String::from_str(env, strkey)) + } + /// Creates an `Address` corresponding to the provided Stellar strkey. /// /// The only supported strkey types are account keys (`G...`) and contract keys (`C...`). Any diff --git a/soroban-sdk/src/tests/address.rs b/soroban-sdk/src/tests/address.rs index 196112cd5..82008392b 100644 --- a/soroban-sdk/src/tests/address.rs +++ b/soroban-sdk/src/tests/address.rs @@ -1,5 +1,18 @@ use crate::{Address, Bytes, Env, String, TryIntoVal}; +#[test] +fn test_account_address_str_conversions() { + let env = Env::default(); + + let strkey = "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ"; + + let address = Address::from_str(&env, &strkey); + assert_eq!( + address.to_string().to_string(), + "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ" + ); +} + #[test] fn test_account_address_conversions() { let env = Env::default();