Skip to content

Commit

Permalink
build(deps): upgrade bdk, ecdsa_fun, sigma_fun, bitcoin-harness
Browse files Browse the repository at this point in the history
- bdk to 0.21.0 comit-network#1107
- ecdsa_fun to a44969a comit-network#1121
- sigma_fun to a44969a comit-network#1120
- bitcoin-harness upgrade was required, waiting for merge at coblox/bitcoin-harness-rs#25
  • Loading branch information
delta1 committed Sep 6, 2022
1 parent 9021e83 commit 67fd705
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 285 deletions.
192 changes: 70 additions & 122 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Please have a look at the [contribution guidelines](./CONTRIBUTING.md).
## Rust Version Support

Please note that only the latest stable Rust toolchain is supported.
All stable toolchains since 1.58 _should_ work.
All stable toolchains since 1.60 _should_ work.

## Contact

Expand Down
2 changes: 1 addition & 1 deletion monero-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl<'c> MoneroWalletRpc {

/// Sends amount to address
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
Ok(self.client().transfer_single(0, amount, address).await?)
self.client().transfer_single(0, amount, address).await
}

pub async fn address(&self) -> Result<GetAddress> {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.59"
channel = "1.60"
components = ["clippy"]
targets = ["armv7-unknown-linux-gnueabihf"]
10 changes: 5 additions & 5 deletions swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ async-trait = "0.1"
atty = "0.2"
backoff = { version = "0.4", features = [ "tokio" ] }
base64 = "0.13"
bdk = "0.16"
bdk = "0.21"
big-bytes = "1"
bitcoin = { version = "0.27", features = [ "rand", "use-serde" ] }
bitcoin = { version = "0.28", features = [ "rand", "use-serde" ] }
bmrng = "0.5"
comfy-table = "5.0"
config = { version = "0.11", default-features = false, features = [ "toml" ] }
Expand All @@ -26,7 +26,7 @@ curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" }
data-encoding = "2.3"
dialoguer = "0.10"
directories-next = "2"
ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde" ] }
ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] }
ed25519-dalek = "1"
futures = { version = "0.3", default-features = false }
hex = "0.4"
Expand All @@ -47,7 +47,7 @@ serde_cbor = "0.11"
serde_json = "1"
serde_with = { version = "1", features = [ "macros" ] }
sha2 = "0.9"
sigma_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "ed25519", "serde" ] }
sigma_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "ed25519", "serde", "secp256k1", "alloc" ] }
sqlx = { version = "0.5", features = [ "sqlite", "runtime-tokio-rustls", "offline" ] }
structopt = "0.3"
strum = { version = "0.24", features = [ "derive" ] }
Expand All @@ -74,7 +74,7 @@ tokio-tar = "0.3"
zip = "0.5"

[dev-dependencies]
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs" }
bitcoin-harness = { git = "https://github.com/delta1/bitcoin-harness-rs" } # https://github.com/coblox/bitcoin-harness-rs/pull/25
get-port = "3"
hyper = "0.14"
monero-harness = { path = "../monero-harness" }
Expand Down
24 changes: 12 additions & 12 deletions swap/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use wallet::WalletBuilder;
use crate::bitcoin::wallet::ScriptStatus;
use ::bitcoin::hashes::hex::ToHex;
use ::bitcoin::hashes::Hash;
use ::bitcoin::{secp256k1, SigHash};
use ::bitcoin::{secp256k1, Sighash};
use anyhow::{bail, Context, Result};
use bdk::miniscript::descriptor::Wsh;
use bdk::miniscript::{Descriptor, Segwitv0};
Expand Down Expand Up @@ -78,7 +78,7 @@ impl SecretKey {
self.inner.to_bytes()
}

