From fddb40332a4e9f485ee34a3d5d141fcc1ee03890 Mon Sep 17 00:00:00 2001 From: Tommy Volk Date: Mon, 28 Oct 2024 16:57:36 -0400 Subject: [PATCH] chore(deps): bump Keypair en/decoding to bitcoin v0.32 --- fedimint-core/src/encoding/secp256k1.rs | 6 ++-- gateway/ln-gateway/src/db.rs | 33 ++++++++++++++----- .../ln-gateway/src/gateway_module_v2/mod.rs | 7 ++-- .../src/gateway_module_v2/receive_sm.rs | 9 ++--- .../src/gateway_module_v2/send_sm.rs | 9 ++--- modules/fedimint-ln-client/src/db.rs | 15 ++++++--- modules/fedimint-ln-client/src/lib.rs | 15 ++++++--- modules/fedimint-ln-client/src/pay.rs | 3 +- modules/fedimint-ln-client/src/receive.rs | 17 +++++++--- .../src/contracts/outgoing.rs | 2 +- modules/fedimint-ln-tests/tests/tests.rs | 11 ++++--- modules/fedimint-lnv2-client/src/lib.rs | 8 +++-- .../fedimint-lnv2-client/src/receive_sm.rs | 9 ++--- modules/fedimint-lnv2-client/src/send_sm.rs | 15 ++++----- modules/fedimint-mint-client/src/input.rs | 3 +- modules/fedimint-mint-client/src/lib.rs | 28 ++++++++++------ modules/fedimint-mint-client/src/oob.rs | 3 +- modules/fedimint-mint-client/src/output.rs | 14 +++++--- modules/fedimint-mint-tests/tests/tests.rs | 3 +- modules/fedimint-wallet-client/src/deposit.rs | 19 ++++++----- 20 files changed, 136 insertions(+), 93 deletions(-) diff --git a/fedimint-core/src/encoding/secp256k1.rs b/fedimint-core/src/encoding/secp256k1.rs index 0681a595959..4a1ce16dc93 100644 --- a/fedimint-core/src/encoding/secp256k1.rs +++ b/fedimint-core/src/encoding/secp256k1.rs @@ -71,19 +71,19 @@ impl Decodable for secp256k1::schnorr::Signature { } } -impl Encodable for bitcoin30::key::KeyPair { +impl Encodable for bitcoin::key::Keypair { fn consensus_encode(&self, writer: &mut W) -> Result { self.secret_bytes().consensus_encode(writer) } } -impl Decodable for bitcoin30::key::KeyPair { +impl Decodable for bitcoin::key::Keypair { fn consensus_decode( d: &mut D, modules: &ModuleDecoderRegistry, ) -> Result { let sec_bytes = <[u8; 32]>::consensus_decode(d, modules)?; - Self::from_seckey_slice(secp256k1::global::SECP256K1, &sec_bytes) // FIXME: evaluate security risk of global ctx + Self::from_seckey_slice(bitcoin::secp256k1::global::SECP256K1, &sec_bytes) // FIXME: evaluate security risk of global ctx .map_err(DecodeError::from_err) } } diff --git a/gateway/ln-gateway/src/db.rs b/gateway/ln-gateway/src/db.rs index 4d5dc03d8dc..47d2cae2b4f 100644 --- a/gateway/ln-gateway/src/db.rs +++ b/gateway/ln-gateway/src/db.rs @@ -3,12 +3,16 @@ 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_29::Keypair; 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}; @@ -119,23 +123,32 @@ impl GatewayDbtxNcExt for DatabaseTransaction<'_, Cap> { } async fn load_gateway_keypair(&mut self) -> Option { - self.get_value(&GatewayPublicKey).await + self.get_value(&GatewayPublicKey) + .await + .map(|kp| bitcoin32_to_bitcoin30_keypair(&kp)) } async fn load_gateway_keypair_assert_exists(&mut self) -> KeyPair { - self.get_value(&GatewayPublicKey) - .await - .expect("Gateway keypair does not exist") + bitcoin32_to_bitcoin30_keypair( + &self + .get_value(&GatewayPublicKey) + .await + .expect("Gateway keypair does not exist"), + ) } async fn load_or_create_gateway_keypair(&mut self) -> KeyPair { if let Some(key_pair) = self.get_value(&GatewayPublicKey).await { - key_pair + bitcoin32_to_bitcoin30_keypair(&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, &key_pair).await; + self.insert_new_entry( + &GatewayPublicKey, + &bitcoin30_to_bitcoin32_keypair(&key_pair), + ) + .await; key_pair } } @@ -312,7 +325,7 @@ struct GatewayPublicKey; impl_db_record!( key = GatewayPublicKey, - value = KeyPair, + value = Keypair, db_prefix = DbKeyPrefix::GatewayPublicKey, ); @@ -483,7 +496,11 @@ 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, &key_pair).await; + dbtx.insert_new_entry( + &GatewayPublicKey, + &bitcoin30_to_bitcoin32_keypair(&key_pair), + ) + .await; let gateway_configuration = GatewayConfigurationV0 { password: "EXAMPLE".to_string(), diff --git a/gateway/ln-gateway/src/gateway_module_v2/mod.rs b/gateway/ln-gateway/src/gateway_module_v2/mod.rs index 1f7349314ea..67cfbf4275e 100644 --- a/gateway/ln-gateway/src/gateway_module_v2/mod.rs +++ b/gateway/ln-gateway/src/gateway_module_v2/mod.rs @@ -16,6 +16,7 @@ use fedimint_client::sm::util::MapStateTransitions; use fedimint_client::sm::{Context, DynState, ModuleNotifier, State, StateTransition}; use fedimint_client::transaction::{ClientOutput, TransactionBuilder}; use fedimint_client::{sm_enum_variant_translation, DynGlobalClientContext}; +use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::config::FederationId; use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId}; use fedimint_core::db::DatabaseTransaction; @@ -303,7 +304,7 @@ impl GatewayClientModuleV2 { max_delay, min_contract_amount, invoice: payload.invoice, - claim_keypair: self.keypair, + claim_keypair: bitcoin30_to_bitcoin32_keypair(&self.keypair), }, state: SendSMState::Sending, }); @@ -383,7 +384,7 @@ impl GatewayClientModuleV2 { operation_id, contract: contract.clone(), out_point: OutPoint { txid, out_idx }, - refund_keypair, + refund_keypair: bitcoin30_to_bitcoin32_keypair(&refund_keypair), }, state: ReceiveSMState::Funding, }), @@ -436,7 +437,7 @@ impl GatewayClientModuleV2 { operation_id, contract: contract.clone(), out_point: OutPoint { txid, out_idx }, - refund_keypair, + refund_keypair: bitcoin30_to_bitcoin32_keypair(&refund_keypair), }, state: ReceiveSMState::Funding, })] diff --git a/gateway/ln-gateway/src/gateway_module_v2/receive_sm.rs b/gateway/ln-gateway/src/gateway_module_v2/receive_sm.rs index 4957ac12843..160378b2a87 100644 --- a/gateway/ln-gateway/src/gateway_module_v2/receive_sm.rs +++ b/gateway/ln-gateway/src/gateway_module_v2/receive_sm.rs @@ -9,12 +9,11 @@ use fedimint_api_client::query::FilterMapThreshold; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::{Decoder, OperationId}; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::endpoint_constants::AWAIT_OUTPUT_OUTCOME_ENDPOINT; use fedimint_core::module::ApiRequestErased; -use fedimint_core::secp256k1::KeyPair; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::task::sleep; use fedimint_core::{NumPeersExt, OutPoint, PeerId, TransactionId}; use fedimint_lnv2_common::contracts::IncomingContract; @@ -58,7 +57,7 @@ pub struct ReceiveSMCommon { pub operation_id: OperationId, pub contract: IncomingContract, pub out_point: OutPoint, - pub refund_keypair: KeyPair, + pub refund_keypair: Keypair, } #[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)] @@ -262,9 +261,7 @@ impl ReceiveStateMachine { agg_decryption_key, )), amount: old_state.common.contract.commitment.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair( - &old_state.common.refund_keypair, - )], + keys: vec![old_state.common.refund_keypair], }; let outpoints = global_context diff --git a/gateway/ln-gateway/src/gateway_module_v2/send_sm.rs b/gateway/ln-gateway/src/gateway_module_v2/send_sm.rs index e5cdb2477e3..afe66c6a163 100644 --- a/gateway/ln-gateway/src/gateway_module_v2/send_sm.rs +++ b/gateway/ln-gateway/src/gateway_module_v2/send_sm.rs @@ -3,10 +3,9 @@ use std::fmt; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; -use fedimint_core::secp256k1::KeyPair; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::{Amount, OutPoint}; use fedimint_lnv2_client::LightningInvoice; use fedimint_lnv2_common::contracts::{OutgoingContract, PaymentImage}; @@ -48,7 +47,7 @@ pub struct SendSMCommon { pub max_delay: u64, pub min_contract_amount: Amount, pub invoice: LightningInvoice, - pub claim_keypair: KeyPair, + pub claim_keypair: Keypair, } #[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)] @@ -219,9 +218,7 @@ impl SendStateMachine { OutgoingWitness::Claim(preimage), )), amount: old_state.common.contract.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair( - &old_state.common.claim_keypair, - )], + keys: vec![old_state.common.claim_keypair], }; let outpoints = global_context diff --git a/modules/fedimint-ln-client/src/db.rs b/modules/fedimint-ln-client/src/db.rs index f0e9aacc504..7e37999e008 100644 --- a/modules/fedimint-ln-client/src/db.rs +++ b/modules/fedimint-ln-client/src/db.rs @@ -4,10 +4,11 @@ use bitcoin30::hashes::sha256; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::module::registry::ModuleDecoderRegistry; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::{impl_db_lookup, impl_db_record, OutPoint, TransactionId}; use fedimint_ln_common::{LightningGateway, LightningGatewayRegistration}; use lightning_invoice::Bolt11Invoice; -use secp256k1::{KeyPair, PublicKey}; +use secp256k1::PublicKey; use serde::Serialize; use strum_macros::EnumIter; @@ -101,7 +102,7 @@ pub(crate) fn get_v1_migrated_state( #[derive(Debug, Clone, Decodable)] pub struct LightningReceiveConfirmedInvoiceV0 { invoice: Bolt11Invoice, - receiving_key: KeyPair, + receiving_key: Keypair, } let decoders = ModuleDecoderRegistry::default(); @@ -265,6 +266,7 @@ mod tests { use std::str::FromStr; use fedimint_client::db::migrate_state; + use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::{IntoDynInstance, OperationId}; use fedimint_core::encoding::Encodable; use fedimint_core::{BitcoinHash, TransactionId}; @@ -288,7 +290,8 @@ mod tests { cqp2rzjq0ag45qspt2vd47jvj3t5nya5vsn0hlhf5wel8h779npsrspm6eeuqtjuuqqqqgqqyqqqqqqqqqqqqqqqc9q\ yysgqddrv0jqhyf3q6z75rt7nrwx0crxme87s8rx2rt8xr9slzu0p3xg3f3f0zmqavtmsnqaj5v0y5mdzszah7thrmg\ 2we42dvjggjkf44egqheymyw",).expect("Invalid invoice"); - let claim_key = KeyPair::new(secp256k1::SECP256K1, &mut thread_rng()); + let claim_key = + bitcoin30_to_bitcoin32_keypair(&KeyPair::new(secp256k1::SECP256K1, &mut thread_rng())); let operation_id = OperationId::new_random(); let txid = TransactionId::from_byte_array([42; 32]); @@ -366,7 +369,8 @@ mod tests { async fn test_sm_migration_to_v2_confirmed() -> anyhow::Result<()> { let operation_id = OperationId::new_random(); let instance_id = 0x42; - let claim_key = KeyPair::new(secp256k1::SECP256K1, &mut thread_rng()); + let claim_key = + bitcoin30_to_bitcoin32_keypair(&KeyPair::new(secp256k1::SECP256K1, &mut thread_rng())); let dummy_invoice = Bolt11Invoice::from_str("lntbs1u1pj8308gsp5xhxz908q5usddjjm6mfq6nwc2nu62twwm6za69d32kyx8h49a4hqpp5j5egfqw9kf5e96nk\ 6htr76a8kggl0xyz3pzgemv887pya4flguzsdp5235xzmntwvsxvmmjypex2en4dejxjmn8yp6xsefqvesh2cm9wsss\ cqp2rzjq0ag45qspt2vd47jvj3t5nya5vsn0hlhf5wel8h779npsrspm6eeuqtjuuqqqqgqqyqqqqqqqqqqqqqqqc9q\ @@ -436,7 +440,8 @@ mod tests { cqp2rzjq0ag45qspt2vd47jvj3t5nya5vsn0hlhf5wel8h779npsrspm6eeuqtjuuqqqqgqqyqqqqqqqqqqqqqqqc9q\ yysgqddrv0jqhyf3q6z75rt7nrwx0crxme87s8rx2rt8xr9slzu0p3xg3f3f0zmqavtmsnqaj5v0y5mdzszah7thrmg\ 2we42dvjggjkf44egqheymyw",).expect("Invalid invoice"); - let claim_key = KeyPair::new(secp256k1::SECP256K1, &mut thread_rng()); + let claim_key = + bitcoin30_to_bitcoin32_keypair(&KeyPair::new(secp256k1::SECP256K1, &mut thread_rng())); let operation_id = OperationId::new_random(); let txid = TransactionId::from_byte_array([42; 32]); diff --git a/modules/fedimint-ln-client/src/lib.rs b/modules/fedimint-ln-client/src/lib.rs index 5e5dad96ed1..03e8c322474 100644 --- a/modules/fedimint-ln-client/src/lib.rs +++ b/modules/fedimint-ln-client/src/lib.rs @@ -53,6 +53,7 @@ use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::module::{ ApiVersion, CommonModuleInit, ModuleCommon, ModuleInit, MultiApiVersion, }; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::task::{timeout, MaybeSend, MaybeSync}; use fedimint_core::util::update_merge::UpdateMerge; use fedimint_core::util::{backoff_util, retry, BoxStream}; @@ -146,7 +147,7 @@ impl PayType { pub enum ReceivingKey { /// The keypair used to receive payments for ourselves, we will use this to /// sweep to our own ecash wallet on success - Personal(KeyPair), + Personal(Keypair), /// A public key of another user, the lightning payment will be locked to /// this key for them to claim on success External(PublicKey), @@ -156,7 +157,9 @@ impl ReceivingKey { /// The public key of the receiving key pub fn public_key(&self) -> PublicKey { match self { - ReceivingKey::Personal(keypair) => keypair.public_key(), + ReceivingKey::Personal(keypair) => { + bitcoin32_to_bitcoin30_secp256k1_pubkey(&keypair.public_key()) + } ReceivingKey::External(public_key) => *public_key, } } @@ -725,7 +728,7 @@ impl LightningClientModule { }; let outgoing_payment = OutgoingContractData { - recovery_key: user_sk, + recovery_key: bitcoin30_to_bitcoin32_keypair(&user_sk), contract_account: OutgoingContractAccount { amount: contract_amount, contract: contract.clone(), @@ -1507,8 +1510,10 @@ impl LightningClientModule { extra_meta: M, gateway: Option, ) -> anyhow::Result<(OperationId, Bolt11Invoice, [u8; 32])> { - let receiving_key = - ReceivingKey::Personal(KeyPair::new(&self.secp, &mut rand::rngs::OsRng)); + let receiving_key = ReceivingKey::Personal(bitcoin30_to_bitcoin32_keypair(&KeyPair::new( + &self.secp, + &mut rand::rngs::OsRng, + ))); self.create_bolt11_invoice_internal( amount, description, diff --git a/modules/fedimint-ln-client/src/pay.rs b/modules/fedimint-ln-client/src/pay.rs index a52bb615991..31be379b43d 100644 --- a/modules/fedimint-ln-client/src/pay.rs +++ b/modules/fedimint-ln-client/src/pay.rs @@ -4,7 +4,6 @@ use bitcoin30::hashes::sha256; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::config::FederationId; use fedimint_core::core::{Decoder, OperationId}; use fedimint_core::encoding::{Decodable, Encodable}; @@ -547,7 +546,7 @@ async fn try_refund_outgoing_contract( let refund_client_input = ClientInput:: { input: refund_input, amount: contract_data.contract_account.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair(&refund_key)], + keys: vec![refund_key], }; let (txid, out_points) = global_context diff --git a/modules/fedimint-ln-client/src/receive.rs b/modules/fedimint-ln-client/src/receive.rs index 5068b9817c6..dd644b22924 100644 --- a/modules/fedimint-ln-client/src/receive.rs +++ b/modules/fedimint-ln-client/src/receive.rs @@ -5,9 +5,12 @@ use fedimint_api_client::api::DynModuleApi; use fedimint_client::sm::{ClientSMDatabaseTransaction, DynState, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; +use fedimint_core::bitcoin_migration::{ + bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_keypair, +}; use fedimint_core::core::{IntoDynInstance, ModuleInstanceId, OperationId}; use fedimint_core::encoding::{Decodable, Encodable}; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::task::sleep; use fedimint_core::{OutPoint, TransactionId}; use fedimint_ln_common::contracts::incoming::IncomingContractAccount; @@ -93,7 +96,7 @@ impl IntoDynInstance for LightningReceiveStateMachine { pub struct LightningReceiveSubmittedOfferV0 { pub offer_txid: TransactionId, pub invoice: Bolt11Invoice, - pub payment_keypair: KeyPair, + pub payment_keypair: Keypair, } #[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)] @@ -248,9 +251,13 @@ impl LightningReceiveConfirmedInvoice { Ok(contract) => { match receiving_key { ReceivingKey::Personal(keypair) => { - let (txid, out_points) = - Self::claim_incoming_contract(dbtx, contract, keypair, global_context) - .await; + let (txid, out_points) = Self::claim_incoming_contract( + dbtx, + contract, + bitcoin32_to_bitcoin30_keypair(&keypair), + global_context, + ) + .await; LightningReceiveStateMachine { operation_id: old_state.operation_id, state: LightningReceiveStates::Funded(LightningReceiveFunded { diff --git a/modules/fedimint-ln-common/src/contracts/outgoing.rs b/modules/fedimint-ln-common/src/contracts/outgoing.rs index 56627544632..307038a6c47 100644 --- a/modules/fedimint-ln-common/src/contracts/outgoing.rs +++ b/modules/fedimint-ln-common/src/contracts/outgoing.rs @@ -55,7 +55,7 @@ impl OutgoingContract { #[derive(Debug, Clone, Eq, PartialEq, Hash, Encodable, Decodable, Serialize, Deserialize)] pub struct OutgoingContractData { - pub recovery_key: bitcoin30::key::KeyPair, + pub recovery_key: bitcoin::key::Keypair, pub contract_account: OutgoingContractAccount, } diff --git a/modules/fedimint-ln-tests/tests/tests.rs b/modules/fedimint-ln-tests/tests/tests.rs index 8bcd27e430d..645748a5177 100644 --- a/modules/fedimint-ln-tests/tests/tests.rs +++ b/modules/fedimint-ln-tests/tests/tests.rs @@ -606,6 +606,7 @@ mod fedimint_migration_tests { use anyhow::ensure; use bitcoin_hashes::{sha256, Hash}; use fedimint_client::module::init::DynClientModuleInit; + use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::config::FederationId; use fedimint_core::core::OperationId; use fedimint_core::db::{ @@ -889,7 +890,9 @@ mod fedimint_migration_tests { invoice .consensus_encode(&mut submitted_offer_variant) .expect("Invoice is encodable"); - let receiving_key = ReceivingKey::Personal(KeyPair::new_global(&mut OsRng)); + let receiving_key = ReceivingKey::Personal(bitcoin30_to_bitcoin32_keypair( + &KeyPair::new_global(&mut OsRng), + )); receiving_key .consensus_encode(&mut submitted_offer_variant) .expect("ReceivingKey is encodable"); @@ -909,7 +912,7 @@ mod fedimint_migration_tests { .consensus_encode(&mut submitted_offer_variant) .expect("Invoice is encodable"); let keypair = KeyPair::new_global(&mut OsRng); - keypair + bitcoin30_to_bitcoin32_keypair(&keypair) .consensus_encode(&mut submitted_offer_variant) .expect("Keypair is encodable"); @@ -924,7 +927,7 @@ mod fedimint_migration_tests { .consensus_encode(&mut confirmed_variant) .expect("Invoice is encodable"); let keypair = KeyPair::new_global(&mut OsRng); - keypair + bitcoin30_to_bitcoin32_keypair(&keypair) .consensus_encode(&mut confirmed_variant) .expect("Keypair is encodable"); confirmed_variant @@ -945,7 +948,7 @@ mod fedimint_migration_tests { contract: outgoing_contract.clone(), }; let contract = OutgoingContractData { - recovery_key: KeyPair::from_secret_key(&secp, &sk), + recovery_key: bitcoin30_to_bitcoin32_keypair(&KeyPair::from_secret_key(&secp, &sk)), contract_account: outgoing_account, }; let ln_common = LightningPayCommon { diff --git a/modules/fedimint-lnv2-client/src/lib.rs b/modules/fedimint-lnv2-client/src/lib.rs index 04824e1e8dd..48932f9fb1b 100644 --- a/modules/fedimint-lnv2-client/src/lib.rs +++ b/modules/fedimint-lnv2-client/src/lib.rs @@ -29,7 +29,9 @@ use fedimint_client::sm::util::MapStateTransitions; use fedimint_client::sm::{Context, DynState, ModuleNotifier, State, StateTransition}; use fedimint_client::transaction::{ClientOutput, TransactionBuilder}; use fedimint_client::{sm_enum_variant_translation, DynGlobalClientContext}; -use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_network; +use fedimint_core::bitcoin_migration::{ + bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_network, +}; use fedimint_core::config::FederationId; use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId}; use fedimint_core::db::{DatabaseTransaction, IDatabaseTransactionOpsCoreTyped}; @@ -600,7 +602,7 @@ impl LightningClientModule { gateway_api: gateway_api_clone.clone(), contract: contract_clone.clone(), invoice: LightningInvoice::Bolt11(invoice_clone.clone()), - refund_keypair, + refund_keypair: bitcoin30_to_bitcoin32_keypair(&refund_keypair), }, state: SendSMState::Funding, })] @@ -890,7 +892,7 @@ impl LightningClientModule { common: ReceiveSMCommon { operation_id, contract: contract.clone(), - claim_keypair, + claim_keypair: bitcoin30_to_bitcoin32_keypair(&claim_keypair), agg_decryption_key, }, state: ReceiveSMState::Pending, diff --git a/modules/fedimint-lnv2-client/src/receive_sm.rs b/modules/fedimint-lnv2-client/src/receive_sm.rs index 515bd399116..923cca3c0ba 100644 --- a/modules/fedimint-lnv2-client/src/receive_sm.rs +++ b/modules/fedimint-lnv2-client/src/receive_sm.rs @@ -1,10 +1,9 @@ -use bitcoin30::key::KeyPair; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::OutPoint; use fedimint_lnv2_common::contracts::IncomingContract; use fedimint_lnv2_common::{LightningInput, LightningInputV0}; @@ -32,7 +31,7 @@ impl ReceiveStateMachine { pub struct ReceiveSMCommon { pub operation_id: OperationId, pub contract: IncomingContract, - pub claim_keypair: KeyPair, + pub claim_keypair: Keypair, pub agg_decryption_key: AggregateDecryptionKey, } @@ -115,9 +114,7 @@ impl ReceiveStateMachine { old_state.common.agg_decryption_key, )), amount: old_state.common.contract.commitment.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair( - &old_state.common.claim_keypair, - )], + keys: vec![old_state.common.claim_keypair], }; let out_points = global_context diff --git a/modules/fedimint-lnv2-client/src/send_sm.rs b/modules/fedimint-lnv2-client/src/send_sm.rs index a71b050a054..a90279a41e2 100644 --- a/modules/fedimint-lnv2-client/src/send_sm.rs +++ b/modules/fedimint-lnv2-client/src/send_sm.rs @@ -5,10 +5,11 @@ use bitcoin30::secp256k1; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; +use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_keypair; use fedimint_core::config::FederationId; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::task::sleep; use fedimint_core::util::SafeUrl; use fedimint_core::{OutPoint, TransactionId}; @@ -45,7 +46,7 @@ pub struct SendSMCommon { pub gateway_api: SafeUrl, pub contract: OutgoingContract, pub invoice: LightningInvoice, - pub refund_keypair: KeyPair, + pub refund_keypair: Keypair, } #[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)] @@ -100,7 +101,7 @@ impl State for SendStateMachine { context.federation_id, self.common.contract.clone(), self.common.invoice.clone(), - self.common.refund_keypair, + bitcoin32_to_bitcoin30_keypair(&self.common.refund_keypair), context.clone(), ), move |dbtx, response, old_state| { @@ -219,9 +220,7 @@ impl SendStateMachine { OutgoingWitness::Cancel(signature), )), amount: old_state.common.contract.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair( - &old_state.common.refund_keypair, - )], + keys: vec![old_state.common.refund_keypair], }; let outpoints = global_context @@ -280,9 +279,7 @@ impl SendStateMachine { OutgoingWitness::Refund, )), amount: old_state.common.contract.amount, - keys: vec![bitcoin30_to_bitcoin32_keypair( - &old_state.common.refund_keypair, - )], + keys: vec![old_state.common.refund_keypair], }; let outpoints = global_context diff --git a/modules/fedimint-mint-client/src/input.rs b/modules/fedimint-mint-client/src/input.rs index c737404f4e7..a906086810e 100644 --- a/modules/fedimint-mint-client/src/input.rs +++ b/modules/fedimint-mint-client/src/input.rs @@ -1,7 +1,6 @@ use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::{Amount, TransactionId}; @@ -141,7 +140,7 @@ impl MintInputStateCreated { let refund_input = ClientInput:: { input: MintInput::new_v0(amount, spendable_note.note()), - keys: vec![bitcoin30_to_bitcoin32_keypair(&spendable_note.spend_key)], + keys: vec![spendable_note.spend_key], amount, }; diff --git a/modules/fedimint-mint-client/src/lib.rs b/modules/fedimint-mint-client/src/lib.rs index e1207e51dcd..31f17e1f3b3 100644 --- a/modules/fedimint-mint-client/src/lib.rs +++ b/modules/fedimint-mint-client/src/lib.rs @@ -49,7 +49,7 @@ use fedimint_client::transaction::{ ClientInput, ClientInputBundle, ClientInputSM, ClientOutput, TransactionBuilder, }; use fedimint_client::{sm_enum_variant_translation, DynGlobalClientContext}; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; +use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_pubkey; use fedimint_core::config::{FederationId, FederationIdPrefix}; use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId}; use fedimint_core::db::{ @@ -62,7 +62,8 @@ use fedimint_core::module::registry::{ModuleDecoderRegistry, ModuleRegistry}; use fedimint_core::module::{ ApiVersion, CommonModuleInit, ModuleCommon, ModuleInit, MultiApiVersion, }; -use fedimint_core::secp256k1::{All, KeyPair, Secp256k1}; +use fedimint_core::secp256k1::{All, Secp256k1}; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::util::{BoxFuture, BoxStream, NextOrPending, SafeUrl}; use fedimint_core::{ apply, async_trait_maybe_send, push_db_pair_items, Amount, OutPoint, PeerId, Tiered, @@ -390,7 +391,7 @@ impl OOBNotes { pub struct OOBNoteV2 { pub amount: Amount, pub sig: Signature, - pub key: KeyPair, + pub key: Keypair, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Encodable, Decodable)] @@ -1258,7 +1259,7 @@ impl MintClientModule { inputs.push(ClientInput { input: MintInput::new_v0(amount, note), - keys: vec![bitcoin30_to_bitcoin32_keypair(&spendable_note.spend_key)], + keys: vec![spendable_note.spend_key], amount, }); @@ -1707,7 +1708,9 @@ impl MintClientModule { bail!("Note {idx} has an invalid federation signature"); } - let expected_nonce = Nonce(snote.spend_key.public_key()); + let expected_nonce = Nonce(bitcoin32_to_bitcoin30_secp256k1_pubkey( + &snote.spend_key.public_key(), + )); if note.nonce != expected_nonce { bail!("Note {idx} cannot be spent using the supplied spend key"); } @@ -2110,7 +2113,7 @@ impl State for MintClientStateMachines { #[derive(Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize, Encodable, Decodable)] pub struct SpendableNote { pub signature: tbs::Signature, - pub spend_key: KeyPair, + pub spend_key: Keypair, } impl fmt::Debug for SpendableNote { @@ -2130,7 +2133,9 @@ impl fmt::Display for SpendableNote { impl SpendableNote { pub fn nonce(&self) -> Nonce { - Nonce(self.spend_key.public_key()) + Nonce(bitcoin32_to_bitcoin30_secp256k1_pubkey( + &self.spend_key.public_key(), + )) } fn note(&self) -> Note { @@ -2169,7 +2174,7 @@ pub struct SpendableNoteUndecoded { // verifying they serialize and decode the same. #[serde(serialize_with = "serdect::array::serialize_hex_lower_or_bin")] pub signature: [u8; 48], - pub spend_key: KeyPair, + pub spend_key: Keypair, } impl fmt::Display for SpendableNoteUndecoded { @@ -2190,7 +2195,9 @@ impl fmt::Debug for SpendableNoteUndecoded { impl SpendableNoteUndecoded { fn nonce(&self) -> Nonce { - Nonce(self.spend_key.public_key()) + Nonce(bitcoin32_to_bitcoin30_secp256k1_pubkey( + &self.spend_key.public_key(), + )) } pub fn decode(self) -> anyhow::Result { @@ -2325,6 +2332,7 @@ mod tests { use std::str::FromStr; use bitcoin_hashes::Hash; + use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::config::FederationId; use fedimint_core::encoding::Decodable; use fedimint_core::invite_code::{InviteCode, InviteCodeV2}; @@ -2584,7 +2592,7 @@ mod tests { notes: iter::repeat(OOBNoteV2 { amount: Amount::from_msats(1), sig: Signature(bls12_381::G1Affine::generator()), - key: SecretKey::new(&mut OsRng).keypair(SECP256K1), + key: bitcoin30_to_bitcoin32_keypair(&SecretKey::new(&mut OsRng).keypair(SECP256K1)), }) .take(NUMBER_OF_NOTES) .collect(), diff --git a/modules/fedimint-mint-client/src/oob.rs b/modules/fedimint-mint-client/src/oob.rs index 0f59eb26b34..12e1e61eab5 100644 --- a/modules/fedimint-mint-client/src/oob.rs +++ b/modules/fedimint-mint-client/src/oob.rs @@ -4,7 +4,6 @@ use std::time::SystemTime; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle, ClientInputSM}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::{runtime, Amount, TransactionId}; @@ -180,7 +179,7 @@ async fn try_cancel_oob_spend( ( ClientInput { input: MintInput::new_v0(amount, spendable_note.note()), - keys: vec![bitcoin30_to_bitcoin32_keypair(&spendable_note.spend_key)], + keys: vec![spendable_note.spend_key], amount, }, ClientInputSM { diff --git a/modules/fedimint-mint-client/src/output.rs b/modules/fedimint-mint-client/src/output.rs index ebc92bd2f9f..169250ed657 100644 --- a/modules/fedimint-mint-client/src/output.rs +++ b/modules/fedimint-mint-client/src/output.rs @@ -8,11 +8,15 @@ use fedimint_api_client::query::FilterMapThreshold; use fedimint_client::module::ClientContext; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::DynGlobalClientContext; +use fedimint_core::bitcoin_migration::{ + bitcoin30_to_bitcoin32_keypair, bitcoin32_to_bitcoin30_secp256k1_pubkey, +}; use fedimint_core::core::{Decoder, OperationId}; use fedimint_core::db::IDatabaseTransactionOpsCoreTyped; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::module::ApiRequestErased; -use fedimint_core::secp256k1::{KeyPair, Secp256k1, Signing}; +use fedimint_core::secp256k1::{Secp256k1, Signing}; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::task::sleep; use fedimint_core::{Amount, NumPeersExt, OutPoint, PeerId, Tiered}; use fedimint_derive_secret::{ChildId, DerivableSecret}; @@ -339,7 +343,7 @@ pub struct MintOutputStatesSucceeded { pub struct NoteIssuanceRequest { /// Spend key from which the note nonce (corresponding public key) is /// derived - spend_key: KeyPair, + spend_key: Keypair, /// Key to unblind the blind signature supplied by the mint for this note blinding_key: BlindingKey, } @@ -364,7 +368,7 @@ impl NoteIssuanceRequest { let blinded_nonce = blind_message(nonce.to_message(), blinding_key); let cr = NoteIssuanceRequest { - spend_key, + spend_key: bitcoin30_to_bitcoin32_keypair(&spend_key), blinding_key, }; @@ -373,7 +377,9 @@ impl NoteIssuanceRequest { /// Return nonce of the e-cash note being requested pub fn nonce(&self) -> Nonce { - Nonce(self.spend_key.public_key()) + Nonce(bitcoin32_to_bitcoin30_secp256k1_pubkey( + &self.spend_key.public_key(), + )) } pub fn blinded_message(&self) -> BlindedMessage { diff --git a/modules/fedimint-mint-tests/tests/tests.rs b/modules/fedimint-mint-tests/tests/tests.rs index 3d2a90a7b34..99bd5a12054 100644 --- a/modules/fedimint-mint-tests/tests/tests.rs +++ b/modules/fedimint-mint-tests/tests/tests.rs @@ -384,6 +384,7 @@ mod fedimint_migration_tests { use fedimint_client::derivable_secret::{ChildId, DerivableSecret}; use fedimint_client::module::init::recovery::{RecoveryFromHistory, RecoveryFromHistoryCommon}; use fedimint_client::module::init::DynClientModuleInit; + use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; use fedimint_core::core::OperationId; use fedimint_core::db::{ Database, DatabaseVersion, DatabaseVersionKeyV0, IDatabaseTransactionOpsCoreTyped, @@ -498,7 +499,7 @@ mod fedimint_migration_tests { let spendable_note = SpendableNote { signature: sig, - spend_key: keypair, + spend_key: bitcoin30_to_bitcoin32_keypair(&keypair), }; dbtx.insert_new_entry( diff --git a/modules/fedimint-wallet-client/src/deposit.rs b/modules/fedimint-wallet-client/src/deposit.rs index 3e3646f368a..aac6a2aaf60 100644 --- a/modules/fedimint-wallet-client/src/deposit.rs +++ b/modules/fedimint-wallet-client/src/deposit.rs @@ -4,9 +4,12 @@ use std::time::{Duration, SystemTime}; use fedimint_client::sm::{ClientSMDatabaseTransaction, State, StateTransition}; use fedimint_client::transaction::{ClientInput, ClientInputBundle}; use fedimint_client::DynGlobalClientContext; -use fedimint_core::bitcoin_migration::bitcoin30_to_bitcoin32_keypair; +use fedimint_core::bitcoin_migration::{ + bitcoin32_to_bitcoin30_keypair, bitcoin32_to_bitcoin30_secp256k1_pubkey, +}; use fedimint_core::core::OperationId; use fedimint_core::encoding::{Decodable, Encodable}; +use fedimint_core::secp256k1_29::Keypair; use fedimint_core::task::sleep; use fedimint_core::txoproof::TxOutProof; use fedimint_core::{Amount, OutPoint, TransactionId}; @@ -54,7 +57,7 @@ impl State for DepositStateMachine { StateTransition::new( await_created_btc_transaction_submitted( context.clone(), - created_state.tweak_key, + bitcoin32_to_bitcoin30_keypair(&created_state.tweak_key), ), |_db, (btc_tx, out_idx), old_state| { Box::pin(async move { transition_tx_seen(old_state, btc_tx, out_idx) }) @@ -269,7 +272,9 @@ pub(crate) async fn transition_btc_tx_confirmed( txout_proof, awaiting_confirmation_state.btc_transaction, awaiting_confirmation_state.out_idx, - awaiting_confirmation_state.tweak_key.public_key(), + bitcoin32_to_bitcoin30_secp256k1_pubkey( + &awaiting_confirmation_state.tweak_key.public_key(), + ), ) .expect("TODO: handle API returning faulty proofs"); @@ -279,9 +284,7 @@ pub(crate) async fn transition_btc_tx_confirmed( let client_input = ClientInput:: { input: wallet_input, - keys: vec![bitcoin30_to_bitcoin32_keypair( - &awaiting_confirmation_state.tweak_key, - )], + keys: vec![awaiting_confirmation_state.tweak_key], amount, }; @@ -309,7 +312,7 @@ pub enum DepositStates { #[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)] pub struct CreatedDepositState { - pub(crate) tweak_key: KeyPair, + pub(crate) tweak_key: Keypair, pub(crate) timeout_at: SystemTime, } @@ -318,7 +321,7 @@ pub struct WaitingForConfirmationsDepositState { /// Key pair of which the public was used to tweak the federation's wallet /// descriptor. The secret key is later used to sign the fedimint claim /// transaction. - tweak_key: KeyPair, + tweak_key: Keypair, /// The bitcoin transaction is saved as soon as we see it so the transaction /// can be re-transmitted if it's evicted from the mempool. pub(crate) btc_transaction: bitcoin30::Transaction,