From 94637ed525d9b56519014eff4ea4062b021db77b Mon Sep 17 00:00:00 2001 From: Damir Vodenicarevic Date: Fri, 29 Dec 2023 17:38:42 +0100 Subject: [PATCH] fix wallet export version (#4603) * fix wallet export version * tolerance on key encoding * fmt --- Cargo.lock | 149 +++++++++++++++++++------------------- massa-wallet/src/error.rs | 2 + massa-wallet/src/lib.rs | 36 ++++++++- 3 files changed, 109 insertions(+), 78 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f810e9ca937..bc19ba14f2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,9 +156,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.76" +version = "1.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59d2a3357dde987206219e78ecfbbb6e8dad06cbb65292758d3270e6254f7355" +checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9" [[package]] name = "arrayref" @@ -254,7 +254,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -265,7 +265,7 @@ checksum = "fdf6721fb0140e4f897002dd086c06f6c27775df19cfe1fccb21181a48fd2c98" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -330,7 +330,7 @@ dependencies = [ "cfg-if", "libc", "miniz_oxide", - "object 0.32.1", + "object 0.32.2", "rustc-demangle", ] @@ -379,7 +379,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -628,7 +628,7 @@ dependencies = [ "heck", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -875,9 +875,9 @@ dependencies = [ [[package]] name = "crossbeam" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c" +checksum = "6eb9105919ca8e40d437fc9cbb8f1975d916f1bd28afe795a48aae32a2cc8920" dependencies = [ "cfg-if", "crossbeam-channel", @@ -889,9 +889,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.9" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c3242926edf34aec4ac3a77108ad4854bffaa2e4ddc1824124ce59231302d5" +checksum = "82a9b73a36529d9c47029b9fb3a6f0ea3cc916a261195352ba19e770fc1748b2" dependencies = [ "cfg-if", "crossbeam-utils", @@ -910,21 +910,20 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.16" +version = "0.9.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d2fe95351b870527a5d09bf563ed3c97c0cffb87cf1c78a591bf48bb218d9aa" +checksum = "0e3681d554572a651dda4186cd47240627c3d0114d45a95f6ad27f2f22e7548d" dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", - "memoffset", ] [[package]] name = "crossbeam-queue" -version = "0.3.9" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9bcf5bdbfdd6030fb4a1c497b5d5fc5921aa2f60d359a17e249c0e6df3de153" +checksum = "adc6598521bb5a83d491e8c1fe51db7296019d2ca3cb93cc6c2a20369a4d78a2" dependencies = [ "cfg-if", "crossbeam-utils", @@ -932,9 +931,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.17" +version = "0.8.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d96137f14f244c37f989d9fff8f95e6c18b918e71f36638f8c49112e4c78f" +checksum = "c3a430a770ebd84726f584a90ee7f020d28db52c6d02138900f22341f866d39c" dependencies = [ "cfg-if", ] @@ -1010,7 +1009,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -1058,7 +1057,7 @@ dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", "strsim", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -1080,7 +1079,7 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core 0.20.3", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -1215,7 +1214,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -1362,7 +1361,7 @@ dependencies = [ "darling 0.20.3", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -1504,9 +1503,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" dependencies = [ "futures-channel", "futures-core", @@ -1519,9 +1518,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", "futures-sink", @@ -1529,15 +1528,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" [[package]] name = "futures-executor" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" dependencies = [ "futures-core", "futures-task", @@ -1546,32 +1545,32 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" [[package]] name = "futures-macro" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] name = "futures-sink" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" [[package]] name = "futures-task" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" [[package]] name = "futures-timer" @@ -1585,9 +1584,9 @@ dependencies = [ [[package]] name = "futures-util" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-channel", "futures-core", @@ -3543,7 +3542,7 @@ dependencies = [ "cfg-if", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -3729,7 +3728,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -3746,9 +3745,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.1" +version = "0.32.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" dependencies = [ "memchr", ] @@ -3951,7 +3950,7 @@ dependencies = [ "pest_meta", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -3992,7 +3991,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -4118,7 +4117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" dependencies = [ "proc-macro2 1.0.71", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -4239,7 +4238,7 @@ dependencies = [ "prost", "prost-types", "regex", - "syn 2.0.42", + "syn 2.0.43", "tempfile", "which 4.4.2", ] @@ -4254,7 +4253,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -4765,7 +4764,7 @@ checksum = "5a32af5427251d2e4be14fc151eabe18abb4a7aad5efee7044da9f096c906a43" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -4785,11 +4784,11 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.22" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" +checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -4894,7 +4893,7 @@ checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -4945,7 +4944,7 @@ dependencies = [ "darling 0.20.3", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -4983,7 +4982,7 @@ checksum = "91d129178576168c589c9ec973feedf7d3126c01ac2bf08795109aa35b69fb8f" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -5200,7 +5199,7 @@ dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", "rustversion", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -5242,9 +5241,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.42" +version = "2.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b7d0a2c048d661a1a59fcd7355baa232f7ed34e0ee4df2eef3c1c1c0d3852d8" +checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", @@ -5302,22 +5301,22 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "thiserror" -version = "1.0.51" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7" +checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.51" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df" +checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -5431,7 +5430,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -5560,7 +5559,7 @@ dependencies = [ "proc-macro2 1.0.71", "prost-build", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -5680,7 +5679,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -5947,7 +5946,7 @@ dependencies = [ "once_cell", "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", "wasm-bindgen-shared", ] @@ -5981,7 +5980,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -6528,9 +6527,9 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "winnow" -version = "0.5.30" +version = "0.5.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b5c3db89721d50d0e2a673f5043fc4722f76dcc352d7b1ab8b8288bed4ed2c5" +checksum = "97a4882e6b134d6c28953a387571f1acdd3496830d5e36c5e3a1075580ea641c" dependencies = [ "memchr", ] @@ -6597,7 +6596,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] @@ -6617,7 +6616,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2 1.0.71", "quote 1.0.33", - "syn 2.0.42", + "syn 2.0.43", ] [[package]] diff --git a/massa-wallet/src/error.rs b/massa-wallet/src/error.rs index 2e9ea39d5ca..fba6d80ca02 100644 --- a/massa-wallet/src/error.rs +++ b/massa-wallet/src/error.rs @@ -24,4 +24,6 @@ pub enum WalletError { MissingKeyError(Address), /// `MassaCipher` error: {0} MassaCipherError(#[from] massa_cipher::CipherError), + /// Version error: {0} + VersionError(String), } diff --git a/massa-wallet/src/lib.rs b/massa-wallet/src/lib.rs index 251c5b0e121..6e20116bf0d 100644 --- a/massa-wallet/src/lib.rs +++ b/massa-wallet/src/lib.rs @@ -22,6 +22,8 @@ use std::str::FromStr; mod error; +const WALLET_VERSION: u64 = 1; + /// Contains the keypairs created in the wallet. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Wallet { @@ -59,8 +61,19 @@ impl Wallet { let path = entry.path(); if path.is_file() { let content = &std::fs::read(&path)?[..]; - let wallet = serde_yaml::from_slice::(content)?; - let secret_key = decrypt( + let mut wallet = serde_yaml::from_slice::(content)?; + if wallet.version == 0 { + // fix bug in handling version 0 + wallet.version = 1; + } + // check version + if wallet.version != WALLET_VERSION { + return Err(WalletError::VersionError(format!( + "Unsupported wallet version {}", + wallet.version + ))); + } + let mut secret_key = decrypt( &password, CipherData { salt: wallet.salt, @@ -68,6 +81,23 @@ impl Wallet { encrypted_bytes: wallet.ciphered_data, }, )?; + // check secret key length + match secret_key.len() { + 33 => { + // standard compliant: version(1B) + privkey(32B) + }, + 65 => { + // version(1B) + privkey(32B) + pubkey(32B) + // truncate to standard compliant: version(1B) + privkey(32B) + secret_key.truncate(33); + }, + 32 | 64 if wallet.version == 0 => { + return Err(WalletError::VersionError("Your wallet is from an old version that does not follow the standard. Please create a new wallet.".to_string())) + } + _ => { + return Err(WalletError::VersionError("Invalid wallet/version matching: your wallet does not follow its version's secret key encoding format.".to_string())) + } + } keys.insert( Address::from_str(&wallet.address)?, KeyPair::from_bytes(&secret_key)?, @@ -174,7 +204,7 @@ impl Wallet { for (addr, keypair) in &self.keys { let encrypted_secret = encrypt(&self.password, &keypair.to_bytes())?; let file_formatted = WalletFileFormat { - version: keypair.get_version(), + version: WALLET_VERSION, nickname: addr.to_string(), address: addr.to_string(), salt: encrypted_secret.salt,