Byron Hambly 2 weeks ago committed by GitHub
commit 216080cbd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -31,6 +31,22 @@ jobs:
- name: Run clippy with all features enabled
run: cargo clippy --workspace --all-targets --all-features -- -D warnings
check_stable:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4.1.1
- uses: dtolnay/rust-toolchain@stable
- name: Override rust stable
run: |
rustup override set stable
- name: Run cargo check on rust stable
run: cargo check --all-targets
bdk_test:
runs-on: ubuntu-latest
steps:

218
Cargo.lock generated

@ -329,6 +329,25 @@ dependencies = [
"serde",
]
[[package]]
name = "bincode"
version = "2.0.0-rc.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95"
dependencies = [
"bincode_derive",
"serde",
]
[[package]]
name = "bincode_derive"
version = "2.0.0-rc.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e30759b3b99a1b802a7a3aa21c85c3ded5c28e1c83170d82d70f08bbf7f3e4c"
dependencies = [
"virtue",
]
[[package]]
name = "bit-set"
version = "0.5.2"
@ -353,7 +372,7 @@ dependencies = [
"base64 0.13.1",
"bech32",
"bitcoin_hashes",
"secp256k1",
"secp256k1 0.24.1",
"serde",
]
@ -1084,10 +1103,11 @@ checksum = "5caaa75cbd2b960ff1e5392d2cfb1f44717fffe12fc1f32b7b5d1267f99732a6"
[[package]]
name = "ecdsa_fun"
version = "0.7.1"
source = "git+https://github.com/LLFourn/secp256kfun#9657d8c12fd26df5e57254a0063eaf41082a38ca"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fd850b7ece4e4ddaa1478d5de36b6d4d599f2d521f73456ca706b4e2b32a4ec"
dependencies = [
"bincode",
"bincode 1.3.3",
"rand_chacha 0.3.1",
"secp256kfun",
"sigma_fun",
@ -1278,12 +1298,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "futures"
version = "0.3.30"
@ -2439,7 +2453,6 @@ dependencies = [
"futures",
"monero-rpc",
"rand 0.7.3",
"spectral",
"testcontainers",
"tokio",
"tracing",
@ -2573,42 +2586,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "num"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1"
dependencies = [
"num-integer",
"num-traits",
"rand 0.4.6",
"rustc-serialize",
]
[[package]]
name = "num-complex"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b288631d7878aaf59442cffd36910ea604ecd7745c36054328595114001c9656"
dependencies = [
"num-traits",
"rustc-serialize",
]
[[package]]
name = "num-conv"
version = "0.1.0"
@ -2625,29 +2602,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
"rustc-serialize",
]
[[package]]
name = "num-traits"
version = "0.2.15"
@ -3131,19 +3085,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi",
]
[[package]]
name = "rand"
version = "0.7.3"
@ -3189,21 +3130,6 @@ dependencies = [
"rand_core 0.6.2",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.5.1"
@ -3249,15 +3175,6 @@ dependencies = [
"rand_core 0.6.2",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "redox_syscall"
version = "0.2.10"
@ -3476,12 +3393,6 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6"
[[package]]
name = "rustc-serialize"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
[[package]]
name = "rustc_version"
version = "0.3.3"
@ -3657,7 +3568,27 @@ checksum = "ff55dc09d460954e9ef2fa8a7ced735a964be9981fd50e870b2b3b0705e14964"
dependencies = [
"bitcoin_hashes",
"rand 0.8.3",
"secp256k1-sys",
"secp256k1-sys 0.6.1",
"serde",
]
[[package]]
name = "secp256k1"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f"
dependencies = [
"secp256k1-sys 0.8.1",
"serde",
]
[[package]]
name = "secp256k1"
version = "0.28.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10"
dependencies = [
"secp256k1-sys 0.9.2",
"serde",
]
@ -3670,18 +3601,50 @@ dependencies = [
"cc",
]
[[package]]
name = "secp256k1-sys"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e"
dependencies = [
"cc",
]
[[package]]
name = "secp256k1-sys"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5d1746aae42c19d583c3c1a8c646bfad910498e2051c551a7f2e3c0c9fbb7eb"
dependencies = [
"cc",
]
[[package]]
name = "secp256kfun"
version = "0.7.1"
source = "git+https://github.com/LLFourn/secp256kfun#9657d8c12fd26df5e57254a0063eaf41082a38ca"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecc2adce3ef929c5dc7dacdd612d65ab98002ee18119215ce25d8054ed53c1a"
dependencies = [
"bincode 2.0.0-rc.3",
"digest 0.10.7",
"rand_core 0.6.2",
"secp256k1",
"secp256k1 0.27.0",
"secp256k1 0.28.2",
"secp256kfun_arithmetic_macros",
"serde",
"subtle-ng",
]
[[package]]
name = "secp256kfun_arithmetic_macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91b7c385a72530ebfe6010ff476ad9e235743fb33408a360052bb706f1481e1e"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "security-framework"
version = "2.3.1"
@ -3908,8 +3871,9 @@ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "sigma_fun"
version = "0.4.1"
source = "git+https://github.com/LLFourn/secp256kfun#9657d8c12fd26df5e57254a0063eaf41082a38ca"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b8e9462de42c6f14c7e20154d18d8e9e8683750798885e76f06973317b1cb1d"
dependencies = [
"curve25519-dalek-ng",
"digest 0.10.7",
@ -4033,15 +3997,6 @@ dependencies = [
"sha-1",
]
[[package]]
name = "spectral"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae3c15181f4b14e52eeaac3efaeec4d2764716ce9c86da0c934c3e318649c5ba"
dependencies = [
"num",
]
[[package]]
name = "spin"
version = "0.5.2"
@ -4301,7 +4256,6 @@ dependencies = [
"serial_test",
"sha2 0.10.8",
"sigma_fun",
"spectral",
"sqlx",
"structopt",
"strum",
@ -5104,6 +5058,12 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "virtue"
version = "0.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314"
[[package]]
name = "void"
version = "1.0.2"

@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [ "monero-harness", "monero-rpc", "swap", "monero-wallet" ]
[patch.crates-io]

@ -10,7 +10,6 @@ anyhow = "1"
futures = "0.3"
monero-rpc = { path = "../monero-rpc" }
rand = "0.7"
spectral = "0.6"
testcontainers = "0.14"
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "macros" ] }
tracing = "0.1"

@ -1,6 +1,5 @@
use monero_harness::Monero;
use monero_rpc::monerod::MonerodRpc as _;
use spectral::prelude::*;
use std::time::Duration;
use testcontainers::clients::Cli;
use tokio::time;
@ -21,12 +20,12 @@ async fn init_miner_and_mine_to_miner_address() {
let miner_wallet = monero.wallet("miner").unwrap();
let got_miner_balance = miner_wallet.balance().await.unwrap();
assert_that!(got_miner_balance).is_greater_than(0);
assert!(got_miner_balance > 0);
time::sleep(Duration::from_millis(1010)).await;
// after a bit more than 1 sec another block should have been mined
let block_height = monerod.client().get_block_count().await.unwrap().count;
assert_that(&block_height).is_greater_than(70);
assert!(block_height > 70);
}

@ -1,6 +1,5 @@
use monero_harness::{Monero, MoneroWalletRpc};
use monero_rpc::wallet::MoneroWalletRpc as _;
use spectral::prelude::*;
use std::time::Duration;
use testcontainers::clients::Cli;
use tokio::time::sleep;
@ -29,7 +28,7 @@ async fn fund_transfer_and_check_tx_key() {
// check alice balance
let got_alice_balance = alice_wallet.balance().await.unwrap();
assert_that(&got_alice_balance).is_equal_to(fund_alice);
assert_eq!(got_alice_balance, fund_alice);
// transfer from alice to bob
let bob_address = bob_wallet.address().await.unwrap().address;
@ -41,7 +40,7 @@ async fn fund_transfer_and_check_tx_key() {
wait_for_wallet_to_catch_up(bob_wallet, send_to_bob).await;
let got_bob_balance = bob_wallet.balance().await.unwrap();
assert_that(&got_bob_balance).is_equal_to(send_to_bob);
assert_eq!(got_bob_balance, send_to_bob);
// check if tx was actually seen
let tx_id = transfer.tx_hash;
@ -52,7 +51,7 @@ async fn fund_transfer_and_check_tx_key() {
.await
.expect("failed to check tx by key");
assert_that!(res.received).is_equal_to(send_to_bob);
assert_eq!(res.received, send_to_bob);
}
async fn wait_for_wallet_to_catch_up(wallet: &MoneroWalletRpc, expected_balance: u64) {

@ -27,7 +27,7 @@ data-encoding = "2.6"
dialoguer = "0.11"
digest = "0.10.7"
directories-next = "2"
ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] }
ecdsa_fun = { version = "0.10", default-features = false, features = [ "libsecp_compat", "serde", "adaptor" ] }
ed25519-dalek = "1"
futures = { version = "0.3", default-features = false }
hex = "0.4"
@ -48,7 +48,7 @@ serde_cbor = "0.11"
serde_json = "1"
serde_with = { version = "1", features = [ "macros" ] }
sha2 = "0.10"
sigma_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "ed25519", "serde", "secp256k1", "alloc" ] }
sigma_fun = { version = "0.7", default-features = false, features = [ "ed25519", "serde", "secp256k1", "alloc" ] }
sqlx = { version = "0.6", features = [ "sqlite", "runtime-tokio-rustls", "offline" ] }
structopt = "0.3"
strum = { version = "0.26", features = [ "derive" ] }
@ -84,7 +84,6 @@ port_check = "0.2"
proptest = "1"
serde_cbor = "0.11"
serial_test = "3.0"
spectral = "0.6"
tempfile = "3"
testcontainers = "0.14"

@ -302,7 +302,7 @@ pub mod rendezvous {
fn inject_disconnected(&mut self, peer_id: &PeerId) {
for i in 0..self.rendezvous_nodes.len() {
let mut node = &mut self.rendezvous_nodes[i];
let node = &mut self.rendezvous_nodes[i];
if peer_id == &node.peer_id {
node.connection_status = ConnectionStatus::Disconnected;
}
@ -325,7 +325,7 @@ pub mod rendezvous {
_error: &DialError,
) {
for i in 0..self.rendezvous_nodes.len() {
let mut node = &mut self.rendezvous_nodes[i];
let node = &mut self.rendezvous_nodes[i];
if let Some(id) = peer_id {
if id == node.peer_id {
node.connection_status = ConnectionStatus::Disconnected;

@ -16,6 +16,7 @@ pub use crate::bitcoin::timelocks::{BlockHeight, ExpiredTimelocks};
pub use ::bitcoin::util::amount::Amount;
pub use ::bitcoin::util::psbt::PartiallySignedTransaction;
pub use ::bitcoin::{Address, Network, Transaction, Txid};
use bitcoin::secp256k1::ecdsa;
pub use ecdsa_fun::adaptor::EncryptedSignature;
pub use ecdsa_fun::fun::Scalar;
pub use ecdsa_fun::Signature;
@ -25,9 +26,8 @@ pub use wallet::Wallet;
pub use wallet::WalletBuilder;
use crate::bitcoin::wallet::ScriptStatus;
use ::bitcoin::hashes::hex::ToHex;
use ::bitcoin::hashes::Hash;
use ::bitcoin::{secp256k1, Sighash};
use ::bitcoin::Sighash;
use anyhow::{bail, Context, Result};
use bdk::miniscript::descriptor::Wsh;
use bdk::miniscript::{Descriptor, Segwitv0};
@ -206,20 +206,21 @@ pub fn verify_encsig(
#[error("encrypted signature is invalid")]
pub struct InvalidEncryptedSignature;
pub fn build_shared_output_descriptor(A: Point, B: Point) -> Descriptor<bitcoin::PublicKey> {
pub fn build_shared_output_descriptor(
A: Point,
B: Point,
) -> Result<Descriptor<bitcoin::PublicKey>> {
const MINISCRIPT_TEMPLATE: &str = "c:and_v(v:pk(A),pk_k(B))";
// NOTE: This shouldn't be a source of error, but maybe it is
let A = ToHex::to_hex(&secp256k1::PublicKey::from(A));
let B = ToHex::to_hex(&secp256k1::PublicKey::from(B));
let miniscript = MINISCRIPT_TEMPLATE.replace('A', &A).replace('B', &B);
let miniscript = MINISCRIPT_TEMPLATE
.replace('A', &A.to_string())
.replace('B', &B.to_string());
let miniscript =
bdk::miniscript::Miniscript::<bitcoin::PublicKey, Segwitv0>::from_str(&miniscript)
.expect("a valid miniscript");
Descriptor::Wsh(Wsh::new(miniscript).expect("a valid descriptor"))
Ok(Descriptor::Wsh(Wsh::new(miniscript)?))
}
pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result<SecretKey> {
@ -250,6 +251,13 @@ pub fn current_epoch(
ExpiredTimelocks::None
}
// Transform the ecdsa der signature bytes into a secp256kfun ecdsa signature type.
pub fn extract_ecdsa_sig(sig: &[u8]) -> Result<Signature> {
let data = &sig[..sig.len() - 1];
let sig = ecdsa::Signature::from_der(data)?.serialize_compact();
Signature::from_bytes(sig).ok_or(anyhow::anyhow!("invalid signature"))
}
/// Bitcoin error codes: https://github.com/bitcoin/bitcoin/blob/97d3500601c1d28642347d014a6de1e38f53ae4e/src/rpc/protocol.h#L23
pub enum RpcErrorCode {
/// Transaction or block was rejected by network rules. Error code -26.
@ -323,6 +331,8 @@ mod tests {
use super::*;
use crate::env::{GetConfig, Regtest};
use crate::protocol::{alice, bob};
use bitcoin::secp256k1;
use ecdsa_fun::fun::marker::{NonZero, Public};
use rand::rngs::OsRng;
use uuid::Uuid;
@ -468,4 +478,16 @@ mod tests {
transaction
)
}
#[test]
fn compare_point_hex() {
// secp256kfun Point and secp256k1 PublicKey should have the same bytes and hex representation
let secp = secp256k1::Secp256k1::default();
let keypair = secp256k1::KeyPair::new(&secp, &mut OsRng);
let pubkey = keypair.public_key();
let point: Point<_, Public, NonZero> = Point::from_bytes(pubkey.serialize()).unwrap();
assert_eq!(pubkey.to_string(), point.to_string());
}
}

@ -5,7 +5,8 @@ use crate::bitcoin::{
};
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{
EcdsaSighashType, OutPoint, PackedLockTime, Script, Sequence, Sighash, TxIn, TxOut, Txid,
secp256k1, EcdsaSighashType, OutPoint, PackedLockTime, Script, Sequence, Sighash, TxIn, TxOut,
Txid,
};
use anyhow::Result;
use bdk::miniscript::Descriptor;
@ -105,8 +106,8 @@ impl TxCancel {
A: PublicKey,
B: PublicKey,
spending_fee: Amount,
) -> Self {
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0);
) -> Result<Self> {
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0)?;
let tx_in = TxIn {
previous_output: tx_lock.as_outpoint(),
@ -136,12 +137,12 @@ impl TxCancel {
)
.expect("sighash");
Self {
Ok(Self {
inner: transaction,
digest,
output_descriptor: cancel_output_descriptor,
lock_output_descriptor: tx_lock.output_descriptor.clone(),
}
})
}
pub fn txid(&self) -> Txid {
@ -202,25 +203,27 @@ impl TxCancel {
let A = ::bitcoin::PublicKey {
compressed: true,
inner: A.0.into(),
inner: secp256k1::PublicKey::from_slice(&A.0.to_bytes())?,
};
let B = ::bitcoin::PublicKey {
compressed: true,
inner: B.0.into(),
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
};
// The order in which these are inserted doesn't matter
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a.into(),
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b.into(),
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);

@ -31,7 +31,7 @@ impl TxLock {
C: EstimateFeeRate,
D: BatchDatabase,
{
let lock_output_descriptor = build_shared_output_descriptor(A.0, B.0);
let lock_output_descriptor = build_shared_output_descriptor(A.0, B.0)?;
let address = lock_output_descriptor
.address(wallet.get_network())
.expect("can derive address from descriptor");
@ -83,7 +83,7 @@ impl TxLock {
}
};
let descriptor = build_shared_output_descriptor(A.0, B.0);
let descriptor = build_shared_output_descriptor(A.0, B.0)?;
let legit_shared_output_script = descriptor.script_pubkey();
if shared_output_candidate.script_pubkey != legit_shared_output_script {
@ -253,7 +253,7 @@ mod tests {
fn estimated_tx_lock_script_size_never_changes(a in crate::proptest::ecdsa_fun::point(), b in crate::proptest::ecdsa_fun::point()) {
proptest::prop_assume!(a != b);
let computed_size = build_shared_output_descriptor(a, b).script_pubkey().len();
let computed_size = build_shared_output_descriptor(a, b).unwrap().script_pubkey().len();
assert_eq!(computed_size, SCRIPT_SIZE);
}

@ -1,7 +1,7 @@
use crate::bitcoin::wallet::Watchable;
use crate::bitcoin::{self, Address, Amount, PunishTimelock, Transaction, TxCancel, Txid};
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{EcdsaSighashType, Sighash};
use ::bitcoin::{secp256k1, EcdsaSighashType, Sighash};
use anyhow::{Context, Result};
use bdk::bitcoin::Script;
use bdk::miniscript::Descriptor;
@ -64,18 +64,20 @@ impl TxPunish {
let A = a.public().try_into()?;
let B = B.try_into()?;
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
// The order in which these are inserted doesn't matter
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a.into(),
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b.into(),
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);

@ -6,7 +6,7 @@ use crate::bitcoin::{
use ::bitcoin::{Sighash, Txid};
use anyhow::{bail, Context, Result};
use bdk::miniscript::Descriptor;
use bitcoin::secp256k1::ecdsa;
use bitcoin::secp256k1;
use bitcoin::util::sighash::SighashCache;
use bitcoin::{EcdsaSighashType, Script};
use ecdsa_fun::adaptor::{Adaptor, HashTranscript};
@ -16,6 +16,8 @@ use ecdsa_fun::Signature;
use sha2::Sha256;
use std::collections::HashMap;
use super::extract_ecdsa_sig;
#[derive(Clone, Debug)]
pub struct TxRedeem {
inner: Transaction,
@ -64,7 +66,7 @@ impl TxRedeem {
) -> Result<Transaction> {
verify_encsig(
B,
PublicKey::from(s_a.clone()),
PublicKey::from(s_a),
&self.digest(),
&encrypted_signature,
)
@ -79,25 +81,27 @@ impl TxRedeem {
let A = ::bitcoin::PublicKey {
compressed: true,
inner: a.public.into(),
inner: secp256k1::PublicKey::from_slice(&a.public.to_bytes())?,
};
let B = ::bitcoin::PublicKey {
compressed: true,
inner: B.0.into(),
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
};
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
// The order in which these are inserted doesn't matter
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a.into(),
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b.into(),
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);
@ -123,11 +127,11 @@ impl TxRedeem {
[inputs @ ..] => bail!(TooManyInputs(inputs.len())),
};
let sigs = match input.witness.iter().collect::<Vec<_>>().as_slice() {
let sigs = match input.witness.to_vec().as_slice() {
[sig_1, sig_2, _script] => [sig_1, sig_2]
.iter()
.map(|sig| ecdsa::Signature::from_der(&sig[..sig.len() - 1]).map(Signature::from))
.collect::<std::result::Result<Vec<_>, _>>(),
.into_iter()
.map(|sig| extract_ecdsa_sig(sig))
.collect::<Result<Vec<_>, _>>(),
[] => bail!(EmptyWitnessStack),
[witnesses @ ..] => bail!(NotThreeWitnesses(witnesses.len())),
}?;

@ -4,7 +4,7 @@ use crate::bitcoin::{
TooManyInputs, Transaction, TxCancel,
};
use crate::{bitcoin, monero};
use ::bitcoin::secp256k1::ecdsa;
use ::bitcoin::secp256k1;
use ::bitcoin::util::sighash::SighashCache;
use ::bitcoin::{EcdsaSighashType, Script, Sighash, Txid};
use anyhow::{bail, Context, Result};
@ -12,6 +12,8 @@ use bdk::miniscript::Descriptor;
use ecdsa_fun::Signature;
use std::collections::HashMap;
use super::extract_ecdsa_sig;
#[derive(Debug)]
pub struct TxRefund {
inner: Transaction,
@ -62,25 +64,27 @@ impl TxRefund {
let A = ::bitcoin::PublicKey {
compressed: true,
inner: A.0.into(),
inner: secp256k1::PublicKey::from_slice(&A.0.to_bytes())?,
};
let B = ::bitcoin::PublicKey {
compressed: true,
inner: B.0.into(),
inner: secp256k1::PublicKey::from_slice(&B.0.to_bytes())?,
};
let sig_a = secp256k1::ecdsa::Signature::from_compact(&sig_a.to_bytes())?;
let sig_b = secp256k1::ecdsa::Signature::from_compact(&sig_b.to_bytes())?;
// The order in which these are inserted doesn't matter
satisfier.insert(
A,
::bitcoin::EcdsaSig {
sig: sig_a.into(),
sig: sig_a,
hash_ty: EcdsaSighashType::All,
},
);
satisfier.insert(
B,
::bitcoin::EcdsaSig {
sig: sig_b.into(),
sig: sig_b,
hash_ty: EcdsaSighashType::All,
},
);
@ -130,11 +134,11 @@ impl TxRefund {
[inputs @ ..] => bail!(TooManyInputs(inputs.len())),
};
let sigs = match input.witness.iter().collect::<Vec<_>>().as_slice() {
let sigs = match input.witness.to_vec().as_slice() {
[sig_1, sig_2, _script] => [sig_1, sig_2]
.iter()
.map(|sig| ecdsa::Signature::from_der(&sig[..sig.len() - 1]).map(Signature::from))
.collect::<std::result::Result<Vec<_>, _>>(),
.into_iter()
.map(|sig| extract_ecdsa_sig(sig))
.collect::<Result<Vec<_>, _>>(),
[] => bail!(EmptyWitnessStack),
[witnesses @ ..] => bail!(NotThreeWitnesses(witnesses.len())),
}?;

@ -310,7 +310,8 @@ impl State2 {
self.a.public(),
self.B,
self.tx_cancel_fee,
);
)
.expect("valid cancel tx");
let tx_refund =
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
@ -335,7 +336,7 @@ impl State2 {
self.a.public(),
self.B,
self.tx_cancel_fee,
);
)?;
bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig)
.context("Failed to verify cancel transaction")?;
let tx_punish = bitcoin::TxPunish::new(
@ -458,6 +459,7 @@ impl State3 {
self.B,
self.tx_cancel_fee,
)
.expect("valid cancel tx")
}
pub fn tx_refund(&self) -> TxRefund {

@ -242,7 +242,7 @@ impl State1 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)?;
let tx_refund =
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);
@ -315,7 +315,8 @@ impl State2 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)
.expect("valid cancel tx");
let tx_cancel_sig = self.b.sign(tx_cancel.digest());
let tx_punish = bitcoin::TxPunish::new(
&tx_cancel,
@ -449,7 +450,7 @@ impl State3 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)?;
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
@ -530,7 +531,7 @@ impl State4 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)?;
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
@ -611,7 +612,7 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)?;
let tx_lock_status = bitcoin_wallet.status_of_script(&self.tx_lock).await?;
let tx_cancel_status = bitcoin_wallet.status_of_script(&tx_cancel).await?;
@ -634,7 +635,7 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)?;
let tx = bitcoin_wallet.get_raw_transaction(tx_cancel.txid()).await?;
@ -651,7 +652,7 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
)
)?
.complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone())
.context("Failed to complete Bitcoin cancel transaction")?;
@ -674,7 +675,7 @@ impl State6 {
self.A,
self.b.public(),
self.tx_cancel_fee,
);
)?;
let tx_refund =
bitcoin::TxRefund::new(&tx_cancel, &self.refund_address, self.tx_refund_fee);

Loading…
Cancel
Save