near / fast-auth-signer

https://fast-auth-signer.vercel.app
MIT License
29 stars 8 forks source link

Run BTC sign in parallel #196

Open Pessina opened 3 months ago

Pessina commented 3 months ago

We're facing challenges with nonce conflicts in our BTC transaction signing process due to simultaneous transactions on the NEAR blockchain. Currently, we've shifted to sequential chain signature requests to avoid these conflicts, but this workaround significantly hampers performance.

The objective of this ticket is to explore and implement a method that allows for parallel processing without nonce duplication.

Sequential signature code:

https://github.com/near/fast-auth-signer/blob/d7fb38aa8bfad5cb2528dc38641da2206e73258b/packages/near-fast-auth-signer/src/utils/multi-chain/chains/Bitcoin.ts#L431

mikedotexe commented 2 months ago

Happy to hop on a call and/or help out. I know what you're talking about, and it's an interesting pattern the bitcoinjs-lib package has for us, where you pass in a key to signInput and signInputAsync.

I believe the goal here is to individually sign each input sighash (using SIGHASH_ALL) using NEAR batch Actions. I tested this on MyNEARWallet, to make sure it supported it, and it does. So the end user sends an array of FunctionCall Actions, each calling sign on the MPC contract. Then we'd get back all the UTXO signatures and not deal with multiple wallet clicks/redirects.

I've spent a decent amount of time looking into this, and it seems like the bitjoinjs-lib uses the bip174 library for the PSBT, but lacks some functionality. (For instance, you cannot call getTransactions from the PSBT, which would be very useful for us.) SO! I think what we'll have to do is work with the ergonomics of these packages, and first construct Transaction objects for each input UTXO we intend to use. The Transaction object differs from the PSBT object and will allow us to use helper methods to obtain the sighash. This sighash is what we need to send to the MPC to sign, and I believe it's the function hashForSignature:

https://github.com/bitcoinjs/bitcoinjs-lib/blob/3c26beb1d205943efbd40f7171ff947dc66ac5c2/ts_src/transaction.ts#L265