@@ -105,54 +105,51 @@ export function getKnownFieldValues(fields: { tag: number; value: Uint8Array }[]
105105}
106106
107107/**
108- * Searches for the next position of the ordinal inscription mark (0063036f7264)
109- * within the raw transaction data, starting from a given position.
108+ * Envelope marker hex constants -- kept as two exact patterns so hex
109+ * pre-filters (hasInscription, parseInscriptionsWithinWitness) match
110+ * the byte sequences ord itself emits, not a shorter substring that
111+ * would widen the false-positive surface.
110112 *
111- * This function looks for a specific sequence of 6 bytes that represents the start of an ordinal inscription.
112- * If the sequence is found, the function returns the index immediately following the inscription mark.
113- * If the sequence is not found, the function returns -1, indicating no inscription mark was found.
114- *
115- * Note: This function uses a simple hardcoded approach based on the fixed length of the inscription mark.
113+ * INSCRIPTION_MARK_HEX = OP_FALSE OP_IF OP_PUSHBYTES_3 'ord'
114+ * BIP110_INSCRIPTION_MARK_HEX = OP_PUSHBYTES_3 'ord' (ord#4545)
115+ */
116+ export const INSCRIPTION_MARK_HEX = '0063036f7264' ;
117+ export const BIP110_INSCRIPTION_MARK_HEX = '036f7264' ;
118+
119+ /**
120+ * Find the next "ord" push in the raw witness. Returns the position
121+ * immediately after the marker plus which envelope shape carries it
122+ * (classic if the marker is preceded by OP_FALSE OP_IF, BIP-110
123+ * otherwise).
116124 *
117- * @returns The position immediately after the inscription mark, or -1 if not found .
125+ * @returns { pointer, isClassic } or null when no more markers .
118126 */
119- export function getNextInscriptionMark ( raw : Uint8Array , startPosition : number ) : number {
120-
121- // OP_FALSE
122- // OP_IF
123- // OP_PUSHBYTES_3: This pushes the next 3 bytes onto the stack.
124- // 0x6f, 0x72, 0x64: These bytes translate to the ASCII string "ord"
125- const inscriptionMark = new Uint8Array ( [ OP_FALSE , OP_IF , OP_PUSHBYTES_3 , 0x6f , 0x72 , 0x64 ] ) ;
126-
127- for ( let index = startPosition ; index <= raw . length - 6 ; index ++ ) {
128- if ( raw [ index ] === inscriptionMark [ 0 ] &&
129- raw [ index + 1 ] === inscriptionMark [ 1 ] &&
130- raw [ index + 2 ] === inscriptionMark [ 2 ] &&
131- raw [ index + 3 ] === inscriptionMark [ 3 ] &&
132- raw [ index + 4 ] === inscriptionMark [ 4 ] &&
133- raw [ index + 5 ] === inscriptionMark [ 5 ] ) {
134- return index + 6 ;
127+ export function getNextInscriptionMark ( raw : Uint8Array , startPosition : number ) : { pointer : number ; isClassic : boolean } | null {
128+
129+ for ( let i = startPosition ; i <= raw . length - 4 ; i ++ ) {
130+ if ( raw [ i ] === OP_PUSHBYTES_3 &&
131+ raw [ i + 1 ] === 0x6f &&
132+ raw [ i + 2 ] === 0x72 &&
133+ raw [ i + 3 ] === 0x64 ) {
134+ const isClassic = i >= 2 && raw [ i - 2 ] === OP_FALSE && raw [ i - 1 ] === OP_IF ;
135+ return { pointer : i + 4 , isClassic } ;
135136 }
136137 }
137138
138- return - 1 ;
139+ return null ;
139140}
140141
141142/**
142143 * Checks if an inscription mark is found within a witness array.
143- * The Inscription mark hex corresponds to OP_FALSE, OP_IF, OP_PUSHBYTES_3, 'o', 'r', 'd'.
144-
145- * This code can potentially return false positive matches!
144+ * Matches EITHER envelope shape via its exact hex pattern:
145+ * - classic (OP_FALSE OP_IF OP_PUSHBYTES_3 'ord')
146+ * - BIP-110 (OP_PUSHBYTES_3 'ord', ord#4545)
146147 *
147- * @param witness - Array of strings, each representing a hexadecimal encoded witness element.
148- * @returns True if an inscription mark is found, false otherwise.
148+ * This code can potentially return false positive matches!
149149 */
150150export function hasInscription ( witness : string [ ] ) : boolean {
151-
152- // OP_FALSE (0x00), OP_IF (0x63), OP_PUSHBYTES_3 (0x03), 'o', 'r', 'd' (0x6f, 0x72, 0x64)
153- const inscriptionMarkHex = '0063036f7264' ;
154-
155- return isStringInArrayOfStrings ( inscriptionMarkHex , witness ) ;
151+ return isStringInArrayOfStrings ( INSCRIPTION_MARK_HEX , witness )
152+ || isStringInArrayOfStrings ( BIP110_INSCRIPTION_MARK_HEX , witness ) ;
156153}
157154
158155/**
@@ -354,21 +351,20 @@ export function measureInscriptionSize(witness: string[]): number | null {
354351 return null ;
355352 }
356353
357- // Find the witness element that contains the inscription (the tapscript)
358- // OP_FALSE (0x00), OP_IF (0x63), OP_PUSHBYTES_3 (0x03), 'o', 'r', 'd' (0x6f, 0x72, 0x64)
359- const inscriptionMarkHex = '0063036f7264' ;
360- const element = witness . find ( e => e . includes ( inscriptionMarkHex ) ) ;
354+ // Find the witness element that contains the inscription (the tapscript).
355+ // The 4-byte "ord" push is the shared marker across classic + BIP-110.
356+ const element = witness . find ( e => e . includes ( INSCRIPTION_MARK_HEX ) ) ;
361357 if ( ! element ) {
362358 return null ;
363359 }
364360
365361 const raw = hexToBytes ( element ) ;
366362
367- // Find the start of the inscription using the inscription mark
368- const startPosition = getNextInscriptionMark ( raw , 0 ) ;
369-
370- if ( startPosition === - 1 ) {
371- return null ; // Inscription mark not found
363+ const mark = getNextInscriptionMark ( raw , 0 ) ;
364+ if ( ! mark || ! mark . isClassic ) {
365+ // Test-only helper; measuring a BIP-110 envelope would need the
366+ // drop-balance walk. Not needed today (no on-chain BIP-110 tx).
367+ return null ;
372368 }
373369
374370 // Find the position of last OP_ENDIF (0x68)
@@ -378,11 +374,10 @@ export function measureInscriptionSize(witness: string[]): number | null {
378374 return null ; // OP_ENDIF not found
379375 }
380376
381- // The size of the inscription is from the start position to the last OP_ENDIF
382- const inscriptionSize = opEndIfIndex - startPosition ;
383-
384- // Add the size of the inscription mark (6 bytes) + OP_ENDIF (1 byte)
385- return inscriptionSize + 7 ;
377+ // From the start of the classic wrapper (OP_FALSE OP_IF = 2 bytes
378+ // before the 4-byte "ord" push, so mark.pointer - 6) to one past
379+ // OP_ENDIF.
380+ return opEndIfIndex + 1 - ( mark . pointer - 6 ) ;
386381}
387382
388383/**
0 commit comments