-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cosmos]: Add NativeInjective blockchain, cover with tests
- Loading branch information
1 parent
eae1347
commit fffc406
Showing
16 changed files
with
256 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "tw_native_injective" | ||
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"] } |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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::injective_public_key::InjectiveEthSecp256PublicKey; | ||
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; | ||
|
||
pub struct NativeInjectiveContext; | ||
|
||
impl CosmosContext for NativeInjectiveContext { | ||
type Address = Address; | ||
type TxHasher = Keccak256Hasher; | ||
type PrivateKey = Secp256PrivateKey; | ||
type PublicKey = InjectiveEthSecp256PublicKey; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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::context::NativeInjectiveContext; | ||
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_coin_entry::prefix::NoPrefix; | ||
use tw_cosmos_sdk::address::{Address, CosmosAddress}; | ||
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 NativeInjectiveEntry; | ||
|
||
impl CoinEntry for NativeInjectiveEntry { | ||
type AddressPrefix = NoPrefix; | ||
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; | ||
|
||
fn parse_address( | ||
&self, | ||
coin: &dyn CoinContext, | ||
address: &str, | ||
_prefix: Option<Self::AddressPrefix>, | ||
) -> AddressResult<Self::Address> { | ||
Address::from_str_with_coin(coin, address) | ||
} | ||
|
||
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) | ||
} | ||
|
||
fn sign(&self, coin: &dyn CoinContext, input: Self::SigningInput<'_>) -> Self::SigningOutput { | ||
TWSigner::<NativeInjectiveContext>::sign(coin, input) | ||
} | ||
|
||
fn preimage_hashes( | ||
&self, | ||
_coin: &dyn CoinContext, | ||
_input: Self::SigningInput<'_>, | ||
) -> Self::PreSigningOutput { | ||
todo!() | ||
} | ||
|
||
fn compile( | ||
&self, | ||
_coin: &dyn CoinContext, | ||
_input: Self::SigningInput<'_>, | ||
_signatures: Vec<SignatureBytes>, | ||
_public_keys: Vec<PublicKeyBytes>, | ||
) -> Self::SigningOutput { | ||
todo!() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// 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 context; | ||
pub mod entry; | ||
pub mod injective_public_key; |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
// file LICENSE at the root of the source code distribution tree. | ||
|
||
mod native_evmos; | ||
mod native_injective; |
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 |
---|---|---|
@@ -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 native_injective_sign; |
97 changes: 97 additions & 0 deletions
97
rust/tw_any_coin/tests/chains/native_injective/native_injective_sign.rs
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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::borrow::Cow; | ||
use tw_any_coin::ffi::tw_any_signer::tw_any_signer_sign; | ||
use tw_coin_entry::error::SigningErrorType; | ||
use tw_cosmos_sdk::test_utils::proto_utils::{make_amount, make_fee, make_message}; | ||
use tw_encoding::hex::{DecodeHex, ToHex}; | ||
use tw_memory::test_utils::tw_data_helper::TWDataHelper; | ||
use tw_proto::Cosmos::Proto; | ||
use tw_proto::Cosmos::Proto::mod_Message::OneOfmessage_oneof as MessageEnum; | ||
use tw_proto::{deserialize, serialize}; | ||
|
||
const NATIVE_INJECTIVE_COIN_TYPE: u32 = 10000060; | ||
|
||
fn account_17396_private_key() -> Cow<'static, [u8]> { | ||
"9ee18daf8e463877aaf497282abc216852420101430482a28e246c179e2c5ef1" | ||
.decode_hex() | ||
.unwrap() | ||
.into() | ||
} | ||
|
||
fn send_tx_input() -> Proto::SigningInput<'static> { | ||
let send_msg = Proto::mod_Message::Send { | ||
from_address: "inj13u6g7vqgw074mgmf2ze2cadzvkz9snlwcrtq8a".into(), | ||
to_address: "inj1xmpkmxr4as00em23tc2zgmuyy2gr4h3wgcl6vd".into(), | ||
amounts: vec![make_amount("inj", "10000000000")], | ||
..Proto::mod_Message::Send::default() | ||
}; | ||
Proto::SigningInput { | ||
account_number: 17396, | ||
chain_id: "injective-1".into(), | ||
sequence: 1, | ||
fee: Some(make_fee(110000, make_amount("inj", "100000000000000"))), | ||
private_key: account_17396_private_key(), | ||
messages: vec![make_message(MessageEnum::send_coins_message(send_msg))], | ||
..Proto::SigningInput::default() | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_sign_native_injective_tx_protobuf() { | ||
let input = Proto::SigningInput { | ||
signing_mode: Proto::SigningMode::Protobuf, | ||
..send_tx_input() | ||
}; | ||
let input_data = TWDataHelper::create(serialize(&input).unwrap()); | ||
|
||
let output = TWDataHelper::wrap(unsafe { | ||
tw_any_signer_sign(input_data.ptr(), NATIVE_INJECTIVE_COIN_TYPE) | ||
}) | ||
.to_vec() | ||
.expect("!tw_any_signer_sign returned nullptr"); | ||
|
||
let output: Proto::SigningOutput = deserialize(&output).unwrap(); | ||
assert_eq!(output.error, SigningErrorType::OK); | ||
assert!(output.error_message.is_empty()); | ||
|
||
// https://www.mintscan.io/injective/txs/135DD2C4A1910E4334A9C0F15125DA992E724EBF23FEB9638FCB71218BB064A5 | ||
assert_eq!( | ||
output.serialized, | ||
r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASngEKfgp0Ci0vaW5qZWN0aXZlLmNyeXB0by52MWJldGExLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBFoMa4O4vZgn5QcnDK20mbfjqQlSRvaiITKB94PYd8mLJWdCdBsGOfMXdo/k9MJ2JmDCESKDp2hdgVUH3uMikXMSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpAx2vkplmzeK7n3puCFGPWhLd0l/ZC/CYkGl+stH+3S3hiCvIe7uwwMpUlNaSwvT8HwF1kNUp+Sx2m0Uo1x5xcFw=="}"# | ||
); | ||
assert_eq!(output.signature.to_hex(), "c76be4a659b378aee7de9b821463d684b77497f642fc26241a5facb47fb74b78620af21eeeec3032952535a4b0bd3f07c05d64354a7e4b1da6d14a35c79c5c17"); | ||
} | ||
|
||
#[test] | ||
fn test_sign_native_injective_tx_json() { | ||
let input = Proto::SigningInput { | ||
signing_mode: Proto::SigningMode::JSON, | ||
..send_tx_input() | ||
}; | ||
let input_data = TWDataHelper::create(serialize(&input).unwrap()); | ||
|
||
let output = TWDataHelper::wrap(unsafe { | ||
tw_any_signer_sign(input_data.ptr(), NATIVE_INJECTIVE_COIN_TYPE) | ||
}) | ||
.to_vec() | ||
.expect("!tw_any_signer_sign returned nullptr"); | ||
|
||
let output: Proto::SigningOutput = deserialize(&output).unwrap(); | ||
assert_eq!(output.error, SigningErrorType::OK); | ||
assert!(output.error_message.is_empty()); | ||
|
||
// This transaction hasn't been broadcasted. | ||
assert_eq!( | ||
output.json, | ||
r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"100000000000000","denom":"inj"}],"gas":"110000"},"memo":"","msg":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[{"amount":"10000000000","denom":"inj"}],"from_address":"inj13u6g7vqgw074mgmf2ze2cadzvkz9snlwcrtq8a","to_address":"inj1xmpkmxr4as00em23tc2zgmuyy2gr4h3wgcl6vd"}}],"signatures":[{"pub_key":{"type":"injective/PubKeyEthSecp256k1","value":"BFoMa4O4vZgn5QcnDK20mbfjqQlSRvaiITKB94PYd8mLJWdCdBsGOfMXdo/k9MJ2JmDCESKDp2hdgVUH3uMikXM="},"signature":"7wwedceebL95DwbClz5AzEp2Z74itHC7raiV976DcacfjrJ58oDfjbAO5UOZQAlihiYBP7PpyISFQ72FPRhdZA=="}]}}"# | ||
); | ||
assert_eq!( | ||
output.signature_json, | ||
r#"[{"pub_key":{"type":"injective/PubKeyEthSecp256k1","value":"BFoMa4O4vZgn5QcnDK20mbfjqQlSRvaiITKB94PYd8mLJWdCdBsGOfMXdo/k9MJ2JmDCESKDp2hdgVUH3uMikXM="},"signature":"7wwedceebL95DwbClz5AzEp2Z74itHC7raiV976DcacfjrJ58oDfjbAO5UOZQAlihiYBP7PpyISFQ72FPRhdZA=="}]"# | ||
); | ||
} |
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
Oops, something went wrong.