Skip to content

Conversation

@mariocynicys
Copy link
Collaborator

This PR adds support for basic P2PKH, P2WPKH, and single-input P2SH signing via WalletConnect.
These basic signing operations are the ones needed for swaps v1, which is implemented here as well for WalletConnect.

P2(W)PKH signing is used in taker fee/maker payment/taker payment
P2SH signing is used in taker payment spend/maker payment spend/taker payment refund/maker payment refund

swaps v2 will use the existing WalletConnect signing functions + some new methods for the co-operative signing step.

Advised to review commit by commit to rule out the bulky chore changes.

@mariocynicys
Copy link
Collaborator Author

There are two pending fixmes that i will resolve in this PR. The PR is still reviewable at its current state and won't change much.

@smk762
Copy link

smk762 commented Aug 4, 2025

Are any docs updates required for this PR?
For testing, should I simply attempt swaps with each of the script types as the only utxos in wallet?

@mariocynicys
Copy link
Collaborator Author

mariocynicys commented Aug 4, 2025

Are any docs updates required for this PR?

nope

For testing, should I simply attempt swaps with each of the script types as the only utxos in wallet?

p2sh support doesn't mean spending p2sh utxos that the wallet own, but rather spending p2sh utxos that are byproduct of the swap in intermediate steps.
you wanna test using p2(w)pkh coins like what any normal wallet has.
but currently p2pkh coins will break so please test only from a segwit wallet for now.
and don't trade zcash on the KDF from this PR as that will break too.

@mariocynicys
Copy link
Collaborator Author

this wallet should be used for testing.
a custom mnemonic could be set here.

…s in utxo_common

so kdf won't panic cuz we don't have walletconnect
we better remove these panics tho

note that not all these methods are needed for swap v1. but i did them
nontheless
@mariocynicys
Copy link
Collaborator Author

mariocynicys commented Aug 5, 2025

added a utility test function in my last commit that performs a basic swap (will figure out how to test for the refund case without having to wait eternity, but for now this tests the swap going the pretty way).

to run the test basically you wanna have two walletconnect capable wallets that support utxo (i.e. this boy) and have them perform trades with each other.

the test trades tbtc against tbtc, as the test wallet above doesn't support other utxo coins that could be traded. in the test we instantiate two coins tbtc-1 and tbtc-2 but they are essentially the same coin with the same blockchain and same electrums.

this is how to run the test: cargo test --test 'mm2_tests_main' test_walletconnect_swap -- --nocapture
the test will print out a pairing url for the first KDF instance. this is to be copied and supplied for the walletconnect wallet to establish the session. WC requests will follow regarding getting utxo addresses and signing psbts. they should be approved (unless you wanna wait an eternity to test the refund path).
same goes for the second KDF instance, a pairing url will be printed in the terminal that is to be used for session establishment.

i disabled watchers by default for now until i fix #2571 (will fix it inline in this PR to be fixed in another PR)
i disabled watchers for walletconnect instantiated coins.

cc/ @smk762
I already tested this manually btw. you can skip this if you want.

requires manual testing with walletconnect capable wallets by the test step by step
@mariocynicys mariocynicys force-pushed the btc-walletconnect-for-swaps branch from daaf758 to c3e6b08 Compare August 5, 2025 12:08
@shamardy
Copy link
Collaborator

shamardy commented Aug 5, 2025

will figure out how to test for the refund case without having to wait eternity

There is a custom time lock config option for that.

idk why we sent the watcher message one time then moved to the next step and created a handle to keep sending it periodically.
this commit makes it so we don't do the initial send but only create the handler to keep sending it periodically as before (this part didin't change, but the log message was put in the appropriate place)
this is to be handled in a different PR since it seems it might be complext.
we need to sign the watcher msg with the htlc privkey which means we will ask walletconnect for one more signature. this might get entangled with p2p crate code
via reverting back to use derive_htlc_key_pair using the unique swap data
regarding whether we should bake fork_id into the sighash_type or would the signer already do it for us
shamardy
shamardy previously approved these changes Sep 6, 2025
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Only one non-blocker nit!

pub fn derive_htlc_pubkey(coin: &UtxoCoinFields, swap_unique_data: &[u8]) -> [u8; 33] {
match coin.priv_key_policy {
PrivKeyPolicy::WalletConnect { public_key, .. } => public_key.0,
_ => derive_htlc_key_pair(coin, swap_unique_data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't like the wildcard pattern, please use an exhaustive list.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in derive_htlc_pubkey just like in derive_htlc_key_pair
@smk762
Copy link

smk762 commented Sep 8, 2025

cc/ @smk762 I already tested this manually btw. you can skip this if you want.

Are there any related docs updates needed?

@mariocynicys
Copy link
Collaborator Author

Are there any related docs updates needed?

api wise, there are no new methods or updated endpoints.

we can write some docs (more of user docs) about the fact that popups that will show up in the user's walletconnnect-capable wallet to ask for address/pubkey sharing, msg & transaction signing. if that makes sense.

onur-ozkan
onur-ozkan previously approved these changes Sep 8, 2025
Copy link
Collaborator

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks

it wasn't like that before, so leaving it as is to avoid unforeseen errors
@github-actions
Copy link

github-actions bot commented Sep 8, 2025

KDF WASM Playground Previews

d01b12e: https://8f2e07db.kdf-wasm-playground.pages.dev (Original WASM: 33M, Gzipped WASM: 11M)
58a72a6: https://d17bfba4.kdf-wasm-playground.pages.dev (Original WASM: 33M, Gzipped WASM: 11M)
31cd36f: https://fd0b43be.kdf-wasm-playground.pages.dev (Original WASM: 33M, Gzipped WASM: 11M)

@mariocynicys
Copy link
Collaborator Author

fyi, the wasm problem was induced by this commit (51905d4) that adds rust-bitcoin to wasm deps (because it's needed for psbt creation) which caused pulling secp256k1 v24 (while we already have secp256k1 v20 in use) and wasm doesn't like the two crates present at the same time.
the solutions im trying right now is bumping and/or downgrading some crates to get a single secp256k1 version.

mariocynicys added a commit that referenced this pull request Oct 1, 2025
but not yet for wasm!

since taproot signing uses rust-bitcoin (to no re-invent the wheel), adding rust-bitcoin dependency on wasm will break linking just
like what happened in #2566

since #262 already fixes this issue by converging all versions
of secp (and thus rust-bitcoin) and enabling them on wasm environment, let's defer wasm support till that PR is merged
@github-actions github-actions bot added the [] label Nov 30, 2025
@shamardy shamardy removed the [] label Dec 1, 2025
@github-actions github-actions bot added the [] label Dec 3, 2025
@shamardy shamardy removed the [] label Dec 9, 2025
@github-actions github-actions bot added the [] label Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants