bitcoinjs / bitcoinjs-lib

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

HOW to convert TransactionBuilder.fromHex to Psbt.fromHex ? #1796

Closed stefaniepei closed 2 years ago

stefaniepei commented 2 years ago

HOW to convert TransactionBuilder.fromHex to Psbt.fromHex ?

I am currently on version 5.2.0, and I want to upgrade to 6.0.1, but I changed TransactionBuilder.fromHex to Psbt.fromHex, and always reported an error magic number.

Test hex as 0200000000010234ba9c204b5a485427729f53347c421d193582255f026f69f8c76f6e4680172f0000000092004730440220727d5d2e14e153ec59942033f2cd89edfba18b581b8d9631cc4664113cf1b60102203bdacc4b55896269626de209755817522b6b0739a81f57d73ecac1331fa85d84010047522103a22f646f375f7b4da865643e9c5bec286c8ec43835176971a7861f75a590ca742103061c80153f8e4be3013d37b3ef9103cd3f477615d52f001a130425d97c4eb95e52aeffffffff3f0ecd811895aa34a88ac77089e64437b56329b795d37ff807e693ad3ab235150100000017160014f8fce1c03859625dd7851088930fc2bcb66cd5bcffffffff03ee2a00000000000017a914748d81aa676b2b464d7a2d5c6cf4af7f7f2bad14870a0000000000000017a914cb793b3f0c560cb95c0b4d941bc06289484ebfce87d00700000000000017a9147eabf95b9700eefe66efe4dbf1a15b27117dbef687000247304402202c2f3181fc1ef44154e381ed663174c33c472cb3b35de6dcd566d3a8841d251d022043a76ef925b70ef5807f8c123e996fdc8817e18411aa72f83a7580af98258830012103a22f646f375f7b4da865643e9c5bec286c8ec43835176971a7861f75a590ca7400000000

junderw commented 2 years ago

There never was any TransactionBuilder.fromHex... I don't know what you are talking about.

junderw commented 2 years ago

I see your tx is trying to sign 2 inputs, one is P2SH-Multisig(2 of 2) and one is P2SH-P2WPKH.

Here's a P2SH-Multisig example. (it's 2 of 4, but you should be able to understand)

https://github.com/bitcoinjs/bitcoinjs-lib/blob/v6.0.1/test/integration/transactions.spec.ts#L202-L248

Here's a P2SH-P2WPKH example.

https://github.com/bitcoinjs/bitcoinjs-lib/blob/v6.0.1/test/integration/transactions.spec.ts#L250-L292

junderw commented 2 years ago

Also, here's an example of a normal P2PKH transaction that uses the PSBT exporting to send to multiple signers.

This is also how we would do multisig signing when sending between multiple people.

https://github.com/bitcoinjs/bitcoinjs-lib/blob/v6.0.1/test/integration/transactions.spec.ts#L84-L169

Please let me know if you have any questions. Please show code examples with your question.

stefaniepei commented 2 years ago

There never was any TransactionBuilder.fromHex... I don't know what you are talking about.

const transaction = bitcoin.Transaction.fromHex(txHex) const txBuild = bitcoin.TransactionBuilder.fromTransaction(transaction, bitcoin.networks.regtest) ...

In version 5.2.0, txBuild is what I want How can I get this Psbt or others like txBuild in version 6.0.1

Thanks

junderw commented 2 years ago

const transaction = bitcoin.Transaction.fromHex(txHex) const txBuild = bitcoin.TransactionBuilder.fromTransaction(transaction, bitcoin.networks.regtest) ...

In version 5.2.0, txBuild is what I want How can I get this Psbt or others like txBuild in version 6.0.1

Thanks

Read my other replies.

Thanks.

stefaniepei commented 2 years ago

I see your tx is trying to sign 2 inputs, one is P2SH-Multisig(2 of 2) and one is P2SH-P2WPKH.

How do you know 2 inputs, one is this and one is that ?

Thanks

junderw commented 2 years ago

I have spend hundreds of hours writing binary parsers for bitcoin transactions.

I can see your tx and what kind of inputs they are by just looking at the hex.

I am not joking.

Thanks

stefaniepei commented 2 years ago

U are amazing

Thanks