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

[pull] master from broxus:master #16

Merged
merged 6 commits into from
Jan 15, 2025
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ log = "0.4"
num-bigint = "0.4"
once_cell = "1.12.0"
parking_lot = "0.12.0"
pbkdf2 = { version = "0.9.0", optional = true }
pbkdf2 = { version = "0.12.2", optional = true }
quick_cache = "0.4.1"
rand = { version = "0.8", features = ["getrandom"] , optional = true }
secstr = { version = "0.5.0", features = ["serde"], optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = { version = "0.9.9", optional = true }
sha2 = { version = "0.10.8", optional = true }
thiserror = "1.0"
tiny-jsonrpc = { version = "0.6.0", default-features = false, optional = true }
tokio = { version = "1", default-features = false, features = ["sync"] }
Expand Down
4 changes: 2 additions & 2 deletions nekoton-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ chacha20poly1305 = { version = "0.10.1", optional = true }
hex = "0.4"
hmac = "0.11.0"
js-sys = { version = "0.3", optional = true }
pbkdf2 = { version = "0.9.0", optional = true }
pbkdf2 = { version = "0.12.2", optional = true }
secstr = { version = "0.5.0", features = ["serde"], optional = true }
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.9.9"
sha2 = "0.10.8"
thiserror = "1.0"
zeroize = { version = "1", optional = true }

Expand Down
4 changes: 2 additions & 2 deletions nekoton-utils/src/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chacha20poly1305::aead::Aead;
use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
use pbkdf2::pbkdf2;
use pbkdf2::pbkdf2_hmac;
use secstr::{SecUtf8, SecVec};
use zeroize::Zeroize;

Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn encrypt(
// Calculates symmetric key from user password, using pbkdf2
pub fn symmetric_key_from_password(password: &str, salt: &[u8]) -> Key {
let mut pbkdf2_hash = SecVec::new(vec![0; CREDENTIAL_LEN]);
pbkdf2::<hmac::Hmac<sha2::Sha256>>(
pbkdf2_hmac::<sha2::Sha256>(
password.as_bytes(),
salt,
N_ITER,
Expand Down
38 changes: 29 additions & 9 deletions src/core/token_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl TokenWallet {
owner: MsgAddressInt,
root_token_contract: MsgAddressInt,
handler: Arc<dyn TokenWalletSubscriptionHandler>,
preload_transactions: bool,
) -> Result<TokenWallet> {
let state = match transport.get_contract_state(&root_token_contract).await? {
RawContractState::Exists(state) => state,
Expand All @@ -52,16 +53,35 @@ impl TokenWallet {
} = state.guess_details()?;

let address = state.get_wallet_address(version, &owner)?;

let mut balance = Default::default();
let contract_subscription = ContractSubscription::subscribe(
clock.clone(),
transport,
address,
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
Some(&mut make_transactions_handler(handler.as_ref(), version)),
)
.await?;

let contract_subscription = {
let handler = handler.as_ref();

// NOTE: create handler beforehead to prevent lifetime issues
let mut on_transactions_found = match preload_transactions {
true => Some(make_transactions_handler(handler, version)),
false => None,
};

// Manual map is used here due to unsoundness
// See issue: https://github.com/rust-lang/rust/issues/84305
#[allow(trivial_casts)]
#[allow(clippy::manual_map)]
let on_transactions_found = match &mut on_transactions_found {
Some(handler) => Some(handler as _),
None => None,
};

ContractSubscription::subscribe(
clock.clone(),
transport,
address,
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
on_transactions_found,
)
.await?
};

handler.on_balance_changed(balance.clone());

Expand Down
11 changes: 6 additions & 5 deletions src/crypto/mnemonic/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Error;
use ed25519_dalek::Keypair;
use hmac::{Mac, NewMac};
use pbkdf2::pbkdf2;
use pbkdf2::{
hmac::{Hmac, Mac},
pbkdf2_hmac,
};

use super::LANGUAGE;

Expand All @@ -23,14 +25,13 @@ pub fn derive_from_phrase(phrase: &str) -> Result<Keypair, Error> {
anyhow::bail!("Expected 24 words")
}

let password = hmac::Hmac::<sha2::Sha512>::new_from_slice(phrase.as_bytes())
let password = Hmac::<sha2::Sha512>::new_from_slice(phrase.as_bytes())
.unwrap()
.finalize()
.into_bytes();

let mut res = [0; 512 / 8];
pbkdf2::<hmac::Hmac<sha2::Sha512>>(&password, SALT, PBKDF_ITERATIONS, &mut res);

pbkdf2_hmac::<sha2::Sha512>(&password, SALT, PBKDF_ITERATIONS, &mut res);
let secret = ed25519_dalek::SecretKey::from_bytes(&res[0..32])?;
let public = ed25519_dalek::PublicKey::from(&secret);
Ok(Keypair { secret, public })
Expand Down
Loading