Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 2842b10

Browse files
authored
add support for gateway adddresses in rpcs (#390)
1 parent ab85ca1 commit 2842b10

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

Diff for: gateway-crypto/src/no_std.rs

+11
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ pub fn eth_hash_string(eth_hash: &[u8; 32]) -> String {
199199
format!("0x{}", hex::encode(eth_hash))
200200
}
201201

202+
pub fn gateway_str_to_address(address_str: &str) -> Option<[u8; 32]> {
203+
if address_str.len() == 66 && &address_str[0..2] == "0x" {
204+
if let Ok(bytes) = hex::decode(&address_str[2..66]) {
205+
if let Ok(hex_address) = bytes.try_into() {
206+
return Some(hex_address);
207+
}
208+
}
209+
}
210+
return None;
211+
}
212+
202213
#[cfg(test)]
203214
mod test {
204215
use super::*;

Diff for: node/src/api.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ where
362362
.get_accounts(&at)
363363
.map_err(runtime_err)?
364364
.map_err(chain_err)?;
365-
Ok(accounts) // XXX try_into?
365+
366+
Ok(accounts)
366367
}
367368

368369
fn chain_assets_meta(&self, at: Option<<B as BlockT>::Hash>) -> RpcResult<ApiAssetMeta> {

Diff for: pallets/cash/src/chains.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl FromStr for ChainAccount {
131131
impl From<ChainAccount> for String {
132132
fn from(asset: ChainAccount) -> String {
133133
match asset {
134-
ChainAccount::Gate(_) => String::from("GATE"), // XXX
134+
ChainAccount::Gate(address) => format!("GATE:0x{}", hex::encode(address)),
135135
ChainAccount::Eth(address) => format!("ETH:0x{}", hex::encode(address)),
136136
ChainAccount::Dot(_) => String::from("DOT"), // XXX
137137
}
@@ -338,6 +338,7 @@ impl FromStr for ChainId {
338338
match s.to_ascii_uppercase().as_str() {
339339
"ETH" => Ok(ChainId::Eth),
340340
"DOT" => Ok(ChainId::Dot),
341+
"GATE" => Ok(ChainId::Gate),
341342
_ => Err(Reason::BadChainId),
342343
}
343344
}
@@ -809,7 +810,10 @@ impl Chain for Gateway {
809810
}
810811

811812
fn str_to_address(_addr: &str) -> Result<Self::Address, Reason> {
812-
panic!("XXX not implemented");
813+
match gateway_crypto::gateway_str_to_address(_addr) {
814+
Some(s) => Ok(s),
815+
None => Err(Reason::BadAddress),
816+
}
813817
}
814818

815819
fn address_string(_address: &Self::Address) -> String {

Diff for: pallets/cash/src/core.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,19 @@ mod tests {
410410
fn test_get_accounts() -> Result<(), Reason> {
411411
let jared = ChainAccount::from_str("Eth:0x18c8F1222083997405F2E482338A4650ac02e1d6")?;
412412
let geoff = ChainAccount::from_str("Eth:0x8169522c2c57883e8ef80c498aab7820da539806")?;
413+
let alice_bytes: [u8; 32] = [6; 32];
414+
let alice = ChainAccount::Gate(alice_bytes);
415+
413416
new_test_ext().execute_with(|| {
414417
AssetsWithNonZeroBalance::insert(&jared, &Uni, ());
415418
AssetsWithNonZeroBalance::insert(&jared, &Wbtc, ());
416419

417420
// geoff only cash
418421
CashPrincipals::insert(&geoff, CashPrincipal::from_nominal("1"));
422+
CashPrincipals::insert(&alice, CashPrincipal::from_nominal("1"));
419423

420424
let accounts: Vec<ChainAccount> = super::get_accounts::<Test>()?;
421-
assert_eq!(accounts, vec![jared, geoff]);
425+
assert_eq!(accounts, vec![alice, jared, geoff]);
422426

423427
Ok(())
424428
})

Diff for: pallets/cash/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl<T: Config> Module<T> {
840840
Ok(internal::assets::get_assets::<T>()?)
841841
}
842842

843-
/// Get the rates for the given asset.
843+
/// Get the a list of all chain accounts
844844
pub fn get_accounts() -> Result<Vec<ChainAccount>, Reason> {
845845
Ok(core::get_accounts::<T>()?)
846846
}

Diff for: pallets/cash/src/trx_req.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ mod tests {
3030
use super::*;
3131
use trx_request;
3232

33-
const ALAN: [u8; 20] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
34-
const ETH: [u8; 20] = [
35-
238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238,
36-
238, 238,
37-
];
33+
const ALAN: [u8; 20] = [1; 20];
34+
const ETH: [u8; 20] = [238; 20];
3835

3936
#[test]
4037
fn test_account_to_chain_account() {

0 commit comments

Comments
 (0)