bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Better types for opcodes

Open vostrnad opened this issue 1 year ago • 3 comments

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.

vostrnad avatar Aug 06 '23 22:08 vostrnad

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.

junderw avatar Aug 07 '23 00:08 junderw

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

ChrisCho-H avatar Jan 05 '24 01:01 ChrisCho-H

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

ChrisCho-H avatar Jan 05 '24 14:01 ChrisCho-H