paulmillr / scure-btc-signer

Audited & minimal library for creating, signing & decoding Bitcoin transactions.
https://paulmillr.com/noble/#scure
MIT License
151 stars 39 forks source link

btc.Transaction.fromPSBT() fails in expo@51.0.14 & react-native@0.74.2 #97

Closed ph101pp closed 3 months ago

ph101pp commented 3 months ago

Hello!

Thank you for your work on the scure libraries, they work great!

That said, I'm having trouble getting btc.Transaction.fromPSBT working in react-native@0.74.2

Other things work great: Creating btc.selectUTXO, tx.toPSBT, just btc.Transaction.fromPSBT fails with:

Reader(magic): TypeError: Cannot read property 'prototype' of undefined

Thrown somewhere in decodeStream.

We have polyfills for crypto, TextEncode/TextDecode, Buffer, stream etc.

This same code works in a web environment with the same inputs.

Any input/help would be appreciated!

This might be an issue for micro-packed instead. Let me know and I can open an issue there.

paulmillr commented 3 months ago

Spent a lot of time on this but it works correctly in react native 0.74.2.

You will need to create a reproducible repo with exact code which is breaking.

ph101pp commented 3 months ago

Thank you for your time!

I created this repo with expo setup that reproduces the error:

https://github.com/ph101pp/expo-scure-btc-signer/

The problem doesn't seem to be with react-native itself, but with expo and most likely the metro bundler that it is using: https://github.com/facebook/metro

Expo is the standard when it comes to building production level react-native apps, it would be amazing if @scure/btc-signer could work with this setup, but so far I wasn't able to get it to work.

paulmillr commented 3 months ago

We don't support Buffers. You must always use Uint8Array. Something somewhere would break, because buffer is shit. For example, it completely redefines semantics of slice to make it like subarray instead of copy.

ph101pp commented 3 months ago

I wasn't aware of that re: Buffers! Thanks!

Turns out that fastestsmallesttextencoderdecoder TextDecoder shim isn't up to full spec / only supports utf-8 for speed purposes ( I'm assuming that is the issue). Replacing the shim fixed the issue.

Other shims that make similar compromises suggest to use Buffer for other types of conversions, but after your comment above I'm assuming using TextDecoder was a deliberate choice.

Thanks again for your time and sorry for the churn, I was banging my head against the table.