diff --git a/Cargo.lock b/Cargo.lock index bb5e613..f3aae68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -388,6 +388,7 @@ version = "0.0.1" dependencies = [ "anomaly", "prost-amino", + "prost-amino-derive", "rust_decimal", "serde", "sha2", diff --git a/cosmos-stdtx/Cargo.toml b/cosmos-stdtx/Cargo.toml index 0c226dc..ffa77c9 100644 --- a/cosmos-stdtx/Cargo.toml +++ b/cosmos-stdtx/Cargo.toml @@ -16,6 +16,7 @@ circle-ci = { repository = "tendermint/kms" } [dependencies] anomaly = "0.1" prost-amino = "0.5" +prost-amino-derive = "0.5" rust_decimal = "1.1" serde = { version = "1", features = ["serde_derive"] } sha2 = "0.8" diff --git a/cosmos-stdtx/src/lib.rs b/cosmos-stdtx/src/lib.rs index 11434d9..bc1d49f 100644 --- a/cosmos-stdtx/src/lib.rs +++ b/cosmos-stdtx/src/lib.rs @@ -16,6 +16,7 @@ pub mod address; pub mod error; pub mod msg; pub mod schema; +pub mod stdtx; pub mod type_name; pub use self::{address::Address, error::Error, msg::Msg, schema::Schema, type_name::TypeName}; diff --git a/cosmos-stdtx/src/stdtx.rs b/cosmos-stdtx/src/stdtx.rs new file mode 100644 index 0000000..c7f7252 --- /dev/null +++ b/cosmos-stdtx/src/stdtx.rs @@ -0,0 +1,75 @@ +//! StdTx Amino types + +use crate::type_name::TypeName; +use prost_amino::{encode_length_delimiter, Message}; +use prost_amino_derive::Message; + +/// StdTx Amino type +#[derive(Clone, Message)] +pub struct StdTx { + /// Messages in transction + #[prost_amino(bytes, repeated, tag = "1")] + pub msg: Vec>, + + /// Feeds to be paid + #[prost_amino(message)] + pub fee: Option, + + /// Signatures + #[prost_amino(message, repeated)] + pub signatures: Vec, + + /// Memo field + #[prost_amino(string)] + pub memo: String, +} + +impl StdTx { + /// Encode this [`StdTx`] in Amino encoding identifying it with the given + /// type name (e.g. `cosmos-sdk/StdTx`) + pub fn to_amino_bytes(&self, type_name: &TypeName) -> Vec { + let mut amino_tx = type_name.amino_prefix(); + self.encode(&mut amino_tx).expect("LEB128 encoding error"); + + let mut amino_encoded = vec![]; + encode_length_delimiter(amino_tx.len(), &mut amino_encoded).expect("LEB128 encoding error"); + amino_encoded.append(&mut amino_tx); + amino_encoded + } +} + +/// StdFee amino type +#[derive(Clone, Message)] +pub struct StdFee { + /// Fee to be paid + #[prost_amino(message, repeated, tag = "1")] + pub amount: Vec, + + /// Gas requested for transaction + #[prost_amino(uint64)] + pub gas: u64, +} + +/// Coin amino type +#[derive(Clone, Message)] +pub struct Coin { + /// Denomination of coin + #[prost_amino(string, tag = "1")] + pub denom: String, + + /// Amount of the given denomination + #[prost_amino(string)] + pub amount: String, +} + +/// StdSignature amino type +#[derive(Clone, Message)] +pub struct StdSignature { + /// Public key which can verify this signature + #[prost_amino(bytes, tag = "1", amino_name = "tendermint/PubKeySecp256k1")] + pub pub_key: Vec, + + /// Serialized signature + #[prost_amino(bytes)] + pub signature: Vec, +}