This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cosmos-stdtx: Msg builder and encoder
Support for programatically building messages from fields and values, validating them against the schema, and encoding the resulting messages as Amino.
- Loading branch information
1 parent
087c427
commit 22c11d7
Showing
13 changed files
with
532 additions
and
18 deletions.
There are no files selected for viewing
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
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,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 | ||
} | ||
} |
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
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 | ||
} | ||
} |
Oops, something went wrong.