diff --git a/bitcoin-rpc-provider/Cargo.toml b/bitcoin-rpc-provider/Cargo.toml index 3d6bff11..f8f5c5e3 100644 --- a/bitcoin-rpc-provider/Cargo.toml +++ b/bitcoin-rpc-provider/Cargo.toml @@ -12,4 +12,5 @@ dlc-manager = {path = "../dlc-manager"} lightning = { version = "0.0.118" } log = "0.4.14" rust-bitcoin-coin-selection = { version = "0.1.0", git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection", rev = "405451929568422f7df809e35d6ad8f36fccce90", features = ["rand"] } +secp256k1 = { version = "0.24", features = ["global-context"] } simple-wallet = {path = "../simple-wallet"} diff --git a/bitcoin-rpc-provider/src/lib.rs b/bitcoin-rpc-provider/src/lib.rs index 22432d7b..440b2e3f 100644 --- a/bitcoin-rpc-provider/src/lib.rs +++ b/bitcoin-rpc-provider/src/lib.rs @@ -27,6 +27,7 @@ use json::EstimateMode; use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator}; use log::error; use rust_bitcoin_coin_selection::select_coins; +use secp256k1::SECP256K1; /// The minimum feerate we are allowed to send, as specify by LDK. const MIN_FEERATE: u32 = 253; @@ -195,13 +196,13 @@ impl ContractSignerProvider for BitcoinCoreProvider { // if not something has gone wrong assert_eq!(label_map.len(), 1); - let pk = self + let sk = self .client .lock() .unwrap() .dump_private_key(address) .map_err(rpc_err_to_manager_err)?; - Ok(SimpleSigner::new(pk.inner)) + Ok(SimpleSigner::new(SECP256K1, sk.inner)) } else { let sk = SecretKey::new(&mut thread_rng()); let network = self.get_network()?; @@ -219,7 +220,7 @@ impl ContractSignerProvider for BitcoinCoreProvider { ) .map_err(rpc_err_to_manager_err)?; - Ok(SimpleSigner::new(sk)) + Ok(SimpleSigner::new(SECP256K1, sk)) } } diff --git a/dlc-manager/src/lib.rs b/dlc-manager/src/lib.rs index 16849cb4..e45f1456 100644 --- a/dlc-manager/src/lib.rs +++ b/dlc-manager/src/lib.rs @@ -50,8 +50,8 @@ use dlc_messages::ser_impls::{read_address, write_address}; use error::Error; use lightning::ln::msgs::DecodeError; use lightning::util::ser::{Readable, Writeable, Writer}; -use secp256k1_zkp::XOnlyPublicKey; -use secp256k1_zkp::{PublicKey, SecretKey}; +use secp256k1_zkp::{PublicKey, SecretKey, Signing}; +use secp256k1_zkp::{Secp256k1, XOnlyPublicKey}; use std::collections::HashMap; use std::ops::Deref; use std::sync::RwLock; @@ -96,18 +96,22 @@ pub trait ContractSigner: Clone { #[derive(Debug, Copy, Clone)] pub struct SimpleSigner { secret_key: SecretKey, + public_key: PublicKey, } impl SimpleSigner { /// Creates a new [`SimpleSigner`] from the provided secret key. - pub fn new(secret_key: SecretKey) -> Self { - Self { secret_key } + pub fn new(secp256k1_ctx: &Secp256k1, secret_key: SecretKey) -> Self { + Self { + secret_key, + public_key: secret_key.public_key(secp256k1_ctx), + } } } impl ContractSigner for SimpleSigner { fn get_public_key(&self) -> Result { - Ok(self.secret_key.public_key(SECP256K1)) + Ok(self.public_key) } fn get_secret_key(&self) -> Result { diff --git a/mocks/src/mock_wallet.rs b/mocks/src/mock_wallet.rs index de547c4b..117a729c 100644 --- a/mocks/src/mock_wallet.rs +++ b/mocks/src/mock_wallet.rs @@ -4,6 +4,7 @@ use bitcoin::psbt::PartiallySignedTransaction; use bitcoin::secp256k1::PublicKey; use bitcoin::{Address, PackedLockTime, Script, Transaction, TxOut}; use dlc_manager::{error::Error, Blockchain, ContractSignerProvider, SimpleSigner, Utxo, Wallet}; +use secp256k1_zkp::SECP256K1; use secp256k1_zkp::{rand::seq::SliceRandom, SecretKey}; use crate::mock_blockchain::MockBlockchain; @@ -54,7 +55,7 @@ impl ContractSignerProvider for MockWallet { } fn derive_contract_signer(&self, _: [u8; 32]) -> Result { - Ok(SimpleSigner::new(get_secret_key())) + Ok(SimpleSigner::new(SECP256K1, get_secret_key())) } fn get_secret_key_for_pubkey(&self, _: &PublicKey) -> Result { diff --git a/simple-wallet/src/lib.rs b/simple-wallet/src/lib.rs index 0b8b3c2a..b9586213 100644 --- a/simple-wallet/src/lib.rs +++ b/simple-wallet/src/lib.rs @@ -194,9 +194,9 @@ where let pubkey = PublicKey::from_secret_key(&self.secp_ctx, &seckey); self.storage.upsert_key(&pubkey.serialize(), &seckey)?; self.storage.upsert_key(&keys_id, &seckey)?; - Ok(SimpleSigner::new(seckey)) + Ok(SimpleSigner::new(&self.secp_ctx, seckey)) } - Some(seckey) => Ok(SimpleSigner::new(seckey)), + Some(seckey) => Ok(SimpleSigner::new(&self.secp_ctx, seckey)), } }