bitcoinjs / bitcoinjs-lib

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

Trying to Sign a PSBT using bitcoinjs-lib #1945

Closed Taha-daboussi closed 1 year ago

Taha-daboussi commented 1 year ago

Hello, I am trying to sign a PSBT that is generated by magicEden (NFT marketplace) so I can purchase ordinals NFT from it using code,

am using node js, and I was able to debug the site to find how they generate the PSBT by using this Code.

import { Psbt } from 'bitcoinjs-lib'
const PsbtInstance = new Psbt();
// unsignedBase64 is the site endpoint response 
const psbt = Psbt.fromBase64(unsignedBase64);

but what I am failing to locate is how they sign this PSBT so they can initiate the Transaction since as I was debugging the site I noticed that they send the PSBT to UNISAT extension which will sign the PSBT and return it

any help is appreciated,

Quick notice am not trying to do any malicious or unethical automation on magiceden , all of this is just for sake of learning

junderw commented 1 year ago

It would help tremendously if you post a PSBT.

Depending on the types of inputs and sighash types used in the inputs, the code you call to sign it changes widely.

There are many examples of signing PSBTs on our README (there's a huge list of links).

However, we don't have any examples of script-path p2tr signing outside of simple multisig (using SIGADD).

If any of your inputs that need signing are script-path p2tr, then it will be fairly difficult.

Taha-daboussi commented 1 year ago

Hi @junderw , Thanks for the Response .

The PSBT that am trying to sign is the following :

"70736274ff0100fd93010200000004d866e467f7305761dffb22ef087e7062eb88f824ee7a9af58993be530d14ab7b0500000000ffffffffd866e467f7305761dffb22ef087e7062eb88f824ee7a9af58993be530d14ab7b0400000000ffffffffaef5ebc209c3cc9fc5d3f1312f314876c7e077ace8e44cbd3a5a3a3213c5827c0100000000ffffffffd866e467f7305761dffb22ef087e7062eb88f824ee7a9af58993be530d14ab7b0600000000ffffffff07b00400000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b102700000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b0ff5000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb56672d05000000000000160014c015c65276d5f38d599d445c4cb03aa7aa0dc365580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b64ac06000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b00000000000100fd0e0302000000000104373d861a2b9a909bfa008149f81b485879b47f595b369b1227d78832222159710500000000ffffffff17ba72de7bb9c259902b6ff91a511744b331e7a9ed809d18422ff6382e3a88c30500000000ffffffff10c1b25f100548e649a282684ab66708ddfdc5c3861057b5b26f8d6415147c610100000000ffffffff17ba72de7bb9c259902b6ff91a511744b331e7a9ed809d18422ff6382e3a88c30600000000ffffffff07b00400000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b102700000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66bc7fe00000000000017a914a30d1b7c2541f58f1225b58906f06d98196df1c8876b05000000000000160014c015c65276d5f38d599d445c4cb03aa7aa0dc365580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b001e08000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b02473044022006237f7af20635007a1bfa85b038a33ba95ba72317f2023cb5e24c33a36d716002203a0f40d74a51be26cbaa52e1208898e37a47664808008e45c7117d8c15e23b32012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d430247304402204b2b371220cda7dac928de7d0e0c4b69f426b939cd1b8670342e6f1c6a25c360022014769e7bc3bd766b0b925ae516d4fd633785451a650c44149a32e53ff0fed00a012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d430141adad611eb75691be8b31cb192f2076cb41a589834a696c5b6935217608de7dc5dffd42074e315cf658278985fab8720d0206b2f5a91abb2cba2cf482125d83c9830247304402202dbcfb884a4cbe31009bc544ba39d40120ec52b1987160d43e402ccd2ba96a82022035c87ab072aa702cabc398787fece4702908d46f2c3fec02e8af6b9278af0871012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d4300000000000100fd0e0302000000000104373d861a2b9a909bfa008149f81b485879b47f595b369b1227d78832222159710500000000ffffffff17ba72de7bb9c259902b6ff91a511744b331e7a9ed809d18422ff6382e3a88c30500000000ffffffff10c1b25f100548e649a282684ab66708ddfdc5c3861057b5b26f8d6415147c610100000000ffffffff17ba72de7bb9c259902b6ff91a511744b331e7a9ed809d18422ff6382e3a88c30600000000ffffffff07b00400000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b102700000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66bc7fe00000000000017a914a30d1b7c2541f58f1225b58906f06d98196df1c8876b05000000000000160014c015c65276d5f38d599d445c4cb03aa7aa0dc365580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b001e08000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b02473044022006237f7af20635007a1bfa85b038a33ba95ba72317f2023cb5e24c33a36d716002203a0f40d74a51be26cbaa52e1208898e37a47664808008e45c7117d8c15e23b32012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d430247304402204b2b371220cda7dac928de7d0e0c4b69f426b939cd1b8670342e6f1c6a25c360022014769e7bc3bd766b0b925ae516d4fd633785451a650c44149a32e53ff0fed00a012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d430141adad611eb75691be8b31cb192f2076cb41a589834a696c5b6935217608de7dc5dffd42074e315cf658278985fab8720d0206b2f5a91abb2cba2cf482125d83c9830247304402202dbcfb884a4cbe31009bc544ba39d40120ec52b1987160d43e402ccd2ba96a82022035c87ab072aa702cabc398787fece4702908d46f2c3fec02e8af6b9278af0871012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d4300000000000100fdcf0202000000000104d01fbde8490f7685a588867647fe05e2570bff49a8d8573683d7c1748e0187090300000000ffffffff077bdb34712feb6ea5ddd839be7e5ab56e8f1c93db500e528f069d3a4b0b0d6c0400000000ffffffff150f6fae4457873701521473e6dee2f1944e0404dca06b942469da4bb7e416a40100000000ffffffff7b112b95c36135101150f202891ec51a10fec4f6fb7cda2830864d408caab4af0600000000ffffffff07b004000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb56671027000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb566795f501000000000017a91463bd1f520e6bf1b0e68bf6157909911a59ecf2f0879f0b000000000000160014c015c65276d5f38d599d445c4cb03aa7aa0dc3655802000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb56675802000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb5667e496000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb56670140544d07d28f2397adfb8055d8cc2fdf56fa03f9ab789e12b2b65d4df53c72a75b8d8aaa478361b62898212d005565ed6f7ed518cf76a52864c0c04d6fce7fa17101404c333d64713bc61cbd53e84dd005442878500aaedbff937e0f56b16be05c2ffb4934808843a08107f8cfa39665d51ef792b70f77ee138342850b1ec780e97e3c014198cc2d6d26db06064bec0687d4e584aefc032f0be0d010911f38f963d2990a34c2983f83c9176412994d89d152b0697008c59e30b8ce70151ae0d844133d20e68301407338c1af5b137215fce78d66709206ca6265cacb3f2c38c009fe6452c347680b830c4fef62d687d54eb860b115086150e5fe91993f0eb319b59260b737329ede0000000001012b1027000000000000225120ad2803f3d5a8c3390922b12da490510c54a456091fd042153319d71993eb566701172059a6e79daaa5db2fdf3757ccdfcb5bc73ce3bc5fe373162fcb4f33df1e1c501e000100fd0e0302000000000104373d861a2b9a909bfa008149f81b485879b47f595b369b1227d78832222159710500000000ffffffff17ba72de7bb9c259902b6ff91a511744b331e7a9ed809d18422ff6382e3a88c30500000000ffffffff10c1b25f100548e649a282684ab66708ddfdc5c3861057b5b26f8d6415147c610100000000ffffffff17ba72de7bb9c259902b6ff91a511744b331e7a9ed809d18422ff6382e3a88c30600000000ffffffff07b00400000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b102700000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66bc7fe00000000000017a914a30d1b7c2541f58f1225b58906f06d98196df1c8876b05000000000000160014c015c65276d5f38d599d445c4cb03aa7aa0dc365580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b580200000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b001e08000000000016001484b9282c6222dfcf8ccea862fefb59de5defb66b02473044022006237f7af20635007a1bfa85b038a33ba95ba72317f2023cb5e24c33a36d716002203a0f40d74a51be26cbaa52e1208898e37a47664808008e45c7117d8c15e23b32012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d430247304402204b2b371220cda7dac928de7d0e0c4b69f426b939cd1b8670342e6f1c6a25c360022014769e7bc3bd766b0b925ae516d4fd633785451a650c44149a32e53ff0fed00a012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d430141adad611eb75691be8b31cb192f2076cb41a589834a696c5b6935217608de7dc5dffd42074e315cf658278985fab8720d0206b2f5a91abb2cba2cf482125d83c9830247304402202dbcfb884a4cbe31009bc544ba39d40120ec52b1987160d43e402ccd2ba96a82022035c87ab072aa702cabc398787fece4702908d46f2c3fec02e8af6b9278af0871012103b14668a8e02420981a62e2302c8723f9b254a6a8847be108e612babce7929d43000000000000000000000000"

Am not not sure if this will help but the wallet that am trying to sign with is a Native Segwit P2WPKH

junderw commented 1 year ago

You can sign P2WPKH like this.

https://github.com/bitcoinjs/bitcoinjs-lib/blob/v6.1.3/test/integration/transactions.spec.ts#L326-L358

But the 3rd input is a P2TR.

https://github.com/bitcoinjs/bitcoinjs-lib/blob/v6.1.3/test/integration/taproot.spec.ts#L94-L159

Taha-daboussi commented 1 year ago

Thanks @junderw, quick question , this solution will allow me to sign the given PSBT using my private key directly without the need to add input and add output?

Taha-daboussi commented 1 year ago

i was trying this code as i said in a different issues , which I think maybe can solve this issue if I edit it

import * as ecc from 'tiny-secp256k1';
import ECPairFactory from 'ecpair';
const bitcoin = require('bitcoinjs-lib')
const ECPair = ECPairFactory(ecc);

const network = bitcoin.networks.bitcoin
const alice = ECPair.fromWIF('',);
const data = 'my Hex Psbt'
// const psbt = bitcoin.Psbt.fromBase64(data)
let psbt = bitcoin.Psbt.fromHex(data);

psbt.signAllInputs(alice); 

psbt.finalizeAllInputs();
psbt.extractTransaction()

const rawHex = psbt.extractTransaction().toHex()
console.log(rawHex)
Taha-daboussi commented 1 year ago

@junderw since both of the above parts that you gave me don't use any PSTB to sign the Transaction.