Skip to content

Commit f326823

Browse files
committed
Enable OP_CAT in script debugger
1 parent 78a1896 commit f326823

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/lib/utils/bitcoin/script/interpreter.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ import {
133133
bytesToHex,
134134
bytesToIntLE,
135135
bytesToUIntLE,
136+
concatBytes,
136137
intLEToBytes,
137138
uint8ArrayEqual,
138139
} from '$lib/utils/uintarray'
@@ -188,6 +189,7 @@ enum ScriptWarning {
188189
codeseparator,
189190
mixedCheckMultisigAndCheckSigAdd,
190191
nonMinimalIf,
192+
cat,
191193
}
192194

193195
const warningMessageMap: Record<ScriptWarning, string> = {
@@ -201,6 +203,7 @@ const warningMessageMap: Record<ScriptWarning, string> = {
201203
[ScriptWarning.mixedCheckMultisigAndCheckSigAdd]:
202204
'OP_CHECKMULTISIG and OP_CHECKSIGADD cannot be used in the same script',
203205
[ScriptWarning.nonMinimalIf]: 'argument of OP_IF/NOTIF is not minimal',
206+
[ScriptWarning.cat]: 'OP_CAT is only available on signet',
204207
}
205208

206209
type ScriptErrorCode = keyof typeof errorMessageMap
@@ -417,7 +420,7 @@ export const evalScript = (
417420
const opcode = reader.readByte()
418421

419422
if (
420-
opcode === OP_CAT ||
423+
// opcode === OP_CAT ||
421424
opcode === OP_SUBSTR ||
422425
opcode === OP_LEFT ||
423426
opcode === OP_RIGHT ||
@@ -823,6 +826,23 @@ export const evalScript = (
823826
}
824827
break
825828

829+
case OP_CAT:
830+
{
831+
if (stack.length < 2) {
832+
return setStackSizeError(2)
833+
}
834+
const vch1 = stacktop(-2)!
835+
const vch2 = stacktop(-1)!
836+
if (vch1.length + vch2.length > MAX_SCRIPT_ELEMENT_SIZE) {
837+
return setError(SCRIPT_ERR_PUSH_SIZE)
838+
}
839+
popstack()
840+
popstack()
841+
stack.push(concatBytes([vch1, vch2]))
842+
loopHighlight = { count: 1, color: 'success' }
843+
}
844+
break
845+
826846
case OP_SIZE:
827847
// (in -- in size)
828848
if (stack.length === 0) {

tests/script/interpreter.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
OP_ADD,
1717
OP_BOOLAND,
1818
OP_BOOLOR,
19+
OP_CAT,
1920
OP_CHECKLOCKTIMEVERIFY,
2021
OP_CHECKMULTISIG,
2122
OP_CHECKMULTISIGVERIFY,
@@ -165,8 +166,8 @@ describe('evalScript', () => {
165166

166167
test('should return error for incorrect scripts', () => {
167168
const testcases: Array<[string, string[], number]> = [
168-
// disabled opcode OP_CAT
169-
['7e', [], 16],
169+
// disabled opcode OP_SUBSTR
170+
['7f', [], 16],
170171
// incomplete push
171172
['01', [], 15],
172173
['4c', [], 15],
@@ -211,6 +212,7 @@ describe('evalScript', () => {
211212
[OP_SWAP, 2],
212213
[OP_TUCK, 2],
213214

215+
[OP_CAT, 2],
214216
[OP_SIZE, 1],
215217

216218
[OP_EQUAL, 2],

0 commit comments

Comments
 (0)