diff --git a/Cargo.lock b/Cargo.lock index feb3d8ccd..5cdb12be6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,15 +191,6 @@ version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" -[[package]] -name = "base64-compat" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a8d4d2746f89841e49230dd26917df1876050f95abafafbe34f47cb534b88d7" -dependencies = [ - "byteorder", -] - [[package]] name = "bdk" version = "0.24.0" @@ -258,10 +249,11 @@ checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" [[package]] name = "bip39" -version = "1.0.1" -source = "git+https://github.com/get10101/rust-bip39?rev=991760d3364a9cb18d8ff89ac6ad1abef2ed333f#991760d3364a9cb18d8ff89ac6ad1abef2ed333f" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" dependencies = [ - "bitcoin_hashes 0.9.7", + "bitcoin_hashes", "rand_core", "serde", "unicode-normalization", @@ -275,7 +267,7 @@ checksum = "0694ea59225b0c5f3cb405ff3f670e4828358ed26aec49dc352f730f0cb1a8a3" dependencies = [ "base64 0.13.1", "bech32 0.9.1", - "bitcoin_hashes 0.11.0", + "bitcoin_hashes", "secp256k1", "serde", ] @@ -289,22 +281,6 @@ dependencies = [ "bech32 0.8.1", ] -[[package]] -name = "bitcoin-test-utils" -version = "0.1.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" -dependencies = [ - "bitcoin", - "bitcoincore-rpc", - "bitcoincore-rpc-json", -] - -[[package]] -name = "bitcoin_hashes" -version = "0.9.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ce18265ec2324ad075345d5814fbeed4f41f0a660055dc78840b74d19b874b1" - [[package]] name = "bitcoin_hashes" version = "0.11.0" @@ -314,30 +290,6 @@ dependencies = [ "serde", ] -[[package]] -name = "bitcoincore-rpc" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0261b2bb7617e0c91b452a837bbd1291fd34ad6990cb8e3ffc28239cc045b5ca" -dependencies = [ - "bitcoincore-rpc-json", - "jsonrpc", - "log", - "serde", - "serde_json", -] - -[[package]] -name = "bitcoincore-rpc-json" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c231bea28e314879c5aef240f6052e8a72a369e3c9f9b20d9bfbb33ad18029b2" -dependencies = [ - "bitcoin", - "serde", - "serde_json", -] - [[package]] name = "bitflags" version = "1.3.2" @@ -782,8 +734,8 @@ dependencies = [ [[package]] name = "dlc" -version = "0.3.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +version = "0.4.0" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "bitcoin", "miniscript", @@ -794,12 +746,11 @@ dependencies = [ [[package]] name = "dlc-manager" -version = "0.3.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +version = "0.4.0" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "async-trait", "bitcoin", - "bitcoin-test-utils", "dlc", "dlc-messages", "dlc-trie", @@ -811,8 +762,8 @@ dependencies = [ [[package]] name = "dlc-messages" -version = "0.3.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +version = "0.4.0" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "bitcoin", "dlc", @@ -824,7 +775,7 @@ dependencies = [ [[package]] name = "dlc-sled-storage-provider" version = "0.1.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "bitcoin", "dlc-manager", @@ -836,8 +787,8 @@ dependencies = [ [[package]] name = "dlc-trie" -version = "0.3.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +version = "0.4.0" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "bitcoin", "dlc", @@ -1388,18 +1339,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "jsonrpc" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f8423b78fc94d12ef1a4a9d13c348c9a78766dda0cc18817adf0faf77e670c8" -dependencies = [ - "base64-compat", - "serde", - "serde_derive", - "serde_json", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -1425,7 +1364,7 @@ dependencies = [ [[package]] name = "lightning" version = "0.0.113" -source = "git+https://github.com/get10101/rust-lightning/?rev=77fc8a11b0f10fb7e31f246b59a37455d4f0d41d#77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" +source = "git+https://github.com/p2pderivatives/rust-lightning/?branch=split-tx-experiment#be41078f333577f7d513ea26543dcc79b0588698" dependencies = [ "bitcoin", ] @@ -1433,7 +1372,7 @@ dependencies = [ [[package]] name = "lightning-background-processor" version = "0.0.113" -source = "git+https://github.com/get10101/rust-lightning/?rev=77fc8a11b0f10fb7e31f246b59a37455d4f0d41d#77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" +source = "git+https://github.com/p2pderivatives/rust-lightning/?branch=split-tx-experiment#be41078f333577f7d513ea26543dcc79b0588698" dependencies = [ "bitcoin", "lightning", @@ -1443,7 +1382,7 @@ dependencies = [ [[package]] name = "lightning-block-sync" version = "0.0.113" -source = "git+https://github.com/get10101/rust-lightning/?rev=77fc8a11b0f10fb7e31f246b59a37455d4f0d41d#77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" +source = "git+https://github.com/p2pderivatives/rust-lightning/?branch=split-tx-experiment#be41078f333577f7d513ea26543dcc79b0588698" dependencies = [ "bitcoin", "chunked_transfer", @@ -1460,7 +1399,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9680857590c3529cf8c7d32b04501f215f2bf1e029fdfa22f4112f66c1741e4" dependencies = [ "bech32 0.9.1", - "bitcoin_hashes 0.11.0", + "bitcoin_hashes", "lightning", "num-traits", "secp256k1", @@ -1469,7 +1408,7 @@ dependencies = [ [[package]] name = "lightning-net-tokio" version = "0.0.113" -source = "git+https://github.com/get10101/rust-lightning/?rev=77fc8a11b0f10fb7e31f246b59a37455d4f0d41d#77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" +source = "git+https://github.com/p2pderivatives/rust-lightning/?branch=split-tx-experiment#be41078f333577f7d513ea26543dcc79b0588698" dependencies = [ "bitcoin", "lightning", @@ -1479,7 +1418,7 @@ dependencies = [ [[package]] name = "lightning-persister" version = "0.0.113" -source = "git+https://github.com/get10101/rust-lightning/?rev=77fc8a11b0f10fb7e31f246b59a37455d4f0d41d#77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" +source = "git+https://github.com/p2pderivatives/rust-lightning/?branch=split-tx-experiment#be41078f333577f7d513ea26543dcc79b0588698" dependencies = [ "bitcoin", "libc", @@ -1490,7 +1429,7 @@ dependencies = [ [[package]] name = "lightning-rapid-gossip-sync" version = "0.0.113" -source = "git+https://github.com/get10101/rust-lightning/?rev=77fc8a11b0f10fb7e31f246b59a37455d4f0d41d#77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" +source = "git+https://github.com/p2pderivatives/rust-lightning/?branch=split-tx-experiment#be41078f333577f7d513ea26543dcc79b0588698" dependencies = [ "bitcoin", "lightning", @@ -1542,6 +1481,7 @@ dependencies = [ "rand", "reqwest", "rust_decimal", + "secp256k1-zkp", "serde", "sha2", "simple-wallet", @@ -1610,7 +1550,6 @@ dependencies = [ "clap", "diesel", "diesel_migrations", - "dlc-manager", "futures", "hex 0.4.3", "ln-dlc-node", @@ -1646,12 +1585,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "memchr" version = "2.5.0" @@ -1733,7 +1666,6 @@ dependencies = [ "bdk", "diesel", "diesel_migrations", - "dlc-manager", "flutter_rust_bridge", "lightning-invoice", "ln-dlc-node", @@ -1892,7 +1824,7 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "p2pd-oracle-client" version = "0.1.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "chrono", "dlc-manager", @@ -1933,7 +1865,7 @@ dependencies = [ "instant", "libc", "redox_syscall", - "smallvec 1.10.0", + "smallvec", "winapi", ] @@ -1946,7 +1878,7 @@ dependencies = [ "cfg-if", "libc", "redox_syscall", - "smallvec 1.10.0", + "smallvec", "windows-sys 0.45.0", ] @@ -2378,7 +2310,7 @@ version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b1629c9c557ef9b293568b338dddfc8208c98a18c59d722a9d53f859d9c9b62" dependencies = [ - "bitcoin_hashes 0.11.0", + "bitcoin_hashes", "rand", "secp256k1-sys", "serde", @@ -2555,7 +2487,7 @@ dependencies = [ [[package]] name = "simple-wallet" version = "0.1.0" -source = "git+https://github.com/get10101/rust-dlc?rev=3c57f779a1c6f10db19630af7e8d34a69d0e4c39#3c57f779a1c6f10db19630af7e8d34a69d0e4c39" +source = "git+https://github.com/get10101/rust-dlc?branch=feature/ln-dlc-channels#b3d31730874ffe422d27d4bbdbb508532a5ba8ee" dependencies = [ "bitcoin", "dlc", @@ -2590,15 +2522,6 @@ dependencies = [ "parking_lot 0.11.2", ] -[[package]] -name = "smallvec" -version = "0.6.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97fcaeba89edba30f044a10c6a3cc39df9c3f17d7cd829dd1446cab35f890e0" -dependencies = [ - "maybe-uninit", -] - [[package]] name = "smallvec" version = "1.10.0" @@ -2775,6 +2698,21 @@ dependencies = [ "time-core", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.25.0" @@ -2966,7 +2904,7 @@ dependencies = [ "serde", "serde_json", "sharded-slab", - "smallvec 1.10.0", + "smallvec", "thread_local", "time 0.3.20", "tracing", @@ -3030,11 +2968,11 @@ checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" [[package]] name = "unicode-normalization" -version = "0.1.9" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09c8070a9942f5e7cfccd93f490fdebd230ee3c3c9f107cb25bad5351ef671cf" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" dependencies = [ - "smallvec 0.6.14", + "tinyvec", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 1f1556570..151d74a9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,17 +3,16 @@ members = ["coordinator", "maker", "mobile/native/", "crates/*"] resolver = "2" [patch.crates-io] -bip39 = { git = "https://github.com/get10101/rust-bip39", rev = "991760d3364a9cb18d8ff89ac6ad1abef2ed333f" } # Until https://github.com/rust-bitcoin/rust-bip39/pull/38 is merged and released -dlc-manager = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -dlc-messages = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -dlc = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -dlc-sled-storage-provider = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -p2pd-oracle-client = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -dlc-trie = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -simple-wallet = { git = "https://github.com/get10101/rust-dlc", rev = "3c57f779a1c6f10db19630af7e8d34a69d0e4c39" } -lightning = { git = "https://github.com/get10101/rust-lightning/", rev = "77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" } -lightning-background-processor = { git = "https://github.com/get10101/rust-lightning/", rev = "77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" } -lightning-block-sync = { git = "https://github.com/get10101/rust-lightning/", rev = "77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" } -lightning-net-tokio = { git = "https://github.com/get10101/rust-lightning/", rev = "77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" } -lightning-persister = { git = "https://github.com/get10101/rust-lightning/", rev = "77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" } -lightning-rapid-gossip-sync = { git = "https://github.com/get10101/rust-lightning/", rev = "77fc8a11b0f10fb7e31f246b59a37455d4f0d41d" } +dlc-manager = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +dlc-messages = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +dlc = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +dlc-sled-storage-provider = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +p2pd-oracle-client = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +dlc-trie = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +simple-wallet = { git = "https://github.com/get10101/rust-dlc", branch = "feature/ln-dlc-channels" } +lightning = { git = "https://github.com/p2pderivatives/rust-lightning/", branch = "split-tx-experiment" } +lightning-background-processor = { git = "https://github.com/p2pderivatives/rust-lightning/", branch = "split-tx-experiment" } +lightning-block-sync = { git = "https://github.com/p2pderivatives/rust-lightning/", branch = "split-tx-experiment" } +lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/", branch = "split-tx-experiment" } +lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", branch = "split-tx-experiment" } +lightning-rapid-gossip-sync = { git = "https://github.com/p2pderivatives/rust-lightning/", branch = "split-tx-experiment" } diff --git a/coordinator/Cargo.toml b/coordinator/Cargo.toml index fea3cb357..63897f713 100644 --- a/coordinator/Cargo.toml +++ b/coordinator/Cargo.toml @@ -12,7 +12,7 @@ bitcoin = "0.29" clap = { version = "4", features = ["derive"] } diesel = { version = "2.0.0", features = ["r2d2", "postgres"] } diesel_migrations = "2.0.0" -dlc-manager = { version = "0.3.0", features = ["use-serde"] } +dlc-manager = { version = "0.4.0", features = ["use-serde"] } # required for the ContractInput type futures = "0.3" ln-dlc-node = { path = "../crates/ln-dlc-node" } rand = "0.8.5" diff --git a/coordinator/src/routes.rs b/coordinator/src/routes.rs index 582f9c980..4fd04133b 100644 --- a/coordinator/src/routes.rs +++ b/coordinator/src/routes.rs @@ -20,7 +20,6 @@ use diesel::r2d2::ConnectionManager; use diesel::r2d2::Pool; use diesel::PgConnection; use dlc_manager::contract::contract_input::ContractInput; -use dlc_manager::Wallet; use ln_dlc_node::node::Node; use serde::Deserialize; use serde::Serialize; @@ -88,10 +87,10 @@ pub async fn post_fake_scid( pub async fn get_new_address( State(app_state): State>, ) -> Result, AppError> { - let address = - app_state.node.wallet.get_new_address().map_err(|e| { - AppError::InternalServerError(format!("Failed to get new address: {e:#}")) - })?; + let address = app_state + .node + .get_new_address() + .map_err(|e| AppError::InternalServerError(format!("Failed to get new address: {e:#}")))?; Ok(Json(address.to_string())) } diff --git a/crates/ln-dlc-node/Cargo.toml b/crates/ln-dlc-node/Cargo.toml index 24e59fcd0..2695387e6 100644 --- a/crates/ln-dlc-node/Cargo.toml +++ b/crates/ln-dlc-node/Cargo.toml @@ -10,14 +10,14 @@ description = "A common interface for using Lightning and DLC channels side-by-s anyhow = { version = "1", features = ["backtrace"] } bdk = { version = "0.24.0", features = ["key-value-db"] } bdk-ldk = { path = "../bdk-ldk" } -bip39 = "1.0.1" +bip39 = { version = "2", features = ["rand_core"] } bitcoin = "0.29" bitcoin-bech32 = "0.12" -dlc = { version = "0.3.0" } -dlc-manager = { version = "0.3.0", features = ["use-serde"] } -dlc-messages = { version = "0.3.0" } -dlc-sled-storage-provider = { version = "0.1.0" } -dlc-trie = { version = "0.3.0" } +dlc = { version = "0.4.0" } +dlc-manager = { version = "0.4.0", features = ["use-serde"] } +dlc-messages = { version = "0.4.0" } +dlc-sled-storage-provider = { version = "0.1.0", features = ["wallet"] } +dlc-trie = { version = "0.4.0" } futures = "0.3" hex = "0.3" hkdf = "0.12" @@ -32,6 +32,7 @@ log = "0.4.17" p2pd-oracle-client = { version = "0.1.0" } rand = "0.8.5" reqwest = { version = "0.11", default-features = false, features = ["json"] } +secp256k1-zkp = { version = "0.7.0" } serde = "1.0.147" sha2 = "0.10" simple-wallet = "0.1.0" diff --git a/crates/ln-dlc-node/src/dlc_custom_signer.rs b/crates/ln-dlc-node/src/dlc_custom_signer.rs new file mode 100644 index 000000000..822fefc24 --- /dev/null +++ b/crates/ln-dlc-node/src/dlc_custom_signer.rs @@ -0,0 +1,373 @@ +//! This file has temporarily been copied from `https://github.com/p2pderivatives/rust-dlc/pull/97`. +//! We should reimplement some of these traits for production. + +use bitcoin::Script; +use bitcoin::Transaction; +use bitcoin::TxOut; +use lightning::chain::keysinterface::BaseSign; +use lightning::chain::keysinterface::ExtraSign; +use lightning::chain::keysinterface::InMemorySigner; +use lightning::chain::keysinterface::KeyMaterial; +use lightning::chain::keysinterface::KeysInterface; +use lightning::chain::keysinterface::KeysManager; +use lightning::chain::keysinterface::Recipient; +use lightning::chain::keysinterface::Sign; +use lightning::chain::keysinterface::SpendableOutputDescriptor; +use lightning::ln::chan_utils::ChannelPublicKeys; +use lightning::ln::msgs::DecodeError; +use lightning::ln::script::ShutdownScript; +use lightning::util::ser::Writeable; +use secp256k1_zkp::ecdsa::RecoverableSignature; +use secp256k1_zkp::Secp256k1; +use secp256k1_zkp::SecretKey; +use secp256k1_zkp::Signing; +use std::sync::Arc; +use std::sync::Mutex; + +pub struct CustomSigner { + in_memory_signer: Arc>, + // TODO(tibo): this might not be safe. + channel_public_keys: ChannelPublicKeys, +} + +impl CustomSigner { + pub fn new(in_memory_signer: InMemorySigner) -> Self { + Self { + channel_public_keys: in_memory_signer.pubkeys().clone(), + in_memory_signer: Arc::new(Mutex::new(in_memory_signer)), + } + } +} + +impl Clone for CustomSigner { + fn clone(&self) -> Self { + Self { + in_memory_signer: self.in_memory_signer.clone(), + channel_public_keys: self.channel_public_keys.clone(), + } + } +} + +impl BaseSign for CustomSigner { + fn get_per_commitment_point( + &self, + idx: u64, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> secp256k1_zkp::PublicKey { + self.in_memory_signer + .lock() + .unwrap() + .get_per_commitment_point(idx, secp_ctx) + } + + fn release_commitment_secret(&self, idx: u64) -> [u8; 32] { + self.in_memory_signer + .lock() + .unwrap() + .release_commitment_secret(idx) + } + + fn validate_holder_commitment( + &self, + holder_tx: &lightning::ln::chan_utils::HolderCommitmentTransaction, + preimages: Vec, + ) -> Result<(), ()> { + self.in_memory_signer + .lock() + .unwrap() + .validate_holder_commitment(holder_tx, preimages) + } + + fn pubkeys(&self) -> &lightning::ln::chan_utils::ChannelPublicKeys { + &self.channel_public_keys + } + + fn channel_keys_id(&self) -> [u8; 32] { + self.in_memory_signer.lock().unwrap().channel_keys_id() + } + + fn sign_counterparty_commitment( + &self, + commitment_tx: &lightning::ln::chan_utils::CommitmentTransaction, + preimages: Vec, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result< + ( + secp256k1_zkp::ecdsa::Signature, + Vec, + ), + (), + > { + self.in_memory_signer + .lock() + .unwrap() + .sign_counterparty_commitment(commitment_tx, preimages, secp_ctx) + } + + fn validate_counterparty_revocation( + &self, + idx: u64, + secret: &secp256k1_zkp::SecretKey, + ) -> Result<(), ()> { + self.in_memory_signer + .lock() + .unwrap() + .validate_counterparty_revocation(idx, secret) + } + + fn sign_holder_commitment_and_htlcs( + &self, + commitment_tx: &lightning::ln::chan_utils::HolderCommitmentTransaction, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result< + ( + secp256k1_zkp::ecdsa::Signature, + Vec, + ), + (), + > { + self.in_memory_signer + .lock() + .unwrap() + .sign_holder_commitment_and_htlcs(commitment_tx, secp_ctx) + } + + fn sign_justice_revoked_output( + &self, + justice_tx: &bitcoin::Transaction, + input: usize, + amount: u64, + per_commitment_key: &secp256k1_zkp::SecretKey, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result { + self.in_memory_signer + .lock() + .unwrap() + .sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key, secp_ctx) + } + + fn sign_justice_revoked_htlc( + &self, + justice_tx: &bitcoin::Transaction, + input: usize, + amount: u64, + per_commitment_key: &secp256k1_zkp::SecretKey, + htlc: &lightning::ln::chan_utils::HTLCOutputInCommitment, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result { + self.in_memory_signer + .lock() + .unwrap() + .sign_justice_revoked_htlc( + justice_tx, + input, + amount, + per_commitment_key, + htlc, + secp_ctx, + ) + } + + fn sign_counterparty_htlc_transaction( + &self, + htlc_tx: &bitcoin::Transaction, + input: usize, + amount: u64, + per_commitment_point: &secp256k1_zkp::PublicKey, + htlc: &lightning::ln::chan_utils::HTLCOutputInCommitment, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result { + self.in_memory_signer + .lock() + .unwrap() + .sign_counterparty_htlc_transaction( + htlc_tx, + input, + amount, + per_commitment_point, + htlc, + secp_ctx, + ) + } + + fn sign_closing_transaction( + &self, + closing_tx: &lightning::ln::chan_utils::ClosingTransaction, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result { + self.in_memory_signer + .lock() + .unwrap() + .sign_closing_transaction(closing_tx, secp_ctx) + } + + fn sign_channel_announcement( + &self, + msg: &lightning::ln::msgs::UnsignedChannelAnnouncement, + secp_ctx: &secp256k1_zkp::Secp256k1, + ) -> Result< + ( + secp256k1_zkp::ecdsa::Signature, + secp256k1_zkp::ecdsa::Signature, + ), + (), + > { + self.in_memory_signer + .lock() + .unwrap() + .sign_channel_announcement(msg, secp_ctx) + } + + fn sign_holder_anchor_input( + &self, + anchor_tx: &Transaction, + input: usize, + secp_ctx: &Secp256k1, + ) -> Result { + self.in_memory_signer + .lock() + .unwrap() + .sign_holder_anchor_input(anchor_tx, input, secp_ctx) + } + + fn provide_channel_parameters( + &mut self, + channel_parameters: &lightning::ln::chan_utils::ChannelTransactionParameters, + ) { + self.in_memory_signer + .lock() + .unwrap() + .provide_channel_parameters(channel_parameters); + } +} + +impl ExtraSign for CustomSigner { + fn sign_with_fund_key_callback(&self, cb: &mut F) + where + F: FnMut(&secp256k1_zkp::SecretKey), + { + self.in_memory_signer + .lock() + .unwrap() + .sign_with_fund_key_callback(cb) + } + + fn set_channel_value_satoshis(&mut self, value: u64) { + self.in_memory_signer + .lock() + .unwrap() + .set_channel_value_satoshis(value) + } +} + +impl Writeable for CustomSigner { + fn write(&self, writer: &mut W) -> Result<(), std::io::Error> { + self.in_memory_signer.lock().unwrap().write(writer) + } +} + +impl Sign for CustomSigner {} + +pub struct CustomKeysManager { + keys_manager: KeysManager, +} + +impl CustomKeysManager { + pub fn new(keys_manager: KeysManager) -> Self { + Self { keys_manager } + } +} + +impl CustomKeysManager { + #[allow(clippy::result_unit_err)] + pub fn spend_spendable_outputs( + &self, + descriptors: &[&SpendableOutputDescriptor], + outputs: Vec, + change_destination_script: Script, + feerate_sat_per_1000_weight: u32, + secp_ctx: &Secp256k1, + ) -> Result { + self.keys_manager.spend_spendable_outputs( + descriptors, + outputs, + change_destination_script, + feerate_sat_per_1000_weight, + secp_ctx, + ) + } +} + +impl KeysInterface for CustomKeysManager { + type Signer = CustomSigner; + + fn get_node_secret(&self, recipient: Recipient) -> Result { + self.keys_manager.get_node_secret(recipient) + } + + fn get_inbound_payment_key_material(&self) -> KeyMaterial { + self.keys_manager.get_inbound_payment_key_material() + } + + fn get_destination_script(&self) -> Script { + self.keys_manager.get_destination_script() + } + + fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { + self.keys_manager.get_shutdown_scriptpubkey() + } + + fn get_secure_random_bytes(&self) -> [u8; 32] { + self.keys_manager.get_secure_random_bytes() + } + + fn read_chan_signer(&self, reader: &[u8]) -> Result { + let in_memory = self.keys_manager.read_chan_signer(reader)?; + Ok(CustomSigner::new(in_memory)) + } + + fn sign_invoice( + &self, + hrp_bytes: &[u8], + invoice_data: &[bitcoin::bech32::u5], + recipient: Recipient, + ) -> Result { + self.keys_manager + .sign_invoice(hrp_bytes, invoice_data, recipient) + } + + fn ecdh( + &self, + recipient: Recipient, + other_key: &secp256k1_zkp::PublicKey, + tweak: Option<&secp256k1_zkp::Scalar>, + ) -> Result { + self.keys_manager.ecdh(recipient, other_key, tweak) + } + + fn generate_channel_keys_id( + &self, + inbound: bool, + channel_value_satoshis: u64, + user_channel_id: u128, + ) -> [u8; 32] { + self.keys_manager + .generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id) + } + + fn derive_channel_signer( + &self, + channel_value_satoshis: u64, + channel_keys_id: [u8; 32], + ) -> Self::Signer { + let inner = self + .keys_manager + .derive_channel_signer(channel_value_satoshis, channel_keys_id); + let pubkeys = inner.pubkeys(); + + CustomSigner { + channel_public_keys: pubkeys.clone(), + in_memory_signer: Arc::new(Mutex::new(inner)), + } + } +} diff --git a/crates/ln-dlc-node/src/lib.rs b/crates/ln-dlc-node/src/lib.rs index a9e8a4626..04a463126 100644 --- a/crates/ln-dlc-node/src/lib.rs +++ b/crates/ln-dlc-node/src/lib.rs @@ -1,6 +1,6 @@ use crate::ln::TracingLogger; use bitcoin::secp256k1::PublicKey; -use dlc_manager::custom_signer::CustomSigner; +use dlc_custom_signer::CustomSigner; use dlc_messages::message_handler::MessageHandler as DlcMessageHandler; use lightning::chain; use lightning::chain::chainmonitor; @@ -26,17 +26,20 @@ use std::sync::Arc; use std::sync::Mutex; mod disk; +mod dlc_custom_signer; mod ln; mod ln_dlc_wallet; mod on_chain_wallet; -pub mod seed; mod util; pub mod node; +pub mod seed; #[cfg(test)] mod tests; +pub use node::dlc_channel::Dlc; + type ConfirmableMonitor = ( ChannelMonitor, Arc, diff --git a/crates/ln-dlc-node/src/ln/event_handler.rs b/crates/ln-dlc-node/src/ln/event_handler.rs index b0b2a3f12..53f7a6e89 100644 --- a/crates/ln-dlc-node/src/ln/event_handler.rs +++ b/crates/ln-dlc-node/src/ln/event_handler.rs @@ -1,3 +1,4 @@ +use crate::dlc_custom_signer::CustomKeysManager; use crate::ln::coordinator_config; use crate::ln::JUST_IN_TIME_CHANNEL_OUTBOUND_LIQUIDITY_SAT; use crate::ln_dlc_wallet::LnDlcWallet; @@ -13,7 +14,6 @@ use crate::PendingInterceptedHtlcs; use anyhow::anyhow; use bitcoin::secp256k1::Secp256k1; use bitcoin_bech32::WitnessProgram; -use dlc_manager::custom_signer::CustomKeysManager; use lightning::chain::chaininterface::BroadcasterInterface; use lightning::chain::chaininterface::ConfirmationTarget; use lightning::chain::chaininterface::FeeEstimator; diff --git a/crates/ln-dlc-node/src/node/channel_manager.rs b/crates/ln-dlc-node/src/node/channel_manager.rs index 9538f289d..0c0eb6173 100644 --- a/crates/ln-dlc-node/src/node/channel_manager.rs +++ b/crates/ln-dlc-node/src/node/channel_manager.rs @@ -1,10 +1,10 @@ +use crate::dlc_custom_signer::CustomKeysManager; use crate::ln::TracingLogger; use crate::ln_dlc_wallet::LnDlcWallet; use crate::ChainMonitor; use crate::ConfirmableMonitor; use anyhow::Result; use bitcoin::BlockHash; -use dlc_manager::custom_signer::CustomKeysManager; use lightning::chain::BestBlock; use lightning::chain::ChannelMonitorUpdateStatus; use lightning::chain::Confirm; diff --git a/crates/ln-dlc-node/src/node/dlc_channel.rs b/crates/ln-dlc-node/src/node/dlc_channel.rs index 5f92561be..ff1d14401 100644 --- a/crates/ln-dlc-node/src/node/dlc_channel.rs +++ b/crates/ln-dlc-node/src/node/dlc_channel.rs @@ -19,13 +19,20 @@ use dlc_manager::payout_curve::RoundingInterval; use dlc_manager::payout_curve::RoundingIntervals; use dlc_manager::Oracle; use dlc_manager::Storage; -use dlc_messages::sub_channel::SubChannelMessage; +use dlc_messages::Message; +use dlc_messages::SubChannelMessage; use lightning::ln::channelmanager::ChannelDetails; use std::time::SystemTime; use std::time::UNIX_EPOCH; use trade::cfd::calculate_margin; use trade::TradeParams; +pub struct Dlc { + pub id: [u8; 32], + pub offer_collateral: u64, + pub accept_collateral: u64, +} + impl Node { // TODO: This API doesn't belong in this crate! pub fn trade(&self, trade_params: TradeParams) -> Result { @@ -64,7 +71,6 @@ impl Node { let contract_input = ContractInput { offer_collateral: margin_trader, accept_collateral: margin_coordinator, - maturity_time: maturity_time as u32, fee_rate: 2, contract_infos: vec![ContractInputInfo { contract_descriptor: dummy_contract_descriptor(total_collateral), @@ -104,9 +110,9 @@ impl Node { ) .unwrap(); - self.dlc_message_handler.send_subchannel_message( + self.dlc_message_handler.send_message( channel_details.counterparty.node_id, - SubChannelMessage::Request(sub_channel_offer), + Message::SubChannel(SubChannelMessage::Offer(sub_channel_offer)), ); Ok(()) @@ -122,17 +128,37 @@ impl Node { .accept_sub_channel(channel_id) .map_err(|e| anyhow!(e.to_string()))?; - self.dlc_message_handler - .send_subchannel_message(node_id, SubChannelMessage::Accept(accept_sub_channel)); + self.dlc_message_handler.send_message( + node_id, + Message::SubChannel(SubChannelMessage::Accept(accept_sub_channel)), + ); Ok(()) } - pub fn get_dlcs(&self) -> Result> { - self.dlc_manager + pub fn get_confirmed_dlcs(&self) -> Result> { + let confimed_dlcs = self + .dlc_manager .get_store() .get_contracts() - .map_err(|e| anyhow!("Unable to get contracts from manager: {e:#}")) + .map_err(|e| anyhow!("Unable to get contracts from manager: {e:#}"))? + .iter() + .filter_map(|contract| match contract { + Contract::Confirmed(signed) => Some((contract.get_id(), signed)), + _ => None, + }) + .map(|(id, signed)| Dlc { + id, + offer_collateral: signed + .accepted_contract + .offered_contract + .offer_params + .collateral, + accept_collateral: signed.accepted_contract.accept_params.collateral, + }) + .collect(); + + Ok(confimed_dlcs) } pub fn process_incoming_messages(&self) -> Result<()> { @@ -153,37 +179,37 @@ impl Node { let messages = dlc_message_handler.get_and_clear_received_messages(); for (node_id, msg) in messages { - tracing::debug!(from = %node_id, "Processing DLC-manager message"); - let resp = dlc_manager - .on_dlc_message(&msg, node_id) - .map_err(|e| anyhow!(e.to_string()))?; + match msg { + Message::OnChain(_) | Message::Channel(_) => { + tracing::debug!(from = %node_id, "Processing DLC-manager message"); + let resp = dlc_manager + .on_dlc_message(&msg, node_id) + .map_err(|e| anyhow!(e.to_string()))?; - if let Some(msg) = resp { - tracing::debug!(to = %node_id, "Sending DLC-manager message"); - dlc_message_handler.send_message(node_id, msg); - } - } - - let sub_channel_messages = - dlc_message_handler.get_and_clear_received_sub_channel_messages(); + if let Some(msg) = resp { + tracing::debug!(to = %node_id, "Sending DLC-manager message"); + dlc_message_handler.send_message(node_id, msg); + } + } + Message::SubChannel(msg) => { + tracing::debug!( + from = %node_id, + msg = %sub_channel_message_as_str(&msg), + "Processing sub-channel message" + ); + let resp = sub_channel_manager + .on_sub_channel_message(&msg, &node_id) + .map_err(|e| anyhow!(e.to_string()))?; - for (node_id, msg) in sub_channel_messages { - tracing::debug!( - from = %node_id, - msg = %sub_channel_message_as_str(&msg), - "Processing sub-channel message" - ); - let resp = sub_channel_manager - .on_sub_channel_message(&msg, &node_id) - .map_err(|e| anyhow!(e.to_string()))?; - - if let Some(msg) = resp { - tracing::debug!( - to = %node_id, - msg = %sub_channel_message_as_str(&msg), - "Sending sub-channel message" - ); - dlc_message_handler.send_subchannel_message(node_id, msg); + if let Some(msg) = resp { + tracing::debug!( + to = %node_id, + msg = %sub_channel_message_as_str(&msg), + "Sending sub-channel message" + ); + dlc_message_handler.send_message(node_id, Message::SubChannel(msg)); + } + } } } @@ -200,55 +226,54 @@ impl Node { // TODO: To be deleted once we configure a proper payout curve pub(crate) fn dummy_contract_descriptor(total_collateral: u64) -> ContractDescriptor { ContractDescriptor::Numerical(NumericalDescriptor { - payout_function: PayoutFunction { - payout_function_pieces: vec![ - PayoutFunctionPiece::PolynomialPayoutCurvePiece( - PolynomialPayoutCurvePiece::new(vec![ - PayoutPoint { - event_outcome: 0, - outcome_payout: 0, - extra_precision: 0, - }, - PayoutPoint { - event_outcome: 50_000, - outcome_payout: 0, - extra_precision: 0, - }, - ]) - .unwrap(), - ), - PayoutFunctionPiece::PolynomialPayoutCurvePiece( - PolynomialPayoutCurvePiece::new(vec![ - PayoutPoint { - event_outcome: 50_000, - outcome_payout: 0, - extra_precision: 0, - }, - PayoutPoint { - event_outcome: 60_000, - outcome_payout: total_collateral, - extra_precision: 0, - }, - ]) - .unwrap(), - ), - PayoutFunctionPiece::PolynomialPayoutCurvePiece( - PolynomialPayoutCurvePiece::new(vec![ - PayoutPoint { - event_outcome: 60_000, - outcome_payout: total_collateral, - extra_precision: 0, - }, - PayoutPoint { - event_outcome: 1048575, - outcome_payout: total_collateral, - extra_precision: 0, - }, - ]) - .unwrap(), - ), - ], - }, + payout_function: PayoutFunction::new(vec![ + PayoutFunctionPiece::PolynomialPayoutCurvePiece( + PolynomialPayoutCurvePiece::new(vec![ + PayoutPoint { + event_outcome: 0, + outcome_payout: 0, + extra_precision: 0, + }, + PayoutPoint { + event_outcome: 50_000, + outcome_payout: 0, + extra_precision: 0, + }, + ]) + .unwrap(), + ), + PayoutFunctionPiece::PolynomialPayoutCurvePiece( + PolynomialPayoutCurvePiece::new(vec![ + PayoutPoint { + event_outcome: 50_000, + outcome_payout: 0, + extra_precision: 0, + }, + PayoutPoint { + event_outcome: 60_000, + outcome_payout: total_collateral, + extra_precision: 0, + }, + ]) + .unwrap(), + ), + PayoutFunctionPiece::PolynomialPayoutCurvePiece( + PolynomialPayoutCurvePiece::new(vec![ + PayoutPoint { + event_outcome: 60_000, + outcome_payout: total_collateral, + extra_precision: 0, + }, + PayoutPoint { + event_outcome: 1048575, + outcome_payout: total_collateral, + extra_precision: 0, + }, + ]) + .unwrap(), + ), + ]) + .unwrap(), rounding_intervals: RoundingIntervals { intervals: vec![RoundingInterval { begin_interval: 0, @@ -267,7 +292,7 @@ fn sub_channel_message_as_str(msg: &SubChannelMessage) -> &str { use SubChannelMessage::*; match msg { - Request(_) => "Request", + Offer(_) => "Offer", Accept(_) => "Accept", Confirm(_) => "Confirm", Finalize(_) => "Finalize", diff --git a/crates/ln-dlc-node/src/node/mod.rs b/crates/ln-dlc-node/src/node/mod.rs index c3779727b..c4be28f24 100644 --- a/crates/ln-dlc-node/src/node/mod.rs +++ b/crates/ln-dlc-node/src/node/mod.rs @@ -1,4 +1,5 @@ use crate::disk; +use crate::dlc_custom_signer::CustomKeysManager; use crate::ln::app_config; use crate::ln::coordinator_config; use crate::ln::EventHandler; @@ -14,8 +15,7 @@ use crate::FakeChannelPaymentRequests; use crate::InvoicePayer; use crate::PaymentInfoStorage; use crate::PeerManager; -use ::dlc_manager::custom_signer::CustomKeysManager; -use ::dlc_manager::sub_channel_manager::SubChannelState; +use ::dlc_manager::subchannel::SubChannelState; use ::dlc_manager::Storage; use anyhow::anyhow; use anyhow::ensure; @@ -26,7 +26,7 @@ use bitcoin::blockdata::constants::genesis_block; use bitcoin::secp256k1::PublicKey; use bitcoin::Network; use dlc_messages::message_handler::MessageHandler as DlcMessageHandler; -use dlc_messages::sub_channel::SubChannelMessage; +use dlc_messages::SubChannelMessage; use dlc_sled_storage_provider::SledStorageProvider; use lightning::chain; use lightning::chain::chainmonitor; @@ -57,7 +57,7 @@ use std::time::SystemTime; mod channel_manager; mod connection; -mod dlc_channel; +pub(crate) mod dlc_channel; mod dlc_manager; pub(crate) mod invoice; mod ln_channel; @@ -76,7 +76,7 @@ const BROADCAST_NODE_ANNOUNCEMENT_INTERVAL: Duration = Duration::from_secs(60); pub struct Node { network: Network, - pub wallet: Arc, + pub(crate) wallet: Arc, pub(crate) peer_manager: Arc, invoice_payer: Arc>, pub(crate) channel_manager: Arc, @@ -328,12 +328,10 @@ impl Node { )?; let dlc_manager = Arc::new(dlc_manager); - let sub_channel_manager = sub_channel_manager::build( - dlc_manager.clone(), - ln_dlc_wallet.clone(), - channel_manager.clone(), - storage.clone(), - )?; + let sub_channel_manager = { + let (height, _) = ln_dlc_wallet.tip()?; + sub_channel_manager::build(channel_manager.clone(), dlc_manager.clone(), height as u64)? + }; // Connection manager tokio::spawn({ @@ -460,7 +458,7 @@ impl Node { tracing::info!("Found sub_channels: {}", sub_channels.len()); let sub_channel = match sub_channels.iter().find(|sub_channel| { - dbg!(sub_channel.counter_party) == dbg!(*pubkey) + sub_channel.counter_party == *pubkey && matches!(&sub_channel.state, SubChannelState::Offered(_)) }) { None => { @@ -490,9 +488,11 @@ impl Node { } }; - dlc_message_handler.send_subchannel_message( + dlc_message_handler.send_message( node_id, - SubChannelMessage::Accept(accept_sub_channel), + dlc_messages::Message::SubChannel(SubChannelMessage::Accept( + accept_sub_channel, + )), ); } diff --git a/crates/ln-dlc-node/src/node/sub_channel_manager.rs b/crates/ln-dlc-node/src/node/sub_channel_manager.rs index 20b6e0f99..45d2c5651 100644 --- a/crates/ln-dlc-node/src/node/sub_channel_manager.rs +++ b/crates/ln-dlc-node/src/node/sub_channel_manager.rs @@ -2,13 +2,13 @@ use crate::ln_dlc_wallet::LnDlcWallet; use crate::node::channel_manager::ChannelManager; use crate::node::dlc_manager::DlcManager; use anyhow::Result; -use bitcoin::secp256k1::Secp256k1; +use dlc_manager::sub_channel_manager; use dlc_manager::SystemTimeProvider; use dlc_sled_storage_provider::SledStorageProvider; use p2pd_oracle_client::P2PDOracleClient; use std::sync::Arc; -pub(crate) type SubChannelManager = dlc_manager::sub_channel_manager::SubChannelManager< +pub(crate) type SubChannelManager = sub_channel_manager::SubChannelManager< Arc, Arc, Arc, @@ -20,20 +20,13 @@ pub(crate) type SubChannelManager = dlc_manager::sub_channel_manager::SubChannel >; pub(crate) fn build( - dlc_manager: Arc, - ln_dlc_wallet: Arc, channel_manager: Arc, - storage: Arc, + dlc_manager: Arc, + height: u64, ) -> Result> { - let (height, _) = ln_dlc_wallet.tip()?; Ok(Arc::new(SubChannelManager::new( - Secp256k1::new(), - ln_dlc_wallet.clone(), channel_manager.clone(), - storage, - ln_dlc_wallet.clone(), dlc_manager, - ln_dlc_wallet, - height as u64, + height, ))) } diff --git a/crates/ln-dlc-node/src/node/wallet.rs b/crates/ln-dlc-node/src/node/wallet.rs index 181021877..eb7859e66 100644 --- a/crates/ln-dlc-node/src/node/wallet.rs +++ b/crates/ln-dlc-node/src/node/wallet.rs @@ -1,6 +1,8 @@ use crate::node::Node; use anyhow::anyhow; use anyhow::Result; +use bdk::wallet::AddressIndex; +use bitcoin::Address; use lightning::chain::Confirm; #[derive(Debug, Clone)] @@ -19,6 +21,17 @@ impl Node { self.wallet.inner().sync(confirmables).unwrap(); } + pub fn get_new_address(&self) -> Result
{ + let address = self + .wallet + .inner() + .get_wallet() + .unwrap() + .get_address(AddressIndex::New)?; + + Ok(address.address) + } + pub fn get_on_chain_balance(&self) -> Result { self.wallet.inner().get_balance().map_err(|e| anyhow!(e)) } diff --git a/crates/ln-dlc-node/src/tests/add_dlc.rs b/crates/ln-dlc-node/src/tests/add_dlc.rs index a8e7cc2ab..6081835a7 100644 --- a/crates/ln-dlc-node/src/tests/add_dlc.rs +++ b/crates/ln-dlc-node/src/tests/add_dlc.rs @@ -4,7 +4,7 @@ use crate::tests::init_tracing; use crate::tests::wait_until; use anyhow::anyhow; use bitcoin::Amount; -use dlc_manager::sub_channel_manager::SubChannelState; +use dlc_manager::subchannel::SubChannelState; use dlc_manager::Storage; use std::time::Duration; diff --git a/crates/ln-dlc-node/src/tests/mod.rs b/crates/ln-dlc-node/src/tests/mod.rs index b4d38686b..a3f1808ac 100644 --- a/crates/ln-dlc-node/src/tests/mod.rs +++ b/crates/ln-dlc-node/src/tests/mod.rs @@ -34,8 +34,6 @@ use std::net::TcpListener; use std::path::PathBuf; use std::sync::Once; use std::time::Duration; -use std::time::SystemTime; -use std::time::UNIX_EPOCH; mod add_dlc; mod bitcoind; @@ -303,68 +301,60 @@ fn dummy_contract_input( ) -> ContractInput { let total_collateral = offer_collateral + accept_collateral; - let maturity_time = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs() - + 86_400; // in a day's time - ContractInput { offer_collateral, accept_collateral, - maturity_time: maturity_time as u32, fee_rate: 2, contract_infos: vec![ContractInputInfo { contract_descriptor: ContractDescriptor::Numerical(NumericalDescriptor { - payout_function: PayoutFunction { - payout_function_pieces: vec![ - PayoutFunctionPiece::PolynomialPayoutCurvePiece( - PolynomialPayoutCurvePiece::new(vec![ - PayoutPoint { - event_outcome: 0, - outcome_payout: 0, - extra_precision: 0, - }, - PayoutPoint { - event_outcome: 50_000, - outcome_payout: 0, - extra_precision: 0, - }, - ]) - .unwrap(), - ), - PayoutFunctionPiece::PolynomialPayoutCurvePiece( - PolynomialPayoutCurvePiece::new(vec![ - PayoutPoint { - event_outcome: 50_000, - outcome_payout: 0, - extra_precision: 0, - }, - PayoutPoint { - event_outcome: 60_000, - outcome_payout: total_collateral, - extra_precision: 0, - }, - ]) - .unwrap(), - ), - PayoutFunctionPiece::PolynomialPayoutCurvePiece( - PolynomialPayoutCurvePiece::new(vec![ - PayoutPoint { - event_outcome: 60_000, - outcome_payout: total_collateral, - extra_precision: 0, - }, - PayoutPoint { - event_outcome: 1048575, - outcome_payout: total_collateral, - extra_precision: 0, - }, - ]) - .unwrap(), - ), - ], - }, + payout_function: PayoutFunction::new(vec![ + PayoutFunctionPiece::PolynomialPayoutCurvePiece( + PolynomialPayoutCurvePiece::new(vec![ + PayoutPoint { + event_outcome: 0, + outcome_payout: 0, + extra_precision: 0, + }, + PayoutPoint { + event_outcome: 50_000, + outcome_payout: 0, + extra_precision: 0, + }, + ]) + .unwrap(), + ), + PayoutFunctionPiece::PolynomialPayoutCurvePiece( + PolynomialPayoutCurvePiece::new(vec![ + PayoutPoint { + event_outcome: 50_000, + outcome_payout: 0, + extra_precision: 0, + }, + PayoutPoint { + event_outcome: 60_000, + outcome_payout: total_collateral, + extra_precision: 0, + }, + ]) + .unwrap(), + ), + PayoutFunctionPiece::PolynomialPayoutCurvePiece( + PolynomialPayoutCurvePiece::new(vec![ + PayoutPoint { + event_outcome: 60_000, + outcome_payout: total_collateral, + extra_precision: 0, + }, + PayoutPoint { + event_outcome: 1048575, + outcome_payout: total_collateral, + extra_precision: 0, + }, + ]) + .unwrap(), + ), + ]) + .unwrap(), rounding_intervals: RoundingIntervals { intervals: vec![RoundingInterval { begin_interval: 0, diff --git a/maker/Cargo.toml b/maker/Cargo.toml index 556f5ba1c..f498b363f 100644 --- a/maker/Cargo.toml +++ b/maker/Cargo.toml @@ -12,7 +12,6 @@ bitcoin = "0.29" clap = { version = "4", features = ["derive"] } diesel = { version = "2.0.0", features = ["r2d2", "postgres"] } diesel_migrations = "2.0.0" -dlc-manager = { version = "0.3.0", features = ["use-serde"] } futures = "0.3" hex = "0.4.3" ln-dlc-node = { path = "../crates/ln-dlc-node" } diff --git a/maker/src/routes.rs b/maker/src/routes.rs index 1424e3d25..e03d2883c 100644 --- a/maker/src/routes.rs +++ b/maker/src/routes.rs @@ -13,7 +13,6 @@ use diesel::r2d2; use diesel::r2d2::ConnectionManager; use diesel::r2d2::Pool; use diesel::PgConnection; -use dlc_manager::Wallet; use ln_dlc_node::node::Node; use ln_dlc_node::node::NodeInfo; use serde::Deserialize; @@ -54,10 +53,10 @@ pub struct Invoice { } pub async fn index(State(app_state): State>) -> Result, AppError> { - let address = - app_state.node.wallet.get_new_address().map_err(|e| { - AppError::InternalServerError(format!("Failed to get new address: {e:#}")) - })?; + let address = app_state + .node + .get_new_address() + .map_err(|e| AppError::InternalServerError(format!("Failed to get new address: {e:#}")))?; let offchain = app_state.node.get_ldk_balance(); let onchain = app_state @@ -87,10 +86,10 @@ pub async fn index(State(app_state): State>) -> Result pub async fn get_new_address( State(app_state): State>, ) -> Result, AppError> { - let address = - app_state.node.wallet.get_new_address().map_err(|e| { - AppError::InternalServerError(format!("Failed to get new address: {e:#}")) - })?; + let address = app_state + .node + .get_new_address() + .map_err(|e| AppError::InternalServerError(format!("Failed to get new address: {e:#}")))?; Ok(Json(address.to_string())) } diff --git a/mobile/native/Cargo.toml b/mobile/native/Cargo.toml index 361d5b783..278f27288 100644 --- a/mobile/native/Cargo.toml +++ b/mobile/native/Cargo.toml @@ -11,7 +11,6 @@ anyhow = "1" bdk = { version = "0.24.0", features = ["key-value-db"] } diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] } diesel_migrations = "2.0.0" -dlc-manager = { version = "0.3.0", features = ["use-serde"] } # required for the dlc_manager::Wallet trait flutter_rust_bridge = "1.63.1" lightning-invoice = { version = "0.21" } ln-dlc-node = { path = "../../crates/ln-dlc-node" } diff --git a/mobile/native/src/ln_dlc/mod.rs b/mobile/native/src/ln_dlc/mod.rs index 5569e463b..fd4fe6dfe 100644 --- a/mobile/native/src/ln_dlc/mod.rs +++ b/mobile/native/src/ln_dlc/mod.rs @@ -15,12 +15,11 @@ use bdk::bitcoin::secp256k1::rand::thread_rng; use bdk::bitcoin::secp256k1::rand::RngCore; use bdk::bitcoin::Network; use bdk::bitcoin::XOnlyPublicKey; -use dlc_manager::contract::Contract; -use dlc_manager::Wallet; use lightning_invoice::Invoice; use ln_dlc_node::node::Node; use ln_dlc_node::node::NodeInfo; use ln_dlc_node::seed::Bip39Seed; +use ln_dlc_node::Dlc; use state::Storage; use std::net::TcpListener; use std::path::Path; @@ -134,7 +133,7 @@ pub fn run(data_dir: String) -> Result<()> { // periodically update for positions runtime.spawn(async move { loop { - let contracts = match node_cloned.get_dlcs() { + let contracts = match node_cloned.get_confirmed_dlcs() { Ok(contracts) => contracts, Err(e) => { tracing::error!("Failed to retrieve DLCs from node: {e:#}"); @@ -144,7 +143,10 @@ pub fn run(data_dir: String) -> Result<()> { }; // Assumes that there is only one contract, i.e. one position - if let Some(Contract::Confirmed(contract)) = contracts.get(0) { + if let Some(Dlc { + offer_collateral, .. + }) = contracts.get(0) + { // TODO: Load position data from database and fill in the values; the collateral // can be taken from the DLC event::publish(&EventInternal::PositionUpdateNotification(PositionTrade { @@ -156,11 +158,7 @@ pub fn run(data_dir: String) -> Result<()> { liquidation_price: 0.0, unrealized_pnl: 0, position_state: PositionStateTrade::Open, - collateral: contract - .accepted_contract - .offered_contract - .offer_params - .collateral, + collateral: *offer_collateral, })); } @@ -177,7 +175,6 @@ pub fn run(data_dir: String) -> Result<()> { pub fn get_new_address() -> Result { let node = NODE.try_get().context("failed to get ln dlc node")?; let address = node - .wallet .get_new_address() .map_err(|e| anyhow!("Failed to get new address: {e}"))?; Ok(address.to_string())