bitcoinjs / bitcoinjs-lib

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

How to use PSBT to sign raw transaction hex from bitcoin core cli #1635

Closed neocarmack closed 3 years ago

neocarmack commented 3 years ago

I used the bitcoin-cli createrawtransaction to create the txhex. And it can be signed by using: bitcoin-cli signrawtransactionwithkey.

I can use TransactionBuilder from bitcoinjs-lib to sign the txhex as well:

  var txhex = '0200000001b1 xxxxx xxxxxx   00000'; // too long to past here.
  const BTC_TESTNET = bitcoin.networks.testnet;

  var txb = bitcoin.TransactionBuilder.fromTransaction (bitcoin.Transaction.fromHex (txhex), BTC_TESTNET);

  const alice = bitcoin.ECPair.fromWIF('example wif cQuy6PDHYrq6bp5vsev3eL1VTcC5fAKZhWB1HHUFoU4goXeSY5  ',BTC_TESTNET)
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: alice.publicKey, network: BTC_TESTNET })
  const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: BTC_TESTNET })

  txb.sign({
        prevOutScriptType: 'p2pkh',
        vin: 0,
        keyPair: alice, 
    })
  txb.build().toHex()

But TransactionBuilder is going to be removed from future release. Officially the PSBT is suggested. Could anyone give me some info about how to use PSBT to sign hex created from bitcoin-cli?

junderw commented 3 years ago

bitcoin core has a method for creating PSBTs instead of "rawtransaction"s

Use that instead.

junderw commented 3 years ago

https://bitcoincore.org/en/doc/0.20.0/rpc/wallet/walletcreatefundedpsbt/