Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/gem_algorand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = { workspace = true }
[features]
default = []
rpc = ["dep:chrono", "dep:chain_traits", "dep:gem_client"]
signer = ["dep:gem_hash", "dep:hex", "dep:num-traits", "dep:signer"]
signer = ["dep:hex", "dep:num-traits"]
reqwest = ["gem_client/reqwest"]
chain_integration_tests = ["rpc", "reqwest", "settings/testkit"]

Expand All @@ -23,9 +23,9 @@ chrono = { workspace = true, optional = true }
primitives = { path = "../primitives" }
chain_traits = { path = "../chain_traits", optional = true }
gem_client = { path = "../gem_client", optional = true }
gem_hash = { path = "../gem_hash", optional = true }
gem_hash = { path = "../gem_hash" }
hex = { workspace = true, optional = true }
signer = { path = "../signer", optional = true }
signer = { path = "../signer" }

[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt"] }
Expand Down
22 changes: 15 additions & 7 deletions crates/gem_algorand/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl Address for AlgorandAddress {
}
}

pub fn validate_address(address: &str) -> bool {
AlgorandAddress::is_valid(address)
}

impl AlgorandAddress {
fn checksum(bytes: &[u8; 32]) -> [u8; ADDRESS_CHECKSUM_LENGTH] {
let digest = sha512_256(bytes);
Expand All @@ -53,15 +57,19 @@ impl fmt::Display for AlgorandAddress {
mod tests {
use super::*;

const VALID_ADDRESS: &str = "QKDS2YGDHDFZFAAGA4HAF3AJIKW5ZN46P66QDR3ELCXKKJUJTPJSXVHNQU";

#[test]
fn test_algorand_address() {
assert!(AlgorandAddress::is_valid("QKDS2YGDHDFZFAAGA4HAF3AJIKW5ZN46P66QDR3ELCXKKJUJTPJSXVHNQU"));
assert!(!AlgorandAddress::is_valid(""));
assert!(!AlgorandAddress::is_valid("invalid"));
assert!(!AlgorandAddress::is_valid("QKDS2YGDHDFZFAAGA4HAF3AJIKW5ZN46P66QDR3ELCXKKJUJTPJSXVHNQX"));
let parsed = AlgorandAddress::from_str(VALID_ADDRESS).unwrap();

assert!(validate_address(VALID_ADDRESS));
assert_eq!(parsed.to_string(), VALID_ADDRESS);
assert_eq!(parsed.as_bytes().len(), 32);

let addr = AlgorandAddress::from_str("QKDS2YGDHDFZFAAGA4HAF3AJIKW5ZN46P66QDR3ELCXKKJUJTPJSXVHNQU").unwrap();
assert_eq!(addr.to_string(), "QKDS2YGDHDFZFAAGA4HAF3AJIKW5ZN46P66QDR3ELCXKKJUJTPJSXVHNQU");
assert_eq!(addr.as_bytes().len(), 32);
assert!(!validate_address(""));
assert!(!validate_address("invalid"));
// wrong checksum (last char flipped)
assert!(!validate_address("QKDS2YGDHDFZFAAGA4HAF3AJIKW5ZN46P66QDR3ELCXKKJUJTPJSXVHNQX"));
}
}
4 changes: 1 addition & 3 deletions crates/gem_algorand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ pub mod rpc;
#[cfg(feature = "rpc")]
pub mod provider;

#[cfg(feature = "signer")]
pub mod address;
pub mod constants;
pub mod models;
#[cfg(feature = "signer")]
pub mod signer;

#[cfg(feature = "signer")]
pub use address::AlgorandAddress;
pub use address::{AlgorandAddress, validate_address};
#[cfg(feature = "rpc")]
pub use rpc::client::AlgorandClient;
#[cfg(feature = "signer")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use primitives::{SignerError, decode_hex};
use primitives::{Address as AddressTrait, SignerError, decode_hex};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
Expand Down Expand Up @@ -39,6 +39,24 @@ impl FromStr for AccountAddress {
}
}

impl AddressTrait for AccountAddress {
fn try_parse(address: &str) -> Option<Self> {
Self::from_hex(address).ok()
}

fn as_bytes(&self) -> &[u8] {
&self.0
}

fn encode(&self) -> String {
self.to_string()
}
}

pub fn validate_address(address: &str) -> bool {
AccountAddress::is_valid(address)
}

impl fmt::Display for AccountAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{}", ::hex::encode(self.0))
Expand All @@ -49,9 +67,19 @@ impl fmt::Display for AccountAddress {
mod tests {
use super::*;

const VALID_ADDRESS: &str = "0x6467997d9c3a5bc9f714e17a168984595ce9bec7350645713a1fe7983a7f5fcc";

#[test]
fn parse_address_short_hex() {
let address = AccountAddress::from_hex("0x1").unwrap();
assert_eq!(address.to_string(), format!("0x{}", "00".repeat(31) + "01"));
fn test_aptos_address() {
let parsed = AccountAddress::from_hex(VALID_ADDRESS).unwrap();

assert!(validate_address(VALID_ADDRESS));
assert_eq!(parsed.to_string(), VALID_ADDRESS);
assert_eq!(parsed.as_bytes().len(), 32);
assert!(!validate_address("invalid"));

// short hex is left-padded to 32 bytes (Aptos framework address convention)
let short = AccountAddress::from_hex("0x1").unwrap();
assert_eq!(short.to_string(), format!("0x{}", "00".repeat(31) + "01"));
}
}
2 changes: 2 additions & 0 deletions crates/gem_aptos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod constants;
pub use constants::*;
pub mod address;
pub use address::{AccountAddress, validate_address};
pub mod models;
pub use models::*;
pub mod r#move;
Expand Down
2 changes: 1 addition & 1 deletion crates/gem_aptos/src/models/signer_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::AccountAddress;
use crate::r#move::{EntryFunction, TypeTag};
use crate::signer::AccountAddress;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/gem_aptos/src/move/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::signer::AccountAddress;
use crate::AccountAddress;
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use primitives::{SignerError, decode_hex};
Expand Down
2 changes: 1 addition & 1 deletion crates/gem_aptos/src/move/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::signer::AccountAddress;
use crate::AccountAddress;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/gem_aptos/src/move/values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::signer::AccountAddress;
use crate::AccountAddress;
use serde::Serialize;
use serde::ser::{SerializeSeq, Serializer};

Expand Down
4 changes: 2 additions & 2 deletions crates/gem_aptos/src/signer/chain_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::str::from_utf8;

use super::abi::{PANORA_ROUTER_ENTRY_PARAMS, PANORA_ROUTER_FUNCTION, PANORA_ROUTER_MODULE};
use super::{
AccountAddress, EntryFunction, EntryFunctionPayload, build_raw_transaction, build_submit_transaction_bcs, expiration_timestamp_secs, sign_message as sign_aptos_message,
sign_raw_transaction,
EntryFunction, EntryFunctionPayload, build_raw_transaction, build_submit_transaction_bcs, expiration_timestamp_secs, sign_message as sign_aptos_message, sign_raw_transaction,
};
use crate::AccountAddress;
use crate::token_id::is_fungible_asset_token_id;
use crate::{APTOS_TRANSFER_FUNCTION, DELEGATION_POOL_ADD_STAKE_FUNCTION, DELEGATION_POOL_UNLOCK_FUNCTION, DELEGATION_POOL_WITHDRAW_FUNCTION, ENTRY_FUNCTION_PAYLOAD_TYPE};

Expand Down
2 changes: 0 additions & 2 deletions crates/gem_aptos/src/signer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod abi;
mod address;
mod chain_signer;
mod payload;
mod transaction;

pub use crate::models::{DeprecatedPayload, RawTransaction, Script, TransactionPayloadBCS};
pub use crate::r#move::{EntryFunction, ModuleId, StructTag, TypeTag};
pub use address::AccountAddress;
pub use chain_signer::AptosChainSigner;
pub use payload::EntryFunctionPayload;
pub use transaction::{build_raw_transaction, build_submit_transaction_bcs, expiration_timestamp_secs, sign_message, sign_raw_transaction};
3 changes: 2 additions & 1 deletion crates/gem_aptos/src/signer/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use primitives::SignerError;
use signer::Ed25519KeyPair;
use std::time::SystemTime;

use super::{AccountAddress, EntryFunction};
use super::EntryFunction;
use crate::AccountAddress;

const RAW_TRANSACTION_SALT: &[u8] = b"APTOS::RawTransaction";
const MESSAGE_SALT: &[u8] = b"APTOS::Message";
Expand Down
86 changes: 86 additions & 0 deletions crates/gem_cosmos/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use bech32::hrp::Hrp;
use primitives::chain_cosmos::CosmosChain;
use primitives::{Address as AddressTrait, Chain};
use std::error::Error;

pub struct CosmosAddress {
hrp: String,
bytes: Vec<u8>,
}

impl CosmosAddress {
fn has_chain_hrp(address: &str, chain: Chain) -> bool {
let Some(cosmos_chain) = CosmosChain::from_chain(chain) else {
return false;
};
Self::try_parse(address).is_some_and(|address| address.hrp == cosmos_chain.hrp())
}

pub fn is_valid_for_chain(address: &str, chain: Chain) -> bool {
Self::has_chain_hrp(address, chain)
}

pub fn convert(address: &str, hrp: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
let (_, decoded) = bech32::decode(address)?;
let new_hrp = Hrp::parse(hrp)?;
let encoded = bech32::encode::<bech32::Bech32>(new_hrp, decoded.as_slice())?;

Ok(encoded)
}
}

impl AddressTrait for CosmosAddress {
fn try_parse(address: &str) -> Option<Self> {
let (hrp, bytes) = bech32::decode(address).ok()?;
let hrp = hrp.as_str().to_string();
(CosmosChain::all().any(|chain| chain.hrp() == hrp) && bytes.len() == 20).then_some(Self { hrp, bytes })
}

fn as_bytes(&self) -> &[u8] {
&self.bytes
}

fn encode(&self) -> String {
let hrp = Hrp::parse(&self.hrp).expect("valid Cosmos address hrp");
bech32::encode::<bech32::Bech32>(hrp, &self.bytes).expect("valid Cosmos address bytes")
}
}

pub fn validate_address(address: &str, chain: Chain) -> bool {
CosmosAddress::is_valid_for_chain(address, chain)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_cosmos_address_convert() {
let cosmos_address = "cosmos1h3laqcrmul79zwtw6j63ncsl0adfj07wgupylj";
let expected = "osmosis1h3laqcrmul79zwtw6j63ncsl0adfj07wm8vf00";

let output_address = CosmosAddress::convert(cosmos_address, "osmosis").unwrap();
assert_eq!(expected, output_address);
}

#[test]
fn test_invalid_cosmos_address() {
// invalid checksum
let cosmos_address = "cosmos1h3laqcrmul79zwtw6j63ncsl0adfj07wgu";

let result = CosmosAddress::convert(cosmos_address, "osmosis");
assert!(result.is_err());
}

#[test]
fn test_cosmos_address() {
let address = "cosmos1h3laqcrmul79zwtw6j63ncsl0adfj07wgupylj";
let parsed = CosmosAddress::try_parse(address).unwrap();

assert!(validate_address(address, Chain::Cosmos));
assert_eq!(parsed.as_bytes().len(), 20);
assert_eq!(parsed.encode(), address);
assert!(!validate_address(address, Chain::Osmosis));
assert!(!validate_address("invalid", Chain::Cosmos));
}
}
34 changes: 0 additions & 34 deletions crates/gem_cosmos/src/converter.rs

This file was deleted.

4 changes: 3 additions & 1 deletion crates/gem_cosmos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod address;
pub mod constants;
pub mod converter;

#[cfg(feature = "rpc")]
pub mod rpc;
Expand All @@ -11,3 +11,5 @@ pub mod provider;
pub mod signer;

pub mod models;

pub use address::validate_address;
Loading
Loading