From 229ab43886c13d3a5e12ea0614f3c67ebc95cac4 Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Mon, 6 Jun 2022 10:38:48 +0100 Subject: [PATCH] Get maker to import whole multisig descriptor Previously when the maker received a proof of funding it would import the multisig redeemscripts, but in order for those coins to be recognized by the is_utxo_ours_and_spendable() function the redeemscripts would have to be imported in descriptor form. --- src/lib.rs | 2 +- src/maker_protocol.rs | 8 +++-- src/wallet_sync.rs | 77 ++++++++++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 221169b4..21fe56d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -627,7 +627,7 @@ pub fn recover_from_incomplete_coinswap( .enumerate() { wallet - .import_wallet_redeemscript(&rpc, &swapcoin.1.get_contract_redeemscript()) + .import_wallet_contract_redeemscript(&rpc, &swapcoin.1.get_contract_redeemscript()) .unwrap(); let signed_contract_tx = swapcoin.1.get_fully_signed_contract_tx(); diff --git a/src/maker_protocol.rs b/src/maker_protocol.rs index 7f52f6d6..29ed0154 100644 --- a/src/maker_protocol.rs +++ b/src/maker_protocol.rs @@ -31,7 +31,8 @@ use crate::contracts; use crate::contracts::SwapCoin; use crate::contracts::{ calculate_coinswap_fee, find_funding_output, read_hashvalue_from_contract, - read_locktime_from_contract, MAKER_FUNDING_TX_VBYTE_SIZE, + read_locktime_from_contract, read_pubkeys_from_multisig_redeemscript, + MAKER_FUNDING_TX_VBYTE_SIZE, }; use crate::directory_servers::post_maker_address_to_directory_servers; use crate::error::Error; @@ -597,10 +598,13 @@ fn handle_proof_of_funding( funding_outputs.iter(), incoming_swapcoin_keys.iter() ) { + let (pubkey1, pubkey2) = + read_pubkeys_from_multisig_redeemscript(&funding_info.multisig_redeemscript) + .ok_or(Error::Protocol("invalid multisig redeemscript"))?; wallet .read() .unwrap() - .import_wallet_redeemscript(&rpc, &funding_info.multisig_redeemscript)?; + .import_wallet_multisig_redeemscript(&rpc, &pubkey1, &pubkey2)?; wallet.read().unwrap().import_tx_with_merkleproof( &rpc, &funding_info.funding_tx, diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index cdd8876b..91399421 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -1731,27 +1731,14 @@ impl Wallet { .unwrap() .descriptor; - let address_label = self.get_core_wallet_label(); - let result = rpc - .import_multi( - &[ImportMultiRequest { - timestamp: ImportMultiRescanSince::Now, - descriptor: Some(&descriptor), - watchonly: Some(true), - label: Some(&address_label), - ..Default::default() - }], - Some(&ImportMultiOptions { - rescan: Some(false), - }), - ) - .unwrap(); - for r in result { - if !r.success { - //TODO proper error handling - panic!("failed import"); - } - } + import_multisig_redeemscript_descriptor( + rpc, + &my_pubkey, + other_pubkey, + &self.get_core_wallet_label(), + ) + .unwrap(); + //redeemscript and descriptor show up in `getaddressinfo` only after // the address gets outputs on it ( @@ -1762,7 +1749,7 @@ impl Wallet { ) } - pub fn import_wallet_redeemscript( + pub fn import_wallet_contract_redeemscript( &self, rpc: &Client, redeemscript: &Script, @@ -1770,6 +1757,20 @@ impl Wallet { import_redeemscript(rpc, redeemscript, &self.get_core_wallet_label()) } + pub fn import_wallet_multisig_redeemscript( + &self, + rpc: &Client, + pubkey1: &PublicKey, + pubkey2: &PublicKey, + ) -> Result<(), Error> { + Ok(import_multisig_redeemscript_descriptor( + rpc, + &pubkey1, + &pubkey2, + &self.get_core_wallet_label(), + )?) + } + pub fn import_tx_with_merkleproof( &self, rpc: &Client, @@ -1885,6 +1886,38 @@ pub fn import_watchonly_redeemscript( import_redeemscript(rpc, redeemscript, &WATCH_ONLY_SWAPCOIN_LABEL.to_string()) } +fn import_multisig_redeemscript_descriptor( + rpc: &Client, + pubkey1: &PublicKey, + pubkey2: &PublicKey, + address_label: &String, +) -> Result<(), bitcoincore_rpc::Error> { + let descriptor = rpc + .get_descriptor_info(&format!("wsh(sortedmulti(2,{},{}))", pubkey1, pubkey2))? + .descriptor; + let result = rpc + .import_multi( + &[ImportMultiRequest { + timestamp: ImportMultiRescanSince::Now, + descriptor: Some(&descriptor), + watchonly: Some(true), + label: Some(&address_label), + ..Default::default() + }], + Some(&ImportMultiOptions { + rescan: Some(false), + }), + ) + .unwrap(); + for r in result { + if !r.success { + //TODO proper error handling + panic!("failed import"); + } + } + Ok(()) +} + pub fn import_redeemscript( rpc: &Client, redeemscript: &Script,