Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
cosmos-stdtx: add Amino types for encoding StdTx
Browse files Browse the repository at this point in the history
The schema-based encoder is only for `Msg`. This adds a `StdTx` type
using `prost-amino-derive` to programatically declare it.
  • Loading branch information
tony-iqlusion committed Jan 26, 2020
1 parent 22c11d7 commit 1dc247f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cosmos-stdtx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions cosmos-stdtx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
75 changes: 75 additions & 0 deletions cosmos-stdtx/src/stdtx.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<u8>>,

/// Feeds to be paid
#[prost_amino(message)]
pub fee: Option<StdFee>,

/// Signatures
#[prost_amino(message, repeated)]
pub signatures: Vec<StdSignature>,

/// 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<u8> {
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<Coin>,

/// 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<u8>,

/// Serialized signature
#[prost_amino(bytes)]
pub signature: Vec<u8>,
}

0 comments on commit 1dc247f

Please sign in to comment.