/
compiler-operation-helpers.ts
376 lines (348 loc) · 12.1 KB
/
compiler-operation-helpers.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
import { decodeHdPrivateKey, deriveHdPath } from '../key/key.js';
import { resolveScriptIdentifier } from '../language/language.js';
import type {
AnyCompilerConfiguration,
CompilationContextBCH,
CompilationData,
CompilerConfiguration,
CompilerOperation,
CompilerOperationErrorFatal,
CompilerOperationResult,
CompilerOperationSkip,
WalletTemplateHdKey,
} from '../lib.js';
import { CompilerDefaults } from './compiler-defaults.js';
/**
* Attempt a series of compiler operations, skipping to the next operation if
* the current operation returns a {@link CompilerOperationSkip} (indicating it
* failed and can be skipped). The `finalOperation` may not be skipped, and must
* either return {@link CompilerOperationSuccess} or
* {@link CompilerOperationError}.
*
* @param operations - an array of skippable operations to try
* @param finalOperation - a final, un-skippable operation
*/
export const attemptCompilerOperations =
<CompilationContext = CompilationContextBCH>(
operations: CompilerOperation<CompilationContext, true>[],
finalOperation: CompilerOperation<CompilationContext>,
): CompilerOperation<CompilationContext> =>
(identifier, data, configuration) => {
// eslint-disable-next-line functional/no-loop-statements
for (const operation of operations) {
const result = operation(identifier, data, configuration);
if (result.status !== 'skip') return result;
}
return finalOperation(identifier, data, configuration);
};
/**
* Modify a compiler operation to verify that certain properties exist in the
* {@link CompilationData} and {@link CompilerConfiguration} before executing
* the provided operation. If the properties don't exist, an error message
* is returned.
*
* This is useful for eliminating repetitive existence checks.
*/
export const compilerOperationRequires =
<
CanBeSkipped extends boolean,
RequiredDataProperties extends keyof CompilationData<unknown>,
RequiredConfigurationProperties extends keyof CompilerConfiguration,
CompilationContext = CompilationContextBCH,
>({
/**
* If `true`, the accepted operation may return `false`, and any missing
* properties will cause the returned operation to return `false` (meaning
* the operation should be skipped)
*/
canBeSkipped,
/**
* An array of the top-level properties required in the
* {@link CompilationData}.
*/
dataProperties,
/**
* An array of the top-level properties required in the
* {@link CompilerConfiguration}
*/
configurationProperties,
/**
* The operation to run if all required properties exist
*/
operation,
}: {
canBeSkipped: CanBeSkipped;
dataProperties: RequiredDataProperties[];
configurationProperties: RequiredConfigurationProperties[];
operation: (
identifier: string,
data: CompilationData<CompilationContext> &
Required<
Pick<CompilationData<CompilationContext>, RequiredDataProperties>
>,
configuration: CompilerConfiguration<CompilationContext> &
Required<
Pick<
CompilerConfiguration<CompilationContext>,
RequiredConfigurationProperties
>
>,
) => CompilerOperationResult<CanBeSkipped>;
}): CompilerOperation<CompilationContext, CanBeSkipped> =>
// eslint-disable-next-line complexity
(identifier, data, configuration) => {
// eslint-disable-next-line functional/no-loop-statements
for (const property of configurationProperties) {
if (configuration[property] === undefined)
return (
canBeSkipped
? { status: 'skip' }
: {
error: `Cannot resolve "${identifier}" - the "${property}" property was not provided in the compiler configuration.`,
status: 'error',
}
) as CanBeSkipped extends true
? CompilerOperationSkip
: CompilerOperationErrorFatal;
}
// eslint-disable-next-line functional/no-loop-statements
for (const property of dataProperties) {
if (
(data[property] as (typeof data)[typeof property] | undefined) ===
undefined
)
return (
canBeSkipped
? { status: 'skip' }
: {
error: `Cannot resolve "${identifier}" - the "${property}" property was not provided in the compilation data.`,
status: 'error',
}
) as CanBeSkipped extends true
? CompilerOperationSkip
: CompilerOperationErrorFatal;
}
return operation(
identifier,
data as Required<
Pick<CompilationData<CompilationContext>, RequiredDataProperties>
>,
configuration as CompilerConfiguration<CompilationContext> &
Required<
Pick<
CompilerConfiguration<CompilationContext>,
RequiredConfigurationProperties
>
>,
);
};
export const compilerOperationAttemptBytecodeResolution =
compilerOperationRequires({
canBeSkipped: true,
configurationProperties: [],
dataProperties: ['bytecode'],
operation: (identifier, data) => {
const bytecode = data.bytecode[identifier];
if (bytecode !== undefined) {
return { bytecode, status: 'success' };
}
return { status: 'skip' };
},
});
// eslint-disable-next-line complexity
export const compilerOperationHelperDeriveHdPrivateNode = ({
addressIndex,
entityId,
entityHdPrivateKey,
configuration,
hdKey,
identifier,
}: {
addressIndex: number;
entityId: string;
entityHdPrivateKey: string;
configuration: {
ripemd160: NonNullable<CompilerConfiguration['ripemd160']>;
secp256k1: NonNullable<CompilerConfiguration['secp256k1']>;
sha256: NonNullable<CompilerConfiguration['sha256']>;
sha512: NonNullable<CompilerConfiguration['sha512']>;
};
hdKey: WalletTemplateHdKey;
identifier: string;
}): CompilerOperationResult => {
const addressOffset =
hdKey.addressOffset ?? CompilerDefaults.hdKeyAddressOffset;
const privateDerivationPath =
hdKey.privateDerivationPath ?? CompilerDefaults.hdKeyPrivateDerivationPath;
const i = addressIndex + addressOffset;
const validPrivatePathWithIndex = /^m(?:\/(?:[0-9]+|i)'?)*$/u;
if (!validPrivatePathWithIndex.test(privateDerivationPath)) {
return {
error: `Could not generate ${identifier} - the path "${privateDerivationPath}" is not a valid "privateDerivationPath".`,
status: 'error',
};
}
const instancePath = privateDerivationPath.replace('i', i.toString());
const masterContents = decodeHdPrivateKey(entityHdPrivateKey, configuration);
if (typeof masterContents === 'string') {
return {
error: `Could not generate ${identifier} - the HD private key provided for ${entityId} could not be decoded: ${masterContents}`,
status: 'error',
};
}
const instanceNode = deriveHdPath(
masterContents.node,
instancePath,
configuration,
);
if (typeof instanceNode === 'string') {
return {
error: `Could not generate ${identifier} - the path "${instancePath}" could not be derived for entity "${entityId}": ${instanceNode}`,
status: 'error',
};
}
return {
bytecode: instanceNode.privateKey,
status: 'success',
};
};
export const compilerOperationHelperUnknownEntity = (
identifier: string,
variableId: string,
) => ({
error: `Identifier "${identifier}" refers to an HdKey, but the "entityOwnership" for "${variableId}" is not available in this compiler configuration.`,
status: 'error' as const,
});
export const compilerOperationHelperAddressIndex = (identifier: string) => ({
error: `Identifier "${identifier}" refers to an HdKey, but "hdKeys.addressIndex" was not provided in the compilation data.`,
status: 'error' as const,
});
export const compilerOperationHelperDeriveHdKeyPrivate = ({
configuration,
hdKeys,
identifier,
}: {
configuration: {
entityOwnership: NonNullable<CompilerConfiguration['entityOwnership']>;
ripemd160: NonNullable<CompilerConfiguration['ripemd160']>;
secp256k1: NonNullable<CompilerConfiguration['secp256k1']>;
sha256: NonNullable<CompilerConfiguration['sha256']>;
sha512: NonNullable<CompilerConfiguration['sha512']>;
variables: NonNullable<CompilerConfiguration['variables']>;
};
hdKeys: NonNullable<CompilationData['hdKeys']>;
identifier: string;
}): CompilerOperationResult => {
const { addressIndex, hdPrivateKeys } = hdKeys;
const [variableId] = identifier.split('.') as [string];
const entityId = configuration.entityOwnership[variableId];
if (entityId === undefined) {
return compilerOperationHelperUnknownEntity(identifier, variableId);
}
if (addressIndex === undefined) {
return compilerOperationHelperAddressIndex(identifier);
}
const entityHdPrivateKey =
hdPrivateKeys === undefined ? undefined : hdPrivateKeys[entityId];
if (entityHdPrivateKey === undefined) {
return {
error: `Identifier "${identifier}" refers to an HdKey owned by "${entityId}", but an HD private key for this entity (or an existing signature) was not provided in the compilation data.`,
recoverable: true,
status: 'error',
};
}
/**
* Guaranteed to be an `HdKey` if this method is reached in the compiler.
*/
const hdKey = configuration.variables[variableId] as WalletTemplateHdKey;
return compilerOperationHelperDeriveHdPrivateNode({
addressIndex,
configuration,
entityHdPrivateKey,
entityId,
hdKey,
identifier,
});
};
/**
* Returns `false` if the target script ID doesn't exist in the compiler
* configuration (allows for the caller to generate the error message).
*
* If the compilation produced errors, returns a
* {@link CompilerOperationErrorFatal}.
*
* If the compilation was successful, returns the compiled bytecode as a
* `Uint8Array`.
*/
export const compilerOperationHelperCompileScript = <CompilationContext>({
targetScriptId,
data,
configuration,
}: {
targetScriptId: string;
data: CompilationData<CompilationContext>;
configuration: AnyCompilerConfiguration<CompilationContext>;
}) => {
const signingTarget = configuration.scripts[targetScriptId];
const compiledTarget = resolveScriptIdentifier({
configuration,
data,
identifier: targetScriptId,
});
if (signingTarget === undefined || compiledTarget === false) {
return false;
}
if (typeof compiledTarget === 'string') {
return {
error: compiledTarget,
status: 'error',
} as CompilerOperationErrorFatal;
}
return compiledTarget.bytecode;
};
/**
* Returns either the properly generated `coveredBytecode` or a
* {@link CompilerOperationErrorFatal}.
*/
export const compilerOperationHelperGenerateCoveredBytecode = <
CompilationContext,
>({
data,
configuration,
identifier,
sourceScriptIds,
unlockingScripts,
}: {
data: CompilationData<CompilationContext>;
configuration: AnyCompilerConfiguration<CompilationContext>;
identifier: string;
sourceScriptIds: string[];
unlockingScripts: { [unlockingScriptId: string]: string };
}): CompilerOperationErrorFatal | Uint8Array => {
const currentScriptId = sourceScriptIds[sourceScriptIds.length - 1];
if (currentScriptId === undefined) {
return {
error: `Identifier "${identifier}" requires a signing serialization, but "coveredBytecode" cannot be determined because the compiler configuration's "sourceScriptIds" is empty.`,
status: 'error',
};
}
const targetLockingScriptId = unlockingScripts[currentScriptId];
if (targetLockingScriptId === undefined) {
return {
error: `Identifier "${identifier}" requires a signing serialization, but "coveredBytecode" cannot be determined because "${currentScriptId}" is not present in the compiler configuration's "unlockingScripts".`,
status: 'error',
};
}
const result = compilerOperationHelperCompileScript({
configuration,
data,
targetScriptId: targetLockingScriptId,
});
if (result === false) {
return {
error: `Identifier "${identifier}" requires a signing serialization that covers an unknown locking script, "${targetLockingScriptId}".`,
status: 'error',
};
}
return result;
};