Understanding Bitcoin: Why walletprocesspsbt
closes the PSBT but doesn’t return the Hex transaction
Since the popular wallet uses processpsbt to finalize the Payment Script Binary Tree (PSBT) and add a bip32.derivation path, you would correctly assume that this command would return a finalized PSBT. However, adding a new derivation path to an already signed and finalized PSBT does not directly return the hex of the transaction.
In Bitcoin, the hash of every transaction is represented as a 64-byte hexadecimal string. When you add a new derivation path to a PSBT, the wallet processpsbt creates this new hexadecimal representation of the transaction’s hash. This is done by using the bip32.derivation
method and then hashing the resulting binary data.
Step by step, this is what happens:
- The wallet processpsbt signs the input transaction with its private key.
- It creates a Payment Script Binary Tree (PSBT) from the signed transaction, which contains all the information needed to create the output.
- The wallet processpsbt adds a new derivation path to the PSBT using the “bip32.derivation” method, specifying a custom derivation path.
When you run “walletprocesspsbt finalize psbt”, it generates a finalized PSBT with the added derivation path. However, instead of directly returning the transaction hex, it returns an object that contains the finalized PSBT and other metadata.
Why doesn’t it return the transaction hex?
The Wallet processpsbt returns the finalized PSBT because it is a binary format that represents the payment script tree. The “bip32.derivation” method uses a hashing algorithm (in this case, SHA-256) to generate the new derivation path and its associated hash value.
When you pass the finalized PSBT to an RPC command such as “getrawtransaction” or print its contents using “dumpwallet”, the wallet processpsbt returns the same object containing the finalized PSBT and other metadata. However, the hexadecimal representation of the transaction hash is not returned directly; instead, it is embedded in the object.
Example use case:
Suppose you have a signed transaction with 30-byte input “txid” and “vout” indexes (e.g., “txinmap” = [0x1234, 0x5678]). Sign the transaction with the wallet’s processpsbt private key:
walletprocesspsbt commit txid vout1 txinmap=0x1234 0x5678 --derivepath=myderivation
After running this command, you will get a committed PSBT that contains the binary data of the signed transaction and other metadata. Then, add a new derivation path to the PSBT using “bip32.derivation”:
walletprocesspsbt commit psbt txid vout1 txinmap=0x1234 0x5678 --derivepath=myderivation --addpath=bip32path
Wallet processpsbt returns an object that contains the committed PSBT and other metadata, including the transaction hash as a hexadecimal string.
In summary, if you add a new derivation path to an already signed and finalized PSBT using “walletprocesspsbt”, the command returns an object containing the finalized PSBT and other metadata. The transaction hexadecimal is not returned directly; instead, it is embedded in the object.