Skip to content

Commit

Permalink
Update deps and get compiling (#167)
Browse files Browse the repository at this point in the history
* Update deps and update

* Update winnow to `v0.7.0`

* Get compiling

* Get tests compiling
  • Loading branch information
praveenperera authored Feb 5, 2025
1 parent ec66f1f commit 319b578
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 211 deletions.
395 changes: 249 additions & 146 deletions rust/Cargo.lock

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ macros = { path = "./crates/macros" }

# bdk / bitcoin
bdk_core = { version = "0.4" }
bdk_wallet = { version = "1.0.0", features = ["keys-bip39", "file_store"] }
bdk_wallet = { version = "1.1", features = ["keys-bip39", "file_store"] }
bip39 = { version = "2.0.0", features = ["zeroize"] }

# bitcoin nodes
bdk_esplora = { version = "0.20", features = ["async-https", "tokio"] }
bdk_electrum = { version = "0.20", features = [
bdk_electrum = { version = "0.21", features = [
"use-rustls-ring",
], default-features = false }
# bdk_bitcoind_rpc = { version = "0.12", features = [] }
Expand All @@ -39,8 +39,6 @@ pubport = { version = "0.2.0", git = "https://github.com/bitcoinppl/pubport.git"
] }

bitcoin = "0.32"
bitcoin-units = "0.1"
bitcoin_hashes = "0.14.0"

# async / actors
tokio = { version = "1.38", features = ["rt"] }
Expand All @@ -50,7 +48,7 @@ futures = "0.3.30"
tryhard = "0.5"

# crypto
rand = "0.8.5"
rand = "0.9"
zeroize = "1.8.1"

# hashing
Expand Down Expand Up @@ -81,7 +79,7 @@ redb = "2.1"
nid = "3.0"

# derive stuff
derive_more = { version = "1.0", features = [
derive_more = { version = "2.0", features = [
"display",
"add",
"mul",
Expand Down Expand Up @@ -137,7 +135,7 @@ hex = "0.4.3"
base64 = "0.22.0"

# parsing
winnow = { version = "0.6.0", features = ["simd"] }
winnow = { version = "0.7", features = ["simd"] }
memchr = "2.7"

# sync
Expand Down
4 changes: 2 additions & 2 deletions rust/src/bip39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ mod test {
#[test]
fn check_final_words_are_accurate_12_words() {
// 128 / 8 = 16
let random_bytes = rand::thread_rng().gen::<[u8; 16]>();
let random_bytes = rand::rng().random::<[u8; 16]>();
let words = Mnemonic::from_entropy(&random_bytes)
.expect("failed to create mnemonic")
.words()
Expand All @@ -198,7 +198,7 @@ mod test {
#[test]
fn check_final_words_are_accurate_24_words() {
// 256 / 8 = 32
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
let random_bytes = rand::rng().random::<[u8; 32]>();
let words = Mnemonic::from_entropy(&random_bytes)
.expect("failed to create mnemonic")
.words()
Expand Down
53 changes: 26 additions & 27 deletions rust/src/cove_nfc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use winnow::{
bits::{bits, bool as take_bool, take as take_bits},
Endianness,
},
error::{ErrMode, InputError, ParserError as _},
error::{ContextError, ErrMode},
token::{any, literal, take},
PResult, Parser,
ModalResult, Parser,
};

use crate::cove_nfc::{
Expand All @@ -20,7 +20,10 @@ use crate::cove_nfc::{
record::NdefRecord,
};

pub fn parse_ndef_records(input: &mut Stream<'_>, info: &MessageInfo) -> PResult<Vec<NdefRecord>> {
pub fn parse_ndef_records(
input: &mut Stream<'_>,
info: &MessageInfo,
) -> ModalResult<Vec<NdefRecord>> {
let mut records = Vec::new();
let payload_length = info.payload_length as usize;
let mut total_parsed_bytes = 0;
Expand All @@ -42,7 +45,7 @@ pub fn parse_ndef_records(input: &mut Stream<'_>, info: &MessageInfo) -> PResult
Ok(records)
}

pub fn parse_ndef_record(input: &mut Stream<'_>) -> PResult<NdefRecord> {
pub fn parse_ndef_record(input: &mut Stream<'_>) -> ModalResult<NdefRecord> {
let header = parse_header.parse_next(input)?;
let type_ = parse_type(input, header.type_length)?;
let id = parse_id(input, header.id_length)?;
Expand All @@ -56,7 +59,7 @@ pub fn parse_ndef_record(input: &mut Stream<'_>) -> PResult<NdefRecord> {
})
}

pub fn parse_message_info(input: &mut Stream<'_>) -> PResult<MessageInfo> {
pub fn parse_message_info(input: &mut Stream<'_>) -> ModalResult<MessageInfo> {
let _ = literal([226, 67, 0, 1, 0, 0, 4, 0, 3]).parse_next(input)?;

let length_indicator = be_u8.parse_next(input)?;
Expand All @@ -71,8 +74,8 @@ pub fn parse_message_info(input: &mut Stream<'_>) -> PResult<MessageInfo> {
}

// private
fn parse_header_byte(input: &mut Stream<'_>) -> PResult<(bool, bool, bool, bool, bool, u8)> {
bits::<_, _, InputError<(_, usize)>, _, _>((
fn parse_header_byte(input: &mut Stream<'_>) -> ModalResult<(bool, bool, bool, bool, bool, u8)> {
bits::<_, _, ErrMode<ContextError>, _, _>((
take_bool,
take_bool,
take_bool,
Expand All @@ -81,12 +84,9 @@ fn parse_header_byte(input: &mut Stream<'_>) -> PResult<(bool, bool, bool, bool,
take_bits(3_u8),
))
.parse_next(input)
.map_err(|_: ErrMode<InputError<_>>| {
ErrMode::from_error_kind(input, winnow::error::ErrorKind::Slice)
})
}

fn parse_header(input: &mut Stream<'_>) -> PResult<NdefHeader> {
fn parse_header(input: &mut Stream<'_>) -> ModalResult<NdefHeader> {
let (message_begin, message_end, chunked, short_record, has_id_length, type_name_format) =
parse_header_byte(input)?;

Expand Down Expand Up @@ -129,13 +129,13 @@ fn parse_header(input: &mut Stream<'_>) -> PResult<NdefHeader> {
})
}

fn parse_type(input: &mut Stream<'_>, type_length: u8) -> PResult<Vec<u8>> {
fn parse_type(input: &mut Stream<'_>, type_length: u8) -> ModalResult<Vec<u8>> {
take(type_length as usize)
.map(|s: &[u8]| s.to_vec())
.parse_next(input)
}

fn parse_id(input: &mut Stream<'_>, id_length: Option<u8>) -> PResult<Option<Vec<u8>>> {
fn parse_id(input: &mut Stream<'_>, id_length: Option<u8>) -> ModalResult<Option<Vec<u8>>> {
if let Some(id_len) = id_length {
take(id_len as usize)
.map(|s: &[u8]| Some(s.to_vec()))
Expand All @@ -149,14 +149,11 @@ fn parse_payload(
input: &mut Stream<'_>,
payload_length: u32,
type_: &[u8],
) -> PResult<NdefPayload> {
) -> ModalResult<NdefPayload> {
if type_ == b"T" {
let (is_utf16, language_code_length): (bool, u8) =
bits::<_, _, InputError<(_, usize)>, _, _>((take_bool, take_bits(7_u8)))
.parse_next(input)
.map_err(|_: ErrMode<InputError<_>>| {
ErrMode::from_error_kind(input, winnow::error::ErrorKind::Slice)
})?;
bits::<_, _, ErrMode<ContextError>, _, _>((take_bool, take_bits(7_u8)))
.parse_next(input)?;

let language_code = take(language_code_length as usize).parse_next(input)?;

Expand Down Expand Up @@ -196,7 +193,10 @@ fn parse_payload(
mod tests {
use std::sync::LazyLock;

use winnow::{error::Needed, Bytes};
use winnow::{
error::{ErrMode, Needed},
Bytes,
};

use super::*;

Expand All @@ -207,7 +207,7 @@ mod tests {
Stream::new(Bytes::new(bytes))
}

fn parse_ndef_message(input: &mut Stream<'_>) -> PResult<Vec<NdefRecord>> {
fn parse_ndef_message(input: &mut Stream<'_>) -> ModalResult<Vec<NdefRecord>> {
let info = parse_message_info.parse_next(input)?;
parse_ndef_records(input, &info)
}
Expand Down Expand Up @@ -457,12 +457,10 @@ mod tests {
let mut export = owned_stream((*EXPORT)[..100].to_vec());
let message = parse_ndef_message(&mut export);

assert!(matches!(
message,
Err(winnow::error::ErrMode::Incomplete(Needed::Size(_)))
));
assert!(message.is_err());
assert!(matches!(message, Err(ErrMode::Incomplete(Needed::Size(_)))));

if let Err(winnow::error::ErrMode::Incomplete(needed)) = message {
if let Err(ErrMode::Incomplete(needed)) = message {
assert_eq!(needed, Needed::new(original_length - 101));
}
}
Expand All @@ -482,7 +480,8 @@ mod tests {
Ok(message) => {
assert_eq!(message.len(), 1)
}
Err(winnow::error::ErrMode::Incomplete(Needed::Size(_))) => {

Err(e) if e.is_incomplete() => {
chunks_processed += 1;
data = chunk_data;
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ fn database_location() -> PathBuf {

#[cfg(test)]
fn database_location() -> PathBuf {
use rand::distributions::Alphanumeric;
use rand::distr::Alphanumeric;
use rand::prelude::*;

let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let random_string: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect();
let cove_db = format!("cove_{}.db", random_string);

Expand Down
2 changes: 1 addition & 1 deletion rust/src/database/unsigned_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cmp::Ordering, sync::Arc};

use bitcoin_hashes::{sha256d::Hash, Hash as _};
use bitcoin::hashes::{sha256d::Hash, Hash as _};
use redb::TableDefinition;
use tracing::debug;

Expand Down
2 changes: 1 addition & 1 deletion rust/src/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use base64::prelude::BASE64_STANDARD;
use base64::Engine as _;
use chacha20poly1305::aead::OsRng;
use chacha20poly1305::{aead::Aead as _, AeadCore as _, ChaCha20Poly1305, KeyInit as _};
use chacha20poly1305::{Key, Nonce};
use rand::rngs::OsRng;

use macros::impl_default_for;

Expand Down
2 changes: 1 addition & 1 deletion rust/src/manager/wallet/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use bdk_chain::{
spk_client::{FullScanResponse, SyncResponse},
};
use bdk_wallet::{KeychainKind, TxOrdering};
use bitcoin::Amount;
use bitcoin::{params::Params, Transaction as BdkTransaction};
use bitcoin_units::Amount;
use crossbeam::channel::Sender;
use eyre::Context as _;
use std::time::{Duration, UNIX_EPOCH};
Expand Down
4 changes: 2 additions & 2 deletions rust/src/mnemonic/number_of_bip39_words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ impl NumberOfBip39Words {
match self {
NumberOfBip39Words::Twelve => {
// 128 / 8 = 16
let random_bytes = rand::thread_rng().gen::<[u8; 16]>();
let random_bytes = rand::rng().random::<[u8; 16]>();
Mnemonic::from_entropy(&random_bytes).expect("failed to create mnemonic")
}
NumberOfBip39Words::TwentyFour => {
// 256 / 8 = 32
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
let random_bytes = rand::rng().random::<[u8; 32]>();
Mnemonic::from_entropy(&random_bytes).expect("failed to create mnemonic")
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/push_tx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bitcoin::base64::{prelude::BASE64_URL_SAFE, Engine as _};
use winnow::{
token::{take_until, take_while},
PResult, Parser as _,
Parser as _, Result as WinnowResult,
};

use crate::transaction::ffi::BitcoinTransaction;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl PushTx {
}
}

fn extract_tx<'s>(input: &mut &'s str) -> PResult<&'s str> {
fn extract_tx<'s>(input: &mut &'s str) -> WinnowResult<&'s str> {
// skip until we find "pushtx#t="
let _ = take_until(1.., "pushtx#t=").parse_next(input)?;

Expand Down
6 changes: 3 additions & 3 deletions rust/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub type Unit = unit::Unit;
pub type TransactionDetails = transaction_details::TransactionDetails;
pub type FeeRate = fees::FeeRate;

pub type BdkAmount = bitcoin_units::Amount;
pub type BdkAmount = bitcoin::Amount;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, uniffi::Enum)]
pub enum TransactionDirection {
Expand Down Expand Up @@ -108,8 +108,8 @@ pub struct OutPoint {

impl TxId {
pub fn preview_new() -> Self {
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
let hash = *bitcoin_hashes::sha256d::Hash::from_bytes_ref(&random_bytes);
let random_bytes = rand::rng().random::<[u8; 32]>();
let hash = *bitcoin::hashes::sha256d::Hash::from_bytes_ref(&random_bytes);

Self(BdkTxid::from_raw_hash(hash))
}
Expand Down
8 changes: 4 additions & 4 deletions rust/src/transaction/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ pub struct Amount(pub BdkAmount);
// rust only
impl Amount {
pub fn from_btc(btc: f64) -> Result<Self, eyre::Report> {
Ok(Self(bitcoin_units::Amount::from_btc(btc)?))
Ok(Self(bitcoin::Amount::from_btc(btc)?))
}
}

#[uniffi::export]
impl Amount {
#[uniffi::constructor]
pub fn from_sat(sats: u64) -> Self {
Self(bitcoin_units::Amount::from_sat(sats))
Self(bitcoin::Amount::from_sat(sats))
}

#[uniffi::constructor]
pub fn one_btc() -> Self {
Self(bitcoin_units::Amount::ONE_BTC)
Self(bitcoin::Amount::ONE_BTC)
}

#[uniffi::constructor]
pub fn one_sat() -> Self {
Self(bitcoin_units::Amount::ONE_SAT)
Self(bitcoin::Amount::ONE_SAT)
}

pub fn as_btc(&self) -> f64 {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/transaction/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use derive_more::{AsRef, Deref, Display, From, Into};

// MARK: FeeRate
//
pub type BdkFeeRate = bitcoin_units::FeeRate;
pub type BdkFeeRate = bitcoin::FeeRate;

#[derive(
Debug,
Expand Down
10 changes: 5 additions & 5 deletions rust/src/transaction/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn transaction_preview_confirmed_new() -> Transaction {

impl SentAndReceived {
pub fn preview_new() -> Self {
let rand = rand::thread_rng().gen_range(0..3);
let rand = rand::rng().random_range(0..3);

let direction = if rand == 0 {
TransactionDirection::Outgoing
Expand Down Expand Up @@ -294,17 +294,17 @@ impl SentAndReceived {
}

fn random_block_height() -> u32 {
rand::thread_rng().gen_range(0..850_000)
rand::rng().random_range(0..850_000)
}

fn random_amount() -> u64 {
rand::thread_rng().gen_range(100_000..=200_000_000)
rand::rng().random_range(100_000..=200_000_000)
}

#[uniffi::export]
fn transaction_preview_unconfirmed_new() -> Transaction {
let rand_hours = rand::thread_rng().gen_range(0..4);
let rand_minutes = rand::thread_rng().gen_range(0..60);
let rand_hours = rand::rng().random_range(0..4);
let rand_minutes = rand::rng().random_range(0..60);
let random_last_seen = rand_hours.hours().minutes(rand_minutes);

let last_seen = jiff::Timestamp::now()
Expand Down
2 changes: 1 addition & 1 deletion rust/src/transaction/sent_and_received.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Amount, TransactionDirection, Unit};
use bitcoin_units::Amount as BdkAmount;
use bitcoin::Amount as BdkAmount;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, uniffi::Object)]
pub struct SentAndReceived {
Expand Down
Loading

0 comments on commit 319b578

Please sign in to comment.