Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
implement bincode or accept byte and sequence forms in serde, `Error` is
`FromSliceError`, and `forward_hex`, `backward_hex` and the `schemars` feature
are removed
- **Breaking:** `BlsPkBytes::from_hex` and `BlsSigBytes::from_hex` report
`DecodeFixedLengthBytesError` instead of `hex::FromHexError`, and
`dashcore_rpc_json::HexError` wraps `DecodeVariableLengthBytesError`
- **Breaking:** hashes honour the format precision, so `{:.8}` prints 8 digits
- **Breaking:** `hash_x11::Hash` loses `forward_hex`, `backward_hex`,
`from_bytes_ref` and `from_bytes_mut`, `hash_x11::Midstate` is replaced by
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ resolver = "2"
bincode = { package = "grovedb-bincode", version = "=2.1.0" }
bincode_derive = { package = "grovedb-bincode-derive", version = "=2.1.0" }
futures = "0.3"
hex = "0.4"
hex_lit = "0.1.1"
rayon = "1.11"
secp256k1 = "0.33.1" # must match with version used in `bitcoin-crypto` and `dash-pkc`
serde = { version = "1.0.219", default-features = false }
Expand All @@ -22,6 +20,7 @@ dash-types = { git = "https://github.com/dashpay/base-sdk", rev = "e6402ced257c3
base58ck = { version = "0.5.0" }
bitcoin_hashes = { version = "0.14.101", default-features = false }
bitcoin-crypto = { git = "https://github.com/rust-bitcoin/rust-bitcoin", rev = "7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516", default-features = false }
hex-conservative = { version = "1.3.0" }

[workspace.package]
authors = ["The rust-dashcore Developers"]
Expand Down
3 changes: 1 addition & 2 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dash-network = { path = "../dash-network" }
dash-pkc = { workspace = true, features = ["std"] }
dash-types = { workspace = true, features = ["codec"] }
dashcore_hashes = { path = "../hashes" }
hex = { workspace = true }
hex-conservative = { workspace = true }
internals = { path = "../internals", package = "dashcore-private" }
secp256k1 = { workspace = true }
serde = { workspace = true, features = ["derive", "alloc"], optional = true }
Expand All @@ -38,5 +38,4 @@ tracing = { version = "0.1", optional = true }
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)', 'cfg(fuzzing)', 'cfg(kani)'] }

[dev-dependencies]
hex_lit = { workspace = true, features = ["rust_v_1_46"] }
serde_json = { version = "1.0" }
22 changes: 10 additions & 12 deletions crypto/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use dash_pkc::bls::{
BlsSecretKey as PkcSecretKey, BlsSignature as PkcSignature, Fr,
};
use dash_types::{make_bytes, make_sbytes};
use hex::FromHexError;
use hex_conservative::DecodeFixedLengthBytesError;
#[cfg(feature = "bls")]
use hex_conservative::DisplayHex;
#[cfg(feature = "bls")]
use thiserror::Error as ThisError;
#[cfg(feature = "bls")]
Expand Down Expand Up @@ -93,10 +95,8 @@ impl BlsPkBytes {
}

