From 09ae919d0318955c1e2fa49c0f40dfa049a2c2bf Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Wed, 8 Nov 2023 15:32:53 +0100 Subject: [PATCH] Hashing password according to wallet.dat format --- moat-cli/src/command.rs | 2 +- moat-cli/src/main.rs | 3 ++ .../src/blockchain_payloads/payload_sender.rs | 2 +- wallet-accessor/Cargo.toml | 1 + wallet-accessor/src/wallet_accessor.rs | 30 +++++++++++++------ 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/moat-cli/src/command.rs b/moat-cli/src/command.rs index 9c1f8b4..1bda2b4 100644 --- a/moat-cli/src/command.rs +++ b/moat-cli/src/command.rs @@ -200,7 +200,7 @@ impl Command { blockchain_access_config: &BlockchainAccessConfig, ) -> Result { let wallet_accessor = - WalletAccessor::new(wallet_path.clone(), psw.clone()); + WalletAccessor::create(wallet_path.clone(), psw.clone())?; let note_hashes: Vec = wallet_accessor .get_notes(blockchain_access_config) .await? diff --git a/moat-cli/src/main.rs b/moat-cli/src/main.rs index f710ec9..d526af4 100644 --- a/moat-cli/src/main.rs +++ b/moat-cli/src/main.rs @@ -67,7 +67,10 @@ async fn main() -> Result<(), CliError> { interactor.run_loop().await?; #[rustfmt::skip] + // old wallet.dat file format: // cargo r --release --bin moat-cli -- --wallet-path ~/.dusk/rusk-wallet --config-path ./moat-cli/config.toml --lp-config-path ./moat-cli/lp.json --pwd-hash 7f2611ba158b6dcea4a69c229c303358c5e04493abeadee106a4bfa464d55787 ./moat-cli/request.json + // new wallet.dat file format: + // cargo r --release --bin moat-cli -- --wallet-path ~/.dusk/rusk-wallet --config-path ./moat-cli/config.toml --lp-config-path ./moat-cli/lp.json --pwd-hash 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 ./moat-cli/request.json Ok(()) } diff --git a/moat-core/src/blockchain_payloads/payload_sender.rs b/moat-core/src/blockchain_payloads/payload_sender.rs index e151d48..3a2feb9 100644 --- a/moat-core/src/blockchain_payloads/payload_sender.rs +++ b/moat-core/src/blockchain_payloads/payload_sender.rs @@ -32,7 +32,7 @@ impl PayloadSender { M: AsRef, { let wallet_accessor = - WalletAccessor::new(wallet_path.clone(), password.clone()); + WalletAccessor::create(wallet_path.clone(), password.clone())?; let tx_id = wallet_accessor .execute_contract_method( payload, diff --git a/wallet-accessor/Cargo.toml b/wallet-accessor/Cargo.toml index bd41872..4367d18 100644 --- a/wallet-accessor/Cargo.toml +++ b/wallet-accessor/Cargo.toml @@ -15,3 +15,4 @@ serde = { version = "1", features = ["derive"] } toml-base-config = "0.1" sha2 = "0.10" hex = "0.4" +blake3 = "1.4" diff --git a/wallet-accessor/src/wallet_accessor.rs b/wallet-accessor/src/wallet_accessor.rs index 444e38e..7f054af 100644 --- a/wallet-accessor/src/wallet_accessor.rs +++ b/wallet-accessor/src/wallet_accessor.rs @@ -7,8 +7,9 @@ use crate::wallet_accessor::Password::{Pwd, PwdHash}; use crate::BlockchainAccessConfig; use dusk_bls12_381::BlsScalar; +use dusk_wallet::dat::{read_file_version, DatFileVersion}; use dusk_wallet::gas::Gas; -use dusk_wallet::{DecodedNote, SecureWalletFile, Wallet, WalletPath}; +use dusk_wallet::{DecodedNote, Error, SecureWalletFile, Wallet, WalletPath}; use dusk_wallet_core::MAX_CALL_SIZE; use phoenix_core::transaction::ModuleId; use rkyv::ser::serializers::AllocSerializer; @@ -39,23 +40,34 @@ impl SecureWalletFile for WalletAccessor { } impl WalletAccessor { - pub fn new(path: WalletPath, pwd: Password) -> Self { - Self { - path, + pub fn create( + wallet_path: WalletPath, + pwd: Password, + ) -> Result { + let dat_file_version = read_file_version(&wallet_path)?; + let is_sha256 = + matches!(dat_file_version, DatFileVersion::RuskBinaryFileFormat(_)); + Ok(Self { + path: wallet_path, pwd: pwd.clone(), pwd_bytes: { match &pwd { Pwd(s) => { - let mut hasher = Sha256::new(); - hasher.update(s.as_bytes()); - hasher.finalize().to_vec() + if is_sha256 { + let mut hasher = Sha256::new(); + hasher.update(s.as_bytes()); + hasher.finalize().to_vec() + } else { + let hash = blake3::hash(s.as_bytes()); + hash.as_bytes().to_vec() + } } PwdHash(h) => hex::decode(h.as_str()) .expect("Password hash should be valid hex string") .to_vec(), } }, - } + }) } async fn get_wallet( @@ -63,7 +75,7 @@ impl WalletAccessor { cfg: &BlockchainAccessConfig, ) -> Result, dusk_wallet::Error> { let wallet_accessor = - WalletAccessor::new(self.path.clone(), self.pwd.clone()); + WalletAccessor::create(self.path.clone(), self.pwd.clone())?; let mut wallet = Wallet::from_file(wallet_accessor)?; wallet .connect_with_status(