pub fn sign(&self, digest: SigHash) -> Signature {
pub fn sign(&self, digest: Sighash) -> Signature {
let ecdsa = ECDSA::<Deterministic<Sha256>>::default();

ecdsa.sign(&self.inner, &digest.into_inner())
Expand All @@ -98,7 +98,7 @@ impl SecretKey {
// alice now has s_a and s_b and can refund monero

// self = a, Y = S_b, digest = tx_refund
pub fn encsign(&self, Y: PublicKey, digest: SigHash) -> EncryptedSignature {
pub fn encsign(&self, Y: PublicKey, digest: Sighash) -> EncryptedSignature {
let adaptor = Adaptor::<
HashTranscript<Sha256, rand_chacha::ChaCha20Rng>,
Deterministic<Sha256>,
Expand All @@ -124,12 +124,12 @@ impl From<PublicKey> for Point {
}
}

impl From<PublicKey> for ::bitcoin::PublicKey {
fn from(from: PublicKey) -> Self {
::bitcoin::PublicKey {
compressed: true,
key: from.0.into(),
}
impl TryFrom<PublicKey> for bitcoin::PublicKey {
type Error = bitcoin::util::key::Error;

fn try_from(pubkey: PublicKey) -> Result<Self, Self::Error> {
let bytes = pubkey.0.to_bytes();
bitcoin::PublicKey::from_slice(&bytes)
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ impl From<Scalar> for PublicKey {

pub fn verify_sig(
verification_key: &PublicKey,
transaction_sighash: &SigHash,
transaction_sighash: &Sighash,
sig: &Signature,
) -> Result<()> {
let ecdsa = ECDSA::verify_only();
Expand All @@ -185,7 +185,7 @@ pub struct InvalidSignature;
pub fn verify_encsig(
verification_key: PublicKey,
encryption_key: PublicKey,
digest: &SigHash,
digest: &Sighash,
encsig: &EncryptedSignature,
) -> Result<()> {
let adaptor = Adaptor::<HashTranscript<Sha256>, Deterministic<Sha256>>::default();
Expand Down Expand Up @@ -457,7 +457,7 @@ mod tests {
// transactions have 2 signatures the weight can be up to 8 bytes less than
// the static weight (4 bytes per signature).
fn assert_weight(transaction: Transaction, expected_weight: usize, tx_name: &str) {
let is_weight = transaction.get_weight();
let is_weight = transaction.weight();

assert!(
expected_weight - is_weight <= 8,
Expand Down
40 changes: 24 additions & 16 deletions swap/src/bitcoin/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::bitcoin::wallet::Watchable;
use crate::bitcoin::{
build_shared_output_descriptor, Address, Amount, BlockHeight, PublicKey, Transaction, TxLock,
};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{OutPoint, Script, SigHash, SigHashType, TxIn, TxOut, Txid};
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{EcdsaSighashType, OutPoint, Script, Sighash, TxIn, TxOut, Txid};
use anyhow::Result;
use bdk::miniscript::{Descriptor, DescriptorTrait};
use ecdsa_fun::Signature;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl PartialEq<PunishTimelock> for u32 {
#[derive(Debug)]
pub struct TxCancel {
inner: Transaction,
digest: SigHash,
digest: Sighash,
pub(in crate::bitcoin) output_descriptor: Descriptor<::bitcoin::PublicKey>,
lock_output_descriptor: Descriptor<::bitcoin::PublicKey>,
}
Expand All @@ -110,7 +110,7 @@ impl TxCancel {
previous_output: tx_lock.as_outpoint(),
script_sig: Default::default(),
sequence: cancel_timelock.0,
witness: Vec::new(),
witness: Default::default(),
};

let tx_out = TxOut {
Expand All @@ -125,12 +125,14 @@ impl TxCancel {
output: vec![tx_out],
};

let digest = SigHashCache::new(&transaction).signature_hash(
0, // Only one input: lock_input (lock transaction)
&tx_lock.output_descriptor.script_code(),
tx_lock.lock_amount().as_sat(),
SigHashType::All,
);
let digest = SighashCache::new(&transaction)
.segwit_signature_hash(
0, // Only one input: lock_input (lock transaction)
&tx_lock.output_descriptor.script_code().expect("scriptcode"),
tx_lock.lock_amount().as_sat(),
EcdsaSighashType::All,
)
.expect("sighash");

Self {
inner: transaction,
Expand All @@ -144,7 +146,7 @@ impl TxCancel {
self.inner.txid()
}

pub fn digest(&self) -> SigHash {
pub fn digest(&self) -> Sighash {
self.digest
}

Expand Down Expand Up @@ -198,16 +200,22 @@ impl TxCancel {

let A = ::bitcoin::PublicKey {
compressed: true,
key: A.0.into(),
inner: A.0.into(),
};
let B = ::bitcoin::PublicKey {
compressed: true,
key: B.0.into(),
inner: B.0.into(),
};

// The order in which these are inserted doesn't matter
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
satisfier.insert(B, (sig_b.into(), ::bitcoin::SigHashType::All));
satisfier.insert(A, ::bitcoin::EcdsaSig {
sig: sig_a.into(),
hash_ty: EcdsaSighashType::All,
});
satisfier.insert(B, ::bitcoin::EcdsaSig {
sig: sig_b.into(),
hash_ty: EcdsaSighashType::All,
});

satisfier
};
Expand All @@ -231,7 +239,7 @@ impl TxCancel {
previous_output,
script_sig: Default::default(),
sequence: sequence.map(|seq| seq.0).unwrap_or(0xFFFF_FFFF),
witness: Vec::new(),
witness: Default::default(),
};

let tx_out = TxOut {
Expand Down
12 changes: 6 additions & 6 deletions swap/src/bitcoin/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub struct TxLock {
}

impl TxLock {
pub async fn new<B, D, C>(
wallet: &Wallet<B, D, C>,
pub async fn new<D, C>(
wallet: &Wallet<D, C>,
amount: Amount,
A: PublicKey,
B: PublicKey,
Expand Down Expand Up @@ -57,7 +57,7 @@ impl TxLock {
B: PublicKey,
btc: Amount,
) -> Result<Self> {
let shared_output_candidate = match psbt.global.unsigned_tx.output.as_slice() {
let shared_output_candidate = match psbt.unsigned_tx.output.as_slice() {
[shared_output_candidate, _] if shared_output_candidate.value == btc.as_sat() => {
shared_output_candidate
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl TxLock {
previous_output,
script_sig: Default::default(),
sequence: sequence.unwrap_or(0xFFFF_FFFF),
witness: Vec::new(),
witness: Default::default(),
};

let fee = spending_fee.as_sat();
Expand Down Expand Up @@ -212,7 +212,7 @@ mod tests {

let psbt = bob_make_psbt(A, B, &wallet, agreed_amount).await;
assert_eq!(
psbt.global.unsigned_tx.output.len(),
psbt.unsigned_tx.output.len(),
1,
"psbt should only have a single output"
);
Expand Down Expand Up @@ -264,7 +264,7 @@ mod tests {
async fn bob_make_psbt(
A: PublicKey,
B: PublicKey,
wallet: &Wallet<(), bdk::database::MemoryDatabase, StaticFeeRate>,
wallet: &Wallet<bdk::database::MemoryDatabase, StaticFeeRate>,
amount: Amount,
) -> PartiallySignedTransaction {
let change = wallet.new_address().await.unwrap();
Expand Down
39 changes: 25 additions & 14 deletions swap/src/bitcoin/punish.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::bitcoin::wallet::Watchable;
use crate::bitcoin::{self, Address, Amount, PunishTimelock, Transaction, TxCancel, Txid};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{SigHash, SigHashType};
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{EcdsaSighashType, Sighash};
use anyhow::{Context, Result};
use bdk::bitcoin::Script;
use bdk::miniscript::{Descriptor, DescriptorTrait};
Expand All @@ -10,7 +10,7 @@ use std::collections::HashMap;
#[derive(Debug)]
pub struct TxPunish {
inner: Transaction,
digest: SigHash,
digest: Sighash,
cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>,
watch_script: Script,
}
Expand All @@ -25,12 +25,17 @@ impl TxPunish {
let tx_punish =
tx_cancel.build_spend_transaction(punish_address, Some(punish_timelock), spending_fee);

let digest = SigHashCache::new(&tx_punish).signature_hash(
0, // Only one input: cancel transaction
&tx_cancel.output_descriptor.script_code(),
tx_cancel.amount().as_sat(),
SigHashType::All,
);
let digest = SighashCache::new(&tx_punish)
.segwit_signature_hash(
0, // Only one input: cancel transaction
&tx_cancel
.output_descriptor
.script_code()
.expect("scriptcode"),
tx_cancel.amount().as_sat(),
EcdsaSighashType::All,
)
.expect("sighash");

Self {
inner: tx_punish,
Expand All @@ -40,7 +45,7 @@ impl TxPunish {
}
}

pub fn digest(&self) -> SigHash {
pub fn digest(&self) -> Sighash {
self.digest
}

Expand All @@ -56,12 +61,18 @@ impl TxPunish {
let satisfier = {
let mut satisfier = HashMap::with_capacity(2);

let A = a.public().into();
let B = B.into();
let A = a.public().try_into()?;
let B = B.try_into()?;

// The order in which these are inserted doesn't matter
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
satisfier.insert(B, (sig_b.into(), ::bitcoin::SigHashType::All));
satisfier.insert(A, ::bitcoin::EcdsaSig {
sig: sig_a.into(),
hash_ty: EcdsaSighashType::All,
});
satisfier.insert(B, ::bitcoin::EcdsaSig {
sig: sig_b.into(),
hash_ty: EcdsaSighashType::All,
});

satisfier
};
Expand Down
Loading

0 comments on commit 67fd705

Please sign in to comment.