Skip to content

Commit

Permalink
Get maker to import whole multisig descriptor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chris-belcher committed Jun 6, 2022
1 parent 66b2f63 commit 229ab43
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions src/maker_protocol.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
77 changes: 55 additions & 22 deletions src/wallet_sync.rs
Expand Up @@ -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
(
Expand All @@ -1762,14 +1749,28 @@ impl Wallet {
)
}

pub fn import_wallet_redeemscript(
pub fn import_wallet_contract_redeemscript(
&self,
rpc: &Client,
redeemscript: &Script,
) -> Result<(), bitcoincore_rpc::Error> {
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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 229ab43

Please sign in to comment.