bitcoinjs / bitcoinjs-lib

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

Better types for opcodes #1966

Open vostrnad opened 1 year ago

vostrnad commented 1 year ago

ops.ts exports the list of opcodes as:

const OPS: { [key: string]: number }

That's not a very safe type, as it permits access with any string key. It also doesn't work with code completion.

Can this type be made more specific? It would technically be a breaking change for anyone using this in an unsafe manner, but I believe it's worth it. I'm happy to work on this.

junderw commented 1 year ago

OPS is pretty straightforward, (just as as const at the end) but REVERSE_OPS might be tricky to get to only respond to specific number keys...

Admittedly, when I migrated this library to TypeScript I was a TS beginner, and only feel like I now have a semi-intermediate grasp on it...

PRs are welcome, but I think this won't land until the next major version whenever that is.

ChrisCho-H commented 9 months ago

How about using enum as bitcoin core does? https://github.com/bitcoin/bitcoin/blob/master/src/script/script.h#L72

ChrisCho-H commented 9 months ago

Made PR for this https://github.com/bitcoinjs/bitcoinjs-lib/pull/2028. Any review or commit would be grateful!