bitcoinerlab / descriptors

A TypeScript library for parsing Bitcoin Descriptors, including Miniscript-based ones. Streamlines creating Partially Signed Bitcoin Transactions (PSBTs) from Descriptors. Features BIP32, single-signature, and Hardware Wallet signing capabilities, and facilitates finalizing transactions.
https://bitcoinerlab.com/modules/descriptors
46 stars 14 forks source link

Lookbehind in regexp not supported on iOS #19

Closed serious-sammy closed 1 year ago

serious-sammy commented 1 year ago

Currently, lookbehind regex assertions are not supported in Safari and therefore iOS (all iOS browsers are just reskinned Safari). Because of this, the library can't be used on Safari or iOS, because when imported, the following exception is thrown: SyntaxError: Invalid regular expression: invalid group specifier name. The root cause seems to be this line, which uses a lookbehind ?<.

serious-sammy commented 1 year ago

Replacing with the following seems to help:

.trim()
// Replace one or more consecutive whitespace characters (spaces, tabs,
// or line breaks) with a single space.
.replace(/\s+/g, ' ')
// Now encode numbers to little endian hex. Note that numbers are not
// enclosed in <>, since <> represents hex code already encoded.
// The regex below will match one or more digits within a string,
// except if the sequence is surrounded by "<" and ">"
.replace(/(<\d+>)|\b\d+\b/g, (match) => 
  match.startsWith('<') ? match : numberEncodeAsm(Number(match))
)
// we don't have numbers anymore, now it's safe to remove < and > since we
// know that every remaining is either an op_code or a hex encoded number
.replace(/[<>]/g, '');
landabaso commented 1 year ago

Thanks @serious-sammy . This looks good to me. Do you want to submit a PR with the proposal? I can do it for you if you don't want to (or have the time). Let me know.

serious-sammy commented 1 year ago

I'd appreciate if you would do it 🙏

landabaso commented 1 year ago

Thanks for the report @serious-sammy . I've updated npm with a new version with the change.