From 55499e7875d7a49144c498867aa5113725e9c3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Tue, 6 Jun 2023 16:40:04 +0200 Subject: [PATCH 1/3] core/types/address: use byte array of a string in PKH/implicit addr --- core/src/types/address.rs | 8 ++-- core/src/types/key/mod.rs | 79 +++++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/core/src/types/address.rs b/core/src/types/address.rs index b4184b304a..439adddc2d 100644 --- a/core/src/types/address.rs +++ b/core/src/types/address.rs @@ -1,7 +1,6 @@ //! Implements transparent addresses as described in [Accounts //! Addresses](docs/src/explore/design/ledger/accounts.md#addresses). -use std::borrow::Cow; use std::collections::HashMap; use std::fmt::{Debug, Display}; use std::hash::Hash; @@ -184,14 +183,15 @@ impl Address { /// Try to get a raw hash of an address, only defined for established and /// implicit addresses. - pub fn raw_hash(&self) -> Option> { + pub fn raw_hash(&self) -> Option { match self { Address::Established(established) => { let hash_hex = HEXUPPER.encode(&established.hash); - Some(Cow::Owned(hash_hex)) + Some(hash_hex) } Address::Implicit(ImplicitAddress(implicit)) => { - Some(Cow::Borrowed(&implicit.0)) + let hash_hex = HEXUPPER.encode(&implicit.0); + Some(hash_hex) } Address::Internal(_) => None, } diff --git a/core/src/types/key/mod.rs b/core/src/types/key/mod.rs index 66938fbcdc..ad5917aa68 100644 --- a/core/src/types/key/mod.rs +++ b/core/src/types/key/mod.rs @@ -14,7 +14,7 @@ use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use data_encoding::HEXUPPER; #[cfg(feature = "rand")] use rand::{CryptoRng, RngCore}; -use serde::{Deserialize, Serialize}; +use serde::Serialize; use sha2::{Digest, Sha256}; use thiserror::Error; @@ -301,23 +301,45 @@ pub trait SigScheme: Eq + Ord + Debug + Serialize + Default { PartialOrd, Ord, Hash, - Serialize, - Deserialize, )] -#[serde(transparent)] -pub struct PublicKeyHash(pub(crate) String); +pub struct PublicKeyHash(pub(crate) [u8; address::HASH_LEN]); -const PKH_HASH_LEN: usize = address::HASH_HEX_LEN; +impl serde::Serialize for PublicKeyHash { + fn serialize( + &self, + serializer: S, + ) -> std::result::Result + where + S: serde::Serializer, + { + let encoded = self.to_string(); + serde::Serialize::serialize(&encoded, serializer) + } +} + +impl<'de> serde::Deserialize<'de> for PublicKeyHash { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'de>, + { + use serde::de::Error; + let encoded: String = serde::Deserialize::deserialize(deserializer)?; + Self::from_str(&encoded).map_err(D::Error::custom) + } +} + +const PKH_HEX_LEN: usize = address::HASH_HEX_LEN; +const PKH_LEN: usize = address::HASH_LEN; impl From for String { fn from(pkh: PublicKeyHash) -> Self { - pkh.0 + pkh.to_string() } } impl Display for PublicKeyHash { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) + write!(f, "{}", HEXUPPER.encode(&self.0)) } } @@ -325,32 +347,36 @@ impl FromStr for PublicKeyHash { type Err = PkhFromStringError; fn from_str(s: &str) -> Result { - if s.len() != PKH_HASH_LEN { + if s.len() != PKH_HEX_LEN { return Err(Self::Err::UnexpectedLen(s.len())); } - Ok(Self(s.to_owned())) + let raw_bytes = HEXUPPER + .decode(s.as_bytes()) + .map_err(Self::Err::DecodeUpperHex)?; + let mut bytes: [u8; PKH_LEN] = Default::default(); + bytes.copy_from_slice(&raw_bytes); + Ok(Self(bytes)) } } #[allow(missing_docs)] #[derive(Error, Debug)] pub enum PkhFromStringError { - #[error("Wrong PKH len. Expected {PKH_HASH_LEN}, got {0}")] + #[error("Wrong PKH len. Expected {PKH_HEX_LEN}, got {0}")] UnexpectedLen(usize), + #[error("Failed decoding upper hex with {0}")] + DecodeUpperHex(data_encoding::DecodeError), } impl From<&PK> for PublicKeyHash { fn from(pk: &PK) -> Self { let pk_bytes = pk.try_to_vec().expect("Public key encoding shouldn't fail"); - let mut hasher = Sha256::new(); - hasher.update(pk_bytes); - // hex of the first 40 chars of the hash - PublicKeyHash(format!( - "{:.width$X}", - hasher.finalize(), - width = PKH_HASH_LEN - )) + let full_hash = Sha256::digest(&pk_bytes); + // take first 20 bytes of the hash + let mut hash: [u8; PKH_LEN] = Default::default(); + hash.copy_from_slice(&full_hash[..PKH_LEN]); + PublicKeyHash(hash) } } @@ -369,16 +395,11 @@ impl PublicKeyTmRawHash for common::PublicKey { /// Convert validator's consensus key into address raw hash that is compatible /// with Tendermint pub fn tm_consensus_key_raw_hash(pk: &common::PublicKey) -> String { - match pk { - common::PublicKey::Ed25519(pk) => { - let pkh = PublicKeyHash::from(pk); - pkh.0 - } - common::PublicKey::Secp256k1(pk) => { - let pkh = PublicKeyHash::from(pk); - pkh.0 - } - } + let pkh = match pk { + common::PublicKey::Ed25519(pk) => PublicKeyHash::from(pk), + common::PublicKey::Secp256k1(pk) => PublicKeyHash::from(pk), + }; + pkh.to_string() } /// Convert Tendermint validator's raw hash bytes to Namada raw hash string From 22b9066f51a0b7583ed3588762016ebc44b4a1db Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 7 Jun 2023 05:29:09 +0000 Subject: [PATCH 2/3] [ci] wasm checksums update --- wasm/checksums.json | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/wasm/checksums.json b/wasm/checksums.json index 57869c4ce1..40e95be46f 100644 --- a/wasm/checksums.json +++ b/wasm/checksums.json @@ -1,21 +1,21 @@ { - "tx_bond.wasm": "tx_bond.a98b5b38666d7b28ffb83208086bbffdbf03c8e9d3199ae89db880ea784bc3ab.wasm", - "tx_change_validator_commission.wasm": "tx_change_validator_commission.606a71c0f1dc1e7c2abb54789e4a99787a7c4718c5bd31e9283c02db9c7f42fb.wasm", - "tx_ibc.wasm": "tx_ibc.cd9d2dd7b04f16bf461138491ef2e75fa815ce1e2e6b474f0dfe563fea490a3b.wasm", - "tx_init_account.wasm": "tx_init_account.ce8a35ddc8959b0a73e4a1a6fd226fd0135d7ff0ef4ba517de1cfe53cfce93c0.wasm", - "tx_init_proposal.wasm": "tx_init_proposal.14c1b8b5141101789623fd9ad826b6c3f29cbea4cfad6dec392e6a51fc4e7e66.wasm", - "tx_init_validator.wasm": "tx_init_validator.91ab5b995f11572d043a8f78670dc0f1e3a275c84807f3745eca28757c4c71a3.wasm", - "tx_reveal_pk.wasm": "tx_reveal_pk.e0301b0d4246687110d2f321f158f2465fb6cac1e23c013e88aac89550d26261.wasm", - "tx_transfer.wasm": "tx_transfer.3d5136351aef729e48eb7b3767ec735175b115556abc34f0fb73791bce3fbce9.wasm", - "tx_unbond.wasm": "tx_unbond.9dfdc31ad694e0041b43e5e2230a3afbed0eb0ea121d89af4c850d531e22cf85.wasm", - "tx_unjail_validator.wasm": "tx_unjail_validator.e5d034b4ec784939264dc8fa257a0358ccd2ecb94655f87767d791c446e3dca8.wasm", - "tx_update_vp.wasm": "tx_update_vp.15b6f09d6677c46ab74c4b034ede1d198a42015efe6d78f7fab96e503854ac46.wasm", - "tx_vote_proposal.wasm": "tx_vote_proposal.ff34e5d98abcdf46a9091d81939bfda17cdd118258788c1668d2256e1db4de12.wasm", - "tx_withdraw.wasm": "tx_withdraw.01a1e491a16d7b99317b9127dde518969cf469a2bf76b62fe11dbfc7205e9918.wasm", - "vp_implicit.wasm": "vp_implicit.44390149aa21937fe91db593b242c2810abd316eb4e767eb4eece121f59941cf.wasm", - "vp_masp.wasm": "vp_masp.2502bdf824ce741688d7c153158a56aa49b84d00c6b4835138cfb3506d376f3d.wasm", - "vp_testnet_faucet.wasm": "vp_testnet_faucet.3e222a8ecae546c06e29b056049e9251c99a6d7ec8bd430dce5775be8a4cb268.wasm", - "vp_token.wasm": "vp_token.b34f0386d05f44372368ddd59a463ed2de016be421bd130afb035500b84229f8.wasm", - "vp_user.wasm": "vp_user.bdc2def99d10d3fda732ac2a79639537a2703a496d921f9239776ad08fa66bc2.wasm", - "vp_validator.wasm": "vp_validator.d3b03ce3cab72a0365b380baf972df891154bf42b87a021ec8ac03c888afd731.wasm" + "tx_bond.wasm": "tx_bond.4aea6bf93eb7ca64a3162ca86fd2c8d9c8d324bcff16fdc4d48391aa67563049.wasm", + "tx_change_validator_commission.wasm": "tx_change_validator_commission.c3d7e58d32a0e9302fdf2e5722f163ee2dec422a62503842af6f7e57045d3a4c.wasm", + "tx_ibc.wasm": "tx_ibc.6d26fe5abea04632386e446588667cad1666c38c8cdd5da104f4a8da2a285523.wasm", + "tx_init_account.wasm": "tx_init_account.422badd147072d19c37485bb9af7c59384146ee6178a42f57d90a108d777daa7.wasm", + "tx_init_proposal.wasm": "tx_init_proposal.f6d80d0b0528489cbdd4a5d585de28c4960ca3763fab44db3036e144c5f9743e.wasm", + "tx_init_validator.wasm": "tx_init_validator.ce607a01ae70bfe4a1c55a34a7f5dc6e85254ef35ef18a9f7744c93f3c143f76.wasm", + "tx_reveal_pk.wasm": "tx_reveal_pk.de69f0787e0bf5947444259a75ebaa2ecbed37532b3cbd33a9ddfb27f43e6ea2.wasm", + "tx_transfer.wasm": "tx_transfer.53abc1752f23dda8c085d42e808d971515b90d3781617d45386538c8cc2ff8ba.wasm", + "tx_unbond.wasm": "tx_unbond.5abef3af0d388a84d129b838a367a9ade3303e1e8a2a86969f99cda95cfdbdfe.wasm", + "tx_unjail_validator.wasm": "tx_unjail_validator.f9463fd719f1f93cc5fc900f1e48bd81c0192dcdc378c115cd49618d5baf1d99.wasm", + "tx_update_vp.wasm": "tx_update_vp.cefd67a267f34d8e93b207a39de9e920616f3d4e5e629086bbcb88b079ce6856.wasm", + "tx_vote_proposal.wasm": "tx_vote_proposal.71da34c54ece175129519120bd597b0226896827cfe0e1c76f435489cd899617.wasm", + "tx_withdraw.wasm": "tx_withdraw.a3254ac54957870653255518b980e0f1696e80f1f9a736f5cd8ac19b9ba0788b.wasm", + "vp_implicit.wasm": "vp_implicit.8f8e643cfb1c7d11e15f8a17517935ef159ab9a6a8da747cad4a403c9e15cbc7.wasm", + "vp_masp.wasm": "vp_masp.cd7fae1152e765ad4e3985fbdd0b8b2cb1cea13e2fff812291ebaf484f8676c9.wasm", + "vp_testnet_faucet.wasm": "vp_testnet_faucet.1dc87f66e7a378df435a20f97592e3a7746a3ccb322ca11889f11822d312ee22.wasm", + "vp_token.wasm": "vp_token.56c0f16a5c67e36a10716b10fe392271e205b9c095c7d9bf161dc646dd0644ff.wasm", + "vp_user.wasm": "vp_user.c623820cd01bacc740070d364209abf9f1d83a80c4bda79660df7f3a3624bdcd.wasm", + "vp_validator.wasm": "vp_validator.842e60005998c6c8572d19d48182e916d6ee86784059c61166adc25c0e5707ed.wasm" } \ No newline at end of file From a64dd843fe2794c3805bbeeeda293335d64e9e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 7 Jun 2023 07:34:43 +0200 Subject: [PATCH 3/3] changelog: add #1512 --- .changelog/unreleased/improvements/1512-implicit-addr-bytes.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/1512-implicit-addr-bytes.md diff --git a/.changelog/unreleased/improvements/1512-implicit-addr-bytes.md b/.changelog/unreleased/improvements/1512-implicit-addr-bytes.md new file mode 100644 index 0000000000..250aa01d77 --- /dev/null +++ b/.changelog/unreleased/improvements/1512-implicit-addr-bytes.md @@ -0,0 +1,2 @@ +- Improve the implicit address and PKH in-memory representation. + ([\#1512](https://github.com/anoma/namada/pull/1512)) \ No newline at end of file