Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing password according to wallet.dat format #36

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion moat-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Command {
blockchain_access_config: &BlockchainAccessConfig,
) -> Result<RunResult, Error> {
let wallet_accessor =
WalletAccessor::new(wallet_path.clone(), psw.clone());
WalletAccessor::create(wallet_path.clone(), psw.clone())?;
let note_hashes: Vec<BlsScalar> = wallet_accessor
.get_notes(blockchain_access_config)
.await?
Expand Down
3 changes: 3 additions & 0 deletions moat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion moat-core/src/blockchain_payloads/payload_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl PayloadSender {
M: AsRef<str>,
{
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,
Expand Down
1 change: 1 addition & 0 deletions wallet-accessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ serde = { version = "1", features = ["derive"] }
toml-base-config = "0.1"
sha2 = "0.10"
hex = "0.4"
blake3 = "1.4"
30 changes: 21 additions & 9 deletions wallet-accessor/src/wallet_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,31 +40,42 @@ 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<Self, Error> {
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(
&self,
cfg: &BlockchainAccessConfig,
) -> Result<Wallet<WalletAccessor>, 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(
Expand Down
Loading