Skip to content

Commit

Permalink
Update base64 dependency to 0.21
Browse files Browse the repository at this point in the history
In cosmwasm-crypto replace the use of base64 crate with the use of hex
instead.  It’s used once in tests and once in benchmarks to decode
const binary data.  Using hex works just as well.

In cosmwasm-std update the dependency.
  • Loading branch information
mina86 committed Jul 19, 2023
1 parent aca2de1 commit 2043c15
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion packages/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ criterion = "0.4"
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }
serde_json = "1.0.40"
sha2 = "0.10"
base64 = "0.13.0"
hex = "0.4"
hex-literal = "0.3.1"
english-numbers = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::cmp::min;

const COSMOS_SECP256K1_MSG_HEX: &str = "0a93010a90010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e6412700a2d636f736d6f7331706b707472653766646b6c366766727a6c65736a6a766878686c63337234676d6d6b38727336122d636f736d6f7331717970717870713971637273737a673270767871367273307a716733797963356c7a763778751a100a0575636f736d12073132333435363712650a4e0a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a21034f04181eeba35391b858633a765c4a0c189697b40d216354d50890d350c7029012040a02080112130a0d0a0575636f736d12043230303010c09a0c1a0c73696d642d74657374696e672001";
const COSMOS_SECP256K1_SIGNATURE_HEX: &str = "c9dd20e07464d3a688ff4b710b1fbc027e495e797cfa0b4804da2ed117959227772de059808f765aa29b8f92edf30f4c2c5a438e30d3fe6897daa7141e3ce6f9";
const COSMOS_SECP256K1_PUBKEY_BASE64: &str = "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ";
const COSMOS_SECP256K1_PUBKEY_HEX: &str = "034f04181eeba35391b858633a765c4a0c189697b40d216354d50890d350c70290";

// TEST 3 test vector from https://tools.ietf.org/html/rfc8032#section-7.1
const COSMOS_ED25519_MSG_HEX: &str = "af82";
Expand Down Expand Up @@ -80,7 +80,7 @@ fn bench_crypto(c: &mut Criterion) {
let message = hex::decode(COSMOS_SECP256K1_MSG_HEX).unwrap();
let message_hash = Sha256::digest(message);
let signature = hex::decode(COSMOS_SECP256K1_SIGNATURE_HEX).unwrap();
let public_key = base64::decode(COSMOS_SECP256K1_PUBKEY_BASE64).unwrap();
let public_key = hex::decode(COSMOS_SECP256K1_PUBKEY_HEX).unwrap();
b.iter(|| {
assert!(secp256k1_verify(&message_hash, &signature, &public_key).unwrap());
});
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mod tests {

// Cosmos secp256k1 signature verification
// tendermint/PubKeySecp256k1 pubkey
const COSMOS_SECP256K1_PUBKEY_BASE64: &str = "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ";
const COSMOS_SECP256K1_PUBKEY_HEX: &str = "034f04181eeba35391b858633a765c4a0c189697b40d216354d50890d350c70290";

const COSMOS_SECP256K1_MSG_HEX1: &str = "0a93010a90010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e6412700a2d636f736d6f7331706b707472653766646b6c366766727a6c65736a6a766878686c63337234676d6d6b38727336122d636f736d6f7331717970717870713971637273737a673270767871367273307a716733797963356c7a763778751a100a0575636f736d12073132333435363712650a4e0a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a21034f04181eeba35391b858633a765c4a0c189697b40d216354d50890d350c7029012040a02080112130a0d0a0575636f736d12043230303010c09a0c1a0c73696d642d74657374696e672001";
const COSMOS_SECP256K1_MSG_HEX2: &str = "0a93010a90010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e6412700a2d636f736d6f7331706b707472653766646b6c366766727a6c65736a6a766878686c63337234676d6d6b38727336122d636f736d6f7331717970717870713971637273737a673270767871367273307a716733797963356c7a763778751a100a0575636f736d12073132333435363712670a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a21034f04181eeba35391b858633a765c4a0c189697b40d216354d50890d350c7029012040a020801180112130a0d0a0575636f736d12043230303010c09a0c1a0c73696d642d74657374696e672001";
Expand Down Expand Up @@ -271,7 +271,7 @@ mod tests {

#[test]
fn test_cosmos_secp256k1_verify() {
let public_key = base64::decode(COSMOS_SECP256K1_PUBKEY_BASE64).unwrap();
let public_key = hex::decode(COSMOS_SECP256K1_PUBKEY_HEX).unwrap();

for ((i, msg), sig) in (1..)
.zip(&[
Expand Down
2 changes: 1 addition & 1 deletion packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cosmwasm_1_2 = ["cosmwasm_1_1"]
cosmwasm_1_3 = ["cosmwasm_1_2"]

[dependencies]
base64 = "0.13.0"
base64 = "0.21.0"
cosmwasm-derive = { path = "../derive", version = "1.3.0-rc.0" }
derivative = "2"
forward_ref = "1"
Expand Down
16 changes: 13 additions & 3 deletions packages/std/src/binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use std::ops::Deref;

use base64::engine::{Engine, GeneralPurpose};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};

Expand All @@ -15,17 +16,26 @@ use crate::errors::{StdError, StdResult};
pub struct Binary(#[schemars(with = "String")] pub Vec<u8>);

impl Binary {
/// Base64 encoding engine used in conversion to/from base64.
const B64_ENGINE: GeneralPurpose = GeneralPurpose::new(
&base64::alphabet::STANDARD,
base64::engine::GeneralPurposeConfig::new()
.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
);

/// take an (untrusted) string and decode it into bytes.
/// fails if it is not valid base64
pub fn from_base64(encoded: &str) -> StdResult<Self> {
let binary = base64::decode(encoded).map_err(StdError::invalid_base64)?;
Ok(Binary(binary))
Self::B64_ENGINE
.decode(encoded.as_bytes())
.map(Binary::from)
.map_err(StdError::invalid_base64)
}

/// encode to base64 string (guaranteed to be success as we control the data inside).
/// this returns normalized form (with trailing = if needed)
pub fn to_base64(&self) -> String {
base64::encode(&self.0)
Self::B64_ENGINE.encode(self.0.as_slice())
}

pub fn as_slice(&self) -> &[u8] {
Expand Down

0 comments on commit 2043c15

Please sign in to comment.