bitcoinjs-lib
bitcoinjs-lib copied to clipboard
Can not sign for input #0 with the key
I don't know if creating a P2TR address like this is correct, and it won't be possible to sign in the end.Attached is my code. Could you please help me take a look
const keyPair = ECPair.makeRandom({ network: network }); const p2trSender = bitcoin.payments.p2tr({ pubkey: toXOnly(keyPair.publicKey), network }); const psbt = new bitcoin.Psbt({ network: network }) var totalInputValue = 0; for(var i in utxos){ totalInputValue += utxos[i].satoshi; psbt.addInput({ hash: utxos[i].txid, index: utxos[i].vout, witnessUtxo: { script: p2trSender.output, value: utxos[i].satoshi } }) } const changeValue = totalInputValue - receiverAmount - feeSatoshis; if (changeValue >= 546) { psbt.addOutput({ address: p2trSender.address, value: changeValue }) } psbt.addOutput({ address: senderAddress, value: receiverAmount - 100 }) for (let i = 0; i < utxos.length; i++) { psbt.signInput(i, keyPair); } psbt.finalizeAllInputs(); psbt.extractTransaction();
Please check the integration tests and play around with it a bit first.
https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts
Then let me know if you have any questions.
Taproot transactions are done differently compared to non-taproot.
Used for signing, since the output and address are using a tweaked key
We must tweak the signer in the same way.
You can refer to this: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts#L51-L80