-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interchain-token-service): encode source/recipient addresses as …
…strings instead of XDR (#147) Co-authored-by: Milap Sheth <[email protected]>
- Loading branch information
1 parent
1a5876d
commit 2b3ca63
Showing
8 changed files
with
105 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
2 changes: 1 addition & 1 deletion
2
...racts/interchain-token-service/tests/testdata/interchain_transfer_execute_succeeds.golden
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAXI7N) | ||
topics: (Symbol(executed), String(ethereum), String(test), Bytes(0, 0, 0, 18, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12), BytesN<32>(19, 164, 112, 128, 76, 247, 2, 236, 196, 20, 87, 206, 58, 2, 208, 102, 225, 238, 175, 151, 45, 250, 47, 204, 76, 253, 158, 193, 68, 73, 208, 149), Contract(CBQXZFUKGANX4DCFGMAEZNEIZ6KEN5RRIUPP25SRMZ6WGLVGFAFT5XSD), 1000) | ||
topics: (Symbol(executed), String(ethereum), String(test), Bytes(67, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 89, 82, 69, 53), BytesN<32>(19, 164, 112, 128, 76, 247, 2, 236, 196, 20, 87, 206, 58, 2, 208, 102, 225, 238, 175, 151, 45, 250, 47, 204, 76, 253, 158, 193, 68, 73, 208, 149), Contract(CBQXZFUKGANX4DCFGMAEZNEIZ6KEN5RRIUPP25SRMZ6WGLVGFAFT5XSD), 1000) | ||
data: (Bytes(222, 173)) |
2 changes: 1 addition & 1 deletion
2
...terchain-token-service/tests/testdata/interchain_transfer_message_execute_succeeds.golden
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVAX5) | ||
topics: (Symbol(interchain_transfer_received), String(ethereum), BytesN<32>(19, 164, 112, 128, 76, 247, 2, 236, 196, 20, 87, 206, 58, 2, 208, 102, 225, 238, 175, 151, 45, 250, 47, 204, 76, 253, 158, 193, 68, 73, 208, 149), Bytes(0, 0, 0, 18, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYRE5), 1000) | ||
topics: (Symbol(interchain_transfer_received), String(ethereum), BytesN<32>(19, 164, 112, 128, 76, 247, 2, 236, 196, 20, 87, 206, 58, 2, 208, 102, 225, 238, 175, 151, 45, 250, 47, 204, 76, 253, 158, 193, 68, 73, 208, 149), Bytes(67, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 88, 73, 55, 78), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYRE5), 1000) | ||
data: (None) |
This file contains 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 |
---|---|---|
@@ -1,16 +1,94 @@ | ||
use soroban_sdk::{Address, Env, String}; | ||
use soroban_sdk::{Address, Bytes, Env, String}; | ||
|
||
const ZERO_ADDRESS: &str = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"; | ||
const STELLAR_ADDRESS_LEN: usize = ZERO_ADDRESS.len(); | ||
|
||
pub trait AddressExt { | ||
fn zero(env: &Env) -> Address; | ||
|
||
fn to_string_bytes(&self) -> Bytes; | ||
} | ||
|
||
impl AddressExt for Address { | ||
/// Returns Stellar's "dead" address, represented by the constant `ZERO_ADDRESS`. | ||
/// # Reference | ||
/// - Stellar [GitHub](https://github.com/stellar/js-stellar-base/blob/master/test/unit/address_test.js) | ||
/// Returns Stellar's ["dead"](https://github.com/stellar/js-stellar-base/blob/master/test/unit/address_test.js) address, represented by the constant `ZERO_ADDRESS`. | ||
fn zero(env: &Env) -> Address { | ||
Self::from_string(&String::from_str(env, ZERO_ADDRESS)) | ||
} | ||
|
||
/// Converts Stellar address to a string represented as bytes | ||
fn to_string_bytes(&self) -> Bytes { | ||
let mut address_string_bytes = [0u8; STELLAR_ADDRESS_LEN]; | ||
self.to_string().copy_into_slice(&mut address_string_bytes); | ||
Bytes::from_slice(self.env(), &address_string_bytes) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use soroban_sdk::testutils::Address as _; | ||
use soroban_sdk::{Address, Bytes, Env, String}; | ||
|
||
use super::{AddressExt, ZERO_ADDRESS}; | ||
|
||
#[test] | ||
fn zero_address_to_string() { | ||
let env = &Env::default(); | ||
assert_eq!( | ||
Address::zero(env).to_string(), | ||
String::from_str(env, ZERO_ADDRESS) | ||
); | ||
} | ||
|
||
#[test] | ||
fn string_to_address_to_string() { | ||
let env = &Env::default(); | ||
let cases = [ | ||
ZERO_ADDRESS, | ||
"GC7OHFPWPSWXL4HMN6TXAG54MTZSMJIASWHO6KVRQNHNCXEAHWDSGGC3", | ||
"CB6743BTQ2TZHTUCRSUFAH2X5ICOZGI6I3UCQBY3VFSSJ7IERGXUM7TX", | ||
] | ||
.into_iter() | ||
.map(|s| Bytes::from_slice(env, s.as_bytes())); | ||
|
||
for address_bytes in cases { | ||
let address = Address::from_string_bytes(&address_bytes); | ||
assert_eq!(address.to_string_bytes(), address_bytes); | ||
} | ||
} | ||
|
||
#[test] | ||
fn address_to_string_to_address() { | ||
let env = &Env::default(); | ||
|
||
let cases = [ | ||
Address::zero(env), | ||
Address::generate(env), | ||
env.register_stellar_asset_contract_v2(Address::generate(env)) | ||
.address(), | ||
]; | ||
|
||
for address in cases { | ||
let address_bytes = address.to_string_bytes(); | ||
assert_eq!(Address::from_string_bytes(&address_bytes), address); | ||
} | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "HostError: Error(Value, InvalidInput)")] | ||
fn unsupported_muxed_address_format_fails_on_conversion() { | ||
let env = &Env::default(); | ||
|
||
let unsupported_address = | ||
"MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAACJUQ"; | ||
Address::from_string(&String::from_str(env, unsupported_address)); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "HostError: Error(Value, InvalidInput)")] | ||
fn unsupported_signed_payload_address_format_fails_on_conversion() { | ||
let env = &Env::default(); | ||
|
||
let unsupported_address = "PA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAOQCAQDAQCQMBYIBEFAWDANBYHRAEISCMKBKFQXDAMRUGY4DUAAAAFGBU"; | ||
Address::from_string(&String::from_str(env, unsupported_address)); | ||
} | ||
} |