Skip to content

Commit

Permalink
[Rust/Cosmos]: Move Cosmos blockchain into Rust (#3498)
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan authored Nov 16, 2023
1 parent a60033f commit 7119750
Show file tree
Hide file tree
Showing 203 changed files with 7,743 additions and 1,936 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TestCosmosTransactions {

assertEquals(
output.serialized,
"{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"CvgBCvUBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQS0gEKLWNvc21vczEzazBxMGw3bGcya3IzMmt2dDdseTIzNnBwbGR5OHY5ZHp3aDNnZBItY29zbW9zMWZzN2x1MjhoeDVtOWFrbTdycDBjMjQyMmNuOHIyZjdndXJ1amhmGnIKaAoqL2Nvc21vcy5zdGFraW5nLnYxYmV0YTEuU3Rha2VBdXRob3JpemF0aW9uEjoSNgo0Y29zbW9zdmFsb3BlcjFnanR2bHk5bGVsNnpza3Z3dHZsZzV2aHdwdTljOXdhdzdzeHp3eCABEgYI4LD6pgYSZwpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohA/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1KEgQKAggBGAUSEwoNCgV1YXRvbRIEMjQxOBCp8wUaQIFyfuijGKf87Hz61ZqxasfLI1PZnNge4RDq/tRyB/tZI6p80iGRqHecoV6+84EQkc9GTlNRQOSlApRCsivT9XI=\"}"
"{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"CvgBCvUBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQS0gEKLWNvc21vczEzazBxMGw3bGcya3IzMmt2dDdseTIzNnBwbGR5OHY5ZHp3aDNnZBItY29zbW9zMWZzN2x1MjhoeDVtOWFrbTdycDBjMjQyMmNuOHIyZjdndXJ1amhmGnIKaAoqL2Nvc21vcy5zdGFraW5nLnYxYmV0YTEuU3Rha2VBdXRob3JpemF0aW9uEjogARI2CjRjb3Ntb3N2YWxvcGVyMWdqdHZseTlsZWw2enNrdnd0dmxnNXZod3B1OWM5d2F3N3N4end4EgYI4LD6pgYSZwpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohA/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1KEgQKAggBGAUSEwoNCgV1YXRvbRIEMjQxOBCp8wUaQEAN1nIfDawlHnep2bNEm14w+g7tYybJJT3htcGVS6s9D7va3ed1OUEIk9LZoc3G//VenJ+KLw26SRVBaRukgVI=\"}"
)
assertEquals(output.errorMessage, "")
}
Expand Down
2 changes: 0 additions & 2 deletions codegen-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name = "codegen-v2"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "libparser"
path = "src/lib.rs"
Expand Down
2 changes: 2 additions & 0 deletions include/TrustWalletCore/TWBlockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ enum TWBlockchain {
TWBlockchainSui = 50,
TWBlockchainGreenfield = 51,
TWBlockchainInternetComputer = 52,
TWBlockchainNativeEvmos = 55, // Cosmos
TWBlockchainNativeInjective = 56, // Cosmos
};

TW_EXTERN_C_END
5 changes: 3 additions & 2 deletions registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -3177,6 +3177,7 @@
"curve": "secp256k1",
"publicKeyType": "secp256k1",
"hrp": "thor",
"addressHasher": "sha256ripemd",
"chainId": "thorchain-mainnet-v1",
"explorer": {
"url": "https://viewblock.io/thorchain",
Expand Down Expand Up @@ -3973,7 +3974,7 @@
"coinId": 20009001,
"symbol": "EVMOS",
"decimals": 18,
"blockchain": "Cosmos",
"blockchain": "NativeEvmos",
"chainId": "evmos_9001-2",
"derivation": [
{
Expand Down Expand Up @@ -4365,7 +4366,7 @@
"coinId": 10000060,
"symbol": "INJ",
"decimals": 18,
"blockchain": "Cosmos",
"blockchain": "NativeInjective",
"derivation": [
{
"path": "m/44'/60'/0'/0/0"
Expand Down
84 changes: 84 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
[workspace]
members = [
"chains/tw_cosmos",
"chains/tw_native_evmos",
"chains/tw_native_injective",
"chains/tw_thorchain",
"tw_any_coin",
"tw_aptos",
"tw_bech32_address",
"tw_bitcoin",
"tw_coin_entry",
"tw_coin_registry",
"tw_cosmos_sdk",
"tw_encoding",
"tw_ethereum",
"tw_evm",
Expand Down
13 changes: 13 additions & 0 deletions rust/chains/tw_cosmos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "tw_cosmos"
version = "0.1.0"
edition = "2021"

[dependencies]
tw_coin_entry = { path = "../../tw_coin_entry" }
tw_cosmos_sdk = { path = "../../tw_cosmos_sdk" }
tw_keypair = { path = "../../tw_keypair" }
tw_proto = { path = "../../tw_proto" }

[dev-dependencies]
tw_cosmos_sdk = { path = "../../tw_cosmos_sdk", features = ["test-utils"] }
94 changes: 94 additions & 0 deletions rust/chains/tw_cosmos/src/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use std::str::FromStr;
use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::coin_entry::{CoinEntry, PublicKeyBytes, SignatureBytes};
use tw_coin_entry::derivation::Derivation;
use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_cosmos_sdk::address::{Address, Bech32Prefix};
use tw_cosmos_sdk::context::StandardCosmosContext;
use tw_cosmos_sdk::modules::compiler::tw_compiler::TWTransactionCompiler;
use tw_cosmos_sdk::modules::signer::tw_signer::TWSigner;
use tw_keypair::tw;
use tw_proto::Cosmos::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;

pub struct CosmosEntry;

impl CoinEntry for CosmosEntry {
type AddressPrefix = Bech32Prefix;
type Address = Address;
type SigningInput<'a> = Proto::SigningInput<'a>;
type SigningOutput = Proto::SigningOutput<'static>;
type PreSigningOutput = CompilerProto::PreSigningOutput<'static>;
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;

#[inline]
fn parse_address(
&self,
coin: &dyn CoinContext,
address: &str,
prefix: Option<Self::AddressPrefix>,
) -> AddressResult<Self::Address> {
Address::from_str_with_coin_and_prefix(coin, address.to_string(), prefix)
}

#[inline]
fn parse_address_unchecked(
&self,
_coin: &dyn CoinContext,
address: &str,
) -> AddressResult<Self::Address> {
Address::from_str(address)
}

#[inline]
fn derive_address(
&self,
coin: &dyn CoinContext,
public_key: tw::PublicKey,
_derivation: Derivation,
prefix: Option<Self::AddressPrefix>,
) -> AddressResult<Self::Address> {
Address::with_public_key_coin_context(coin, &public_key, prefix)
}

#[inline]
fn sign(&self, coin: &dyn CoinContext, input: Self::SigningInput<'_>) -> Self::SigningOutput {
TWSigner::<StandardCosmosContext>::sign(coin, input)
}

#[inline]
fn preimage_hashes(
&self,
coin: &dyn CoinContext,
input: Self::SigningInput<'_>,
) -> Self::PreSigningOutput {
TWTransactionCompiler::<StandardCosmosContext>::preimage_hashes(coin, input)
}

#[inline]
fn compile(
&self,
coin: &dyn CoinContext,
input: Self::SigningInput<'_>,
signatures: Vec<SignatureBytes>,
public_keys: Vec<PublicKeyBytes>,
) -> Self::SigningOutput {
TWTransactionCompiler::<StandardCosmosContext>::compile(
coin,
input,
signatures,
public_keys,
)
}
}
7 changes: 7 additions & 0 deletions rust/chains/tw_cosmos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

pub mod entry;
14 changes: 14 additions & 0 deletions rust/chains/tw_native_evmos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "tw_native_evmos"
version = "0.1.0"
edition = "2021"

[dependencies]
tw_coin_entry = { path = "../../tw_coin_entry" }
tw_cosmos_sdk = { path = "../../tw_cosmos_sdk" }
tw_keypair = { path = "../../tw_keypair" }
tw_memory = { path = "../../tw_memory" }
tw_proto = { path = "../../tw_proto" }

[dev-dependencies]
tw_cosmos_sdk = { path = "../../tw_cosmos_sdk", features = ["test-utils"] }
22 changes: 22 additions & 0 deletions rust/chains/tw_native_evmos/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::ethermint_public_key::EthermintEthSecp256PublicKey;
use tw_cosmos_sdk::address::Address;
use tw_cosmos_sdk::context::CosmosContext;
use tw_cosmos_sdk::hasher::keccak256_hasher::Keccak256Hasher;
use tw_cosmos_sdk::private_key::secp256k1::Secp256PrivateKey;
use tw_cosmos_sdk::signature::secp256k1::Secp256k1Signature;

pub struct NativeEvmosContext;

impl CosmosContext for NativeEvmosContext {
type Address = Address;
type TxHasher = Keccak256Hasher;
type PrivateKey = Secp256PrivateKey;
type PublicKey = EthermintEthSecp256PublicKey;
type Signature = Secp256k1Signature;
}
Loading

0 comments on commit 7119750

Please sign in to comment.