atomicDEX-API icon indicating copy to clipboard operation
atomicDEX-API copied to clipboard

feat(wallet-connect): impl BTC (UTxO) basic signing and swaps v1

Open mariocynicys opened this issue 5 months ago • 13 comments

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 avatar Aug 02 '25 22:08 mariocynicys

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.

mariocynicys avatar Aug 02 '25 22:08 mariocynicys

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?

smk762 avatar Aug 04 '25 12:08 smk762

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 avatar Aug 04 '25 12:08 mariocynicys

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

mariocynicys avatar Aug 04 '25 13:08 mariocynicys

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 https://github.com/KomodoPlatform/komodo-defi-framework/issues/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.

mariocynicys avatar Aug 05 '25 11:08 mariocynicys

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.

shamardy avatar Aug 05 '25 14:08 shamardy

tested p2pkh (non-witness) signing and working fine. tested two swaps here (2.5 swaps actually. last swap wasn't complete as i was testing bifrost again): maker: https://mempool.space/testnet4/address/mw9htvmR3trmSAwLi8jmHvAjKjQmdBAXoX taker: https://mempool.space/testnet4/address/mvYeYPkfEXUJ7TCB3hBu488C6D2r2YSVix

to test p2pkh addresses, you wanna edit the walletconnect demo wallet to use 44' derivation path and p2pkh instead of p2wpkh address generation. then in the utility test also use 44' and omit the "address_format" field to default it to legacy. now KDF and walletconnect will both deal with p2pkh addresses. or you can take my word for it :D

mariocynicys avatar Aug 07 '25 16:08 mariocynicys

Did you test TPU/V2 swaps with wallet connect? I see that some of them use derive_htlc_key_pair which will fail for WC. If TPU is not supported yet we should look to support it next.

nah, this PR is only for swaps v1. i opted for not putting v2 on top of this PR to get reviews going quickly. but i already researched how to do it and that would be fairly simple given we already have the utility methods to sign plain p2sh in place.

mariocynicys avatar Aug 30 '25 04:08 mariocynicys

Did you test TPU/V2 swaps with wallet connect? I see that some of them use derive_htlc_key_pair which will fail for WC. If TPU is not supported yet we should look to support it next.

nah, this PR is only for swaps v1. i opted for not putting v2 on top of this PR to get reviews going quickly. but i already researched how to do it and that would be fairly simple given we already have the utility methods to sign plain p2sh in place.

mariocynicys avatar Aug 30 '25 04:08 mariocynicys

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

Are there any related docs updates needed?

smk762 avatar Sep 08 '25 04:09 smk762

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.

mariocynicys avatar Sep 08 '25 06:09 mariocynicys

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)

github-actions[bot] avatar Sep 08 '25 15:09 github-actions[bot]

fyi, the wasm problem was induced by this commit (https://github.com/KomodoPlatform/komodo-defi-framework/pull/2566/commits/51905d400d89e972943f2b7ebb39630f3a8641e7) 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 avatar Sep 09 '25 11:09 mariocynicys