forked from bitauth/libauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
instruction-sets-types.ts
105 lines (96 loc) · 3.29 KB
/
instruction-sets-types.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
export interface AuthenticationInstructionPush<Opcodes = number> {
/**
* The data to be pushed to the stack.
*/
readonly data: Uint8Array;
/**
* The opcode used to serialize this data.
*/
readonly opcode: Opcodes;
}
export interface AuthenticationInstructionOperation<Opcodes = number> {
/**
* The opcode of this instruction's operation.
*/
readonly opcode: Opcodes;
}
enum Bytes {
Uint8 = 1,
Uint16 = 2,
Uint32 = 4,
}
/**
* A properly-formed instruction used by an `AuthenticationVirtualMachine`.
*/
export type AuthenticationInstruction<Opcodes = number> =
| AuthenticationInstructionPush<Opcodes>
| AuthenticationInstructionOperation<Opcodes>;
export type AuthenticationInstructions<
Opcodes = number
> = AuthenticationInstruction<Opcodes>[];
export interface ParsedAuthenticationInstructionPushMalformedLength<
Opcodes = number
> {
/**
* The expected number of length bytes (`length.length`) for this `PUSHDATA` operation.
*/
readonly expectedLengthBytes: Bytes.Uint8 | Bytes.Uint16 | Bytes.Uint32;
/**
* The length `Uint8Array` provided. This instruction is malformed because the length of this `Uint8Array` is shorter than the `expectedLengthBytes`.
*/
readonly length: Uint8Array;
readonly malformed: true;
readonly opcode: Opcodes;
}
export interface ParsedAuthenticationInstructionPushMalformedData<
Opcodes = number
> {
/**
* The data `Uint8Array` provided. This instruction is malformed because the length of this `Uint8Array` is shorter than the `expectedDataBytes`.
*/
readonly data: Uint8Array;
/**
* The expected number of data bytes (`data.length`) for this push operation.
*/
readonly expectedDataBytes: number;
readonly malformed: true;
readonly opcode: Opcodes;
}
export type ParsedAuthenticationInstructionMalformed<Opcodes = number> =
| ParsedAuthenticationInstructionPushMalformedLength<Opcodes>
| ParsedAuthenticationInstructionPushMalformedData<Opcodes>;
/**
* A potentially-malformed `AuthenticationInstruction`. If `malformed` is
* `true`, this could be either
* `ParsedAuthenticationInstructionPushMalformedLength` or
* `ParsedAuthenticationInstructionPushMalformedData`
*
* If the final instruction is a push operation which requires more bytes than
* are available in the remaining portion of a serialized script, that
* instruction will have a `malformed` property with a value of `true`.
* .
*/
export type ParsedAuthenticationInstruction<Opcodes = number> =
| AuthenticationInstruction<Opcodes>
| ParsedAuthenticationInstructionMalformed<Opcodes>;
/**
* An array of authentication instructions which may end with a malformed
* instruction.
*
* **Implementation note**: this type can be improved by only marking the final
* element as potentially malformed. This is waiting on:
* https://github.com/Microsoft/TypeScript/issues/1360
*
* The following type can be used when it doesn't produce the error,
* `A rest element must be last in a tuple type. [1256]`:
* ```ts
* export type ParsedAuthenticationInstructions<Opcodes = number> = [
* ...AuthenticationInstruction<Opcodes>,
* ParsedAuthenticationInstruction<Opcodes>
* ];
* ```
*/
export type ParsedAuthenticationInstructions<Opcodes = number> = (
| AuthenticationInstruction<Opcodes>
| ParsedAuthenticationInstruction<Opcodes>
)[];