Skip to content
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

readPushdata does not handle OP_0 (empty push) #18

Open
hans-crypto opened this issue Sep 11, 2024 · 0 comments
Open

readPushdata does not handle OP_0 (empty push) #18

hans-crypto opened this issue Sep 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@hans-crypto
Copy link
Collaborator

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);
}

@hans-crypto hans-crypto added the bug Something isn't working label Sep 12, 2024
hans-crypto added a commit that referenced this issue Sep 12, 2024
see #2
see ordinals/ord#1403 (Use minimal data-pushes)
see ordinals/ord#2505 (Make docs more precise)
see ordinals/ord#1769 (Correctly parse inscriptions when using minimal opcodes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant