|
| 1 | +import { bigEndianBytesToNumber, hexToBytes, concatUint8Arrays } from '../lib/conversions'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Extracts raw file data from OLGA P2WSH outputs. |
| 5 | + * |
| 6 | + * OLGA (Octet Linked Graphics & Artifacts) encodes data directly in P2WSH |
| 7 | + * output scriptpubkeys. Each output is 34 bytes: 0x00 0x20 + 32 bytes of data. |
| 8 | + * The 32-byte "hash" is NOT a real hash -- it's raw file data. |
| 9 | + * |
| 10 | + * Format: |
| 11 | + * 1. Collect all v0_p2wsh scriptpubkeys (skip index 0 -- reserved for destination) |
| 12 | + * 2. Strip the 0x0020 prefix from each, giving 32 raw bytes per output |
| 13 | + * 3. Concatenate, strip trailing zeros |
| 14 | + * 4. First 2 bytes = big-endian file length |
| 15 | + * 5. Remaining bytes = raw file data |
| 16 | + * |
| 17 | + * Two flavors exist on mainnet: |
| 18 | + * a) Counterparty-issued stamps (Classic Stamps, SRC-721 via CP): |
| 19 | + * Raw file data directly (PNG, GIF, SVG, JSON). No stamp: prefix. |
| 20 | + * b) Direct Bitcoin stamps (SRC-20 OLGA, SRC-101 OLGA): |
| 21 | + * Data starts with "stamp:" prefix, followed by the file content. |
| 22 | + * |
| 23 | + * Source: stampchain-io/btc_stamps indexer/src/index_core/transaction_utils.py |
| 24 | + * Active since block 833,000 (CP_P2WSH_FEAT_BLOCK_START) |
| 25 | + */ |
| 26 | +export function extractOlgaData( |
| 27 | + vout: { scriptpubkey: string, scriptpubkey_type: string }[] |
| 28 | +): Uint8Array | null { |
| 29 | + |
| 30 | + // Collect P2WSH outputs at index > 0 (index 0 is reserved for destination/value) |
| 31 | + // P2WSH scriptpubkey format: 0020 + 32 bytes = 68 hex chars total |
| 32 | + const chunks: Uint8Array[] = []; |
| 33 | + for (let i = 1; i < vout.length; i++) { |
| 34 | + const output = vout[i]; |
| 35 | + if (output.scriptpubkey_type !== 'v0_p2wsh') { |
| 36 | + continue; |
| 37 | + } |
| 38 | + // Must be exactly 68 hex chars (34 bytes: 0x00 0x20 + 32 data bytes) |
| 39 | + if (output.scriptpubkey.length !== 68) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + // Skip the 0020 prefix (4 hex chars), extract 32 raw bytes |
| 43 | + chunks.push(hexToBytes(output.scriptpubkey.substring(4))); |
| 44 | + } |
| 45 | + |
| 46 | + if (chunks.length === 0) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + const allBytes = concatUint8Arrays(chunks); |
| 51 | + |
| 52 | + // Strip trailing zeros (padding) |
| 53 | + let end = allBytes.length; |
| 54 | + while (end > 0 && allBytes[end - 1] === 0) { |
| 55 | + end--; |
| 56 | + } |
| 57 | + if (end < 2) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + const trimmed = allBytes.subarray(0, end); |
| 61 | + |
| 62 | + // First 2 bytes = big-endian file length |
| 63 | + const fileLength = bigEndianBytesToNumber(trimmed.subarray(0, 2)); |
| 64 | + if (fileLength === 0 || fileLength > trimmed.length - 2) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + // Extract the file data (skip 2-byte length prefix) |
| 69 | + const fileData = trimmed.subarray(2, 2 + fileLength); |
| 70 | + |
| 71 | + // Check for "stamp:" prefix (0x7374616d703a) -- used by SRC-20/SRC-101 OLGA |
| 72 | + // Counterparty-issued OLGA stamps do NOT have this prefix. |
| 73 | + const STAMP_PREFIX = new Uint8Array([0x73, 0x74, 0x61, 0x6d, 0x70, 0x3a]); // "stamp:" |
| 74 | + if (fileData.length > STAMP_PREFIX.length && |
| 75 | + fileData[0] === 0x73 && fileData[1] === 0x74 && fileData[2] === 0x61 && |
| 76 | + fileData[3] === 0x6d && fileData[4] === 0x70 && fileData[5] === 0x3a) { |
| 77 | + // Strip the stamp: prefix |
| 78 | + return fileData.subarray(STAMP_PREFIX.length); |
| 79 | + } |
| 80 | + |
| 81 | + // No stamp: prefix -- return raw file data (Counterparty-issued OLGA) |
| 82 | + return fileData; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Detects the MIME type from raw file bytes by checking magic numbers. |
| 87 | + * |
| 88 | + * This is the same approach used by the stampchain indexer |
| 89 | + * (enhanced_mime_detection.py) but simplified for our use case. |
| 90 | + */ |
| 91 | +export function detectMimeType(data: Uint8Array): string | null { |
| 92 | + if (data.length < 4) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + // PNG: 89 50 4e 47 0d 0a 1a 0a |
| 97 | + if (data[0] === 0x89 && data[1] === 0x50 && data[2] === 0x4e && data[3] === 0x47) { |
| 98 | + return 'image/png'; |
| 99 | + } |
| 100 | + |
| 101 | + // GIF87a or GIF89a: 47 49 46 38 (37|39) 61 |
| 102 | + if (data[0] === 0x47 && data[1] === 0x49 && data[2] === 0x46 && data[3] === 0x38) { |
| 103 | + return 'image/gif'; |
| 104 | + } |
| 105 | + |
| 106 | + // WebP: 52 49 46 46 ... 57 45 42 50 (RIFF....WEBP) |
| 107 | + if (data[0] === 0x52 && data[1] === 0x49 && data[2] === 0x46 && data[3] === 0x46 && |
| 108 | + data.length >= 12 && data[8] === 0x57 && data[9] === 0x45 && data[10] === 0x42 && data[11] === 0x50) { |
| 109 | + return 'image/webp'; |
| 110 | + } |
| 111 | + |
| 112 | + // JPEG: ff d8 ff |
| 113 | + if (data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) { |
| 114 | + return 'image/jpeg'; |
| 115 | + } |
| 116 | + |
| 117 | + // BMP: 42 4d (BM) |
| 118 | + if (data[0] === 0x42 && data[1] === 0x4d) { |
| 119 | + return 'image/bmp'; |
| 120 | + } |
| 121 | + |
| 122 | + // Text-based formats: check as UTF-8 |
| 123 | + try { |
| 124 | + const head = new TextDecoder().decode(data.subarray(0, Math.min(data.length, 256))); |
| 125 | + const trimmed = head.trimStart(); |
| 126 | + |
| 127 | + // SVG: starts with '<svg' or '<?xml' |
| 128 | + if (trimmed.startsWith('<svg') || trimmed.startsWith('<?xml')) { |
| 129 | + return 'image/svg+xml'; |
| 130 | + } |
| 131 | + |
| 132 | + // HTML: starts with '<!doctype html', '<html', '<head', '<body' |
| 133 | + const lower = trimmed.toLowerCase(); |
| 134 | + if (lower.startsWith('<!doctype html') || lower.startsWith('<html') || |
| 135 | + lower.startsWith('<head') || lower.startsWith('<body')) { |
| 136 | + return 'text/html'; |
| 137 | + } |
| 138 | + |
| 139 | + // JSON: starts with '{' or '[' |
| 140 | + if (trimmed.startsWith('{') || trimmed.startsWith('[')) { |
| 141 | + return 'application/json'; |
| 142 | + } |
| 143 | + } catch { |
| 144 | + // not valid UTF-8 |
| 145 | + } |
| 146 | + |
| 147 | + return null; |
| 148 | +} |
0 commit comments