From f285e0f6c497c8ef065427c40bbcfe80517b2294 Mon Sep 17 00:00:00 2001 From: aya015757881 <2581015450@qq.com> Date: Thu, 22 Aug 2024 12:43:32 +0800 Subject: [PATCH 1/2] fix: use GBK encoding for Chinese phrase --- Cargo.lock | 65 ++++++++++++++++++++++++++++++ anychain-kms/Cargo.toml | 1 + anychain-kms/src/bip39/mnemonic.rs | 24 ++++------- 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd3e5dd..3aa830d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,6 +246,7 @@ dependencies = [ "bs58 0.4.0", "curve25519-dalek 4.1.3", "ed25519-dalek 1.0.1", + "encoding", "encoding_rs", "group", "hex", @@ -1283,6 +1284,70 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encoding" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +dependencies = [ + "encoding-index-japanese", + "encoding-index-korean", + "encoding-index-simpchinese", + "encoding-index-singlebyte", + "encoding-index-tradchinese", +] + +[[package]] +name = "encoding-index-japanese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-korean" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-simpchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-singlebyte" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-tradchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding_index_tests" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" + [[package]] name = "encoding_rs" version = "0.8.34" diff --git a/anychain-kms/Cargo.toml b/anychain-kms/Cargo.toml index 9410aee..0b2a8d9 100644 --- a/anychain-kms/Cargo.toml +++ b/anychain-kms/Cargo.toml @@ -31,6 +31,7 @@ ed25519-dalek = { workspace = true } curve25519-dalek = { workspace = true } encoding_rs = { version = "0.8.33" } group = "0.13.0" +encoding = "0.2.33" [dev-dependencies] hex-literal = "0.4" diff --git a/anychain-kms/src/bip39/mnemonic.rs b/anychain-kms/src/bip39/mnemonic.rs index 1b296d3..c7b8214 100644 --- a/anychain-kms/src/bip39/mnemonic.rs +++ b/anychain-kms/src/bip39/mnemonic.rs @@ -4,9 +4,7 @@ use super::ErrorKind; use super::Language; use super::MnemonicType; use anyhow::Error; -// use encoding::codec::simpchinese::*; -// use encoding::Encoding; -// use encoding_rs::GB18030; +use encoding::{all::GB18030, EncoderTrap, Encoding}; use std::fmt; use std::mem; use unicode_normalization::UnicodeNormalization; @@ -265,20 +263,12 @@ impl Mnemonic { } pub fn as_bytes(&self) -> Vec { - // // use GBK encoding if language is zh-cn - // if self.lang == Language::ChineseSimplified { - // // let mut d = GB18030_ENCODING.raw_encoder(); - // // let mut d = encoding_rs::GB18030; - // // d.raw_feed(&self.phrase, &mut bytes); - // let mut d = GB18030.new_encoder(); - // let mut bytes = Vec::::new(); - // let (_complete, _read, _written, _) = - // d.encode_from_utf8(&self.phrase, &mut bytes, true); - // bytes - // } else { - // self.phrase().as_bytes().to_vec() - // } - self.phrase().as_bytes().to_vec() + match self.lang { + Language::ChineseSimplified => { + GB18030.encode(self.phrase(), EncoderTrap::Strict).unwrap() + } + _ => self.phrase().as_bytes().to_vec(), + } } } From e041fcd4ccf1d1895ed5ae625306bb1098a36803 Mon Sep 17 00:00:00 2001 From: aya015757881 <2581015450@qq.com> Date: Thu, 22 Aug 2024 12:45:31 +0800 Subject: [PATCH 2/2] refactor: version update --- Cargo.lock | 12 +----------- anychain-kms/Cargo.toml | 3 +-- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3aa830d..cb76d8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,14 +240,13 @@ dependencies = [ [[package]] name = "anychain-kms" -version = "0.1.10" +version = "0.1.11" dependencies = [ "anyhow", "bs58 0.4.0", "curve25519-dalek 4.1.3", "ed25519-dalek 1.0.1", "encoding", - "encoding_rs", "group", "hex", "hex-literal", @@ -1348,15 +1347,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" -[[package]] -name = "encoding_rs" -version = "0.8.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" -dependencies = [ - "cfg-if", -] - [[package]] name = "environmental" version = "1.1.4" diff --git a/anychain-kms/Cargo.toml b/anychain-kms/Cargo.toml index 0b2a8d9..571f3b6 100644 --- a/anychain-kms/Cargo.toml +++ b/anychain-kms/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "anychain-kms" description = "A Rust library providing Key Management Schema for AnyChain. Handles general security and signature algorithms." -version = "0.1.10" +version = "0.1.11" keywords = ["cryptography", "security", "signature", "algorithm"] # Workspace inherited keys @@ -29,7 +29,6 @@ hex = { workspace = true } libsecp256k1 = { workspace = true } ed25519-dalek = { workspace = true } curve25519-dalek = { workspace = true } -encoding_rs = { version = "0.8.33" } group = "0.13.0" encoding = "0.2.33"