bitcoinjs / bip174

A BIP174 compatible partial Transaction encoding library.
MIT License
34 stars 36 forks source link

Won't recognize if magic number is padded with 0x00 #24

Closed Overtorment closed 4 years ago

Overtorment commented 4 years ago

I encountered an issue with Electrum desktop 4.0.3. Displayed QR code with PSBT is:

1) base43 encoded (not a problem) 2) magic number 0x70736274 ("psbt") is padded with 0x00.

Why? I don't know, but if you throw it in bitcoinjs as is - it will choke.

I wrote simple unpad function, but I have a feeling that something is wrong.

Example:

const Base43 = require('electrum43');
const bitcoin = require('bitcoinjs-lib');

function unpadHex(hex) {
  const index = hex.indexOf('70736274')
  if (index !== -1) {
    return hex.substr(index);
  }
  return hex;
}

const hex = Base43.decode('8+065FQS++FH76-QX$/RI8KR6O*V+WR-I0FH.9B49H1+L6I5N1JJ$M+P:3AH:QM2QUFSRR2D1XFX+I2:WTTG3F2HL4P02O2+6JE8VYJNXP:EPJ6KPMHQEJO-I/W.6*ESN:YC6FZ24PJS/QRU0YEAKSZAZM8:$7$HI7UKPG+H:+BNRO20QPOOCI8P45/TNGX-QR.X0P*WP0TAGCHMMO-UGONFLCG2QMMIA$GU6HPNI.9TK2+X99L7GLD0$OHSX2N55/1.X8ZCRH.-06B8L+6A865PIWM8Q.*8BLD2/AY+1E2F-FH6VD+JFZC*-G*IDZQ/U-8G0UOGYV1GA6BC1N.X95R:E12L987Q$TG61X4+SR4SO*IG083GH4L77DF6FL-JAFDX/W5BR4.I*$3S*9CDIW0Z4MXWJE-R9TP-OU3T$L0RHXXV885$GJ$VMOP6DX700GU7CASZGM:-XZQ+QSFXUOE:P/OKNUEIBT.BV8G.V5GRDQ3DT-W5*L4GHD-X2WFV9940YL:4LCTAWQDL..GAD9X6H:ZU064-5MUBKG0O20ZY8FV0RX+O7IL3T3YL0CKGLYZQUYXB+.F2JNN4MV83/V70UWZA6AGSU5SVSJMYXN7RRO02IXX8*QBDCYJU2G*N7+U');

const unpaddedHex = unpadHex(hex);
bitcoin.Psbt.fromHex(unpaddedHex);

(same on runkit: https://runkit.com/overtorment/5f805f125ba08e001c32f318 )

screenshot: (slightly different qr)

image

junderw commented 4 years ago

This is an issue with Electrum OR an issue with the base43 decoder.

https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#specification

Screenshot_20201009_225312

junderw commented 4 years ago

That base43 library looks super suspicious... I'd recommend against using it.

junderw commented 4 years ago

Yeah, that library is BS... broken.

Just use base-x (it's what we use for bs58 in bitcoinjs-lib, so you already have it as a dep.

Selection_001

Overtorment commented 4 years ago

thanks!