micro-bitcoin / uBitcoin

Bitcoin library for microcontrollers. Supports Arduino, mbed, bare metal.
https://micro-bitcoin.github.io/
MIT License
166 stars 34 forks source link

PSBT.parseBase64(...) failed to parse unsigned transaction generated with Electrum #9

Closed phplego closed 3 years ago

phplego commented 3 years ago

Current version of Electrum (4.0.7) does not allow to export unsigned transaction in hex format - only base64. Also it has psbt prefix. So it looks like now only psbt transactions can be generated with Electrum. Just a guess.

For example this is a transaction generated with Electrum (testnet):

cHNidP8BAHQCAAAAASkA3TuiFr9FXleglmFmRM0JkYOuS3DDZX4ptlqYXVz+AAAAAAD9////AuAuAAAAAAAAGXapFJ+aer1gDAyqA5g6d8jD344GLLL6iKw86BIAAAAAABYAFEPiZyhbzPOiCHHX0oL56PPQvYU2CPQcAAABAPUCAAAAAAEBpTlECzz5cPQe0kSl6C1FrcYk3aUayhaASGKRLB6l8e0BAAAAFxYAFKAxhgGP0V1fYc4jnZZDMNUi+gw9/v///wIKGBMAAAAAABYAFNIExxSjrMyz2cX2BbPwbMyGexsNxGYZ2QEAAAAWABTjkyCFwMo1QHLwIgbQuJs5GMgc+wJHMEQCIHZWy+sOhKYP76WSi6HPpJVs7e2RvN6I8IzeQXp3hbS3AiADt/j73CxxZGNmRvJyWbs3H0Qm/Fc38FOO8PckoSCa+QEhAhMSvrpl3DCjTjP2mWewv40nhAxsgSnelq9H95inoh+YB/QcACIGA5NT4+8EpDjIeFZA2l9Yr+3y3yX9M2F7FpJZ7GqWgs6NDNKCdDUAAAAAAAAAAAAAIgIC1z3a8a4vLjVdvwq1RHv2d0v9qTjXZkj0yAFVfel+cvkM0oJ0NQEAAAAAAAAAAA==

image

I tried to parse this Base64 transaction with this code:

PSBT psbt;
psbt.parseBase64("cHNidP8BAHQCAAAAASkA3TuiFr9FXleglmFmRM0JkYOuS3DDZX4ptlqYXVz+AAAAAAD9////AuAuAAAAAAAAGXapFJ+aer1gDAyqA5g6d8jD344GLLL6iKw86BIAAAAAABYAFEPiZyhbzPOiCHHX0oL56PPQvYU2CPQcAAABAPUCAAAAAAEBpTlECzz5cPQe0kSl6C1FrcYk3aUayhaASGKRLB6l8e0BAAAAFxYAFKAxhgGP0V1fYc4jnZZDMNUi+gw9/v///wIKGBMAAAAAABYAFNIExxSjrMyz2cX2BbPwbMyGexsNxGYZ2QEAAAAWABTjkyCFwMo1QHLwIgbQuJs5GMgc+wJHMEQCIHZWy+sOhKYP76WSi6HPpJVs7e2RvN6I8IzeQXp3hbS3AiADt/j73CxxZGNmRvJyWbs3H0Qm/Fc38FOO8PckoSCa+QEhAhMSvrpl3DCjTjP2mWewv40nhAxsgSnelq9H95inoh+YB/QcACIGA5NT4+8EpDjIeFZA2l9Yr+3y3yX9M2F7FpJZ7GqWgs6NDNKCdDUAAAAAAAAAAAAAIgIC1z3a8a4vLjVdvwq1RHv2d0v9qTjXZkj0yAFVfel+cvkM0oJ0NQEAAAAAAAAAAA==");

if (!psbt)
{
      Serial.println("Can't parse tx;");
      return;
}

And it does not work ("Can't parse tx;")

phplego commented 3 years ago

After some investigation I have found exact place where it fails:

image

(Please note that line numbers are slightly shifted because of added status_code in some places)

stepansnigirev commented 3 years ago

Thanks for reporting! I am a bit busy this week, I'll look into this issue over the weekend.

I think Electrum switched from their own transaction format to PSBT, so I will also remove Electrum-specific code from the repo.

phplego commented 3 years ago

Thanks for reporting! I am a bit busy this week, I'll look into this issue over the weekend.

I think Electrum switched from their own transaction format to PSBT, so I will also remove Electrum-specific code from the repo.

Thank you Stepan. Looks like your lib currently is the only one library for doing this on arduino-like microcontrollers (at least in such simple way). It would be extremely super helpful to get it working with transactions again (since PSBT). Good luck!

phplego commented 3 years ago

Further investigation shows that tx hash is not equal:

image

stepansnigirev commented 3 years ago

Should be fixed in https://github.com/micro-bitcoin/uBitcoin/commit/52350a3827c5dc46640e003df07ac41613b4ab39 Can you confirm?

phplego commented 3 years ago

On ESP32 transaction parsed OK image

On ESP8266 it crushes (out of memory I guess)