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

Commit

Permalink
cosmos-stdtx: Msg builder and encoder
Browse files Browse the repository at this point in the history
Support for programatically building messages from fields and values,
validating them against the schema, and encoding the resulting messages
as Amino.
  • Loading branch information
tony-iqlusion committed Jan 26, 2020
1 parent 087c427 commit 22c11d7
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 18 deletions.
73 changes: 73 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cosmos-stdtx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ circle-ci = { repository = "tendermint/kms" }

[dependencies]
anomaly = "0.1"
prost-amino = "0.5"
rust_decimal = "1.1"
serde = { version = "1", features = ["serde_derive"] }
sha2 = "0.8"
subtle-encoding = { version = "0.5", features = ["bech32-preview"] }
thiserror = "1"
toml = "0.5"
6 changes: 6 additions & 0 deletions cosmos-stdtx/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# cosmos-stdtx.rs 🌌

[![Crate][crate-image]][crate-link]
[![Docs][docs-image]][docs-link]
[![Build Status][build-image]][build-link]
[![Safety Dance][safety-image]][safety-link]
[![Apache 2.0 Licensed][license-image]][license-link]
![MSRV][rustc-image]

Expand Down Expand Up @@ -44,8 +46,12 @@ limitations under the License.

[crate-image]: https://img.shields.io/crates/v/cosmos-stdtx.svg
[crate-link]: https://crates.io/crates/cosmos-stdtx
[docs-image]: https://docs.rs/cosmos-stdtx/badge.svg
[docs-link]: https://docs.rs/cosmos-stdtx/
[build-image]: https://circleci.com/gh/tendermint/kms.svg?style=shield
[build-link]: https://circleci.com/gh/tendermint/kms
[safety-image]: https://img.shields.io/badge/unsafe-forbidden-success.svg
[safety-link]: https://github.com/rust-secure-code/safety-dance/
[license-image]: https://img.shields.io/badge/license-Apache2.0-blue.svg
[license-link]: https://github.com/tendermint/kms/blob/master/LICENSE
[rustc-image]: https://img.shields.io/badge/rustc-1.39+-blue.svg
Expand Down
48 changes: 48 additions & 0 deletions cosmos-stdtx/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Address types (account or validator)
use crate::error::{Error, ErrorKind};
use anomaly::ensure;
use std::convert::TryInto;
use subtle_encoding::bech32;

/// Size of an address
pub const ADDRESS_SIZE: usize = 20;

/// Address type
#[derive(Clone, Debug)]
pub struct Address(pub [u8; ADDRESS_SIZE]);

impl Address {
/// Parse an address from its Bech32 form
pub fn from_bech32(addr_bech32: impl AsRef<str>) -> Result<(String, Address), Error> {
let (hrp, addr) = bech32::decode(addr_bech32.as_ref())?;

ensure!(
addr.len() == ADDRESS_SIZE,
ErrorKind::Address,
"invalid length for decoded address: {} (expected {})",
addr.len(),
ADDRESS_SIZE
);

Ok((hrp, Address(addr.as_slice().try_into().unwrap())))
}
}

impl AsRef<[u8]> for Address {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl From<[u8; ADDRESS_SIZE]> for Address {
fn from(addr: [u8; ADDRESS_SIZE]) -> Address {
Address(addr)
}
}

impl From<Address> for [u8; ADDRESS_SIZE] {
fn from(addr: Address) -> [u8; ADDRESS_SIZE] {
addr.0
}
}
14 changes: 14 additions & 0 deletions cosmos-stdtx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ use thiserror::Error;
/// Kinds of errors
#[derive(Copy, Clone, Debug, Error, Eq, PartialEq)]
pub enum ErrorKind {
/// Malformed account or validator address
#[error("address error")]
Address,

/// Input/output errors
#[error("I/O error")]
Io,

/// Parse error
#[error("parse error")]
Parse,

/// Invalid type
#[error("type error")]
Type,
}

impl ErrorKind {
Expand Down Expand Up @@ -62,6 +70,12 @@ impl From<Context<ErrorKind>> for Error {
}
}

impl From<subtle_encoding::Error> for Error {
fn from(source: subtle_encoding::Error) -> Error {
Context::new(ErrorKind::Parse, Some(source.into())).into()
}
}

impl From<toml::de::Error> for Error {
fn from(source: toml::de::Error) -> Error {
Context::new(ErrorKind::Parse, Some(source.into())).into()
Expand Down
5 changes: 4 additions & 1 deletion cosmos-stdtx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, missing_docs, unused_qualifications)]

pub mod address;
pub mod error;
pub mod msg;
pub mod schema;
pub mod type_name;

pub use self::{error::Error, schema::Schema};
pub use self::{address::Address, error::Error, msg::Msg, schema::Schema, type_name::TypeName};
49 changes: 49 additions & 0 deletions cosmos-stdtx/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Transaction message type (i.e `sdk.Msg`)
mod builder;
mod value;

pub use self::{builder::Builder, value::Value};
pub use rust_decimal::Decimal;

use crate::type_name::TypeName;
use prost_amino::encode_length_delimiter as encode_leb128; // Little-endian Base 128

/// Tags are indexes which identify message fields
pub type Tag = u64;

/// Fields in the message
pub type Field = (Tag, Value);

/// Transaction message type (i.e. [`sdk.Msg`]).
/// These serve as the payload for [`StdTx`] transactions.
///
/// [`StdTx`]: https://godoc.org/github.com/cosmos/cosmos-sdk/x/auth/types#StdTx
/// [`sdk.Msg`]: https://godoc.org/github.com/cosmos/cosmos-sdk/types#Msg
#[derive(Clone, Debug)]
pub struct Msg {
/// Name of the message type
type_name: TypeName,

/// Fields in the message
fields: Vec<Field>,
}

impl Msg {
/// Encode this message in the Amino wire format
pub fn to_amino_bytes(&self) -> Vec<u8> {
let mut result = self.type_name.amino_prefix();

for (tag, value) in &self.fields {
// Compute the field prefix, which encodes the tag and wire type code
let prefix = *tag << 3 | value.wire_type();
encode_leb128(prefix as usize, &mut result).expect("LEB128 encoding error");

let mut encoded_value = value.to_amino_bytes();
encode_leb128(encoded_value.len(), &mut result).expect("LEB128 encoding error");
result.append(&mut encoded_value);
}

result
}
}
Loading

0 comments on commit 22c11d7

Please sign in to comment.