diff --git a/fedimint-cli/src/client.rs b/fedimint-cli/src/client.rs index 70d643f0c93..dcd54855482 100644 --- a/fedimint-cli/src/client.rs +++ b/fedimint-cli/src/client.rs @@ -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; @@ -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::()?; 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::()?; @@ -402,10 +398,7 @@ pub async fn handle_command( info!("Paying invoice: {bolt11}"); let lightning_module = client.get_first_module::()?; 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::()?; diff --git a/fedimint-core/src/net/api_announcement.rs b/fedimint-core/src/net/api_announcement.rs index 02556ececda..5f27217319d 100644 --- a/fedimint-core/src/net/api_announcement.rs +++ b/fedimint-core/src/net/api_announcement.rs @@ -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; diff --git a/fedimint-load-test-tool/src/main.rs b/fedimint-load-test-tool/src/main.rs index 3f616d895c0..904cb34f8f6 100644 --- a/fedimint-load-test-tool/src/main.rs +++ b/fedimint-load-test-tool/src/main.rs @@ -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; @@ -632,9 +631,7 @@ async fn get_lightning_gateway( let ln_module = client .get_first_module::() .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)] diff --git a/gateway/ln-gateway/src/db.rs b/gateway/ln-gateway/src/db.rs index 544c14f7167..a28124e0ec9 100644 --- a/gateway/ln-gateway/src/db.rs +++ b/gateway/ln-gateway/src/db.rs @@ -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; @@ -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; + async fn load_gateway_keypair(&mut self) -> Option; /// 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; @@ -124,33 +118,24 @@ impl GatewayDbtxNcExt for DatabaseTransaction<'_, Cap> { .await; } - async fn load_gateway_keypair(&mut self) -> Option { - self.get_value(&GatewayPublicKey) - .await - .map(|kp| bitcoin32_to_bitcoin30_keypair(&kp)) + async fn load_gateway_keypair(&mut self) -> Option { + 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 } } @@ -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(), diff --git a/gateway/ln-gateway/src/federation_manager.rs b/gateway/ln-gateway/src/federation_manager.rs index 8729a10b4ac..e383ee91f53 100644 --- a/gateway/ln-gateway/src/federation_manager.rs +++ b/gateway/ln-gateway/src/federation_manager.rs @@ -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; @@ -68,8 +65,7 @@ impl FederationManager { ) -> AdminResult { 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?; @@ -145,7 +141,7 @@ impl FederationManager { client .value() .get_first_module::()? - .remove_from_federation(bitcoin32_to_bitcoin30_keypair(&gateway_keypair)) + .remove_from_federation(gateway_keypair) .await; Ok(()) @@ -162,7 +158,7 @@ impl FederationManager { .value() .get_first_module::() .expect("Must have client module") - .remove_from_federation(bitcoin32_to_bitcoin30_keypair(&gateway_keypair)) + .remove_from_federation(gateway_keypair) .await; }) .collect::>(); diff --git a/gateway/ln-gateway/src/gateway_module_v2/mod.rs b/gateway/ln-gateway/src/gateway_module_v2/mod.rs index 2cbc08142e9..8d075a89662 100644 --- a/gateway/ln-gateway/src/gateway_module_v2/mod.rs +++ b/gateway/ln-gateway/src/gateway_module_v2/mod.rs @@ -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; @@ -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; @@ -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; @@ -268,14 +264,11 @@ impl GatewayClientModuleV2 { ensure!( secp256k1::SECP256K1 .verify_schnorr( - &bitcoin32_to_bitcoin30_schnorr_signature(&payload.auth), - &payload - .invoice - .consensus_hash_bitcoin30::() - .into(), - &bitcoin32_to_bitcoin30_secp256k1_pubkey(&payload.contract.refund_pk) - .x_only_public_key() - .0, + &payload.auth, + &Message::from_digest( + *payload.invoice.consensus_hash::().as_ref() + ), + &payload.contract.refund_pk.x_only_public_key().0, ) .is_ok(), "Invalid auth signature for the invoice data" @@ -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); } } } diff --git a/gateway/ln-gateway/src/lib.rs b/gateway/ln-gateway/src/lib.rs index 29ba2d2c15f..e98c779987d 100644 --- a/gateway/ln-gateway/src/lib.rs +++ b/gateway/ln-gateway/src/lib.rs @@ -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, @@ -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}; @@ -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 { @@ -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; } } diff --git a/gateway/ln-gateway/src/state_machine/mod.rs b/gateway/ln-gateway/src/state_machine/mod.rs index 46e52c7effb..095a894107c 100644 --- a/gateway/ln-gateway/src/state_machine/mod.rs +++ b/gateway/ln-gateway/src/state_machine/mod.rs @@ -23,17 +23,11 @@ use fedimint_client::transaction::{ ClientOutput, ClientOutputBundle, ClientOutputSM, TransactionBuilder, }; use fedimint_client::{sm_enum_variant_translation, AddStateMachinesError, DynGlobalClientContext}; -use fedimint_core::bitcoin_migration::{ - bitcoin30_to_bitcoin32_keypair, bitcoin30_to_bitcoin32_schnorr_signature, - bitcoin30_to_bitcoin32_secp256k1_pubkey, bitcoin32_to_bitcoin30_keypair, -}; use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId}; use fedimint_core::db::{AutocommitError, DatabaseTransaction}; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::module::{ApiVersion, ModuleInit, MultiApiVersion}; -use fedimint_core::{ - apply, async_trait_maybe_send, secp256k1_27 as secp256k1, Amount, OutPoint, TransactionId, -}; +use fedimint_core::{apply, async_trait_maybe_send, secp256k1, Amount, OutPoint, TransactionId}; use fedimint_ln_client::api::LnFederationApi; use fedimint_ln_client::incoming::{ FundingOfferState, IncomingSmCommon, IncomingSmError, IncomingSmStates, IncomingStateMachine, @@ -53,7 +47,7 @@ use fedimint_ln_common::{ }; use futures::StreamExt; use lightning_invoice::RoutingFees; -use secp256k1::KeyPair; +use secp256k1::Keypair; use serde::{Deserialize, Serialize}; use tracing::{debug, error, info, warn}; @@ -149,12 +143,10 @@ impl ClientModuleInit for GatewayClientInit { Ok(GatewayClientModule { cfg: args.cfg().clone(), notifier: args.notifier().clone(), - redeem_key: bitcoin32_to_bitcoin30_keypair( - &args - .module_root_secret() - .child_key(ChildId(0)) - .to_secp_key(&fedimint_core::secp256k1::Secp256k1::new()), - ), + redeem_key: args + .module_root_secret() + .child_key(ChildId(0)) + .to_secp_key(&fedimint_core::secp256k1::Secp256k1::new()), module_api: args.module_api().clone(), timelock_delta: self.timelock_delta, federation_index: self.federation_index, @@ -166,7 +158,7 @@ impl ClientModuleInit for GatewayClientInit { #[derive(Debug, Clone)] pub struct GatewayClientContext { - redeem_key: KeyPair, + redeem_key: Keypair, timelock_delta: u64, secp: Secp256k1, pub ln_decoder: Decoder, @@ -182,7 +174,7 @@ impl From<&GatewayClientContext> for LightningClientContext { fn from(ctx: &GatewayClientContext) -> Self { LightningClientContext { ln_decoder: ctx.ln_decoder.clone(), - redeem_key: bitcoin30_to_bitcoin32_keypair(&ctx.redeem_key), + redeem_key: ctx.redeem_key, gateway_conn: Arc::new(RealGatewayConnection::default()), } } @@ -196,7 +188,7 @@ impl From<&GatewayClientContext> for LightningClientContext { pub struct GatewayClientModule { cfg: LightningClientConfig, pub notifier: ModuleNotifier, - pub redeem_key: KeyPair, + pub redeem_key: Keypair, timelock_delta: u64, federation_index: u64, module_api: DynModuleApi, @@ -255,9 +247,7 @@ impl GatewayClientModule { LightningGatewayAnnouncement { info: LightningGateway { federation_index: self.federation_index, - gateway_redeem_key: bitcoin30_to_bitcoin32_secp256k1_pubkey( - &self.redeem_key.public_key(), - ), + gateway_redeem_key: self.redeem_key.public_key(), node_pub_key: lightning_context.lightning_public_key, lightning_alias: lightning_context.lightning_alias, api: self.gateway.versioned_api.clone(), @@ -288,7 +278,7 @@ impl GatewayClientModule { &self.module_api, htlc.payment_hash, htlc.outgoing_amount_msat, - &bitcoin30_to_bitcoin32_keypair(&self.redeem_key), + &self.redeem_key, ) .await?; @@ -339,7 +329,7 @@ impl GatewayClientModule { &self.module_api, payment_hash, swap.amount_msat, - &bitcoin30_to_bitcoin32_keypair(&self.redeem_key), + &self.redeem_key, ) .await?; @@ -394,7 +384,7 @@ impl GatewayClientModule { /// removing gateway registrations is best effort, this does not return /// an error and simply emits a warning when the registration cannot be /// removed. - pub async fn remove_from_federation(&self, gateway_keypair: KeyPair) { + pub async fn remove_from_federation(&self, gateway_keypair: Keypair) { // Removing gateway registrations is best effort, so just emit a warning if it // fails if let Err(e) = self.remove_from_federation_inner(gateway_keypair).await { @@ -413,11 +403,11 @@ impl GatewayClientModule { /// peer maintains their own list of registered gateways, the gateway /// needs to provide a signature that is signed by the private key of the /// gateway id to remove the registration. - async fn remove_from_federation_inner(&self, gateway_keypair: KeyPair) -> anyhow::Result<()> { + async fn remove_from_federation_inner(&self, gateway_keypair: Keypair) -> anyhow::Result<()> { let gateway_id = gateway_keypair.public_key(); let challenges = self .module_api - .get_remove_gateway_challenge(bitcoin30_to_bitcoin32_secp256k1_pubkey(&gateway_id)) + .get_remove_gateway_challenge(gateway_id) .await; let fed_public_key = self.cfg.threshold_pub_key; @@ -425,14 +415,13 @@ impl GatewayClientModule { .into_iter() .filter_map(|(peer_id, challenge)| { let msg = create_gateway_remove_message(fed_public_key, peer_id, challenge?); - let signature = - bitcoin30_to_bitcoin32_schnorr_signature(&gateway_keypair.sign_schnorr(msg)); + let signature = gateway_keypair.sign_schnorr(msg); Some((peer_id, signature)) }) .collect::>(); let remove_gateway_request = RemoveGatewayRequest { - gateway_id: bitcoin30_to_bitcoin32_secp256k1_pubkey(&gateway_id), + gateway_id, signatures, }; diff --git a/gateway/ln-gateway/src/state_machine/pay.rs b/gateway/ln-gateway/src/state_machine/pay.rs index 7fef9af2d1d..e22bcf053d8 100644 --- a/gateway/ln-gateway/src/state_machine/pay.rs +++ b/gateway/ln-gateway/src/state_machine/pay.rs @@ -6,7 +6,6 @@ use fedimint_client::transaction::{ ClientInput, ClientInputBundle, ClientOutput, ClientOutputBundle, }; use fedimint_client::{ClientHandleArc, DynGlobalClientContext}; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::config::FederationId; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; @@ -378,7 +377,7 @@ impl GatewayPayInvoice { let payment_parameters = Self::validate_outgoing_account( &outgoing_contract_account, - bitcoin30_to_bitcoin32_keypair(&context.redeem_key), + context.redeem_key, context.timelock_delta, consensus_block_count.unwrap(), &payment_data, @@ -714,7 +713,7 @@ impl GatewayPayClaimOutgoingContract { let client_input = ClientInput:: { input: claim_input, amount: contract.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair(&context.redeem_key)], + keys: vec![context.redeem_key], }; let out_points = global_context @@ -901,7 +900,7 @@ impl GatewayPayCancelContract { &bitcoin::secp256k1::Message::from_digest( *contract.contract.cancellation_message().as_ref(), ), - &bitcoin30_to_bitcoin32_keypair(&context.redeem_key), + &context.redeem_key, ); let cancel_output = LightningOutput::new_v0_cancel_outgoing( contract.contract.contract_id(), diff --git a/gateway/ln-gateway/tests/tests.rs b/gateway/ln-gateway/tests/tests.rs index fe6c612a66c..8271bee670d 100644 --- a/gateway/ln-gateway/tests/tests.rs +++ b/gateway/ln-gateway/tests/tests.rs @@ -12,9 +12,6 @@ use fedimint_client::transaction::{ ClientInput, ClientInputBundle, ClientOutput, ClientOutputBundle, TransactionBuilder, }; use fedimint_client::ClientHandleArc; -use fedimint_core::bitcoin_migration::{ - bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_secp256k1_pubkey, -}; use fedimint_core::config::FederationId; use fedimint_core::core::{IntoDynInstance, OperationId}; use fedimint_core::encoding::Encodable; @@ -67,9 +64,7 @@ async fn user_pay_invoice( invoice: Bolt11Invoice, gateway_id: &PublicKey, ) -> anyhow::Result { - let gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(gateway_id)) - .await; + let gateway = ln_module.select_gateway(gateway_id).await; ln_module.pay_bolt11_invoice(gateway, invoice, ()).await } @@ -168,9 +163,7 @@ async fn gateway_pay_valid_invoice( gateway_id: &PublicKey, ) -> anyhow::Result<()> { let user_lightning_module = &user_client.get_first_module::()?; - let gateway = user_lightning_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(gateway_id)) - .await; + let gateway = user_lightning_module.select_gateway(gateway_id).await; // User client pays test invoice let OutgoingLightningPayment { @@ -280,9 +273,7 @@ async fn test_gateway_enforces_fees() -> anyhow::Result<()> { let user_lightning_module = user_client.get_first_module::()?; let gateway_id = gateway.gateway_id(); - let ln_gateway = user_lightning_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await; + let ln_gateway = user_lightning_module.select_gateway(&gateway_id).await; let gateway_client = gateway.select_client(fed.id()).await?.into_value(); let invoice_amount = sats(250); @@ -397,7 +388,7 @@ async fn test_gateway_cannot_claim_invalid_preimage() -> anyhow::Result<()> { let client_input = ClientInput:: { input: claim_input, amount: outgoing_contract.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair(&gateway_module.redeem_key)], + keys: vec![gateway_module.redeem_key], }; let tx = TransactionBuilder::new().with_inputs( @@ -442,9 +433,7 @@ async fn test_gateway_client_pay_unpayable_invoice() -> anyhow::Result<()> { // Create invoice that cannot be paid let invoice = other_lightning_client.unpayable_invoice(sats(250), None); - let gateway = lightning_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await; + let gateway = lightning_module.select_gateway(&gateway_id).await; // User client pays test invoice let OutgoingLightningPayment { @@ -505,9 +494,7 @@ async fn test_gateway_client_intercept_valid_htlc() -> anyhow::Result<()> { // User client creates invoice in federation let invoice_amount = sats(100); let ln_module = user_client.get_first_module::()?; - let ln_gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await; + let ln_gateway = ln_module.select_gateway(&gateway_id).await; let desc = Description::new("description".to_string())?; let (_invoice_op, invoice, _) = ln_module .create_bolt11_invoice( @@ -598,9 +585,7 @@ async fn test_gateway_client_intercept_htlc_no_funds() -> anyhow::Result<()> { let gateway_client = gateway.select_client(fed.id()).await?.into_value(); // User client creates invoice in federation let ln_module = user_client.get_first_module::()?; - let ln_gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await; + let ln_gateway = ln_module.select_gateway(&gateway_id).await; let desc = Description::new("description".to_string())?; let (_invoice_op, invoice, _) = ln_module .create_bolt11_invoice( @@ -773,9 +758,7 @@ async fn test_gateway_cannot_pay_expired_invoice() -> anyhow::Result<()> { // User client pays test invoice let lightning_module = user_client.get_first_module::()?; - let gateway_module = lightning_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await; + let gateway_module = lightning_module.select_gateway(&gateway_id).await; let OutgoingLightningPayment { payment_type, contract_id, @@ -874,9 +857,7 @@ async fn test_gateway_executes_swaps_between_connected_federations() -> anyhow:: // User creates invoice in federation 2 let invoice_amt = msats(2_500); let ln_module = client2.get_first_module::()?; - let ln_gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await; + let ln_gateway = ln_module.select_gateway(&gateway_id).await; let desc = Description::new("description".to_string())?; let (receive_op, invoice, _) = ln_module .create_bolt11_invoice( diff --git a/modules/fedimint-ln-client/src/cli.rs b/modules/fedimint-ln-client/src/cli.rs index 3443ae0a8e9..ebb5f69b85b 100644 --- a/modules/fedimint-ln-client/src/cli.rs +++ b/modules/fedimint-ln-client/src/cli.rs @@ -2,7 +2,6 @@ use std::{ffi, iter}; use anyhow::Context as _; use clap::Parser; -use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_pubkey; use fedimint_core::core::OperationId; use fedimint_core::secp256k1::PublicKey; use fedimint_core::Amount; @@ -67,12 +66,7 @@ pub(crate) async fn handle_cli_command( gateway_id, force_internal, } => { - let ln_gateway = module - .get_gateway( - gateway_id.map(|pk| bitcoin32_to_bitcoin30_secp256k1_pubkey(&pk)), - force_internal, - ) - .await?; + let ln_gateway = module.get_gateway(gateway_id, force_internal).await?; let desc = Description::new(description)?; let (operation_id, invoice, _) = module @@ -100,12 +94,7 @@ pub(crate) async fn handle_cli_command( } => { let bolt11 = crate::get_invoice(&payment_info, amount, lnurl_comment).await?; info!("Paying invoice: {bolt11}"); - let ln_gateway = module - .get_gateway( - gateway_id.map(|pk| bitcoin32_to_bitcoin30_secp256k1_pubkey(&pk)), - force_internal, - ) - .await?; + let ln_gateway = module.get_gateway(gateway_id, force_internal).await?; let OutgoingLightningPayment { payment_type, diff --git a/modules/fedimint-ln-client/src/lib.rs b/modules/fedimint-ln-client/src/lib.rs index 173c832603f..8c7870f7bd8 100644 --- a/modules/fedimint-ln-client/src/lib.rs +++ b/modules/fedimint-ln-client/src/lib.rs @@ -24,6 +24,7 @@ use anyhow::{anyhow, bail, ensure, format_err, Context}; use api::LnFederationApi; use async_stream::{stream, try_stream}; use bitcoin30::hashes::{sha256, Hash, HashEngine, Hmac, HmacEngine}; +use bitcoin30::secp256k1::ThirtyTwoByteHash; use bitcoin30::Network; use db::{ DbKeyPrefix, LightningGatewayKey, LightningGatewayKeyPrefix, PaymentResult, PaymentResultKey, @@ -44,8 +45,8 @@ use fedimint_client::transaction::{ use fedimint_client::{sm_enum_variant_translation, DynGlobalClientContext}; use fedimint_core::bitcoin_migration::{ bitcoin30_to_bitcoin32_secp256k1_message, bitcoin30_to_bitcoin32_secp256k1_pubkey, - bitcoin32_to_bitcoin30_keypair, bitcoin32_to_bitcoin30_network, - bitcoin32_to_bitcoin30_recoverable_signature, bitcoin32_to_bitcoin30_secp256k1_pubkey, + bitcoin32_to_bitcoin30_network, bitcoin32_to_bitcoin30_recoverable_signature, + bitcoin32_to_bitcoin30_secp256k1_pubkey, }; use fedimint_core::config::FederationId; use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId}; @@ -61,8 +62,8 @@ use fedimint_core::task::{timeout, MaybeSend, MaybeSync}; use fedimint_core::util::update_merge::UpdateMerge; use fedimint_core::util::{backoff_util, retry, BoxStream}; use fedimint_core::{ - apply, async_trait_maybe_send, push_db_pair_items, runtime, secp256k1_27 as secp256k1, Amount, - OutPoint, TransactionId, + apply, async_trait_maybe_send, push_db_pair_items, runtime, secp256k1, Amount, OutPoint, + TransactionId, }; use fedimint_ln_common::config::{FeeToAmount, LightningClientConfig}; use fedimint_ln_common::contracts::incoming::{IncomingContract, IncomingContractOffer}; @@ -91,7 +92,6 @@ use pay::PayInvoicePayload; use rand::rngs::OsRng; use rand::seq::IteratorRandom as _; use rand::{CryptoRng, Rng, RngCore}; -use secp256k1::ThirtyTwoByteHash; use serde::{Deserialize, Serialize}; use serde_json::json; use strum::IntoEnumIterator; @@ -225,7 +225,10 @@ fn invoice_has_internal_payment_markers( .first() .and_then(|rh| rh.0.last()) .map(|hop| (hop.src_node_id, hop.short_channel_id)) - == Some(markers) + == Some(( + bitcoin32_to_bitcoin30_secp256k1_pubkey(&markers.0), + markers.1, + )) } fn invoice_routes_back_to_federation( @@ -901,7 +904,7 @@ impl LightningClientModule { // Route hint instructing payer how to route to gateway let route_hint_last_hop = RouteHintHop { - src_node_id, + src_node_id: bitcoin32_to_bitcoin30_secp256k1_pubkey(&src_node_id), short_channel_id, fees: RoutingFees { base_msat: 0, @@ -1009,9 +1012,7 @@ impl LightningClientModule { .map(|(_, gw)| gw.info) .collect::>() .await; - gateways - .into_iter() - .find(|g| g.gateway_id == bitcoin30_to_bitcoin32_secp256k1_pubkey(gateway_id)) + gateways.into_iter().find(|g| &g.gateway_id == gateway_id) } /// Updates the gateway cache by fetching the latest registered gateways @@ -1152,13 +1153,7 @@ impl LightningClientModule { let markers = self.client_ctx.get_internal_payment_markers()?; - let mut is_internal_payment = invoice_has_internal_payment_markers( - &invoice, - ( - bitcoin32_to_bitcoin30_secp256k1_pubkey(&markers.0), - markers.1, - ), - ); + let mut is_internal_payment = invoice_has_internal_payment_markers(&invoice, markers); if !is_internal_payment { let gateways = dbtx .find_by_prefix(&LightningGatewayKeyPrefix) @@ -1246,7 +1241,7 @@ impl LightningClientModule { change, is_internal_payment, contract_id, - gateway_id: maybe_gateway_id, + gateway_id: maybe_gateway_id.map(|pk| bitcoin30_to_bitcoin32_secp256k1_pubkey(&pk)), }), extra_meta: extra_meta.clone(), }; @@ -1641,7 +1636,7 @@ impl LightningClientModule { receiving_key, rand::rngs::OsRng, expiry_time, - bitcoin32_to_bitcoin30_secp256k1_pubkey(&src_node_id), + src_node_id, short_channel_id, &route_hints, bitcoin32_to_bitcoin30_network(&self.cfg.network), @@ -1654,7 +1649,7 @@ impl LightningClientModule { variant: LightningOperationMetaVariant::Receive { out_point: OutPoint { txid, out_idx: 0 }, invoice: invoice.clone(), - gateway_id, + gateway_id: gateway_id.map(|pk| bitcoin30_to_bitcoin32_secp256k1_pubkey(&pk)), }, extra_meta: extra_meta.clone(), }; @@ -2006,13 +2001,12 @@ pub async fn create_incoming_contract_output( redeem_key: &Keypair, ) -> Result<(LightningOutputV0, Amount, ContractId), IncomingSmError> { let offer = fetch_and_validate_offer(module_api, payment_hash, amount_msat).await?; - let our_pub_key = - secp256k1::PublicKey::from_keypair(&bitcoin32_to_bitcoin30_keypair(redeem_key)); + let our_pub_key = secp256k1::PublicKey::from_keypair(redeem_key); let contract = IncomingContract { hash: offer.hash, encrypted_preimage: offer.encrypted_preimage.clone(), decrypted_preimage: DecryptedPreimage::Pending, - gateway_key: bitcoin30_to_bitcoin32_secp256k1_pubkey(&our_pub_key), + gateway_key: our_pub_key, }; let contract_id = contract.contract_id(); let incoming_output = LightningOutputV0::Contract(ContractOutput { diff --git a/modules/fedimint-ln-common/src/contracts/mod.rs b/modules/fedimint-ln-common/src/contracts/mod.rs index 6d78bf0cbfe..2dab13975eb 100644 --- a/modules/fedimint-ln-common/src/contracts/mod.rs +++ b/modules/fedimint-ln-common/src/contracts/mod.rs @@ -7,7 +7,7 @@ use bitcoin30::hashes::sha256::Hash as Sha256; use bitcoin30::hashes::{hash_newtype, Hash as BitcoinHash}; use fedimint_core::encoding::{Decodable, DecodeError, Encodable}; use fedimint_core::module::registry::ModuleDecoderRegistry; -use fedimint_core::{secp256k1_27 as secp256k1, OutPoint}; +use fedimint_core::{secp256k1, OutPoint}; use serde::{Deserialize, Serialize}; /// Anything representing a contract which thus has an associated [`ContractId`] diff --git a/modules/fedimint-ln-common/src/lib.rs b/modules/fedimint-ln-common/src/lib.rs index e20dce0ea2b..5c052eed756 100644 --- a/modules/fedimint-ln-common/src/lib.rs +++ b/modules/fedimint-ln-common/src/lib.rs @@ -26,7 +26,7 @@ use std::io::{Error, ErrorKind, Read, Write}; use std::time::{Duration, SystemTime}; use anyhow::{bail, Context as AnyhowContext}; -use bitcoin30::hashes::sha256; +use bitcoin::hashes::{sha256, Hash}; use config::LightningClientConfig; use fedimint_client::oplog::OperationLogEntry; use fedimint_client::ClientHandleArc; @@ -35,7 +35,7 @@ use fedimint_core::core::{Decoder, ModuleInstanceId, ModuleKind, OperationId}; use fedimint_core::encoding::{Decodable, DecodeError, Encodable}; use fedimint_core::module::registry::ModuleDecoderRegistry; use fedimint_core::module::{CommonModuleInit, ModuleCommon, ModuleConsensusVersion}; -use fedimint_core::secp256k1_27::Message; +use fedimint_core::secp256k1::Message; use fedimint_core::util::SafeUrl; use fedimint_core::{ extensible_associated_module_type, plugin_types_trait_impl_common, secp256k1, Amount, PeerId, @@ -627,7 +627,7 @@ pub enum LightningOutputError { #[error("The incoming LN account requires more funding (need {0} got {1})")] InsufficientIncomingFunding(Amount, Amount), #[error("No offer found for payment hash {0}")] - NoOffer(fedimint_core::secp256k1_27::hashes::sha256::Hash), + NoOffer(bitcoin30::secp256k1::hashes::sha256::Hash), #[error("Only outgoing contracts support cancellation")] NotOutgoingContract, #[error("Cancellation request wasn't properly signed")] @@ -748,5 +748,5 @@ pub fn create_gateway_remove_message( let guardian_id: u16 = peer_id.into(); message_preimage.append(&mut guardian_id.consensus_encode_to_vec()); message_preimage.append(&mut challenge.consensus_encode_to_vec()); - Message::from_hashed_data::(message_preimage.as_slice()) + Message::from_digest(*sha256::Hash::hash(message_preimage.as_slice()).as_ref()) } diff --git a/modules/fedimint-ln-server/src/lib.rs b/modules/fedimint-ln-server/src/lib.rs index a12563cb3a0..4906be79813 100644 --- a/modules/fedimint-ln-server/src/lib.rs +++ b/modules/fedimint-ln-server/src/lib.rs @@ -11,9 +11,7 @@ use std::time::Duration; use anyhow::{bail, Context}; use bitcoin_hashes::{sha256, Hash as BitcoinHash}; use fedimint_bitcoind::{create_bitcoind, DynBitcoindRpc}; -use fedimint_core::bitcoin_migration::{ - bitcoin30_to_bitcoin32_secp256k1_message, bitcoin30_to_bitcoin32_secp256k1_pubkey, -}; +use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_secp256k1_message; use fedimint_core::config::{ ConfigGenModuleParams, DkgResult, ServerModuleConfig, ServerModuleConsensusConfig, TypedServerModuleConfig, TypedServerModuleConsensusConfig, @@ -572,7 +570,7 @@ impl ServerModule for Lightning { } // … either the user may spend the funds since they sold a valid preimage … DecryptedPreimage::Some(preimage) => match preimage.to_public_key() { - Ok(pub_key) => bitcoin30_to_bitcoin32_secp256k1_pubkey(&pub_key), + Ok(pub_key) => pub_key, Err(_) => return Err(LightningInputError::InvalidPreimage), }, // … or the gateway may claim back funds for not receiving the advertised preimage. @@ -1224,10 +1222,7 @@ impl Lightning { // Verify the supplied schnorr signature is valid let msg = create_gateway_remove_message(fed_public_key, our_peer_id, challenge); - signature.verify( - &bitcoin30_to_bitcoin32_secp256k1_message(&msg), - &gateway_id.x_only_public_key().0, - )?; + signature.verify(&msg, &gateway_id.x_only_public_key().0)?; dbtx.remove_entry(&LightningGatewayKey(gateway_id)).await; info!("Successfully removed gateway: {gateway_id}"); @@ -1248,7 +1243,6 @@ fn record_funded_contract_metric(updated_contract_account: &ContractAccount) { mod tests { use assert_matches::assert_matches; use bitcoin_hashes::{sha256, Hash as BitcoinHash}; - use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_secp256k1_pubkey; use fedimint_core::config::ConfigGenModuleParams; use fedimint_core::db::mem_impl::MemDatabase; use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped}; @@ -1429,11 +1423,9 @@ mod tests { amount, fee: Amount { msats: 0 }, }, - pub_key: bitcoin30_to_bitcoin32_secp256k1_pubkey( - &preimage - .to_public_key() - .expect("should create Schnorr pubkey from preimage"), - ), + pub_key: preimage + .to_public_key() + .expect("should create Schnorr pubkey from preimage"), }; assert_eq!(processed_input_meta, expected_input_meta); diff --git a/modules/fedimint-ln-tests/tests/tests.rs b/modules/fedimint-ln-tests/tests/tests.rs index 29ae88a21eb..e065181b327 100644 --- a/modules/fedimint-ln-tests/tests/tests.rs +++ b/modules/fedimint-ln-tests/tests/tests.rs @@ -3,7 +3,6 @@ use std::sync::Arc; use assert_matches::assert_matches; use fedimint_client::Client; -use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_pubkey; use fedimint_core::util::NextOrPending; use fedimint_core::{sats, secp256k1, Amount}; use fedimint_dummy_client::{DummyClientInit, DummyClientModule}; @@ -51,9 +50,7 @@ async fn pay_invoice( let ln_module = client.get_first_module::()?; ln_module.update_gateway_cache().await?; let gateway = if let Some(gateway_id) = gateway_id { - ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gateway_id)) - .await + ln_module.select_gateway(&gateway_id).await } else { None }; @@ -325,9 +322,7 @@ async fn makes_internal_payments_within_federation() -> anyhow::Result<()> { let gw = gateway(&fixtures, &fed).await; let ln_module = client1.get_first_module::()?; - let ln_gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gw.gateway_id())) - .await; + let ln_gateway = ln_module.select_gateway(&gw.gateway_id()).await; let desc = Description::new("with-gateway-hint".to_string())?; let (op, invoice, _) = ln_module .create_bolt11_invoice( @@ -444,9 +439,7 @@ async fn can_receive_for_other_user() -> anyhow::Result<()> { let keypair = Keypair::new_global(&mut OsRng); let ln_module = client1.get_first_module::()?; - let ln_gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gw.gateway_id())) - .await; + let ln_gateway = ln_module.select_gateway(&gw.gateway_id()).await; let desc = Description::new("with-gateway-hint".to_string())?; let (op, invoice, _) = ln_module .create_bolt11_invoice_for_user( @@ -517,9 +510,7 @@ async fn can_receive_for_other_user_tweaked() -> anyhow::Result<()> { let keypair = Keypair::new_global(&mut OsRng); let ln_module = client1.get_first_module::()?; - let ln_gateway = ln_module - .select_gateway(&bitcoin32_to_bitcoin30_secp256k1_pubkey(&gw.gateway_id())) - .await; + let ln_gateway = ln_module.select_gateway(&gw.gateway_id()).await; let desc = Description::new("with-gateway-hint-tweaked".to_string())?; let (op, invoice, _) = ln_module .create_bolt11_invoice_for_user_tweaked(