bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.7k stars 2.11k forks source link

Offline signing for Psbt class #2051

Closed raphaels10 closed 8 months ago

raphaels10 commented 8 months ago

Hi!

I'm developing a crypto application that leverages Hardware Security Modules (HSMs) to store BIP32 keys and also sign bitcoin transactions.

In the older versions of the lib, i could do it just fine by using the TransactionBuilder class to add the transaction inputs and outputs and then by calling the hashForSignature or hashForWitnessV0 (in the case that the input is Segwit) to generate the hashes to be signed by the private key within the HSM. At the end, i just had to call the setInputScript or setWitness method to append the signatures and generate the fully signed transaction.

With the Psbt class, i can't seem to find a way to do that. There are no methods to generate the hashes to be signed, and no methods to append the signatures to the transaction object. Trying to convert the Psbt to a Transaction object also fails if the transaction hasn't been signed yet. Is there any way to do this offline signing process with the new Psbt class?

junderw commented 8 months ago

Implement an AsyncSigner interface and pass that into signInputAsync (and any of the other Async signing methods).

junderw commented 8 months ago

If you absolutely must smuggle the hash, you could implement a smuggler signer that just exfiltrates the hash, and then an injector signer that compares the hash given to the hash you signed, and if they're equal, inject the signature.

raphaels10 commented 8 months ago

@junderw Just tested it and it's working! Thanks for the help