Skip to content

Instantly share code, notes, and snippets.

@ottosch
Created May 3, 2025 21:50
Show Gist options
  • Save ottosch/bae59428e1955f8477b8d40ab4b8168b to your computer and use it in GitHub Desktop.
Save ottosch/bae59428e1955f8477b8d40ab4b8168b to your computer and use it in GitHub Desktop.
Extract image inscription of tx 4be3a833ee83b4ca7d157d60fbf7411f7528314ce90df8a844f855118bc6ca11
#! /usr/bin/env node
// Invoke with: bitcoin-cli getrawtransaction 4be3a833ee83b4ca7d157d60fbf7411f7528314ce90df8a844f855118bc6ca11 | ./dinner-inscription.js
const { Transaction } = require('bitcoinjs-lib');
if (process.stdin.isTTY) {
console.error('raw tx needed, quitting...');
process.exit(1);
}
let input = '';
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', () => main());
function main() {
const tx = Transaction.fromHex(Buffer.from(input, 'hex'));
let data = Buffer.alloc(0);
for (const input of tx.ins) {
const script = input.script;
let cursor = 0;
const redeemSize = script[0]; // ignore 1st push
cursor++;
cursor += redeemSize;
while (cursor < script.length) {
const opcode = script[cursor];
cursor++;
if (opcode === 0x4c) { // redeem script
break;
} else if (opcode === 0x4d) {
const size = script.readUIntLE(cursor, 2);
cursor += 2;
const chunk = script.subarray(cursor, cursor + size);
data = Buffer.concat([data, chunk]);
cursor += size;
} else {
console.error(`unexpected opcode: ${opcode.toString(16)}`);
process.exit(1);
}
}
}
const start = Buffer.from('/9j', 'utf8');
const end = Buffer.from('//Z', 'utf8');
const img = data.subarray(data.indexOf(start), data.indexOf(end) + end.length).toString('utf8');
console.log(img.replace(/\0+/g, '\n'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment