Skip to content

Commit

Permalink
Merge pull request fedimint#6212 from tvolk131/bitcoin32_v2
Browse files Browse the repository at this point in the history
chore(deps): partially bump bitcoin to v0.32
  • Loading branch information
elsirion authored Oct 27, 2024
2 parents 32a0de2 + b52aac8 commit d131200
Show file tree
Hide file tree
Showing 45 changed files with 305 additions and 120 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ reqwest = { version = "0.12.8", features = [
], default-features = false }
ring = "0.17.8"
secp256k1 = { version = "0.27.0", default-features = false }
secp256k1_29 = { package = "secp256k1", version = "0.29.0", default-features = false }
semver = "1.0.23"
serde = { version = "1.0.213", features = ["derive"] }
serdect = "0.2.0"
Expand Down
2 changes: 1 addition & 1 deletion fedimint-api-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ path = "src/lib.rs"
anyhow = { workspace = true }
async-trait = { workspace = true }
base64 = { workspace = true }
bitcoin30 = { workspace = true }
bitcoin = { workspace = true }
fedimint-core = { workspace = true }
fedimint-logging = { workspace = true }
futures = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions fedimint-api-client/src/api/global_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::result;
use std::sync::Arc;

use anyhow::anyhow;
use bitcoin30::hashes::sha256;
use bitcoin30::secp256k1;
use bitcoin::hashes::sha256;
use bitcoin::secp256k1;
use fedimint_core::admin_client::{
ConfigGenConnectionsRequest, ConfigGenParamsRequest, ConfigGenParamsResponse, PeerServerParams,
};
Expand Down
4 changes: 2 additions & 2 deletions fedimint-api-client/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use anyhow::anyhow;
#[cfg(all(feature = "tor", not(target_family = "wasm")))]
use arti_client::{TorAddr, TorClient, TorClientConfig};
use base64::Engine as _;
use bitcoin30::hashes::sha256;
use bitcoin30::secp256k1;
use bitcoin::hashes::sha256;
use bitcoin::secp256k1;
pub use error::{FederationError, OutputOutcomeError, PeerError};
use fedimint_core::admin_client::{
ConfigGenConnectionsRequest, ConfigGenParamsRequest, ConfigGenParamsResponse, PeerServerParams,
Expand Down
2 changes: 1 addition & 1 deletion fedimint-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ path = "src/lib.rs"
[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
bitcoin30 = { workspace = true }
bitcoin = { workspace = true }
clap = { workspace = true }
clap_complete = "4.5.35"
fedimint-aead = { workspace = true }
Expand Down
46 changes: 35 additions & 11 deletions fedimint-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ use std::str::FromStr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{bail, Context};
use bitcoin30::address::NetworkUnchecked;
use bitcoin30::{secp256k1, Network};
use bitcoin::address::NetworkUnchecked;
use bitcoin::{secp256k1, Network};
use clap::Subcommand;
use fedimint_bip39::Mnemonic;
use fedimint_client::backup::Metadata;
use fedimint_client::ClientHandleArc;
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_network, bitcoin32_to_bitcoin30_amount,
bitcoin32_to_bitcoin30_secp256k1_pubkey, bitcoin32_to_bitcoin30_unchecked_address,
};
use fedimint_core::config::{ClientModuleConfig, FederationId};
use fedimint_core::core::{ModuleInstanceId, ModuleKind, OperationId};
use fedimint_core::encoding::Encodable;
Expand Down Expand Up @@ -155,7 +159,7 @@ pub enum ClientCmd {
#[clap(long)]
amount: BitcoinAmountOrAll,
#[clap(long)]
address: bitcoin30::Address<NetworkUnchecked>,
address: bitcoin::Address<NetworkUnchecked>,
},
/// Upload the (encrypted) snapshot of mint notes to federation
Backup {
Expand Down Expand Up @@ -340,7 +344,10 @@ pub async fn handle_command(
warn!("Command deprecated. Use `fedimint-cli module ln invoice` instead.");
let lightning_module = client.get_first_module::<LightningClientModule>()?;
let ln_gateway = lightning_module
.get_gateway(gateway_id, force_internal)
.get_gateway(
gateway_id.map(|pk| bitcoin32_to_bitcoin30_secp256k1_pubkey(&pk)),
force_internal,
)
.await?;

let lightning_module = client.get_first_module::<LightningClientModule>()?;
Expand Down Expand Up @@ -398,7 +405,10 @@ pub async fn handle_command(
info!("Paying invoice: {bolt11}");
let lightning_module = client.get_first_module::<LightningClientModule>()?;
let ln_gateway = lightning_module
.get_gateway(gateway_id, force_internal)
.get_gateway(
gateway_id.map(|pk| bitcoin32_to_bitcoin30_secp256k1_pubkey(&pk)),
force_internal,
)
.await?;

let lightning_module = client.get_first_module::<LightningClientModule>()?;
Expand Down Expand Up @@ -544,10 +554,14 @@ pub async fn handle_command(
// If the amount is "all", then we need to subtract the fees from
// the amount we are withdrawing
BitcoinAmountOrAll::All => {
let balance =
bitcoin30::Amount::from_sat(client.get_balance().await.msats / 1000);
let balance = bitcoin32_to_bitcoin30_amount(&bitcoin::Amount::from_sat(
client.get_balance().await.msats / 1000,
));
let fees = wallet_module
.get_withdraw_fees(address.clone(), balance)
.get_withdraw_fees(
bitcoin32_to_bitcoin30_unchecked_address(&address),
balance,
)
.await?;
let amount = balance.checked_sub(fees.amount());
if amount.is_none() {
Expand All @@ -558,15 +572,25 @@ pub async fn handle_command(
BitcoinAmountOrAll::Amount(amount) => (
amount,
wallet_module
.get_withdraw_fees(address.clone(), amount)
.get_withdraw_fees(
bitcoin32_to_bitcoin30_unchecked_address(&address),
amount,
)
.await?,
),
};
let absolute_fees = fees.amount();

info!("Attempting withdraw with fees: {fees:?}");

let operation_id = wallet_module.withdraw(address, amount, fees, ()).await?;
let operation_id = wallet_module
.withdraw(
bitcoin32_to_bitcoin30_unchecked_address(&address),
amount,
fees,
(),
)
.await?;

let mut updates = wallet_module
.subscribe_withdraw_updates(operation_id)
Expand Down Expand Up @@ -656,7 +680,7 @@ async fn get_note_summary(client: &ClientHandleArc) -> anyhow::Result<serde_json
.await;
Ok(serde_json::to_value(InfoResponse {
federation_id: client.federation_id(),
network: wallet_client.get_network(),
network: bitcoin30_to_bitcoin32_network(&wallet_client.get_network()),
meta: client.config().await.global.meta.clone(),
total_amount_msat: summary.total_amount(),
total_num_notes: summary.count_items(),
Expand Down
3 changes: 2 additions & 1 deletion fedimint-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ anyhow = { workspace = true }
aquamarine = { workspace = true }
async-stream = { workspace = true }
async-trait = { workspace = true }
bitcoin30 = { workspace = true }
bitcoin = { workspace = true }
fedimint-aead = { workspace = true }
fedimint-api-client = { path = "../fedimint-api-client", version = "=0.5.0-alpha", default-features = false }
fedimint-core = { workspace = true }
Expand All @@ -38,6 +38,7 @@ futures = { workspace = true }
itertools = { workspace = true }
rand = { workspace = true }
reqwest = { workspace = true }
secp256k1 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
strum = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion fedimint-client/src/api_announcements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::{bail, Context};
use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_pubkey;
use fedimint_core::config::ClientConfig;
use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
use fedimint_core::encoding::{Decodable, Encodable};
Expand Down Expand Up @@ -66,7 +67,7 @@ pub async fn run_api_announcement_sync(client_inner: Arc<Client>) {
bail!("Guardian public key not found for peer {}", peer_id);
};

if !announcement.verify(SECP256K1, guardian_pub_key) {
if !announcement.verify(SECP256K1, &bitcoin32_to_bitcoin30_secp256k1_pubkey(guardian_pub_key)) {
bail!("Failed to verify announcement for peer {}", peer_id);
}
}
Expand Down
24 changes: 16 additions & 8 deletions fedimint-client/src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::collections::{BTreeMap, BTreeSet};
use std::io::{Cursor, Error, Read, Write};

use anyhow::{bail, ensure, Context, Result};
use bitcoin30::secp256k1::{KeyPair, PublicKey, Secp256k1, SignOnly};
use bitcoin::secp256k1::{Keypair, PublicKey};
use fedimint_api_client::api::DynGlobalApi;
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_keypair,
};
use fedimint_core::core::backup::{
BackupRequest, SignedBackupRequest, BACKUP_REQUEST_MAX_PAYLOAD_SIZE_BYTES,
};
Expand All @@ -14,6 +17,7 @@ use fedimint_core::encoding::{Decodable, DecodeError, Encodable};
use fedimint_core::module::registry::ModuleDecoderRegistry;
use fedimint_derive_secret::DerivableSecret;
use fedimint_logging::{LOG_CLIENT, LOG_CLIENT_BACKUP, LOG_CLIENT_RECOVERY};
use secp256k1::{Secp256k1, SignOnly};
use serde::{Deserialize, Serialize};
use tracing::{debug, info, warn};

Expand Down Expand Up @@ -223,14 +227,16 @@ impl EncryptedClientBackup {
)?)
}

pub fn into_backup_request(self, keypair: &KeyPair) -> Result<SignedBackupRequest> {
pub fn into_backup_request(self, keypair: &Keypair) -> Result<SignedBackupRequest> {
let keypair = bitcoin32_to_bitcoin30_keypair(keypair);

let request = BackupRequest {
id: keypair.public_key(),
timestamp: fedimint_core::time::now(),
payload: self.0,
};

request.sign(keypair)
request.sign(&keypair)
}

pub fn len(&self) -> usize {
Expand Down Expand Up @@ -402,17 +408,19 @@ impl Client {

/// Static version of [`Self::get_derived_backup_signing_key`] for testing
/// without creating whole `MintClient`
fn get_derived_backup_signing_key_static(secret: &DerivableSecret) -> KeyPair {
secret
.derive_backup_secret()
.to_secp_key(&Secp256k1::<SignOnly>::gen_new())
fn get_derived_backup_signing_key_static(secret: &DerivableSecret) -> Keypair {
bitcoin30_to_bitcoin32_keypair(
&secret
.derive_backup_secret()
.to_secp_key(&Secp256k1::<SignOnly>::gen_new()),
)
}

fn get_derived_backup_encryption_key(&self) -> fedimint_aead::LessSafeKey {
Self::get_derived_backup_encryption_key_static(&self.root_secret())
}

fn get_derived_backup_signing_key(&self) -> KeyPair {
fn get_derived_backup_signing_key(&self) -> Keypair {
Self::get_derived_backup_signing_key_static(&self.root_secret())
}

Expand Down
5 changes: 3 additions & 2 deletions fedimint-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use anyhow::{anyhow, bail, ensure, format_err, Context};
use api::ClientRawFederationApiExt as _;
use async_stream::{stream, try_stream};
use backup::ClientBackup;
use bitcoin30::secp256k1;
use bitcoin::secp256k1;
use db::event_log::{
self, run_event_log_ordering_task, DBTransactionEventLogExt, Event, EventKind, EventLogEntry,
EventLogId,
Expand All @@ -107,6 +107,7 @@ use fedimint_api_client::api::{
ApiVersionSet, DynGlobalApi, DynModuleApi, FederationApiExt, GlobalFederationApiWithCacheExt,
IGlobalFederationApi, WsFederationApi,
};
use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_secp256k1_pubkey;
use fedimint_core::config::{
ClientConfig, FederationId, GlobalClientConfig, JsonClientConfig, ModuleInitRegistry,
};
Expand Down Expand Up @@ -2076,7 +2077,7 @@ impl Client {
dbtx.insert_entry(&ClientConfigKey, &new_config).await;
*(self.config.write().await) = new_config;
guardian_pub_keys
};
}.into_iter().map(|(peer_id, pubkey)| (peer_id, bitcoin30_to_bitcoin32_secp256k1_pubkey(&pubkey))).collect();

Result::<_, ()>::Ok(guardian_pub_keys)
}), None).await.expect("Will retry forever")
Expand Down
2 changes: 1 addition & 1 deletion fedimint-client/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use std::{ffi, marker, ops};

use anyhow::{anyhow, bail};
use bitcoin30::secp256k1::PublicKey;
use bitcoin::secp256k1::PublicKey;
use fedimint_api_client::api::DynGlobalApi;
use fedimint_core::config::ClientConfig;
use fedimint_core::core::{
Expand Down
Loading

0 comments on commit d131200

Please sign in to comment.