Skip to content

readPushdata does not handle OP_0 (empty push) #18

@hans-crypto

Description

@hans-crypto

The halving block has some transactions that inscribe an ordinal and etch a rune.

Inscription 70279625

Inscription 70279628

The pushdata of these txns revealed a bug:

OP_PUSHBYTES_1 02
OP_0

Both inscriptions right now throw an exception, because the parser cant read the data for tag 2.
This is caused because my code for readPushdata does not handle OP_0 at all.

export function readPushdata(raw: Uint8Array, pointer: number): [Uint8Array, number] {
let [opcodeSlice, newPointer] = readBytes(raw, pointer, 1);
const opcode = opcodeSlice[0];
// Handle the special case of OP_1NEGATE (-1)
if (opcode === OP_1NEGATE) {
// OP_1NEGATE pushes the value -1 onto the stack, represented as 0x81 in Bitcoin Script
return [new Uint8Array([0x81]), newPointer];
}
// Handle minimal push numbers OP_PUSHNUM_1 (0x51) to OP_PUSHNUM_16 (0x60)
// which are used to push the values 0x01 (decimal 1) through 0x10 (decimal 16) onto the stack.
// To get the value, we can subtract OP_RESERVED (0x50) from the opcode to get the value to be pushed.
if (opcode >= OP_PUSHNUM_1 && opcode <= OP_PUSHNUM_16) {
// Convert opcode to corresponding byte value
const byteValue = opcode - OP_RESERVED;
return [Uint8Array.from([byteValue]), newPointer];
}
// Handle direct push of 1 to 75 bytes (OP_PUSHBYTES_1 to OP_PUSHBYTES_75)
if (1 <= opcode && opcode <= 75) {
return readBytes(raw, newPointer, opcode);
}
let numBytes: number;
switch (opcode) {
case OP_PUSHDATA1: numBytes = 1; break;
case OP_PUSHDATA2: numBytes = 2; break;
case OP_PUSHDATA4: numBytes = 4; break;
default:
throw new Error(`Invalid push opcode ${ opcode } at position ${pointer}`);
}
let [dataSizeArray, nextPointer] = readBytes(raw, newPointer, numBytes);
let dataSize = littleEndianBytesToNumber(dataSizeArray);
return readBytes(raw, nextPointer, dataSize);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions