Skip to content

Commit

Permalink
Merge pull request fedimint#6304 from tvolk131/bitcoin32_bitcoind
Browse files Browse the repository at this point in the history
chore: partially bump bitcoin to v0.32
  • Loading branch information
tvolk131 authored Nov 8, 2024
2 parents 95c17b4 + 725dcb1 commit d224592
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 207 deletions.
11 changes: 2 additions & 9 deletions fedimint-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use clap::Subcommand;
use fedimint_bip39::Mnemonic;
use fedimint_client::backup::Metadata;
use fedimint_client::ClientHandleArc;
use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_pubkey;
use fedimint_core::config::{ClientModuleConfig, FederationId};
use fedimint_core::core::{ModuleInstanceId, ModuleKind, OperationId};
use fedimint_core::encoding::Encodable;
Expand Down Expand Up @@ -341,10 +340,7 @@ 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.map(|pk| bitcoin32_to_bitcoin30_secp256k1_pubkey(&pk)),
force_internal,
)
.get_gateway(gateway_id, force_internal)
.await?;

let lightning_module = client.get_first_module::<LightningClientModule>()?;
Expand Down Expand Up @@ -402,10 +398,7 @@ 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.map(|pk| bitcoin32_to_bitcoin30_secp256k1_pubkey(&pk)),
force_internal,
)
.get_gateway(gateway_id, force_internal)
.await?;

let lightning_module = client.get_first_module::<LightningClientModule>()?;
Expand Down
2 changes: 1 addition & 1 deletion fedimint-core/src/net/api_announcement.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::BTreeMap;

use bitcoin30::hashes::{sha256, Hash};
use bitcoin30::secp256k1::Message;
use fedimint_core::db::DatabaseLookup;
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::task::MaybeSend;
use fedimint_core::PeerId;
use futures::StreamExt;
use jsonrpsee_core::Serialize;
use secp256k1_27::Message;
use serde::Deserialize;

use crate::bitcoin_migration::bitcoin30_to_bitcoin32_secp256k1_message;
Expand Down
5 changes: 1 addition & 4 deletions fedimint-load-test-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use common::{
use devimint::cmd;
use devimint::util::{GatewayClnCli, GatewayLndCli};
use fedimint_client::ClientHandleArc;
use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_pubkey;
use fedimint_core::endpoint_constants::SESSION_COUNT_ENDPOINT;
use fedimint_core::invite_code::InviteCode;
use fedimint_core::module::ApiRequestErased;
Expand Down Expand Up @@ -632,9 +631,7 @@ async fn get_lightning_gateway(
let ln_module = client
.get_first_module::<LightningClientModule>()
.expect("Must have ln client module");
ln_module
.select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id))
.await
ln_module.select_gateway(&gateway_id).await
}

#[allow(clippy::too_many_arguments)]
Expand Down
53 changes: 17 additions & 36 deletions gateway/ln-gateway/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ use std::collections::BTreeMap;
use bitcoin::Network;
use bitcoin_hashes::sha256;
use fedimint_api_client::api::net::Connector;
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_keypair,
};
use fedimint_core::config::FederationId;
use fedimint_core::db::{
CoreMigrationFn, DatabaseTransaction, DatabaseVersion, IDatabaseTransactionOpsCoreTyped,
};
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::invite_code::InviteCode;
use fedimint_core::secp256k1::Keypair;
use fedimint_core::{
impl_db_lookup, impl_db_record, push_db_pair_items, secp256k1_27 as secp256k1, Amount,
};
use fedimint_core::{impl_db_lookup, impl_db_record, push_db_pair_items, secp256k1, Amount};
use fedimint_ln_common::serde_routing_fees;
use fedimint_lnv2_common::contracts::{IncomingContract, PaymentImage};
use futures::{FutureExt, StreamExt};
use lightning_invoice::RoutingFees;
use rand::rngs::OsRng;
use rand::Rng;
use secp256k1::{KeyPair, Secp256k1};
use secp256k1::{Keypair, Secp256k1};
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
Expand All @@ -40,18 +34,18 @@ pub trait GatewayDbtxNcExt {
async fn remove_federation_config(&mut self, federation_id: FederationId);

/// Returns the keypair that uniquely identifies the gateway.
async fn load_gateway_keypair(&mut self) -> Option<KeyPair>;
async fn load_gateway_keypair(&mut self) -> Option<Keypair>;

/// Returns the keypair that uniquely identifies the gateway.
///
/// # Panics
/// Gateway keypair does not exist.
async fn load_gateway_keypair_assert_exists(&mut self) -> KeyPair;
async fn load_gateway_keypair_assert_exists(&mut self) -> Keypair;

/// Returns the keypair that uniquely identifies the gateway, creating it if
/// it does not exist. Remember to commit the transaction after calling this
/// method.
async fn load_or_create_gateway_keypair(&mut self) -> KeyPair;
async fn load_or_create_gateway_keypair(&mut self) -> Keypair;

async fn load_gateway_config(&mut self) -> Option<GatewayConfiguration>;

Expand Down Expand Up @@ -124,33 +118,24 @@ impl<Cap: Send> GatewayDbtxNcExt for DatabaseTransaction<'_, Cap> {
.await;
}

