Skip to content

Commit

Permalink
Revert dependency bump (#225)
Browse files Browse the repository at this point in the history
* typo

* Partial revert "Bump lnp/bp dependencies for dependency allignment (#215)"

This reverts the lnp/bp dependency bump in commit 7fc6d9b.

* Revert "Update bitcoin and deps (#220)"

This reverts commit 29c41e4.
  • Loading branch information
Lederstrumpf authored May 1, 2022
1 parent d38c01c commit f479084
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 152 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,34 @@ bitvec = { version = "0.22.3" }
fixed-hash = { version = "0.7.0", default-features = false }
hex = "0.4.3"
inet2_addr = { version = "0.5.0", default-features = false, features = ["tor", "strict_encoding"] }
lightning_encoding = "=0.5.13"
lightning_encoding = "=0.5.0-beta.3"
serde_crate = { package = "serde", version = "1", features = ["derive"], optional = true }
strict_encoding = "1.7.9"
strict_encoding_derive = "1.7.6"
strict_encoding = "1.7.4"
strict_encoding_derive = "1.7.4"
thiserror = "1.0.24"
tiny-keccak = { version = "2", features = ["keccak"] }

# crypto libs

bincode = { version = "1.3.3", optional = true }
curve25519-dalek = "3.0.2"
ecdsa_fun = { version = "0.7", default-features = false, features = ["all"], optional = true }
ecdsa_fun = { version = "0.6", default-features = false, features = ["all"], optional = true }
rand = { version = "0.8.4", optional = true }
rand_alt = { package = "rand", version = "0.7.3", features = ["std"] }
rand_chacha = { version = "0.3.1", optional = true }
secp256kfun = { version = "0.7", default-features = false, features = ["std", "serde", "libsecp_compat"], optional = true }
secp256kfun = { version = "0.6", default-features = false, features = ["std", "serde", "libsecp_compat"], optional = true }
sha2 = { version = "0.9", optional = true }
sha3 = "0.10.1"

# blockchain specific
bitcoin = "0.28.0-rc.1"
bitcoin = "0.27"
monero = { version = "0.16" }

[dev-dependencies]
bitcoincore-rpc = { git = "https://github.com/romanz/rust-bitcoincore-rpc", rev = "cf505ae02ecd19eaf20465ec09910e724a4ede7e" }
bitcoincore-rpc = "0.14"
lazy_static = "1.4"
rand_core = { version = "^0.6.3", features = ["getrandom"] }
secp256k1 = { version = "0.21", features = ["rand-std"] }
secp256k1 = { version = "0.20", features = ["rand-std"] }
serde_yaml = "0.8"

[package.metadata.docs.rs]
Expand Down
15 changes: 7 additions & 8 deletions src/bitcoin/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<S: Strategy> Fee for Bitcoin<S> {
strategy: &FeeStrategy<SatPerVByte>,
politic: FeePriority,
) -> Result<Amount, FeeStrategyError> {
if tx.unsigned_tx.output.len() != 1 {
if tx.global.unsigned_tx.output.len() != 1 {
return Err(FeeStrategyError::new(
transaction::Error::MultiUTXOUnsuported,
));
Expand All @@ -137,7 +137,7 @@ impl<S: Strategy> Fee for Bitcoin<S> {
// FIXME This does not account for witnesses
// currently the fees are wrong
// Get the transaction weight
let weight = tx.unsigned_tx.get_weight() as u64;
let weight = tx.global.unsigned_tx.get_weight() as u64;

// Compute the fee amount to set in total
let fee_amount = match strategy {
Expand All @@ -150,7 +150,7 @@ impl<S: Strategy> Fee for Bitcoin<S> {
.ok_or(FeeStrategyError::AmountOfFeeTooHigh)?;

// Apply the fee on the first output
tx.unsigned_tx.output[0].value = input_sum
tx.global.unsigned_tx.output[0].value = input_sum
.checked_sub(fee_amount)
.ok_or(FeeStrategyError::NotEnoughAssets)?
.as_sat();
Expand All @@ -164,18 +164,18 @@ impl<S: Strategy> Fee for Bitcoin<S> {
tx: &PartiallySignedTransaction,
strategy: &FeeStrategy<SatPerVByte>,
) -> Result<bool, FeeStrategyError> {
if tx.unsigned_tx.output.len() != 1 {
if tx.global.unsigned_tx.output.len() != 1 {
return Err(FeeStrategyError::new(
transaction::Error::MultiUTXOUnsuported,
));
}

let input_sum = get_available_input_sat(tx)?.as_sat();
let output_sum = tx.unsigned_tx.output[0].value;
let output_sum = tx.global.unsigned_tx.output[0].value;
let fee = input_sum
.checked_sub(output_sum)
.ok_or(FeeStrategyError::AmountOfFeeTooHigh)?;
let weight = tx.unsigned_tx.get_weight() as u64;
let weight = tx.global.unsigned_tx.get_weight() as u64;

let effective_sat_per_vbyte = SatPerVByte::from_sat(
weight
Expand All @@ -196,7 +196,6 @@ mod tests {
for s in [
"0.0001 BTC/vByte",
"100 satoshi/vByte",
"100 satoshis/vByte",
"10 satoshi/vByte",
"1 satoshi/vByte",
]
Expand All @@ -206,7 +205,7 @@ mod tests {
assert!(parse.is_ok());
}
// MUST fail
for s in ["1 satoshi", "100 vByte"].iter() {
for s in ["100 satoshis/vByte", "1 satoshi", "100 vByte"].iter() {
let parse = SatPerVByte::from_str(s);
assert!(parse.is_err());
}
Expand Down
58 changes: 32 additions & 26 deletions src/bitcoin/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ use crate::script::{DataLock, DataPunishableLock, DoubleKeys, ScriptPath};

use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::script::{Builder, Instruction, Script};
use bitcoin::blockdata::transaction::EcdsaSigHashType;
use bitcoin::blockdata::transaction::SigHashType;
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
use bitcoin::secp256k1::{ecdsa::Signature, Message, PublicKey, Secp256k1, SecretKey, Signing};
use bitcoin::util::sighash::SigHashCache;
use bitcoin::secp256k1::{
key::{PublicKey, SecretKey},
Message, Secp256k1, Signature, Signing,
};
use bitcoin::util::bip143::SigHashCache;

use ecdsa_fun::adaptor::EncryptedSignature;

Expand Down Expand Up @@ -81,8 +84,8 @@ impl From<BitcoinSegwitV0> for Btc {
}

pub struct CoopLock {
a: PublicKey,
b: PublicKey,
a: bitcoin::util::key::PublicKey,
b: bitcoin::util::key::PublicKey,
}

impl CoopLock {
Expand All @@ -92,9 +95,9 @@ impl CoopLock {
..
} = data;
Builder::new()
.push_key(&bitcoin::util::key::PublicKey::new(*alice))
.push_key(&bitcoin::util::ecdsa::PublicKey::new(*alice))
.push_opcode(opcodes::all::OP_CHECKSIGVERIFY)
.push_key(&bitcoin::util::key::PublicKey::new(*bob))
.push_key(&bitcoin::util::ecdsa::PublicKey::new(*bob))
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script()
}
Expand Down Expand Up @@ -123,7 +126,7 @@ impl CoopLock {
_ => Err(Error::MissingPublicKey),
},
)?;
let a = PublicKey::from_slice(bytes).map_err(Error::new)?;
let a = bitcoin::util::key::PublicKey::from_slice(bytes).map_err(Error::new)?;
// Checksig verify
ints.next()
.ok_or(Error::WrongTemplate("Missing opcode"))
Expand All @@ -141,7 +144,7 @@ impl CoopLock {
Err(e) => Err(Error::new(e)),
_ => Err(Error::MissingPublicKey),
})?;
let b = PublicKey::from_slice(bytes).map_err(Error::new)?;
let b = bitcoin::util::key::PublicKey::from_slice(bytes).map_err(Error::new)?;
// Checksig
ints.next()
.ok_or(Error::WrongTemplate("Missing opcode"))
Expand All @@ -162,7 +165,7 @@ impl CoopLock {
Ok(Self { a, b })
}

pub fn get_pubkey(&self, swap_role: SwapRole) -> &PublicKey {
pub fn get_pubkey(&self, swap_role: SwapRole) -> &bitcoin::util::key::PublicKey {
match swap_role {
SwapRole::Alice => &self.a,
SwapRole::Bob => &self.b,
Expand All @@ -171,9 +174,9 @@ impl CoopLock {
}

pub struct PunishLock {
alice: PublicKey,
bob: PublicKey,
punish: PublicKey,
alice: bitcoin::util::key::PublicKey,
bob: bitcoin::util::key::PublicKey,
punish: bitcoin::util::key::PublicKey,
}

impl PunishLock {
Expand All @@ -185,15 +188,15 @@ impl PunishLock {
} = data;
Builder::new()
.push_opcode(opcodes::all::OP_IF)
.push_key(&bitcoin::util::key::PublicKey::new(*alice))
.push_key(&bitcoin::util::ecdsa::PublicKey::new(*alice))
.push_opcode(opcodes::all::OP_CHECKSIGVERIFY)
.push_key(&bitcoin::util::key::PublicKey::new(*bob))
.push_key(&bitcoin::util::ecdsa::PublicKey::new(*bob))
.push_opcode(opcodes::all::OP_CHECKSIG)
.push_opcode(opcodes::all::OP_ELSE)
.push_int(timelock.as_u32().into())
.push_opcode(opcodes::all::OP_CSV)
.push_opcode(opcodes::all::OP_DROP)
.push_key(&bitcoin::util::key::PublicKey::new(*failure))
.push_key(&bitcoin::util::ecdsa::PublicKey::new(*failure))
.push_opcode(opcodes::all::OP_CHECKSIG)
.push_opcode(opcodes::all::OP_ENDIF)
.into_script()
Expand Down Expand Up @@ -231,7 +234,7 @@ impl PunishLock {
_ => Err(Error::MissingPublicKey),
},
)?;
let alice = PublicKey::from_slice(bytes).map_err(Error::new)?;
let alice = bitcoin::util::key::PublicKey::from_slice(bytes).map_err(Error::new)?;
// Checksig verify
ints.next()
.ok_or(Error::WrongTemplate("Missing opcode"))
Expand All @@ -249,7 +252,7 @@ impl PunishLock {
Err(e) => Err(Error::new(e)),
_ => Err(Error::MissingPublicKey),
})?;
let bob = PublicKey::from_slice(bytes).map_err(Error::new)?;
let bob = bitcoin::util::key::PublicKey::from_slice(bytes).map_err(Error::new)?;
// Checksig
ints.next()
.ok_or(Error::WrongTemplate("Missing opcode"))
Expand Down Expand Up @@ -293,7 +296,7 @@ impl PunishLock {
Err(e) => Err(Error::new(e)),
_ => Err(Error::MissingPublicKey),
})?;
let punish = PublicKey::from_slice(bytes).map_err(Error::new)?;
let punish = bitcoin::util::key::PublicKey::from_slice(bytes).map_err(Error::new)?;
// Checksig
ints.next()
.ok_or(Error::WrongTemplate("Missing opcode"))
Expand Down Expand Up @@ -322,7 +325,11 @@ impl PunishLock {
Ok(Self { alice, bob, punish })
}

pub fn get_pubkey(&self, swap_role: SwapRole, script_path: ScriptPath) -> Option<&PublicKey> {
pub fn get_pubkey(
&self,
swap_role: SwapRole,
script_path: ScriptPath,
) -> Option<&bitcoin::util::key::PublicKey> {
match script_path {
ScriptPath::Success => match swap_role {
SwapRole::Alice => Some(&self.alice),
Expand Down Expand Up @@ -445,11 +452,10 @@ pub fn signature_hash(
txin: TxInRef,
script: &Script,
value: u64,
sighash_type: EcdsaSigHashType,
sighash_type: SigHashType,
) -> Sha256dHash {
SigHashCache::new(txin.transaction)
.segwit_signature_hash(txin.index, script, value, sighash_type)
.expect("encoding works")
.signature_hash(txin.index, script, value, sighash_type)
.as_hash()
}

Expand All @@ -461,7 +467,7 @@ pub fn sign_input<C>(
txin: TxInRef,
script: &Script,
value: u64,
sighash_type: EcdsaSigHashType,
sighash_type: SigHashType,
secret_key: &bitcoin::secp256k1::SecretKey,
) -> Result<Signature, bitcoin::secp256k1::Error>
where
Expand All @@ -471,7 +477,7 @@ where
let sighash = signature_hash(txin, script, value, sighash_type);
// Makes signature.
let msg = Message::from_slice(&sighash[..])?;
let mut sig = context.sign_ecdsa(&msg, secret_key);
let mut sig = context.sign(&msg, secret_key);
sig.normalize_s();
Ok(sig)
}
Expand All @@ -487,7 +493,7 @@ pub fn sign_hash(
let context = Secp256k1::new();
// Makes signature.
let msg = Message::from_slice(&sighash[..])?;
let mut sig = context.sign_ecdsa(&msg, secret_key);
let mut sig = context.sign(&msg, secret_key);
sig.normalize_s();
Ok(sig)
}
Expand Down
35 changes: 15 additions & 20 deletions src/bitcoin/segwitv0/buy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::marker::PhantomData;

use bitcoin::blockdata::transaction::{TxIn, TxOut};
use bitcoin::blockdata::witness::Witness;
use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::util::ecdsa::EcdsaSig;
use bitcoin::blockdata::transaction::{SigHashType, TxIn, TxOut};
use bitcoin::secp256k1::Signature;
use bitcoin::util::psbt::PartiallySignedTransaction;
use bitcoin::Address;

Expand Down Expand Up @@ -39,11 +37,7 @@ impl SubTransaction for Buy {
.ok_or(FError::MissingSignature)?
.clone();

psbt.inputs[0].final_script_witness = Some(Witness::from_vec(vec![
bob_sig.to_vec(),
alice_sig.to_vec(),
script.into_bytes(),
]));
psbt.inputs[0].final_script_witness = Some(vec![bob_sig, alice_sig, script.into_bytes()]);

Ok(())
}
Expand All @@ -64,7 +58,7 @@ impl Buyable<Bitcoin<SegwitV0>, MetadataOutput> for Tx<Buy> {
previous_output: output_metadata.out_point,
script_sig: bitcoin::Script::default(),
sequence: 0,
witness: Witness::new(),
witness: vec![],
}],
output: vec![TxOut {
value: output_metadata.tx_out.value,
Expand All @@ -78,6 +72,7 @@ impl Buyable<Bitcoin<SegwitV0>, MetadataOutput> for Tx<Buy> {
// Set the input witness data and sighash type
psbt.inputs[0].witness_utxo = Some(output_metadata.tx_out);
psbt.inputs[0].witness_script = output_metadata.script_pubkey;
psbt.inputs[0].sighash_type = Some(SigHashType::All);

Ok(Tx {
psbt,
Expand All @@ -86,25 +81,25 @@ impl Buyable<Bitcoin<SegwitV0>, MetadataOutput> for Tx<Buy> {
}

fn verify_template(&self, destination_target: Address) -> Result<(), FError> {
(self.psbt.unsigned_tx.version == 2)
(self.psbt.global.unsigned_tx.version == 2)
.then(|| 0)
.ok_or(FError::WrongTemplate("Tx version is not 2"))?;
(self.psbt.unsigned_tx.lock_time == 0)
(self.psbt.global.unsigned_tx.lock_time == 0)
.then(|| 0)
.ok_or(FError::WrongTemplate("LockTime is not set to 0"))?;
(self.psbt.unsigned_tx.input.len() == 1)
(self.psbt.global.unsigned_tx.input.len() == 1)
.then(|| 0)
.ok_or(FError::WrongTemplate("Number of inputs is not 1"))?;
(self.psbt.unsigned_tx.output.len() == 1)
(self.psbt.global.unsigned_tx.output.len() == 1)
.then(|| 0)
.ok_or(FError::WrongTemplate("Number of outputs is not 1"))?;

let txin = &self.psbt.unsigned_tx.input[0];
let txin = &self.psbt.global.unsigned_tx.input[0];
(txin.sequence == 0)
.then(|| 0)
.ok_or(FError::WrongTemplate("Sequence is not set to 0"))?;

let txout = &self.psbt.unsigned_tx.output[0];
let txout = &self.psbt.global.unsigned_tx.output[0];
let script_pubkey = destination_target.script_pubkey();
(txout.script_pubkey == script_pubkey)
.then(|| 0)
Expand All @@ -115,9 +110,9 @@ impl Buyable<Bitcoin<SegwitV0>, MetadataOutput> for Tx<Buy> {

fn extract_witness(tx: bitcoin::Transaction) -> Signature {
let TxIn { witness, .. } = &tx.input[0];
let witness_bytes = witness.to_vec();
let ecdsa_sig = EcdsaSig::from_slice(witness_bytes[0].as_ref())
.expect("Validated transaction on-chain, signature and witness position is correct.");
ecdsa_sig.sig
let bytes: &[u8] = witness[0].as_ref();
// Remove SIGHASH type at the end of the signature
Signature::from_der(&bytes[..bytes.len() - 1])
.expect("Validated transaction on-chain, signature and witness position is correct.")
}
}
Loading

0 comments on commit f479084

Please sign in to comment.