From 6bcb60cfa0c3d8c65a6eb5425412be0ff5e1e4cd Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 29 Nov 2022 18:00:59 +1300 Subject: [PATCH 1/4] Add human readable publickey --- src/components/wasm/src/wasm.rs | 22 +++++++++++++++++- src/libs/globutils/src/wallet.rs | 38 ++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/components/wasm/src/wasm.rs b/src/components/wasm/src/wasm.rs index 55a6e4259..4d0268f39 100644 --- a/src/components/wasm/src/wasm.rs +++ b/src/components/wasm/src/wasm.rs @@ -75,7 +75,7 @@ use { open_blind_asset_record as open_bar, AssetRecordType, AssetRecordType::NonConfidentialAmount_NonConfidentialAssetType, }, - sig::{XfrKeyPair, XfrPublicKey, XfrSecretKey}, + sig::{KeyType, XfrKeyPair, XfrPublicKey, XfrSecretKey}, structs::{ AssetRecordTemplate, AssetType as NoahAssetType, XfrBody, ASSET_TYPE_LENGTH, @@ -1826,6 +1826,12 @@ use ring::pbkdf2; use std::num::NonZeroU32; use std::str; +#[wasm_bindgen] +/// Returns human-readable encoded representation of an XfrPublicKey. +pub fn public_key_to_human(key: &XfrPublicKey) -> String { + wallet::public_key_to_human(key) +} + #[wasm_bindgen] /// Returns bech32 encoded representation of an XfrPublicKey. pub fn public_key_to_bech32(key: &XfrPublicKey) -> String { @@ -1940,6 +1946,20 @@ pub fn create_keypair_from_secret(sk_str: String) -> Result Ok(sk.into_keypair()) } +#[wasm_bindgen] +#[allow(missing_docs)] +pub fn create_keypair_from_secret_secp256k1( + sk_str: String, +) -> Result { + let mut bytes = vec![KeyType::Secp256k1.to_byte()]; + bytes.extend(hex::decode(&sk_str).c(d!()).map_err(error_to_jsvalue)?); + let sk = XfrSecretKey::noah_from_bytes(&bytes) + .c(d!()) + .map_err(error_to_jsvalue)?; + + Ok(sk.into_keypair()) +} + #[wasm_bindgen] #[allow(missing_docs)] pub fn get_pk_from_keypair(kp: &XfrKeyPair) -> XfrPublicKey { diff --git a/src/libs/globutils/src/wallet.rs b/src/libs/globutils/src/wallet.rs index cddc6207d..bba6467ac 100644 --- a/src/libs/globutils/src/wallet.rs +++ b/src/libs/globutils/src/wallet.rs @@ -8,12 +8,13 @@ use bech32::{self, FromBase32, ToBase32}; use bip0039::{Count, Language, Mnemonic}; use ed25519_dalek_bip32::{DerivationPath, ExtendedSecretKey}; use noah::anon_xfr::structs::Nullifier; +use noah::xfr::sig::XfrPublicKeyInner; use noah::{ anon_xfr::{ keys::{AXfrKeyPair, AXfrPubKey}, structs::Commitment, }, - xfr::sig::{XfrKeyPair, XfrPublicKey, XfrSecretKey}, + xfr::sig::{KeyType, XfrKeyPair, XfrPublicKey, XfrSecretKey}, }; use noah_algebra::serialization::NoahFromToBytes; use noah_crypto::basic::hybrid_encryption::{XPublicKey, XSecretKey}; @@ -313,17 +314,40 @@ pub fn nullifier_to_base58(n: &Nullifier) -> String { bs58::encode(&Nullifier::noah_to_bytes(n)).into_string() } +/// Convert a XfrPublicKey to human-readable address +#[inline(always)] +pub fn public_key_to_human(key: &XfrPublicKey) -> String { + match key.inner() { + XfrPublicKeyInner::Ed25519(_) | XfrPublicKeyInner::Secp256k1(_) => { + public_key_to_bech32(key) + } + XfrPublicKeyInner::Address(bytes) => { + // checksum encode + let hex = hex::encode(&bytes); + let mut res = String::from("0x"); + for byte in hex[..40].chars() { + if byte.to_digit(16).unwrap() > 7 { + res += &byte.to_uppercase().to_string(); + } else { + res += &byte.to_string(); + } + } + res + }, + } +} + /// Convert a XfrPublicKey to bech32 human-readable address #[inline(always)] pub fn public_key_to_bech32(key: &XfrPublicKey) -> String { let bytes = &XfrPublicKey::noah_to_bytes(key); - match bytes[0] { - 0u8 => bech32enc_fra(<&[u8; 32]>::try_from(&bytes[1..33]).unwrap()), - 1u8 => bech32enc_eth(<&[u8; 33]>::try_from(&bytes[1..34]).unwrap()), - 2u8 => { - panic!("public key not supported") + let keytype = KeyType::from_byte(bytes[0]); + match keytype { + KeyType::Ed25519 => bech32enc_fra(<&[u8; 32]>::try_from(&bytes[1..33]).unwrap()), + KeyType::Secp256k1 => { + bech32enc_eth(<&[u8; 33]>::try_from(&bytes[1..34]).unwrap()) } - _ => { + KeyType::Address => { panic!("public key not supported") } } From ffd01dc42285a35ed30d38dac84d794a6647b1be Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 29 Nov 2022 18:19:56 +1300 Subject: [PATCH 2/4] fix fmt and lint --- src/libs/globutils/src/wallet.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/globutils/src/wallet.rs b/src/libs/globutils/src/wallet.rs index bba6467ac..0d12236bc 100644 --- a/src/libs/globutils/src/wallet.rs +++ b/src/libs/globutils/src/wallet.rs @@ -323,7 +323,7 @@ pub fn public_key_to_human(key: &XfrPublicKey) -> String { } XfrPublicKeyInner::Address(bytes) => { // checksum encode - let hex = hex::encode(&bytes); + let hex = hex::encode(bytes); let mut res = String::from("0x"); for byte in hex[..40].chars() { if byte.to_digit(16).unwrap() > 7 { @@ -333,7 +333,7 @@ pub fn public_key_to_human(key: &XfrPublicKey) -> String { } } res - }, + } } } From 963b8883a47c8694ae3dffea50933d13d7d74902 Mon Sep 17 00:00:00 2001 From: Sun Date: Mon, 5 Dec 2022 15:06:41 +1300 Subject: [PATCH 3/4] Fix the eth checksum address --- src/libs/globutils/Cargo.toml | 1 + src/libs/globutils/src/wallet.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libs/globutils/Cargo.toml b/src/libs/globutils/Cargo.toml index 01bc71da1..4ba1a5a7f 100644 --- a/src/libs/globutils/Cargo.toml +++ b/src/libs/globutils/Cargo.toml @@ -26,6 +26,7 @@ ed25519-dalek-bip32 = { git = "https://github.com/FindoraNetwork/ed25519-dalek-b tracing = "0.1.13" tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } bs58 = "0.4" +sha3 = "0.10" [dev-dependencies] rand_chacha = "0.3" diff --git a/src/libs/globutils/src/wallet.rs b/src/libs/globutils/src/wallet.rs index 0d12236bc..2a927a820 100644 --- a/src/libs/globutils/src/wallet.rs +++ b/src/libs/globutils/src/wallet.rs @@ -19,6 +19,7 @@ use noah::{ use noah_algebra::serialization::NoahFromToBytes; use noah_crypto::basic::hybrid_encryption::{XPublicKey, XSecretKey}; use ruc::*; +use sha3::{Digest, Keccak256}; /// Randomly generate a 12words-length mnemonic. #[inline(always)] @@ -324,9 +325,15 @@ pub fn public_key_to_human(key: &XfrPublicKey) -> String { XfrPublicKeyInner::Address(bytes) => { // checksum encode let hex = hex::encode(bytes); + + let mut hasher = Keccak256::new(); + hasher.update(hex.as_bytes()); + let hash = hasher.finalize(); + let check_hash = hex::encode(&hash); + let mut res = String::from("0x"); - for byte in hex[..40].chars() { - if byte.to_digit(16).unwrap() > 7 { + for (index, byte) in hex[..40].chars().enumerate() { + if check_hash.chars().nth(index).unwrap().to_digit(16).unwrap() > 7 { res += &byte.to_uppercase().to_string(); } else { res += &byte.to_string(); From 42b50afc45d2933bee1f9165647952324258555d Mon Sep 17 00:00:00 2001 From: Sun Date: Thu, 8 Dec 2022 10:07:40 +1300 Subject: [PATCH 4/4] fix lint --- src/libs/globutils/src/wallet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/globutils/src/wallet.rs b/src/libs/globutils/src/wallet.rs index 2a927a820..59ad3b21c 100644 --- a/src/libs/globutils/src/wallet.rs +++ b/src/libs/globutils/src/wallet.rs @@ -329,7 +329,7 @@ pub fn public_key_to_human(key: &XfrPublicKey) -> String { let mut hasher = Keccak256::new(); hasher.update(hex.as_bytes()); let hash = hasher.finalize(); - let check_hash = hex::encode(&hash); + let check_hash = hex::encode(hash); let mut res = String::from("0x"); for (index, byte) in hex[..40].chars().enumerate() {