/
instruction-sets-utils.ts
484 lines (451 loc) · 16.2 KB
/
instruction-sets-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import {
binToHex,
flattenBinArray,
numberToBinUint16LE,
numberToBinUint32LE,
} from '../../format/format';
import { createCompilerCommonSynchronous } from '../../template/compiler';
import { AuthenticationProgramStateCommon } from '../vm-types';
import { AuthenticationErrorBCH, OpcodesBCH } from './bch/bch';
import { OpcodesBTC } from './btc/btc';
import {
AuthenticationInstruction,
AuthenticationInstructionPush,
ParsedAuthenticationInstruction,
ParsedAuthenticationInstructionMalformed,
ParsedAuthenticationInstructionPushMalformedLength,
ParsedAuthenticationInstructions,
} from './instruction-sets-types';
/**
* A type-guard which checks if the provided instruction is malformed.
* @param instruction - the instruction to check
*/
export const authenticationInstructionIsMalformed = <Opcodes>(
instruction: ParsedAuthenticationInstruction<Opcodes>
): instruction is ParsedAuthenticationInstructionMalformed<Opcodes> =>
'malformed' in instruction;
/**
* A type-guard which checks if the final instruction in the provided array of
* instructions is malformed. (Only the final instruction can be malformed.)
* @param instruction - the array of instructions to check
*/
export const authenticationInstructionsAreMalformed = <Opcodes>(
instructions: ParsedAuthenticationInstructions<Opcodes>
): instructions is ParsedAuthenticationInstructionMalformed<Opcodes>[] =>
instructions.length > 0 &&
authenticationInstructionIsMalformed(instructions[instructions.length - 1]);
/**
* A type-guard which confirms that the final instruction in the provided array
* is not malformed. (Only the final instruction can be malformed.)
* @param instruction - the array of instructions to check
*/
export const authenticationInstructionsAreNotMalformed = <Opcodes>(
instructions: ParsedAuthenticationInstructions<Opcodes>
): instructions is AuthenticationInstruction<Opcodes>[] =>
!authenticationInstructionsAreMalformed(instructions);
enum CommonPushOpcodes {
OP_0 = 0x00,
OP_PUSHDATA_1 = 0x4c,
OP_PUSHDATA_2 = 0x4d,
OP_PUSHDATA_4 = 0x4e,
}
const uint8Bytes = 1;
const uint16Bytes = 2;
const uint32Bytes = 4;
const readLittleEndianNumber = (
script: Uint8Array,
index: number,
length: typeof uint8Bytes | typeof uint16Bytes | typeof uint32Bytes
) => {
const view = new DataView(script.buffer, index, length);
const readAsLittleEndian = true;
return length === uint8Bytes
? view.getUint8(0)
: length === uint16Bytes
? view.getUint16(0, readAsLittleEndian)
: view.getUint32(0, readAsLittleEndian);
};
/**
* Returns the number of bytes used to indicate the length of the push in this
* operation.
* @param opcode - an opcode between 0x00 and 0x4e
*/
export const lengthBytesForPushOpcode = (opcode: number) =>
opcode < CommonPushOpcodes.OP_PUSHDATA_1
? 0
: opcode === CommonPushOpcodes.OP_PUSHDATA_1
? uint8Bytes
: opcode === CommonPushOpcodes.OP_PUSHDATA_2
? uint16Bytes
: uint32Bytes;
/**
* Parse one instruction from the provided script.
*
* Returns an object with an `instruction` referencing a
* `ParsedAuthenticationInstruction`, and a `nextIndex` indicating the next
* index from which to read. If the next index is greater than or equal to the
* length of the script, the script has been fully parsed.
*
* The final `ParsedAuthenticationInstruction` from a serialized script may be
* malformed if 1) the final operation is a push and 2) too few bytes remain for
* the push operation to complete.
*
* @param script - the script from which to read the next instruction
* @param index - the offset from which to begin reading
*/
// eslint-disable-next-line complexity
export const readAuthenticationInstruction = <Opcodes = number>(
script: Uint8Array,
index: number
): {
instruction: ParsedAuthenticationInstruction<Opcodes>;
nextIndex: number;
} => {
const opcode = script[index];
if (opcode > CommonPushOpcodes.OP_PUSHDATA_4) {
return {
instruction: {
opcode: (opcode as unknown) as Opcodes,
},
nextIndex: index + 1,
};
}
const lengthBytes = lengthBytesForPushOpcode(opcode);
if (lengthBytes !== 0 && index + lengthBytes >= script.length) {
const sliceStart = index + 1;
const sliceEnd = sliceStart + lengthBytes;
return {
instruction: {
expectedLengthBytes: lengthBytes,
length: script.slice(sliceStart, sliceEnd),
malformed: true,
opcode: (opcode as unknown) as Opcodes,
},
nextIndex: sliceEnd,
};
}
const dataBytes =
lengthBytes === 0
? opcode
: readLittleEndianNumber(script, index + 1, lengthBytes);
const dataStart = index + 1 + lengthBytes;
const dataEnd = dataStart + dataBytes;
return {
instruction: {
data: script.slice(dataStart, dataEnd),
...(dataEnd > script.length
? {
expectedDataBytes: dataEnd - dataStart,
malformed: true,
}
: undefined),
opcode: (opcode as unknown) as Opcodes,
},
nextIndex: dataEnd,
};
};
/**
* Parse authentication bytecode (`lockingBytecode` or `unlockingBytecode`)
* into `ParsedAuthenticationInstructions`. The method
* `authenticationInstructionsAreMalformed` can be used to check if these
* instructions include a malformed instruction. If not, they are valid
* `AuthenticationInstructions`.
*
* This implementation is common to most bitcoin forks, but the type parameter
* can be used to strongly type the resulting instructions. For example:
*
* ```js
* const instructions = parseAuthenticationBytecode<OpcodesBCH>(script);
* ```
*
* @param script - the serialized script to parse
*/
export const parseBytecode = <Opcodes = number>(script: Uint8Array) => {
const instructions: ParsedAuthenticationInstructions<Opcodes> = [];
// eslint-disable-next-line functional/no-let
let i = 0;
// eslint-disable-next-line functional/no-loop-statement
while (i < script.length) {
const { instruction, nextIndex } = readAuthenticationInstruction<Opcodes>(
script,
i
);
// eslint-disable-next-line functional/no-expression-statement
i = nextIndex;
// eslint-disable-next-line functional/no-expression-statement, functional/immutable-data
instructions.push(instruction);
}
return instructions;
};
/**
* OP_0 is the only single-word push. All other push instructions will
* disassemble to multiple ASM words. (OP_1-OP_16 are handled like normal
* operations.)
*/
const isMultiWordPush = (opcode: number) => opcode !== CommonPushOpcodes.OP_0;
const formatAsmPushHex = (data: Uint8Array) =>
data.length > 0 ? `0x${binToHex(data)}` : '';
const formatMissingBytesAsm = (missing: number) =>
`[missing ${missing} byte${missing === 1 ? '' : 's'}]`;
const hasMalformedLength = <Opcodes>(
instruction: ParsedAuthenticationInstructionMalformed<Opcodes>
): instruction is ParsedAuthenticationInstructionPushMalformedLength<Opcodes> =>
'length' in instruction;
const isPushData = (pushOpcode: number) =>
pushOpcode >= CommonPushOpcodes.OP_PUSHDATA_1;
/**
* Disassemble a malformed authentication instruction into a string description.
* @param opcodes - a mapping of possible opcodes to their string representation
* @param instruction - the malformed instruction to disassemble
*/
export const disassembleParsedAuthenticationInstructionMalformed = <
Opcodes = number
>(
opcodes: { readonly [opcode: number]: string },
instruction: ParsedAuthenticationInstructionMalformed<Opcodes>
): string =>
`${opcodes[(instruction.opcode as unknown) as number]} ${
hasMalformedLength(instruction)
? `${formatAsmPushHex(instruction.length)}${formatMissingBytesAsm(
instruction.expectedLengthBytes - instruction.length.length
)}`
: `${
isPushData((instruction.opcode as unknown) as number)
? `${instruction.expectedDataBytes} `
: ''
}${formatAsmPushHex(instruction.data)}${formatMissingBytesAsm(
instruction.expectedDataBytes - instruction.data.length
)}`
}`;
/**
* Disassemble a properly-formed authentication instruction into a string
* description.
* @param opcodes - a mapping of possible opcodes to their string representation
* @param instruction - the instruction to disassemble
*/
export const disassembleAuthenticationInstruction = <Opcodes = number>(
opcodes: { readonly [opcode: number]: string },
instruction: AuthenticationInstruction<Opcodes>
): string =>
`${opcodes[(instruction.opcode as unknown) as number]}${
'data' in instruction &&
isMultiWordPush((instruction.opcode as unknown) as number)
? ` ${
isPushData((instruction.opcode as unknown) as number)
? `${instruction.data.length} `
: ''
}${formatAsmPushHex(instruction.data)}`
: ''
}`;
/**
* Disassemble a single `ParsedAuthenticationInstruction` (includes potentially
* malformed instructions) into its ASM representation.
*
* @param script - the instruction to disassemble
*/
export const disassembleParsedAuthenticationInstruction = <Opcodes = number>(
opcodes: { readonly [opcode: number]: string },
instruction: ParsedAuthenticationInstruction<Opcodes>
): string =>
authenticationInstructionIsMalformed(instruction)
? disassembleParsedAuthenticationInstructionMalformed<Opcodes>(
opcodes,
instruction
)
: disassembleAuthenticationInstruction<Opcodes>(opcodes, instruction);
/**
* Disassemble an array of `ParsedAuthenticationInstructions` (including
* potentially malformed instructions) into its ASM representation.
*
* @param script - the array of instructions to disassemble
*/
export const disassembleParsedAuthenticationInstructions = <Opcodes = number>(
opcodes: { readonly [opcode: number]: string },
instructions: readonly ParsedAuthenticationInstruction<Opcodes>[]
): string =>
instructions
.map((instruction) =>
disassembleParsedAuthenticationInstruction<Opcodes>(opcodes, instruction)
)
.join(' ');
/**
* Disassemble authentication bytecode into a lossless ASM representation. (All
* push operations are represented with the same opcodes used in the bytecode,
* even when non-minimally encoded.)
*
* @param opcodes - the set to use when determining the name of opcodes, e.g. `OpcodesBCH`
* @param bytecode - the authentication bytecode to disassemble
*/
export const disassembleBytecode = <Opcode = number>(
opcodes: { readonly [opcode: number]: string },
bytecode: Uint8Array
) =>
disassembleParsedAuthenticationInstructions(
opcodes,
parseBytecode<Opcode>(bytecode)
);
/**
* Disassemble BCH authentication bytecode into its ASM representation.
* @param bytecode - the authentication bytecode to disassemble
*/
export const disassembleBytecodeBCH = (bytecode: Uint8Array) =>
disassembleParsedAuthenticationInstructions(
OpcodesBCH,
parseBytecode<OpcodesBCH>(bytecode)
);
/**
* Disassemble BTC authentication bytecode into its ASM representation.
* @param bytecode - the authentication bytecode to disassemble
*/
export const disassembleBytecodeBTC = (bytecode: Uint8Array) =>
disassembleParsedAuthenticationInstructions(
OpcodesBTC,
parseBytecode<OpcodesBTC>(bytecode)
);
/**
* Create an object where each key is an opcode identifier and each value is
* the bytecode value (`Uint8Array`) it represents.
* @param opcodes - An opcode enum, e.g. `OpcodesBCH`
*/
export const generateBytecodeMap = (opcodes: Record<string, unknown>) =>
Object.entries(opcodes)
.filter<[string, number]>(
(entry): entry is [string, number] => typeof entry[1] === 'number'
)
.reduce<{
[opcode: string]: Uint8Array;
}>(
(identifiers, pair) => ({
...identifiers,
[pair[0]]: Uint8Array.of(pair[1]),
}),
{}
);
/**
* Re-assemble a string of disassembled bytecode (see `disassembleBytecode`).
*
* @param opcodes - a mapping of opcodes to their respective Uint8Array
* representation
* @param disassembledBytecode - the disassembled bytecode to re-assemble
*/
export const assembleBytecode = <
Opcodes extends number = OpcodesBCH,
Errors = AuthenticationErrorBCH
>(
opcodes: { readonly [opcode: string]: Uint8Array },
disassembledBytecode: string
) => {
const environment = {
opcodes,
scripts: { asm: disassembledBytecode },
};
return createCompilerCommonSynchronous<
typeof environment,
AuthenticationProgramStateCommon<Opcodes, Errors>,
Opcodes,
Errors
>(environment).generateBytecode('asm', {});
};
/**
* Re-assemble a string of disassembled BCH bytecode (see
* `disassembleBytecodeBCH`).
*
* Note, this method performs automatic minimization of push instructions.
*
* @param disassembledBytecode - the disassembled BCH bytecode to re-assemble
*/
export const assembleBytecodeBCH = (disassembledBytecode: string) =>
assembleBytecode(generateBytecodeMap(OpcodesBCH), disassembledBytecode);
/**
* Re-assemble a string of disassembled BCH bytecode (see
* `disassembleBytecodeBTC`).
*
* Note, this method performs automatic minimization of push instructions.
*
* @param disassembledBytecode - the disassembled BTC bytecode to re-assemble
*/
export const assembleBytecodeBTC = (disassembledBytecode: string) =>
assembleBytecode<OpcodesBTC>(
generateBytecodeMap(OpcodesBTC),
disassembledBytecode
);
const getInstructionLengthBytes = <Opcodes>(
instruction: AuthenticationInstructionPush<Opcodes>
) => {
const opcode = (instruction.opcode as unknown) as number;
const expectedLength = lengthBytesForPushOpcode(opcode);
return expectedLength === uint8Bytes
? Uint8Array.of(instruction.data.length)
: expectedLength === uint16Bytes
? numberToBinUint16LE(instruction.data.length)
: numberToBinUint32LE(instruction.data.length);
};
/**
* Re-serialize a valid authentication instruction.
* @param instruction - the instruction to serialize
*/
export const serializeAuthenticationInstruction = <Opcodes = number>(
instruction: AuthenticationInstruction<Opcodes>
) =>
Uint8Array.from([
(instruction.opcode as unknown) as number,
...('data' in instruction
? [
...(isPushData((instruction.opcode as unknown) as number)
? getInstructionLengthBytes(instruction)
: []),
...instruction.data,
]
: []),
]);
/**
* Re-serialize a malformed authentication instruction.
* @param instruction - the malformed instruction to serialize
*/
export const serializeParsedAuthenticationInstructionMalformed = <
Opcodes = number
>(
instruction: ParsedAuthenticationInstructionMalformed<Opcodes>
) => {
const opcode = (instruction.opcode as unknown) as number;
if (hasMalformedLength(instruction)) {
return Uint8Array.from([opcode, ...instruction.length]);
}
if (isPushData(opcode)) {
return Uint8Array.from([
opcode,
...(opcode === CommonPushOpcodes.OP_PUSHDATA_1
? Uint8Array.of(instruction.expectedDataBytes)
: opcode === CommonPushOpcodes.OP_PUSHDATA_2
? numberToBinUint16LE(instruction.expectedDataBytes)
: numberToBinUint32LE(instruction.expectedDataBytes)),
...instruction.data,
]);
}
return Uint8Array.from([opcode, ...instruction.data]);
};
/**
* Re-serialize a potentially-malformed authentication instruction.
* @param instruction - the potentially-malformed instruction to serialize
*/
export const serializeParsedAuthenticationInstruction = <Opcodes = number>(
instruction: ParsedAuthenticationInstruction<Opcodes>
): Uint8Array =>
authenticationInstructionIsMalformed(instruction)
? serializeParsedAuthenticationInstructionMalformed(instruction)
: serializeAuthenticationInstruction(instruction);
/**
* Re-serialize an array of valid authentication instructions.
* @param instructions - the array of valid instructions to serialize
*/
export const serializeAuthenticationInstructions = <Opcodes = number>(
instructions: readonly AuthenticationInstruction<Opcodes>[]
) => flattenBinArray(instructions.map(serializeAuthenticationInstruction));
/**
* Re-serialize an array of potentially-malformed authentication instructions.
* @param instructions - the array of instructions to serialize
*/
export const serializeParsedAuthenticationInstructions = <Opcodes = number>(
instructions: readonly ParsedAuthenticationInstruction<Opcodes>[]
) =>
flattenBinArray(instructions.map(serializeParsedAuthenticationInstruction));