Skip to content

feature: Added typed opcode definitions #2099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ test/*.js
test/integration/*.js
!test/ts-node-register.js
docs
plugin
plugin
.idea
130 changes: 125 additions & 5 deletions src/ops.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,127 @@
declare const OPS: {
[key: string]: number;
};
declare const REVERSE_OPS: {
[key: number]: string;
export interface Opcodes {
readonly OP_FALSE: number;
readonly OP_0: number;
readonly OP_PUSHDATA1: number;
readonly OP_PUSHDATA2: number;
readonly OP_PUSHDATA4: number;
readonly OP_1NEGATE: number;
readonly OP_RESERVED: number;
readonly OP_TRUE: number;
readonly OP_1: number;
readonly OP_2: number;
readonly OP_3: number;
readonly OP_4: number;
readonly OP_5: number;
readonly OP_6: number;
readonly OP_7: number;
readonly OP_8: number;
readonly OP_9: number;
readonly OP_10: number;
readonly OP_11: number;
readonly OP_12: number;
readonly OP_13: number;
readonly OP_14: number;
readonly OP_15: number;
readonly OP_16: number;
readonly OP_NOP: number;
readonly OP_VER: number;
readonly OP_IF: number;
readonly OP_NOTIF: number;
readonly OP_VERIF: number;
readonly OP_VERNOTIF: number;
readonly OP_ELSE: number;
readonly OP_ENDIF: number;
readonly OP_VERIFY: number;
readonly OP_RETURN: number;
readonly OP_TOALTSTACK: number;
readonly OP_FROMALTSTACK: number;
readonly OP_2DROP: number;
readonly OP_2DUP: number;
readonly OP_3DUP: number;
readonly OP_2OVER: number;
readonly OP_2ROT: number;
readonly OP_2SWAP: number;
readonly OP_IFDUP: number;
readonly OP_DEPTH: number;
readonly OP_DROP: number;
readonly OP_DUP: number;
readonly OP_NIP: number;
readonly OP_OVER: number;
readonly OP_PICK: number;
readonly OP_ROLL: number;
readonly OP_ROT: number;
readonly OP_SWAP: number;
readonly OP_TUCK: number;
readonly OP_CAT: number;
readonly OP_SUBSTR: number;
readonly OP_LEFT: number;
readonly OP_RIGHT: number;
readonly OP_SIZE: number;
readonly OP_INVERT: number;
readonly OP_AND: number;
readonly OP_OR: number;
readonly OP_XOR: number;
readonly OP_EQUAL: number;
readonly OP_EQUALVERIFY: number;
readonly OP_RESERVED1: number;
readonly OP_RESERVED2: number;
readonly OP_1ADD: number;
readonly OP_1SUB: number;
readonly OP_2MUL: number;
readonly OP_2DIV: number;
readonly OP_NEGATE: number;
readonly OP_ABS: number;
readonly OP_NOT: number;
readonly OP_0NOTEQUAL: number;
readonly OP_ADD: number;
readonly OP_SUB: number;
readonly OP_MUL: number;
readonly OP_DIV: number;
readonly OP_MOD: number;
readonly OP_LSHIFT: number;
readonly OP_RSHIFT: number;
readonly OP_BOOLAND: number;
readonly OP_BOOLOR: number;
readonly OP_NUMEQUAL: number;
readonly OP_NUMEQUALVERIFY: number;
readonly OP_NUMNOTEQUAL: number;
readonly OP_LESSTHAN: number;
readonly OP_GREATERTHAN: number;
readonly OP_LESSTHANOREQUAL: number;
readonly OP_GREATERTHANOREQUAL: number;
readonly OP_MIN: number;
readonly OP_MAX: number;
readonly OP_WITHIN: number;
readonly OP_RIPEMD160: number;
readonly OP_SHA1: number;
readonly OP_SHA256: number;
readonly OP_HASH160: number;
readonly OP_HASH256: number;
readonly OP_CODESEPARATOR: number;
readonly OP_CHECKSIG: number;
readonly OP_CHECKSIGVERIFY: number;
readonly OP_CHECKMULTISIG: number;
readonly OP_CHECKMULTISIGVERIFY: number;
readonly OP_CHECKLOCKTIMEVERIFY: number;
readonly OP_CHECKSEQUENCEVERIFY: number;
readonly OP_CHECKSIGADD: number;
readonly OP_NOP1: number;
readonly OP_NOP2: number;
readonly OP_NOP3: number;
readonly OP_NOP4: number;
readonly OP_NOP5: number;
readonly OP_NOP6: number;
readonly OP_NOP7: number;
readonly OP_NOP8: number;
readonly OP_NOP9: number;
readonly OP_NOP10: number;
readonly OP_PUBKEYHASH: number;
readonly OP_PUBKEY: number;
readonly OP_INVALIDOPCODE: number;
}
declare const OPS: Opcodes;
type ReverseOpcodes = {
[K in keyof Opcodes as Opcodes[K]]: K;
};
declare const REVERSE_OPS: ReverseOpcodes;
export { OPS, REVERSE_OPS };
8 changes: 3 additions & 5 deletions src/ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ const OPS = {
OP_INVALIDOPCODE: 255,
};
exports.OPS = OPS;
const REVERSE_OPS = {};
const REVERSE_OPS = Object.fromEntries(
Object.entries(OPS).map(([key, value]) => [value, key]),
);
exports.REVERSE_OPS = REVERSE_OPS;
for (const op of Object.keys(OPS)) {
const code = OPS[op];
REVERSE_OPS[code] = op;
}
4 changes: 3 additions & 1 deletion src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ function fromASM(asm) {
return compile(
asm.split(' ').map(chunkStr => {
// opcode?
if (ops_1.OPS[chunkStr] !== undefined) return ops_1.OPS[chunkStr];
if (ops_1.OPS[chunkStr] !== undefined) {
return ops_1.OPS[chunkStr];
}
typeforce(types.Hex, chunkStr);
// data!
return Buffer.from(chunkStr, 'hex');
Expand Down
153 changes: 147 additions & 6 deletions ts_src/ops.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,143 @@
const OPS: { [key: string]: number } = {
export interface Opcodes {
readonly OP_FALSE: number;
readonly OP_0: number;
readonly OP_PUSHDATA1: number;
readonly OP_PUSHDATA2: number;
readonly OP_PUSHDATA4: number;
readonly OP_1NEGATE: number;
readonly OP_RESERVED: number;
readonly OP_TRUE: number;
readonly OP_1: number;
readonly OP_2: number;
readonly OP_3: number;
readonly OP_4: number;
readonly OP_5: number;
readonly OP_6: number;
readonly OP_7: number;
readonly OP_8: number;
readonly OP_9: number;
readonly OP_10: number;
readonly OP_11: number;
readonly OP_12: number;
readonly OP_13: number;
readonly OP_14: number;
readonly OP_15: number;
readonly OP_16: number;

// control
readonly OP_NOP: number;
readonly OP_VER: number;
readonly OP_IF: number;
readonly OP_NOTIF: number;
readonly OP_VERIF: number;
readonly OP_VERNOTIF: number;
readonly OP_ELSE: number;
readonly OP_ENDIF: number;
readonly OP_VERIFY: number;
readonly OP_RETURN: number;

// stack ops
readonly OP_TOALTSTACK: number;
readonly OP_FROMALTSTACK: number;
readonly OP_2DROP: number;
readonly OP_2DUP: number;
readonly OP_3DUP: number;
readonly OP_2OVER: number;
readonly OP_2ROT: number;
readonly OP_2SWAP: number;
readonly OP_IFDUP: number;
readonly OP_DEPTH: number;
readonly OP_DROP: number;
readonly OP_DUP: number;
readonly OP_NIP: number;
readonly OP_OVER: number;
readonly OP_PICK: number;
readonly OP_ROLL: number;
readonly OP_ROT: number;
readonly OP_SWAP: number;
readonly OP_TUCK: number;

// splice ops
readonly OP_CAT: number;
readonly OP_SUBSTR: number;
readonly OP_LEFT: number;
readonly OP_RIGHT: number;
readonly OP_SIZE: number;

// bit logic
readonly OP_INVERT: number;
readonly OP_AND: number;
readonly OP_OR: number;
readonly OP_XOR: number;
readonly OP_EQUAL: number;
readonly OP_EQUALVERIFY: number;
readonly OP_RESERVED1: number;
readonly OP_RESERVED2: number;

// numeric
readonly OP_1ADD: number;
readonly OP_1SUB: number;
readonly OP_2MUL: number;
readonly OP_2DIV: number;
readonly OP_NEGATE: number;
readonly OP_ABS: number;
readonly OP_NOT: number;
readonly OP_0NOTEQUAL: number;
readonly OP_ADD: number;
readonly OP_SUB: number;
readonly OP_MUL: number;
readonly OP_DIV: number;
readonly OP_MOD: number;
readonly OP_LSHIFT: number;
readonly OP_RSHIFT: number;
readonly OP_BOOLAND: number;
readonly OP_BOOLOR: number;
readonly OP_NUMEQUAL: number;
readonly OP_NUMEQUALVERIFY: number;
readonly OP_NUMNOTEQUAL: number;
readonly OP_LESSTHAN: number;
readonly OP_GREATERTHAN: number;
readonly OP_LESSTHANOREQUAL: number;
readonly OP_GREATERTHANOREQUAL: number;
readonly OP_MIN: number;
readonly OP_MAX: number;
readonly OP_WITHIN: number;

// crypto
readonly OP_RIPEMD160: number;
readonly OP_SHA1: number;
readonly OP_SHA256: number;
readonly OP_HASH160: number;
readonly OP_HASH256: number;
readonly OP_CODESEPARATOR: number;
readonly OP_CHECKSIG: number;
readonly OP_CHECKSIGVERIFY: number;
readonly OP_CHECKMULTISIG: number;
readonly OP_CHECKMULTISIGVERIFY: number;
readonly OP_CHECKLOCKTIMEVERIFY: number;
readonly OP_CHECKSEQUENCEVERIFY: number;

readonly OP_CHECKSIGADD: number;

// expansion
readonly OP_NOP1: number;
readonly OP_NOP2: number;
readonly OP_NOP3: number;
readonly OP_NOP4: number;
readonly OP_NOP5: number;
readonly OP_NOP6: number;
readonly OP_NOP7: number;
readonly OP_NOP8: number;
readonly OP_NOP9: number;
readonly OP_NOP10: number;

// template matching params
readonly OP_PUBKEYHASH: number;
readonly OP_PUBKEY: number;
readonly OP_INVALIDOPCODE: number;
}

const OPS: Opcodes = {
OP_FALSE: 0,
OP_0: 0,
OP_PUSHDATA1: 76,
Expand Down Expand Up @@ -134,10 +273,12 @@ const OPS: { [key: string]: number } = {
OP_INVALIDOPCODE: 255,
};

const REVERSE_OPS: { [key: number]: string } = {};
for (const op of Object.keys(OPS)) {
const code = OPS[op];
REVERSE_OPS[code] = op;
}
type ReverseOpcodes = {
[K in keyof Opcodes as Opcodes[K]]: K;
};

const REVERSE_OPS: ReverseOpcodes = Object.fromEntries(
Object.entries(OPS).map(([key, value]) => [value, key])
) as ReverseOpcodes;

export { OPS, REVERSE_OPS };
8 changes: 6 additions & 2 deletions ts_src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @packageDocumentation
*/
import * as bip66 from './bip66';
import { OPS, REVERSE_OPS } from './ops';
import { Opcodes, OPS, REVERSE_OPS } from './ops';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

345264

import { Stack } from './payments';
import * as pushdata from './push_data';
import * as scriptNumber from './script_number';
Expand Down Expand Up @@ -168,9 +168,11 @@ export function toASM(chunks: Buffer | Array<number | Buffer>): string {
if (chunksIsBuffer(chunks)) {
chunks = decompile(chunks) as Stack;
}

if (!chunks) {
throw new Error('Could not convert invalid chunks to ASM');
}

return chunks
.map(chunk => {
// data?
Expand All @@ -197,7 +199,9 @@ export function fromASM(asm: string): Buffer {
return compile(
asm.split(' ').map(chunkStr => {
// opcode?
if (OPS[chunkStr] !== undefined) return OPS[chunkStr];
if (OPS[chunkStr as keyof Opcodes] !== undefined) {
return OPS[chunkStr as keyof Opcodes];
}
typeforce(types.Hex, chunkStr);

// data!
Expand Down