-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcompile.ts
More file actions
192 lines (182 loc) · 6.02 KB
/
compile.ts
File metadata and controls
192 lines (182 loc) · 6.02 KB
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
import type {
AnyCompilerConfiguration,
AuthenticationProgramStateControlStack,
AuthenticationProgramStateMinimum,
AuthenticationProgramStateStack,
CompilationContextBCH,
CompilationContextCommon,
CompilationData,
CompilationResult,
CompilationResultSuccess,
} from '../lib.js';
import { compileScriptRaw, createEmptyRange } from './resolve.js';
/**
* Parse, resolve, and reduce the selected script using the provided `data` and
* `configuration`.
*
* Note, locktime validation only occurs if `compilationContext` is provided in
* the configuration.
*/
// eslint-disable-next-line complexity
export const compileScript = <
ProgramState extends AuthenticationProgramStateControlStack &
AuthenticationProgramStateMinimum &
AuthenticationProgramStateStack = AuthenticationProgramStateControlStack &
AuthenticationProgramStateMinimum &
AuthenticationProgramStateStack,
CompilationContext extends CompilationContextCommon = CompilationContextBCH,
>(
scriptId: string,
data: CompilationData<CompilationContext>,
configuration: AnyCompilerConfiguration<CompilationContext>,
): CompilationResult<ProgramState> => {
const locktimeDisablingSequenceNumber = 0xffffffff;
const lockTimeTypeBecomesTimestamp = 500000000;
if (data.compilationContext?.transaction.locktime !== undefined) {
if (
configuration.unlockingScriptTimeLockTypes?.[scriptId] === 'height' &&
data.compilationContext.transaction.locktime >=
lockTimeTypeBecomesTimestamp
) {
return {
errorType: 'parse',
errors: [
{
error: `The script "${scriptId}" requires a height-based locktime (less than 500,000,000), but this transaction uses a timestamp-based locktime ("${data.compilationContext.transaction.locktime}").`,
range: createEmptyRange(),
},
],
success: false,
};
}
if (
configuration.unlockingScriptTimeLockTypes?.[scriptId] === 'timestamp' &&
data.compilationContext.transaction.locktime <
lockTimeTypeBecomesTimestamp
) {
return {
errorType: 'parse',
errors: [
{
error: `The script "${scriptId}" requires a timestamp-based locktime (greater than or equal to 500,000,000), but this transaction uses a height-based locktime ("${data.compilationContext.transaction.locktime}").`,
range: createEmptyRange(),
},
],
success: false,
};
}
}
if (
data.compilationContext?.transaction.inputs[
data.compilationContext.inputIndex
]?.sequenceNumber !== undefined &&
configuration.unlockingScriptTimeLockTypes?.[scriptId] !== undefined &&
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
data.compilationContext.transaction.inputs[
data.compilationContext.inputIndex
]!.sequenceNumber === locktimeDisablingSequenceNumber
) {
return {
errorType: 'parse',
errors: [
{
error: `The script "${scriptId}" requires a locktime, but this input's sequence number is set to disable transaction locktime (0xffffffff). This will cause the OP_CHECKLOCKTIMEVERIFY operation to error when the transaction is verified. To be valid, this input must use a sequence number that does not disable locktime.`,
range: createEmptyRange(),
},
],
success: false,
};
}
const rawResult = compileScriptRaw<ProgramState, CompilationContext>({
configuration,
data,
scriptId,
});
if (!rawResult.success) {
return rawResult;
}
const unlocks = configuration.unlockingScripts?.[scriptId];
const unlockingScriptType =
unlocks === undefined
? undefined
: configuration.lockingScriptTypes?.[unlocks];
const isP2shUnlock =
unlockingScriptType === 'p2sh20' || unlockingScriptType === 'p2sh32';
const lockingScriptType = configuration.lockingScriptTypes?.[scriptId];
const isP2shLock =
lockingScriptType === 'p2sh20' || lockingScriptType === 'p2sh32';
if (isP2shLock) {
const transformedResult = compileScriptRaw<
ProgramState,
CompilationContext
>({
configuration: {
...configuration,
scripts: {
p2sh20Locking:
'OP_HASH160 <$(<lockingBytecode> OP_HASH160)> OP_EQUAL',
p2sh32Locking:
'OP_HASH256 <$(<lockingBytecode> OP_HASH256)> OP_EQUAL',
},
variables: { lockingBytecode: { type: 'AddressData' } },
},
data: { bytecode: { lockingBytecode: rawResult.bytecode } },
scriptId:
lockingScriptType === 'p2sh20' ? 'p2sh20Locking' : 'p2sh32Locking',
});
if (!transformedResult.success) {
return transformedResult;
}
return {
...rawResult,
bytecode: transformedResult.bytecode,
transformed:
lockingScriptType === 'p2sh20' ? 'p2sh20-locking' : 'p2sh32-locking',
};
}
if (isP2shUnlock) {
const lockingBytecodeResult = compileScriptRaw<
ProgramState,
CompilationContext
>({
configuration,
data,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
scriptId: unlocks!,
});
if (!lockingBytecodeResult.success) {
return lockingBytecodeResult;
}
const transformedResult = compileScriptRaw<
ProgramState,
CompilationContext
>({
configuration: {
...configuration,
scripts: {
p2shUnlocking: 'unlockingBytecode <lockingBytecode>',
},
variables: {
lockingBytecode: { type: 'AddressData' },
unlockingBytecode: { type: 'AddressData' },
},
},
data: {
bytecode: {
lockingBytecode: lockingBytecodeResult.bytecode,
unlockingBytecode: rawResult.bytecode,
},
},
scriptId: 'p2shUnlocking',
}) as CompilationResultSuccess<ProgramState>;
return {
...rawResult,
bytecode: transformedResult.bytecode,
transformed:
unlockingScriptType === 'p2sh20'
? 'p2sh20-unlocking'
: 'p2sh32-unlocking',
};
}
return rawResult;
};