/// Reads these bytes from a hex string.
pub fn from_hex(s: &str) -> Result<Self, FromHexError> {
let mut bytes = [0u8; BLS_PK_LEN];
hex::decode_to_slice(s, &mut bytes)?;
Ok(Self::from_bytes(bytes))
pub fn from_hex(s: &str) -> Result<Self, DecodeFixedLengthBytesError> {
Ok(Self::from_bytes(hex_conservative::decode_to_array(s)?))
}

/// Returns `true` when every byte is zero.
Expand Down Expand Up @@ -332,10 +332,8 @@ impl BlsSigBytes {
}

/// Reads these bytes from a hex string.
pub fn from_hex(s: &str) -> Result<Self, FromHexError> {
let mut bytes = [0u8; BLS_SIG_LEN];
hex::decode_to_slice(s, &mut bytes)?;
Ok(Self::from_bytes(bytes))
pub fn from_hex(s: &str) -> Result<Self, DecodeFixedLengthBytesError> {
Ok(Self::from_bytes(hex_conservative::decode_to_array(s)?))
}

/// Returns `true` when every byte is zero.
Expand Down Expand Up @@ -407,7 +405,7 @@ impl BlsSignature {

fn point<S: PkcScheme>(self) -> Result<PkcSignature<S>, BlsError> {
PkcSignature::<S>::from_bytes(self.bytes.as_bytes())
.map_err(|_| BlsError::InvalidSignature(hex::encode(self.bytes.as_bytes())))
.map_err(|_| BlsError::InvalidSignature(self.bytes.as_bytes().to_lower_hex_string()))
}
fn verify_secure_in<'a, S, I>(
self,
Expand Down Expand Up @@ -439,7 +437,7 @@ impl BlsSignature {

#[cfg(all(test, feature = "bls"))]
mod tests {
use hex_lit::hex;
use hex_conservative::hex;

/// Operator public keys from the mainnet quorum at height 2300832.
const OPERATOR_KEYS: [[u8; 48]; 3] = [
Expand Down Expand Up @@ -578,7 +576,7 @@ mod tests {
mod benchmarks {
use super::super::*;
use super::{CHAINLOCK_SIG, OPERATOR_KEYS};
use hex_lit::hex;
use hex_conservative::hex;
use std::time::Instant;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dash-spv = { path = "../dash-spv" }
dashcore = { path = "../dash" }
dash-network = { path = "../dash-network", features = ["ffi"] }
tokio = { version = "1", features = ["full"] }
hex = { workspace = true }
hex-conservative = { workspace = true }
tracing = "0.1"
# Use key-wallet-ffi for all wallet-related FFI types
key-wallet-ffi = { path = "../key-wallet-ffi" }
Expand Down
19 changes: 10 additions & 9 deletions dash-spv-ffi/src/bin/ffi_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ptr;
use clap::{Arg, ArgAction, Command};
use dash_network::ffi::FFINetwork;
use dash_spv_ffi::*;
use hex_conservative::DisplayHex;
use key_wallet_ffi::types::FFIBalance;
use key_wallet_ffi::wallet_manager::wallet_manager_add_wallet_from_mnemonic;
use key_wallet_ffi::FFIError;
Expand Down Expand Up @@ -68,7 +69,7 @@ extern "C" fn on_blocks_needed(blocks: *const FFIBlockNeeded, count: u32, _user_
if !blocks.is_null() && count > 0 {
let blocks_slice = unsafe { std::slice::from_raw_parts(blocks, count as usize) };
for block in blocks_slice.iter() {
println!(" - height: {}, hash: {}", block.height, hex::encode(block.hash));
println!(" - height: {}, hash: {}", block.height, block.hash.to_lower_hex_string());
}
}
}
Expand Down Expand Up @@ -98,8 +99,8 @@ extern "C" fn on_chainlock_received(
validated: bool,
_user_data: *mut c_void,
) {
let hash_hex = unsafe { hex::encode(*hash) };
let signature_hex = unsafe { hex::encode(*signature) };
let hash_hex = unsafe { (*hash).to_lower_hex_string() };
let signature_hex = unsafe { (*signature).to_lower_hex_string() };
println!(
"[Sync] ChainLock received: height={}, hash={}, signature={}, validated={}",
height, hash_hex, signature_hex, validated
Expand All @@ -113,7 +114,7 @@ extern "C" fn on_instantlock_received(
validated: bool,
_user_data: *mut c_void,
) {
let txid_hex = unsafe { hex::encode(*txid) };
let txid_hex = unsafe { (*txid).to_lower_hex_string() };
println!(
"[Sync] InstantLock received: txid={}, validated={}, data_len={}",
txid_hex, validated, instantlock_len
Expand Down Expand Up @@ -211,7 +212,7 @@ extern "C" fn on_transaction_detected(
}
let r = unsafe { &*record };
let b = read_balance(balance);
let txid_hex = hex::encode(r.txid);
let txid_hex = r.txid.to_lower_hex_string();
println!(
"[Wallet] TX detected: wallet={}..., txid={}, account_kind={:?}, account_index={}, amount={} duffs, balance[confirmed={}, unconfirmed={}], changed_accounts={}, derived={}",
wallet_short,
Expand Down Expand Up @@ -254,11 +255,11 @@ extern "C" fn on_transactions_swept(
println!(
"[Wallet] TXs swept: wallet={}..., removed=[{}], superseded_by={}, released=[{}], balance[confirmed={}, unconfirmed={}]",
wallet_short,
list.iter().map(hex::encode).collect::<Vec<_>>().join(","),
hex::encode(winner),
list.iter().map(|h| h.to_lower_hex_string()).collect::<Vec<_>>().join(","),
winner.to_lower_hex_string(),
released
.iter()
.map(|o| format!("{}:{}", hex::encode(o.txid), o.vout))
.map(|o| format!("{}:{}", o.txid.to_lower_hex_string(), o.vout))
.collect::<Vec<_>>()
.join(","),
b.confirmed,
Expand All @@ -283,7 +284,7 @@ extern "C" fn on_transaction_instant_locked(
}
let txid_bytes = unsafe { &*txid };
let b = read_balance(balance);
let txid_hex = hex::encode(txid_bytes);
let txid_hex = txid_bytes.to_lower_hex_string();
println!(
"[Wallet] TX instant-locked: wallet={}..., txid={}, islock_len={}, balance[confirmed={}, unconfirmed={}], changed_accounts={}",
wallet_short,
Expand Down
21 changes: 13 additions & 8 deletions dash-spv-ffi/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use dash_spv::network::NetworkEvent;
use dash_spv::sync::{SyncEvent, SyncProgress};
use dash_spv::EventHandler;
use dashcore::hashes::Hash;
use hex_conservative::DisplayHex;
use key_wallet::account::AccountType;
use key_wallet::WalletCoreBalance;
use key_wallet_ffi::managed_account::{FFIAccountType, FFITransactionRecord};
Expand Down Expand Up @@ -1120,7 +1121,7 @@ impl FFIWalletEventCallbacks {
account_balances,
} => {
if let Some(cb) = self.on_transactions_swept {
let wallet_id_hex = hex::encode(wallet_id);
let wallet_id_hex = wallet_id.to_lower_hex_string();
let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default();
let raw_txids: Vec<[u8; 32]> =
txids.iter().map(|t| t.to_byte_array()).collect();
Expand Down Expand Up @@ -1159,7 +1160,7 @@ impl FFIWalletEventCallbacks {
// additive, so a consumer that leaves this one unset keeps
// transactions the wallet has already dropped.
tracing::warn!(
wallet_id = %hex::encode(wallet_id),
wallet_id = %wallet_id.to_lower_hex_string(),
swept = txids.len(),
%superseded_by,
"no on_transactions_swept callback set; the consumer will keep \
Expand All @@ -1175,7 +1176,7 @@ impl FFIWalletEventCallbacks {
addresses_derived,
} => {
if let Some(cb) = self.on_transaction_detected {
let wallet_id_hex = hex::encode(wallet_id);
let wallet_id_hex = wallet_id.to_lower_hex_string();
let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default();
let ffi_record = FFITransactionRecord::from(record.as_ref());
let ffi_balance = FFIBalance::from(*balance);
Expand Down Expand Up @@ -1215,7 +1216,7 @@ impl FFIWalletEventCallbacks {
account_balances,
} => {
if let Some(cb) = self.on_transaction_instant_locked {
let wallet_id_hex = hex::encode(wallet_id);
let wallet_id_hex = wallet_id.to_lower_hex_string();
let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default();
let txid_bytes = *txid.as_byte_array();
let islock_bytes = dashcore::consensus::serialize(instant_lock);
Expand Down Expand Up @@ -1253,7 +1254,7 @@ impl FFIWalletEventCallbacks {
chain_lock,
} => {
if let Some(cb) = self.on_block_processed {
let wallet_id_hex = hex::encode(wallet_id);
let wallet_id_hex = wallet_id.to_lower_hex_string();
let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default();
let ffi_inserted: Vec<FFITransactionRecord> =
inserted.iter().map(FFITransactionRecord::from).collect();
Expand Down Expand Up @@ -1337,7 +1338,7 @@ impl FFIWalletEventCallbacks {
height,
} => {
if let Some(cb) = self.on_sync_height_advanced {
let wallet_id_hex = hex::encode(wallet_id);
let wallet_id_hex = wallet_id.to_lower_hex_string();
let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default();
cb(c_wallet_id.as_ptr(), *height, self.user_data);
}
Expand All @@ -1348,7 +1349,7 @@ impl FFIWalletEventCallbacks {
locked_transactions,
} => {
if let Some(cb) = self.on_chain_lock_processed {
let wallet_id_hex = hex::encode(wallet_id);
let wallet_id_hex = wallet_id.to_lower_hex_string();
let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default();
let ffi_finalized = FFIChainlockedTxid::from_map(locked_transactions);
let finalized_ptr = if ffi_finalized.is_empty() {
Expand Down Expand Up @@ -1637,7 +1638,11 @@ mod tests {
});

let captured = CAPTURED.lock().unwrap().take().expect("callback fired");
assert_eq!(captured.wallet_id_hex, hex::encode(wallet_id), "wallet_id hex-encoding");
assert_eq!(
captured.wallet_id_hex,
wallet_id.to_lower_hex_string(),
"wallet_id hex-encoding"
);
assert_eq!(captured.cl_height, 777, "cl_height");
assert_eq!(captured.cl_hash, expected_hash, "cl_hash round-trip");
assert_eq!(captured.cl_signature, expected_sig, "cl_signature round-trip");
Expand Down
3 changes: 2 additions & 1 deletion dash-spv-ffi/tests/dashd_sync/tests_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::atomic::Ordering;
use dash_spv::test_utils::{DashdTestContext, TestChain};

use super::context::FFITestContext;
use hex_conservative::DisplayHex;

#[test]
fn test_wallet_sync_via_ffi() {
Expand All @@ -17,7 +18,7 @@ fn test_wallet_sync_via_ffi() {
let ctx = FFITestContext::new(dashd.addr);

let wallet_id = ctx.add_wallet(&dashd.wallet.mnemonic);
tracing::info!("Added wallet, ID: {}", hex::encode(&wallet_id));
tracing::info!("Added wallet, ID: {}", wallet_id.to_lower_hex_string());

ctx.run();
tracing::info!("FFI client running");
Expand Down
3 changes: 1 addition & 2 deletions dash-spv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ chrono = "0.4.20"

# Utilities
rand = "0.9"
hex = { workspace = true }
hex-conservative = { workspace = true }

# Parallelization
rayon = { workspace = true }
Expand All @@ -60,7 +60,6 @@ key-wallet-manager = { path = "../key-wallet-manager", features = ["test-utils"]
criterion = { version = "0.8.1", features = ["async_tokio"] }
tempfile = "3.0"
tokio-test = "0.4"
hex = { workspace = true }
test-case = "3.3"

[[bench]]
Expand Down
3 changes: 2 additions & 1 deletion dash-spv/src/chain/chain_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ impl ChainWork {
let hex = hex.strip_prefix("0x").unwrap_or(hex);

// Parse hex string to bytes
let bytes = hex::decode(hex).map_err(|e| format!("Invalid hex: {}", e))?;
let bytes =
hex_conservative::decode_to_vec(hex).map_err(|e| format!("Invalid hex: {}", e))?;

if bytes.len() != 32 {
return Err(format!("Invalid work length: expected 32 bytes, got {}", bytes.len()));
Expand Down
5 changes: 2 additions & 3 deletions dash-spv/src/chain/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! - Bootstrap masternode lists at specific heights

use dashcore::{BlockHash, CompactTarget, Network, Target};
use dashcore_hashes::{hex, Hash};
use dashcore_hashes::Hash;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -1164,8 +1164,7 @@ pub fn testnet_checkpoints() -> Vec<Checkpoint> {

/// Helper to parse hex block hash strings
fn parse_block_hash(s: &str) -> Result<BlockHash, String> {
use hex::FromHex;
let bytes = Vec::<u8>::from_hex(s).map_err(|e| format!("Invalid hex: {}", e))?;
let bytes = hex_conservative::decode_to_vec(s).map_err(|e| format!("Invalid hex: {}", e))?;
if bytes.len() != 32 {
return Err("Invalid hash length: expected 32 bytes".to_string());
}
Expand Down
3 changes: 2 additions & 1 deletion dash-spv/src/client/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;
use tokio::sync::RwLock;

use super::DashSpvClient;
use hex_conservative::DisplayHex;

impl<W: WalletInterface, N: NetworkManager, S: StorageManager> DashSpvClient<W, N, S> {
// ============ Peer Queries ============
Expand Down Expand Up @@ -107,7 +108,7 @@ impl<W: WalletInterface, N: NetworkManager, S: StorageManager> DashSpvClient<W,
"Quorum not found: type {} at or before height {} with hash {}",
quorum_type,
height,
hex::encode(quorum_hash)
quorum_hash.as_byte_array().to_lower_hex_string()
);
tracing::warn!("{}", message);
SpvError::QuorumLookupError(message)
Expand Down
14 changes: 12 additions & 2 deletions dash-spv/src/validation/chainlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ impl<'a> ChainLockValidator<'a> {
mod tests {
use crate::sml_engine::test_support::TestEngine;
use dashcore::ChainLock;
use dashcore_hashes::Hash;
use hex_conservative::DisplayHex;

#[test]
fn chain_locks_verify_against_the_newest_list() {
Expand All @@ -103,14 +105,22 @@ mod tests {

let [chain_lock, next_chain_lock] = ChainLock::mainnet_fixture_pair();
assert_eq!(
hex::encode(chain_lock.request_id().expect("expected to make request id")),
chain_lock
.request_id()
.expect("expected to make request id")
.as_byte_array()
.to_lower_hex_string(),
"969ab4a945632f5fba1331f3d2556d317682142cf8aaa6544e407e683c61a177"
);
engine.verify_chain_lock(&chain_lock).expect("expected to verify chain lock");

// Another one, so the first was not a 1/4 fluke.
assert_eq!(
hex::encode(next_chain_lock.request_id().expect("expected to make request id")),
next_chain_lock
.request_id()
.expect("expected to make request id")
.as_byte_array()
.to_lower_hex_string(),
"675aed91d6098cdf575cc09bfd1ff4f750acde1e793f385c3c72bbb400068d28"
);
engine.verify_chain_lock(&next_chain_lock).expect("expected to verify chain lock");
Expand Down
Loading
Loading