Skip to content

Commit

Permalink
Types: Use bitcoin secret key wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Jul 27, 2022
1 parent 9fabb42 commit c3fd91a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl Exec for Command {
ServiceId::Farcasterd,
Request::SweepBitcoinAddress(SweepBitcoinAddress {
source_address,
source_private_key: secret_key,
source_secret_key: secret_key,
destination_address,
}),
)?;
Expand Down
8 changes: 2 additions & 6 deletions src/databased/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,7 @@ impl Runtime {
address,
secret_key,
}) => {
self.database.set_bitcoin_address(
&address,
&SecretKey::from_slice(&secret_key)
.expect("secret key is not valid secp256k1 secret key"),
)?;
self.database.set_bitcoin_address(&address, &secret_key)?;
}

Request::GetAddressSecretKey(Address::Bitcoin(address)) => {
Expand All @@ -299,7 +295,7 @@ impl Runtime {
source,
Request::AddressSecretKey(request::AddressSecretKey::Bitcoin {
address,
secret_key: secret_key.secret_bytes(),
secret_key,
}),
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ pub enum CheckpointState {
pub enum AddressSecretKey {
Bitcoin {
address: bitcoin::Address,
secret_key: [u8; 32],
secret_key: bitcoin::secp256k1::SecretKey,
},
Monero {
address: String,
Expand Down
6 changes: 3 additions & 3 deletions src/syncerd/bitcoin_syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ fn p2wpkh_script_code(script: &bitcoin::Script) -> Script {
}

fn sweep_address(
source_private_key: [u8; 32],
source_secret_key: bitcoin::secp256k1::SecretKey,
source_address: bitcoin::Address,
dest_address: bitcoin::Address,
client: &Client,
Expand All @@ -446,7 +446,7 @@ fn sweep_address(
None => return Err(Error::Farcaster(format!("Invalid to be swept address"))),
}

let sk = bitcoin::PrivateKey::from_slice(&source_private_key, network)?;
let sk = bitcoin::PrivateKey::new(source_secret_key, network);
let pk = bitcoin::PublicKey::from_private_key(bitcoin::secp256k1::SECP256K1, &sk);

let unspent_txs = client.script_list_unspent(&source_address.script_pubkey())?;
Expand Down Expand Up @@ -932,7 +932,7 @@ fn sweep_polling(
sweep_address_task.addendum.clone()
{
let sweep_address_txs = sweep_address(
addendum.source_private_key,
addendum.source_secret_key,
addendum.source_address,
addendum.destination_address,
&client,
Expand Down
2 changes: 1 addition & 1 deletion src/syncerd/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl StrictDecode for SweepXmrAddress {
#[derive(Clone, Debug, Display, StrictEncode, StrictDecode, Eq, PartialEq, Hash)]
#[display(Debug)]
pub struct SweepBitcoinAddress {
pub source_private_key: [u8; 32],
pub source_secret_key: bitcoin::secp256k1::SecretKey,
pub source_address: bitcoin::Address,
pub destination_address: bitcoin::Address,
}
Expand Down
13 changes: 5 additions & 8 deletions src/walletd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ impl Runtime {
Request::SetAddressSecretKey(AddressSecretKey::Bitcoin {
address: funding_addr.clone(),
secret_key: key_manager
.get_or_derive_bitcoin_key(ArbitratingKeyId::Lock)?
.secret_bytes(),
.get_or_derive_bitcoin_key(ArbitratingKeyId::Lock)?,
}),
)?;
debug!(
Expand Down Expand Up @@ -1325,8 +1324,7 @@ impl Runtime {
Request::SetAddressSecretKey(AddressSecretKey::Bitcoin {
address: funding_addr.clone(),
secret_key: key_manager
.get_or_derive_bitcoin_key(ArbitratingKeyId::Lock)?
.secret_bytes(),
.get_or_derive_bitcoin_key(ArbitratingKeyId::Lock)?,
}),
)?;
debug!(
Expand Down Expand Up @@ -1662,9 +1660,8 @@ impl Runtime {
if let Some(Wallet::Bob(BobState { key_manager, .. })) =
self.wallets.get_mut(&swap_id)
{
let source_private_key = key_manager
.get_or_derive_bitcoin_key(ArbitratingKeyId::Lock)?
.secret_bytes();
let source_secret_key =
key_manager.get_or_derive_bitcoin_key(ArbitratingKeyId::Lock)?;
let destination_address = self
.btc_addrs
.get(&swap_id)
Expand All @@ -1675,7 +1672,7 @@ impl Runtime {
self.identity(),
source,
Request::SweepBitcoinAddress(SweepBitcoinAddress {
source_private_key,
source_secret_key,
source_address,
destination_address,
}),
Expand Down
10 changes: 6 additions & 4 deletions tests/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use farcaster_node::ServiceId;
use microservices::ZMQ_CONTEXT;
use ntest::timeout;
use paste::paste;
use std::convert::TryInto;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::Sender;

Expand Down Expand Up @@ -893,7 +892,10 @@ fn bitcoin_syncer_sweep_address_test() {
let sweep_source_address = bitcoin_rpc.get_new_address(None, None).unwrap();
let sweep_destination_address_1 = bitcoin_rpc.get_new_address(None, None).unwrap();
let sweep_destination_address_2 = bitcoin_rpc.get_new_address(None, None).unwrap();
let wif_private_key = bitcoin_rpc.dump_private_key(&sweep_source_address).unwrap();
let source_secret_key = bitcoin_rpc
.dump_private_key(&sweep_source_address)
.unwrap()
.inner;

let (tx, rx_event) = create_bitcoin_syncer(false, "broadcast");

Expand Down Expand Up @@ -925,7 +927,7 @@ fn bitcoin_syncer_sweep_address_test() {
from_height: None,
retry: true,
addendum: SweepAddressAddendum::Bitcoin(SweepBitcoinAddress {
source_private_key: (&wif_private_key.to_bytes()[..]).try_into().unwrap(),
source_secret_key,
source_address: sweep_source_address.clone(),
destination_address: sweep_destination_address_1.clone(),
}),
Expand Down Expand Up @@ -987,7 +989,7 @@ fn bitcoin_syncer_sweep_address_test() {
from_height: None,
retry: true,
addendum: SweepAddressAddendum::Bitcoin(SweepBitcoinAddress {
source_private_key: (&wif_private_key.to_bytes()[..]).try_into().unwrap(),
source_secret_key,
source_address: sweep_source_address,
destination_address: sweep_destination_address_2.clone(),
}),
Expand Down

0 comments on commit c3fd91a

Please sign in to comment.