Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: StdTx: Better JSON support #391

Closed
wants to merge 2 commits into from
Closed
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: 7 additions & 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 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 = { version = "0.2", path = "../anomaly" }
base64 = "0.12.0"
ecdsa = { version = "0.4", features = ["k256"] }
prost-amino = "0.5"
prost-amino-derive = "0.5"
Expand Down
20 changes: 20 additions & 0 deletions stdtx/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::error::{Error, ErrorKind};
use anomaly::ensure;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::convert::TryInto;
use subtle_encoding::bech32;

Expand Down Expand Up @@ -34,6 +35,25 @@ impl Address {
}
}

impl Serialize for Address {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let s = self.to_bech32("cosmos"); // TODO: how to generalize?
s.serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Address {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let (_hrp, addr) =
Address::from_bech32(s).map_err(|e| de::Error::custom(format!("{:?}", e)))?;
Ok(addr)
}
}

impl AsRef<[u8]> for Address {
fn as_ref(&self) -> &[u8] {
&self.0
Expand Down
52 changes: 47 additions & 5 deletions stdtx/src/amino_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use crate::{Signature, TypeName};
use prost_amino::{encode_length_delimiter, Message};
use prost_amino_derive::Message;
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

use base64;
use serde_json::json;

/// StdTx Amino type
Expand Down Expand Up @@ -40,17 +43,36 @@ impl StdTx {
}

/// StdFee amino type
#[derive(Clone, Message)]
#[derive(Clone, Message, Serialize, Deserialize)]
pub struct StdFee {
/// Fee to be paid
#[prost_amino(message, repeated, tag = "1")]
pub amount: Vec<Coin>,

/// Gas requested for transaction
#[prost_amino(uint64)]
#[serde(serialize_with = "serialize_u64", deserialize_with = "parse_u64")]
pub gas: u64,
}

/// Serialize u64 as a string for proto3 JSON encoding.
pub(crate) fn serialize_u64<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
format!("{}", value).serialize(serializer)
}

/// Parse u64 from a string for proto3 JSON decoding.
pub(crate) fn parse_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
String::deserialize(deserializer)?
.parse::<u64>()
.map_err(|e| D::Error::custom(format!("{}", e)))
}

impl StdFee {
/// Create a [`StdFee`] for a gas-only transaction
pub fn for_gas(gas: u64) -> Self {
Expand All @@ -75,7 +97,7 @@ impl StdFee {
}

/// Coin Amino type
#[derive(Clone, Message)]
#[derive(Clone, Message, Serialize, Deserialize)]
pub struct Coin {
/// Denomination of coin
#[prost_amino(string, tag = "1")]
Expand All @@ -97,21 +119,41 @@ impl Coin {
}

/// StdSignature amino type
#[derive(Clone, Message)]
#[derive(Clone, Message, Serialize, Deserialize)]
pub struct StdSignature {
/// Public key which can verify this signature
#[prost_amino(bytes, tag = "1", amino_name = "tendermint/PubKeySecp256k1")]
pub pub_key: Vec<u8>,
#[serde(serialize_with = "serialize_base64", deserialize_with = "parse_base64")]
pub public_key: Vec<u8>,

/// Serialized signature
#[prost_amino(bytes)]
#[serde(serialize_with = "serialize_base64", deserialize_with = "parse_base64")]
pub signature: Vec<u8>,
}

/// Serialize bytes as base64 string for proto3 JSON encoding.
pub(crate) fn serialize_base64<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&base64::encode(value))
}

/// Parse bytes from a base64 string for proto3 JSON decoding.
pub(crate) fn parse_base64<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
String::deserialize(deserializer).and_then(|string| {
base64::decode(&string).map_err(|err| D::Error::custom(format!("{}", err)))
})
}

impl From<Signature> for StdSignature {
fn from(signature: Signature) -> StdSignature {
StdSignature {
pub_key: vec![],
public_key: vec![],
signature: signature.as_ref().to_vec(),
}
}
Expand Down