Skip to content

Commit

Permalink
feat: add hashing api to wallet secret keys (#4424)
Browse files Browse the repository at this point in the history
Description
---
Partial fulfillment for #4395:
- base_layer/wallet/src/output_manager_service/service.rs, fn hash_secret_key(...)
- base_layer/wallet/src/transaction_service/service.rs, fn hash_secret_key(...)

Motivation and Context
---
Domain separated hashing as per the `tari_crypto` hashing API is needed for secret keys 

How Has This Been Tested?
---
Unit tests pass
Cucumber tests pass
  • Loading branch information
hansieodendaal committed Aug 10, 2022
1 parent 98a7b0c commit d944574
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions base_layer/wallet/src/lib.rs
Expand Up @@ -25,6 +25,7 @@ pub mod util;
pub mod wallet;

pub use operation_id::OperationId;
use tari_crypto::{hash::blake2::Blake256, hash_domain, hashing::DomainSeparatedHasher};

#[macro_use]
extern crate diesel;
Expand Down Expand Up @@ -54,3 +55,10 @@ pub type WalletSqlite = Wallet<
ContactsServiceSqliteDatabase,
KeyManagerSqliteDatabase,
>;

hash_domain!(
WalletSecretKeysDomain,
"com.tari.tari_project.base_layer.wallet.secret_keys",
1
);
type WalletSecretKeysDomainHasher = DomainSeparatedHasher<Blake256, WalletSecretKeysDomain>;
8 changes: 6 additions & 2 deletions base_layer/wallet/src/output_manager_service/service.rs
Expand Up @@ -22,7 +22,6 @@

use std::{convert::TryInto, fmt, sync::Arc};

use blake2::Digest;
use diesel::result::{DatabaseErrorKind, Error as DieselError};
use futures::{pin_mut, StreamExt};
use itertools::Itertools;
Expand Down Expand Up @@ -98,6 +97,7 @@ use crate::{
tasks::TxoValidationTask,
},
types::WalletHasher,
WalletSecretKeysDomainHasher,
};

const LOG_TARGET: &str = "wallet::output_manager_service";
Expand Down Expand Up @@ -2712,7 +2712,11 @@ impl fmt::Display for Balance {
}

fn hash_secret_key(key: &PrivateKey) -> Vec<u8> {
Blake256::new().chain(key.as_bytes()).finalize().to_vec()
WalletSecretKeysDomainHasher::new()
.chain(key.as_bytes())
.finalize()
.as_ref()
.to_vec()
}

#[derive(Debug, Clone)]
Expand Down
8 changes: 6 additions & 2 deletions base_layer/wallet/src/transaction_service/service.rs
Expand Up @@ -66,7 +66,6 @@ use tari_core::{
};
use tari_crypto::{
commitment::HomomorphicCommitmentFactory,
hash::blake2::Blake256,
keys::{DiffieHellmanSharedSecret, PublicKey as PKtrait, SecretKey},
tari_utilities::ByteArray,
};
Expand Down Expand Up @@ -120,6 +119,7 @@ use crate::{
util::watch::Watch,
utxo_scanner_service::RECOVERY_KEY,
OperationId,
WalletSecretKeysDomainHasher,
};

const LOG_TARGET: &str = "wallet::transaction_service::service";
Expand Down Expand Up @@ -2628,7 +2628,11 @@ pub struct PendingCoinbaseSpendingKey {
}

fn hash_secret_key(key: &PrivateKey) -> Vec<u8> {
Blake256::new().chain(key.as_bytes()).finalize().to_vec()
WalletSecretKeysDomainHasher::new()
.chain(key.as_bytes())
.finalize()
.as_ref()
.to_vec()
}

/// Contains the generated TxId and TransactionStatus transaction send result
Expand Down

0 comments on commit d944574

Please sign in to comment.