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

cosmos-stdtx.rs: amino serializer for Cosmos SDK-formatted StdTx transactions #401

Merged
merged 5 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ jobs:
build:
docker:
- image: tendermint/kms:build-2019-06-05-v0 # bump cache keys when modifying this
environment:
CARGO_INCREMENTAL: 0
RUSTFLAGS: -D warnings
steps:
- checkout
- restore_cache:
key: cache-2019-06-05-v0 # bump save_cache key below too
key: cache-2020-01-27-v0 # bump save_cache key below too
- run:
name: Install Rust 1.39.0 # TODO: update Rust in the upstream Docker image
command: |
Expand Down Expand Up @@ -61,7 +64,7 @@ jobs:
cargo build --features=softsign
TMKMS_BIN=./target/debug/tmkms sh tests/support/run-harness-tests.sh
- save_cache:
key: cache-2019-06-05-v0 # bump restore_cache key above too
key: cache-2020-01-27-v0 # bump restore_cache key above too
paths:
- "~/.cargo"
- "./target"
88 changes: 88 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ description = "Tendermint Key Management System"
version = "0.7.1" # Also update html_root_url in lib.rs when bumping this
authors = ["Tony Arcieri <[email protected]>", "Ismail Khoffi <[email protected]>"]
license = "Apache-2.0"
homepage = "https://github.com/tendermint/kms/"
homepage = "https://tendermint.com/"
repository = "https://github.com/tendermint/kms/"
readme = "README.md"
categories = ["cryptography"]
keywords = ["cosmos", "ed25519", "kms", "key-management", "yubihsm"]
edition = "2018"

[workspace]
members = [".", "cosmos-stdtx"]

[badges]
circle-ci = { repository = "tendermint/kms" }

Expand Down
30 changes: 30 additions & 0 deletions cosmos-stdtx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "cosmos-stdtx"
description = "Extensible schema-driven Cosmos StdTx builder and serializer"
version = "0.0.1" # Also update html_root_url in lib.rs when bumping this
authors = ["Tony Arcieri <[email protected]>"]
license = "Apache-2.0"
repository = "https://github.com/tendermint/kms/tree/master/cosmos-stdtx"
readme = "README.md"
categories = ["cryptography", "encoding"]
keywords = ["crypto", "cosmos", "stdtx", "transaction", "tendermint"]
edition = "2018"

[badges]
circle-ci = { repository = "tendermint/kms" }

[dependencies]
anomaly = "0.1"
ecdsa = { version = "0.4", features = ["k256"] }
prost-amino = "0.5"
prost-amino-derive = "0.5"
rust_decimal = "1.1"
serde = { version = "1", features = ["serde_derive"] }
serde_json = "1"
sha2 = "0.8"
subtle-encoding = { version = "0.5", features = ["bech32-preview"] }
thiserror = "1"
toml = "0.5"

[dev-dependencies]
signatory-secp256k1 = "0.18"
64 changes: 64 additions & 0 deletions cosmos-stdtx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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]

Extensible schema-driven [Cosmos] [StdTx] builder and serializer.

## About

**cosmos-stdtx.rs** is a Rust library for composing transactions in the [StdTx]
format used by several [Tendermint]-based networks.

It includes support for cryptographically signing transactions and serializing
them in the [Amino] encoding format.

Definitions of transaction types are easily extensible, and can be defined at
runtime by loading them from a TOML definition file. This allows
**cosmos-stdtx.rs** to be used with any [Tendermint]-based software which
uses the [StdTx] format without requiring upstream modifications.

## Minimum Supported Rust Version

- Rust **1.39+**

## License

Copyright © 2020 Tony Arcieri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

[//]: # (badges)

[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

[//]: # (general links)

[Cosmos]: https://cosmos.network/
[StdTx]: https://godoc.org/github.com/cosmos/cosmos-sdk/x/auth/types#StdTx
[Tendermint]: https://tendermint.com/
[Amino]: https://github.com/tendermint/go-amino
53 changes: 53 additions & 0 deletions cosmos-stdtx/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! 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())))
}

/// Encode this address as Bech32
pub fn to_bech32(&self, hrp: &str) -> String {
bech32::encode(hrp, &self.0)
}
}

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
}
}
Loading