From b89ce30168dd30a56c7dc04952e5a134e24c76b4 Mon Sep 17 00:00:00 2001 From: Harsh Mahajan <007harshmahajan@gmail.com> Date: Tue, 24 Sep 2024 17:47:44 +0530 Subject: [PATCH] initial stuff --- rust/Cargo.lock | 218 +++++++++++++++++++++++++++++++++++------ rust/Cargo.toml | 2 +- rust/src/api/wallet.rs | 40 ++++++++ 3 files changed, 227 insertions(+), 33 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index a04ff47..922d133 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -164,6 +164,12 @@ dependencies = [ "backtrace", ] +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "atomic" version = "0.5.3" @@ -208,6 +214,16 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "base58ck" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c8d66485a3a2ea485c1913c4572ce0256067a5377ac8c75c4960e1cda98605f" +dependencies = [ + "bitcoin-internals 0.3.0", + "bitcoin_hashes 0.14.0", +] + [[package]] name = "base64" version = "0.13.1" @@ -232,6 +248,12 @@ version = "0.10.0-beta" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" +[[package]] +name = "bech32" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d965446196e3b7decd44aa7ee49e31d630118f90ef12f97900f262eb915c951d" + [[package]] name = "bip39" version = "2.0.0" @@ -252,12 +274,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c85783c2fe40083ea54a33aa2f0ba58831d90fcd190f5bdc47e74e84d2a96ae" dependencies = [ "base64 0.21.7", - "bech32", - "bitcoin-internals", + "bech32 0.10.0-beta", + "bitcoin-internals 0.2.0", "bitcoin_hashes 0.13.0", - "hex-conservative", + "hex-conservative 0.1.2", "hex_lit", - "secp256k1", + "secp256k1 0.28.2", + "serde", +] + +[[package]] +name = "bitcoin" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea507acc1cd80fc084ace38544bbcf7ced7c2aa65b653b102de0ce718df668f6" +dependencies = [ + "base58ck", + "bech32 0.11.0", + "bitcoin-internals 0.3.0", + "bitcoin-io", + "bitcoin-units", + "bitcoin_hashes 0.14.0", + "hex-conservative 0.2.1", + "hex_lit", + "secp256k1 0.29.1", "serde", ] @@ -270,12 +310,37 @@ dependencies = [ "serde", ] +[[package]] +name = "bitcoin-internals" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30bdbe14aa07b06e6cfeffc529a1f099e5fbe249524f8125358604df99a4bed2" +dependencies = [ + "serde", +] + +[[package]] +name = "bitcoin-io" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "340e09e8399c7bd8912f495af6aa58bea0c9214773417ffaa8f6460f93aaee56" + [[package]] name = "bitcoin-private" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57" +[[package]] +name = "bitcoin-units" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5285c8bcaa25876d07f37e3d30c303f2609179716e11d688f51e8f1fe70063e2" +dependencies = [ + "bitcoin-internals 0.3.0", + "serde", +] + [[package]] name = "bitcoin_hashes" version = "0.11.0" @@ -288,11 +353,46 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" dependencies = [ - "bitcoin-internals", - "hex-conservative", + "bitcoin-internals 0.2.0", + "hex-conservative 0.1.2", + "serde", +] + +[[package]] +name = "bitcoin_hashes" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16" +dependencies = [ + "bitcoin-io", + "hex-conservative 0.2.1", "serde", ] +[[package]] +name = "bitcoincore-rpc" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedd23ae0fd321affb4bbbc36126c6f49a32818dc6b979395d24da8c9d4e80ee" +dependencies = [ + "bitcoincore-rpc-json", + "jsonrpc", + "log", + "serde", + "serde_json", +] + +[[package]] +name = "bitcoincore-rpc-json" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8909583c5fab98508e80ef73e5592a651c954993dc6b7739963257d19f0e71a" +dependencies = [ + "bitcoin 0.32.2", + "serde", + "serde_json", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -461,9 +561,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.17" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3" dependencies = [ "clap_builder", "clap_derive", @@ -471,9 +571,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.17" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" dependencies = [ "anstream", "anstyle", @@ -483,9 +583,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -716,7 +816,7 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89008f106be6f303695522f2f4c1f28b40c3e8367ed8b3bb227f1f882cb52cc2" dependencies = [ - "bitcoin", + "bitcoin 0.31.2", "byteorder", "libc", "log", @@ -733,7 +833,7 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd6b8388053196e6b2702a45418078a654680ce9e1fd91799f51f67a40118ff5" dependencies = [ - "bitcoin", + "bitcoin 0.31.2", "secp256k1-zkp", "serde", "serde_json", @@ -745,7 +845,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd2c9c3a0acd5a30061dc5f1859b816ef01d320da66d074d9e23aad40e7a7eba" dependencies = [ - "bitcoin", + "bitcoin 0.31.2", "elements", "miniscript", "serde", @@ -1175,6 +1275,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212ab92002354b4819390025006c897e8140934349e8635c9b077f47b4dcbd20" +[[package]] +name = "hex-conservative" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd" +dependencies = [ + "arrayvec", +] + [[package]] name = "hex_lit" version = "0.1.1" @@ -1475,6 +1584,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "jsonrpc" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3662a38d341d77efecb73caf01420cfa5aa63c0253fd7bc05289ef9f6616e1bf" +dependencies = [ + "base64 0.13.1", + "minreq", + "serde", + "serde_json", +] + [[package]] name = "kqueue" version = "1.0.8" @@ -1567,7 +1688,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1771c1d311107f95f0ac02adcd86e981a43c19e2b1f7062dfd0253c292eb8039" dependencies = [ - "bitcoin", + "bitcoin 0.31.2", "rand", "tempfile", "testcontainers", @@ -1629,12 +1750,12 @@ dependencies = [ [[package]] name = "lwk_wollet" version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa84a9154d9b21c3f13911e1623924199cb007af43b171acacf2a5ebc79005d6" +source = "git+https://github.com/Blockstream/lwk?branch=master#a38e6b85db5823df8d872b653c9b212af9ad5232" dependencies = [ "aes-gcm-siv", "base64 0.21.7", "bip39", + "bitcoincore-rpc", "electrum-client", "elements", "elements-miniscript", @@ -1649,6 +1770,7 @@ dependencies = [ "serde_json", "thiserror", "tracing", + "url", ] [[package]] @@ -1669,9 +1791,9 @@ version = "11.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3127e10529a57a8f7fa9b1332c4c2f72baadaca6777798f910dff3c922620b14" dependencies = [ - "bech32", - "bitcoin", - "bitcoin-internals", + "bech32 0.10.0-beta", + "bitcoin 0.31.2", + "bitcoin-internals 0.2.0", ] [[package]] @@ -1683,6 +1805,17 @@ dependencies = [ "adler2", ] +[[package]] +name = "minreq" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763d142cdff44aaadd9268bebddb156ef6c65a0e13486bb81673cf2d8739f9b0" +dependencies = [ + "log", + "serde", + "serde_json", +] + [[package]] name = "mio" version = "0.8.11" @@ -1906,9 +2039,9 @@ dependencies = [ [[package]] name = "portable-atomic" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" +checksum = "d30538d42559de6b034bc76fd6dd4c38961b1ee5c6c56e3808c50128fdbc22ce" [[package]] name = "ppv-lite86" @@ -2271,7 +2404,19 @@ checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" dependencies = [ "bitcoin_hashes 0.13.0", "rand", - "secp256k1-sys", + "secp256k1-sys 0.9.2", + "serde", +] + +[[package]] +name = "secp256k1" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" +dependencies = [ + "bitcoin_hashes 0.13.0", + "rand", + "secp256k1-sys 0.10.1", "serde", ] @@ -2284,6 +2429,15 @@ dependencies = [ "cc", ] +[[package]] +name = "secp256k1-sys" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" +dependencies = [ + "cc", +] + [[package]] name = "secp256k1-zkp" version = "0.10.1" @@ -2292,7 +2446,7 @@ checksum = "c4e48ef9c98bfbcb98bd15693ffa19676cb3e29426b75eda8b73c05cdd7959f8" dependencies = [ "bitcoin-private", "rand", - "secp256k1", + "secp256k1 0.28.2", "secp256k1-zkp-sys", "serde", ] @@ -2304,7 +2458,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ead52f43074bae2ddbd1e0e66e6b170135e76117f5ea9916f33d7bd0b36e29" dependencies = [ "cc", - "secp256k1-sys", + "secp256k1-sys 0.9.2", ] [[package]] @@ -2616,18 +2770,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", @@ -2839,9 +2993,9 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.13" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] name = "universal-hash" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c20045b..17f5b75 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -11,7 +11,7 @@ doctest = false crate-type = ["staticlib", "cdylib"] [dependencies] -lwk_wollet = { version = "0.7.0" } +lwk_wollet = { git = "https://github.com/Blockstream/lwk", branch = "master"} lwk_signer = { version = "0.7.0" } lwk_common = {version = "0.7.0" } thiserror = "1.0.50" diff --git a/rust/src/api/wallet.rs b/rust/src/api/wallet.rs index 7374f47..510a51b 100644 --- a/rust/src/api/wallet.rs +++ b/rust/src/api/wallet.rs @@ -9,6 +9,7 @@ use lwk_wollet::full_scan_with_electrum_client; // use lwk_wollet::elements_miniscript::descriptor; use lwk_wollet::AddressResult; use lwk_wollet::ElectrumClient; +use lwk_wollet::ExternalUtxo; use lwk_wollet::Update; use lwk_wollet::WolletDescriptor; @@ -57,6 +58,9 @@ impl Wallet { let wollet = Wollet::with_fs_persist(network.into(), descriptor, dbpath.clone())?; let opaque = RustOpaque::new(Mutex::new(wollet)); let wallet = Wallet { inner: opaque }; + let utxo = wallet.utxos()?; + let explicit_utxo = wallet.get_wallet()?.explicit_utxos().unwrap(); + let policy_asset = wallet.get_wallet()?.policy_asset(); Ok(wallet) } pub fn sync(&self, electrum_url: String) -> anyhow::Result<(), LwkError> { @@ -89,6 +93,7 @@ impl Wallet { pub fn balances(&self) -> anyhow::Result { let balance = Balances::from(AssetIdMapUInt(self.get_wallet()?.balance()?)); + // Received the change output Ok(balance) } @@ -146,6 +151,41 @@ impl Wallet { Ok(pset.to_string()) } + pub fn make_external(&mut self, utxo: &lwk_wollet::WalletTxOut) -> lwk_wollet::ExternalUtxo { + let wallet = self.get_wallet().unwrap(); + let tx = wallet.transaction(&utxo.outpoint.txid).unwrap().unwrap().tx; + let txout = tx.output.get(utxo.outpoint.vout as usize).unwrap().clone(); + lwk_wollet::ExternalUtxo { + outpoint: utxo.outpoint, + txout, + unblinded: utxo.unblinded, + max_weight_to_satisfy: 100, // TODO + } + } + + pub fn build_with_unblinded_tx( + &self, + sats: u64, + out_address: String, + fee_rate: f32, + asset: String, + external_utxo: ExternalUtxo, + ) -> anyhow::Result { + let wallet = self.get_wallet()?; + let tx_builder = wallet.tx_builder(); + let address = elements::Address::from_str(&out_address)?; + let asset = elements::AssetId::from_str(&asset)?; + let mut pset = wallet + .tx_builder() + .add_recipient(&address, sats, asset)? + .add_external_utxos(vec![external_utxo]) + .unwrap() + .fee_rate(Some(fee_rate)) + .finish() + .unwrap(); + Ok(pset.to_string()) + } + pub fn decode_tx(&self, pset: String) -> anyhow::Result { let mut pset = PartiallySignedTransaction::from_str(&pset)?; let pset_details = self.get_wallet()?.get_details(&mut pset)?;