Skip to content

Commit

Permalink
Remove global context use from dlc-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Feb 6, 2024
1 parent 39893b1 commit bf7fa12
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions bitcoin-rpc-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
7 changes: 4 additions & 3 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()?;
Expand All @@ -219,7 +220,7 @@ impl ContractSignerProvider for BitcoinCoreProvider {
)
.map_err(rpc_err_to_manager_err)?;

Ok(SimpleSigner::new(sk))
Ok(SimpleSigner::new(SECP256K1, sk))
}
}

Expand Down
14 changes: 9 additions & 5 deletions dlc-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<C: Signing>(secp256k1_ctx: &Secp256k1<C>, 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<PublicKey, Error> {
Ok(self.secret_key.public_key(SECP256K1))
Ok(self.public_key)
}

fn get_secret_key(&self) -> Result<SecretKey, Error> {
Expand Down
3 changes: 2 additions & 1 deletion mocks/src/mock_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +55,7 @@ impl ContractSignerProvider for MockWallet {
}

fn derive_contract_signer(&self, _: [u8; 32]) -> Result<Self::Signer, Error> {
Ok(SimpleSigner::new(get_secret_key()))
Ok(SimpleSigner::new(SECP256K1, get_secret_key()))
}

fn get_secret_key_for_pubkey(&self, _: &PublicKey) -> Result<SecretKey, Error> {
Expand Down
4 changes: 2 additions & 2 deletions simple-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down

0 comments on commit bf7fa12

Please sign in to comment.