async fn load_gateway_keypair(&mut self) -> Option<KeyPair> {
self.get_value(&GatewayPublicKey)
.await
.map(|kp| bitcoin32_to_bitcoin30_keypair(&kp))
async fn load_gateway_keypair(&mut self) -> Option<Keypair> {
self.get_value(&GatewayPublicKey).await
}

async fn load_gateway_keypair_assert_exists(&mut self) -> KeyPair {
bitcoin32_to_bitcoin30_keypair(
&self
.get_value(&GatewayPublicKey)
.await
.expect("Gateway keypair does not exist"),
)
async fn load_gateway_keypair_assert_exists(&mut self) -> Keypair {
self.get_value(&GatewayPublicKey)
.await
.expect("Gateway keypair does not exist")
}

async fn load_or_create_gateway_keypair(&mut self) -> KeyPair {
async fn load_or_create_gateway_keypair(&mut self) -> Keypair {
if let Some(key_pair) = self.get_value(&GatewayPublicKey).await {
bitcoin32_to_bitcoin30_keypair(&key_pair)
key_pair
} else {
let context = Secp256k1::new();
let (secret_key, _public_key) = context.generate_keypair(&mut OsRng);
let key_pair = KeyPair::from_secret_key(&context, &secret_key);
self.insert_new_entry(
&GatewayPublicKey,
&bitcoin30_to_bitcoin32_keypair(&key_pair),
)
.await;
let key_pair = Keypair::from_secret_key(&context, &secret_key);
self.insert_new_entry(&GatewayPublicKey, &key_pair).await;
key_pair
}
}
Expand Down Expand Up @@ -497,12 +482,8 @@ mod fedimint_migration_tests {

let context = secp256k1::Secp256k1::new();
let (secret, _) = context.generate_keypair(&mut OsRng);
let key_pair = secp256k1::KeyPair::from_secret_key(&context, &secret);
dbtx.insert_new_entry(
&GatewayPublicKey,
&bitcoin30_to_bitcoin32_keypair(&key_pair),
)
.await;
let key_pair = secp256k1::Keypair::from_secret_key(&context, &secret);
dbtx.insert_new_entry(&GatewayPublicKey, &key_pair).await;

let gateway_configuration = GatewayConfigurationV0 {
password: "EXAMPLE".to_string(),
Expand Down
10 changes: 3 additions & 7 deletions gateway/ln-gateway/src/federation_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use std::sync::Arc;

use bitcoin::secp256k1::Keypair;
use fedimint_client::ClientHandleArc;
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_keypair,
};
use fedimint_core::config::{FederationId, FederationIdPrefix, JsonClientConfig};
use fedimint_core::db::{DatabaseTransaction, NonCommittable};
use fedimint_core::util::Spanned;
Expand Down Expand Up @@ -68,8 +65,7 @@ impl FederationManager {
) -> AdminResult<FederationInfo> {
let federation_info = self.federation_info(federation_id, dbtx).await?;

let gateway_keypair =
bitcoin30_to_bitcoin32_keypair(&dbtx.load_gateway_keypair_assert_exists().await);
let gateway_keypair = dbtx.load_gateway_keypair_assert_exists().await;

self.unannounce_from_federation(federation_id, gateway_keypair)
.await?;
Expand Down Expand Up @@ -145,7 +141,7 @@ impl FederationManager {
client
.value()
.get_first_module::<GatewayClientModule>()?
.remove_from_federation(bitcoin32_to_bitcoin30_keypair(&gateway_keypair))
.remove_from_federation(gateway_keypair)
.await;

Ok(())
Expand All @@ -162,7 +158,7 @@ impl FederationManager {
.value()
.get_first_module::<GatewayClientModule>()
.expect("Must have client module")
.remove_from_federation(bitcoin32_to_bitcoin30_keypair(&gateway_keypair))
.remove_from_federation(gateway_keypair)
.await;
})
.collect::<Vec<_>>();
Expand Down
25 changes: 9 additions & 16 deletions gateway/ln-gateway/src/gateway_module_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::fmt;
use std::sync::Arc;

use anyhow::{anyhow, ensure};
use bitcoin_hashes::sha256;
use bitcoin::hashes::sha256;
use bitcoin::secp256k1::Message;
use fedimint_api_client::api::DynModuleApi;
use fedimint_client::module::init::{ClientModuleInit, ClientModuleInitArgs};
use fedimint_client::module::recovery::NoModuleBackup;
Expand All @@ -19,9 +20,6 @@ use fedimint_client::transaction::{
ClientOutput, ClientOutputBundle, ClientOutputSM, TransactionBuilder,
};
use fedimint_client::{sm_enum_variant_translation, DynGlobalClientContext};
use fedimint_core::bitcoin_migration::{
bitcoin32_to_bitcoin30_schnorr_signature, bitcoin32_to_bitcoin30_secp256k1_pubkey,
};
use fedimint_core::config::FederationId;
use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId};
use fedimint_core::db::DatabaseTransaction;
Expand All @@ -30,9 +28,7 @@ use fedimint_core::module::{
ApiVersion, CommonModuleInit, ModuleCommon, ModuleInit, MultiApiVersion,
};
use fedimint_core::secp256k1::Keypair;
use fedimint_core::{
apply, async_trait_maybe_send, secp256k1_27 as secp256k1, Amount, OutPoint, PeerId,
};
use fedimint_core::{apply, async_trait_maybe_send, secp256k1, Amount, OutPoint, PeerId};
use fedimint_lnv2_common::config::LightningClientConfig;
use fedimint_lnv2_common::contracts::{IncomingContract, PaymentImage};
use fedimint_lnv2_common::gateway_api::SendPaymentPayload;
Expand Down Expand Up @@ -268,14 +264,11 @@ impl GatewayClientModuleV2 {
ensure!(
secp256k1::SECP256K1
.verify_schnorr(
&bitcoin32_to_bitcoin30_schnorr_signature(&payload.auth),
&payload
.invoice
.consensus_hash_bitcoin30::<sha256::Hash>()
.into(),
&bitcoin32_to_bitcoin30_secp256k1_pubkey(&payload.contract.refund_pk)
.x_only_public_key()
.0,
&payload.auth,
&Message::from_digest(
*payload.invoice.consensus_hash::<sha256::Hash>().as_ref()
),
&payload.contract.refund_pk.x_only_public_key().0,
)
.is_ok(),
"Invalid auth signature for the invoice data"
Expand Down Expand Up @@ -368,7 +361,7 @@ impl GatewayClientModuleV2 {

assert!(state.common.contract.verify_forfeit_signature(&signature));

return Err(bitcoin32_to_bitcoin30_schnorr_signature(&signature));
return Err(signature);
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions gateway/ln-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ use fedimint_bip39::{Bip39RootSecretStrategy, Language, Mnemonic};
use fedimint_client::module::init::ClientModuleInitRegistry;
use fedimint_client::secret::RootSecretStrategy;
use fedimint_client::{Client, ClientHandleArc};
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_keypair, bitcoin30_to_bitcoin32_secp256k1_pubkey,
};
use fedimint_core::config::FederationId;
use fedimint_core::core::{
ModuleInstanceId, ModuleKind, LEGACY_HARDCODED_INSTANCE_ID_MINT,
Expand All @@ -58,8 +55,8 @@ use fedimint_core::core::{
use fedimint_core::db::{apply_migrations_server, Database, DatabaseTransaction};
use fedimint_core::invite_code::InviteCode;
use fedimint_core::module::CommonModuleInit;
use fedimint_core::secp256k1::schnorr::Signature;
use fedimint_core::secp256k1::PublicKey;
use fedimint_core::secp256k1_27::schnorr::Signature;
use fedimint_core::task::{sleep, TaskGroup, TaskHandle, TaskShutdownToken};
use fedimint_core::time::duration_since_epoch;
use fedimint_core::util::{SafeUrl, Spanned};
Expand Down Expand Up @@ -382,7 +379,7 @@ impl Gateway {
let mut dbtx = gateway_db.begin_transaction().await;
let keypair = dbtx.load_or_create_gateway_keypair().await;
dbtx.commit_tx().await;
bitcoin30_to_bitcoin32_secp256k1_pubkey(&keypair.public_key())
keypair.public_key()
}

pub fn gateway_id(&self) -> PublicKey {
Expand Down Expand Up @@ -1910,7 +1907,7 @@ impl Gateway {
self.federation_manager
.read()
.await
.unannounce_from_all_federations(bitcoin30_to_bitcoin32_keypair(&gateway_keypair))
.unannounce_from_all_federations(gateway_keypair)
.await;
}
}
Expand Down
Loading

0 comments on commit d224592

Please sign in to comment.