@xo-cash/engine 0.0.1-development.14148570389 → 0.0.1-development.15965991683
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +273 -578
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +420 -905
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -15
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Crypto, buildInvitationCommitSigningPayload, convertMnemonicToSeedBytes,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { binToHex, createCompilerBch, encodeTransaction, generateRandomBytes, generateTransaction, hashTransaction, hexToBin } from "@bitauth/libauth";
|
|
2
|
+
import { Crypto, buildInvitationCommitSigningPayload, computeAccountHash, convertMnemonicToSeedBytes, verifyInvitationCommitSignature } from "@xo-cash/crypto";
|
|
3
|
+
import { COMMIT_IDENTIFIER_BYTE_LENGTH, ExtendedPublicKey, INVITATION_IDENTIFIER_BYTE_LENGTH, TemplateIdentifier } from "@xo-cash/primitives";
|
|
4
|
+
import { InvitationNotFoundInStoreError, State, StorageType, TemplateNotFoundInStoreError, UnspentOutputNotFoundInStoreError, UnspentOutputStatus, createStorageAdapter } from "@xo-cash/state";
|
|
5
|
+
import { compileCashAssemblyString, generateTemplateIdentifier, generateTemplateIdentifier as generateTemplateIdentifier$1, isCashAssemblyExpression, parseTemplate, parseTemplate as parseTemplate$1, scriptToScriptHash } from "@xo-cash/utils";
|
|
5
6
|
import { broadcastTransaction, electrumClients, electrumEvents, getChainStatus, initializeElectrumApplication, stopElectrumNetwork } from "@electrum-cash/application";
|
|
6
7
|
import { fetchScriptHashUnspentTransactionOutputs, subscribeToScriptHashUpdates } from "@electrum-cash/protocol";
|
|
7
8
|
import { electrumServers } from "@electrum-cash/servers";
|
|
8
|
-
import {
|
|
9
|
+
import { XOTemplateLockingTypes } from "@xo-cash/types";
|
|
9
10
|
|
|
10
11
|
//#region source/errors.ts
|
|
11
12
|
/**
|
|
@@ -25,14 +26,6 @@ var TemplateOutputNotFoundError = class extends Error {
|
|
|
25
26
|
}
|
|
26
27
|
};
|
|
27
28
|
/**
|
|
28
|
-
* Error thrown when a template is not found.
|
|
29
|
-
*/
|
|
30
|
-
var TemplateNotFoundError = class extends Error {
|
|
31
|
-
constructor(templateIdentifier) {
|
|
32
|
-
super(`Template not found, templateIdentifier "${templateIdentifier}"`);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
29
|
* Error thrown when a locking script is not found in the template.
|
|
37
30
|
*/
|
|
38
31
|
var TemplateLockingScriptNotFoundError = class extends Error {
|
|
@@ -58,29 +51,27 @@ var UnlockingScriptNotFoundError = class extends Error {
|
|
|
58
51
|
}
|
|
59
52
|
};
|
|
60
53
|
/**
|
|
61
|
-
* Error thrown when a
|
|
54
|
+
* Error thrown when a transaction is not found in the template.
|
|
62
55
|
*/
|
|
63
|
-
var
|
|
64
|
-
constructor(
|
|
65
|
-
|
|
66
|
-
super(scriptIdentifier ? `${defaultMessage}: scriptIdentifier "${scriptIdentifier}"` : defaultMessage);
|
|
56
|
+
var TemplateTransactionNotFoundError = class extends Error {
|
|
57
|
+
constructor(transactionIdentifier) {
|
|
58
|
+
super(`Template transaction not found, transactionIdentifier "${transactionIdentifier}"`);
|
|
67
59
|
}
|
|
68
60
|
};
|
|
69
61
|
/**
|
|
70
|
-
* Error thrown when a
|
|
62
|
+
* Error thrown when a role is not found on a locking script.
|
|
71
63
|
*/
|
|
72
|
-
var
|
|
73
|
-
constructor(
|
|
74
|
-
|
|
75
|
-
super(templateIdentifier ? `${defaultMessage}: templateIdentifier "${templateIdentifier}"` : defaultMessage);
|
|
64
|
+
var TemplateLockingScriptRoleNotFoundError = class extends Error {
|
|
65
|
+
constructor(lockingScriptIdentifier, roleIdentifier) {
|
|
66
|
+
super(`Template locking script role not found, lockingScriptIdentifier "${lockingScriptIdentifier}", roleIdentifier "${roleIdentifier}"`);
|
|
76
67
|
}
|
|
77
68
|
};
|
|
78
69
|
/**
|
|
79
|
-
* Error thrown when
|
|
70
|
+
* Error thrown when an output does not have a locking script.
|
|
80
71
|
*/
|
|
81
|
-
var
|
|
82
|
-
constructor(
|
|
83
|
-
super(`Template
|
|
72
|
+
var TemplateLockingScriptInOutputNotFoundError = class extends Error {
|
|
73
|
+
constructor(outputIdentifier) {
|
|
74
|
+
super(`Template locking script in output not found, outputIdentifier "${outputIdentifier}"`);
|
|
84
75
|
}
|
|
85
76
|
};
|
|
86
77
|
/**
|
|
@@ -141,33 +132,6 @@ var InvalidCommitSignaturesError = class extends Error {
|
|
|
141
132
|
}
|
|
142
133
|
};
|
|
143
134
|
/**
|
|
144
|
-
* Error thrown when an invitation is not found in the store.
|
|
145
|
-
*/
|
|
146
|
-
var InvitationNotFoundInStoreError = class extends Error {
|
|
147
|
-
constructor(invitationIdentifier) {
|
|
148
|
-
const defaultMessage = "Invitation not found in store";
|
|
149
|
-
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
/**
|
|
153
|
-
* Error thrown when an invitation has already been accepted.
|
|
154
|
-
*/
|
|
155
|
-
var InvitationAlreadyAcceptedError = class extends Error {
|
|
156
|
-
constructor(invitationIdentifier) {
|
|
157
|
-
const defaultMessage = "Invitation already accepted";
|
|
158
|
-
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
/**
|
|
162
|
-
* Error thrown when an invitation was created in the future.
|
|
163
|
-
*/
|
|
164
|
-
var InvitationCreatedInFutureError = class extends Error {
|
|
165
|
-
constructor(invitationIdentifier) {
|
|
166
|
-
const defaultMessage = "Invitation created in the future";
|
|
167
|
-
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
/**
|
|
171
135
|
* Error thrown when an invitation has expired.
|
|
172
136
|
*/
|
|
173
137
|
var InvitationExpiredError = class extends Error {
|
|
@@ -177,24 +141,6 @@ var InvitationExpiredError = class extends Error {
|
|
|
177
141
|
}
|
|
178
142
|
};
|
|
179
143
|
/**
|
|
180
|
-
* Error thrown when an invitation has not been accepted.
|
|
181
|
-
*/
|
|
182
|
-
var InvitationNotAcceptedError = class extends Error {
|
|
183
|
-
constructor(invitationIdentifier) {
|
|
184
|
-
const defaultMessage = "Invitation not accepted";
|
|
185
|
-
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
/**
|
|
189
|
-
* Error thrown when an invitation is invalid.
|
|
190
|
-
*/
|
|
191
|
-
var InvalidInvitationError = class extends Error {
|
|
192
|
-
constructor(invitationIdentifier) {
|
|
193
|
-
const defaultMessage = "Invalid invitation";
|
|
194
|
-
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
/**
|
|
198
144
|
* Error thrown when a merged commit is not found.
|
|
199
145
|
*/
|
|
200
146
|
var MergedCommitNotFoundError = class extends Error {
|
|
@@ -212,18 +158,27 @@ var ScriptHashDataNotFoundError = class extends Error {
|
|
|
212
158
|
}
|
|
213
159
|
};
|
|
214
160
|
/**
|
|
161
|
+
* Error thrown when the entity identifier is not found in the invitation.
|
|
162
|
+
*/
|
|
163
|
+
var EntityIdentifierNotFoundInInvitationError = class extends Error {
|
|
164
|
+
constructor(invitationIdentifier) {
|
|
165
|
+
const defaultMessage = "Entity identifier not found in invitation";
|
|
166
|
+
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
215
170
|
* Error thrown when the root commit for an entity cannot be found in the invitation.
|
|
216
171
|
*/
|
|
217
|
-
var
|
|
218
|
-
constructor(
|
|
172
|
+
var RootCommitNotFoundInInvitationError = class extends Error {
|
|
173
|
+
constructor(invitationIdentifier) {
|
|
219
174
|
const defaultMessage = "Root commit not found in invitation";
|
|
220
|
-
super(
|
|
175
|
+
super(invitationIdentifier ? `${defaultMessage}: invitationIdentifier "${invitationIdentifier}"` : defaultMessage);
|
|
221
176
|
}
|
|
222
177
|
};
|
|
223
178
|
/**
|
|
224
179
|
* Error thrown when a parent commit is not found.
|
|
225
180
|
*/
|
|
226
|
-
var
|
|
181
|
+
var ParentCommitNotFoundInInvitationError = class extends Error {
|
|
227
182
|
constructor(parentCommitIdentifier, commitIdentifier) {
|
|
228
183
|
super(`Parent commit not found: ${parentCommitIdentifier} (referenced by commit ${commitIdentifier})`);
|
|
229
184
|
}
|
|
@@ -296,171 +251,6 @@ var InvitationTemplateMismatchError = class extends Error {
|
|
|
296
251
|
}
|
|
297
252
|
};
|
|
298
253
|
/**
|
|
299
|
-
* Error thrown when an unspent output is not found.
|
|
300
|
-
*/
|
|
301
|
-
var UnspentOutputNotFoundError = class extends Error {
|
|
302
|
-
constructor(outpointIdentifier) {
|
|
303
|
-
const defaultMessage = "Unspent output not found";
|
|
304
|
-
super(outpointIdentifier ? `${defaultMessage}: outpointIdentifier "${outpointIdentifier}"` : defaultMessage);
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
/**
|
|
308
|
-
* Error thrown when a role is invalid.
|
|
309
|
-
*/
|
|
310
|
-
var InvalidRoleError = class extends Error {
|
|
311
|
-
constructor(roleIdentifier) {
|
|
312
|
-
const defaultMessage = "Invalid role";
|
|
313
|
-
super(roleIdentifier ? `${defaultMessage}: roleIdentifier "${roleIdentifier}"` : defaultMessage);
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
/**
|
|
317
|
-
* Error thrown when there are multiple roles for the starting action.
|
|
318
|
-
*/
|
|
319
|
-
var MultipleRolesForStartingActionError = class extends Error {
|
|
320
|
-
constructor(actionIdentifier) {
|
|
321
|
-
const defaultMessage = "Multiple roles for starting action";
|
|
322
|
-
super(actionIdentifier ? `${defaultMessage}: actionIdentifier "${actionIdentifier}"` : defaultMessage);
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
/**
|
|
326
|
-
* Error thrown when no role is specified and multiple roles are available to be accepted.
|
|
327
|
-
*/
|
|
328
|
-
var NoRoleSpecifiedAndMultipleRolesAvailableToBeAcceptedError = class extends Error {
|
|
329
|
-
constructor() {
|
|
330
|
-
super("No role specified and multiple roles available to be accepted");
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
/**
|
|
334
|
-
* Error thrown when a role has already been accepted by another entity.
|
|
335
|
-
*/
|
|
336
|
-
var RoleAlreadyAcceptedByAnotherEntityError = class extends Error {
|
|
337
|
-
constructor(roleIdentifier) {
|
|
338
|
-
const defaultMessage = "Role already accepted by another entity";
|
|
339
|
-
super(roleIdentifier ? `${defaultMessage}: roleIdentifier "${roleIdentifier}"` : defaultMessage);
|
|
340
|
-
}
|
|
341
|
-
};
|
|
342
|
-
/**
|
|
343
|
-
* Error thrown when a role cannot start an action.
|
|
344
|
-
*/
|
|
345
|
-
var RoleCannotStartActionError = class extends Error {
|
|
346
|
-
constructor(roleIdentifier, actionIdentifier) {
|
|
347
|
-
const defaultMessage = "Role cannot start action";
|
|
348
|
-
if (roleIdentifier && actionIdentifier) super(`${defaultMessage}: roleIdentifier "${roleIdentifier}", actionIdentifier "${actionIdentifier}"`);
|
|
349
|
-
else if (roleIdentifier) super(`${defaultMessage}: roleIdentifier "${roleIdentifier}"`);
|
|
350
|
-
else if (actionIdentifier) super(`${defaultMessage}: actionIdentifier "${actionIdentifier}"`);
|
|
351
|
-
else super(defaultMessage);
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
/**
|
|
355
|
-
* Error thrown when a role is not available to be accepted.
|
|
356
|
-
*/
|
|
357
|
-
var RoleNotAvailableToBeAcceptedError = class extends Error {
|
|
358
|
-
constructor(roleIdentifier) {
|
|
359
|
-
const defaultMessage = "Role not available to be accepted";
|
|
360
|
-
super(roleIdentifier ? `${defaultMessage}: roleIdentifier "${roleIdentifier}"` : defaultMessage);
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
/**
|
|
364
|
-
* Error thrown when a role is not defined for an action.
|
|
365
|
-
*/
|
|
366
|
-
var RoleNotDefinedForActionError = class extends Error {
|
|
367
|
-
constructor(roleIdentifier, actionIdentifier) {
|
|
368
|
-
const defaultMessage = "Role not defined for action";
|
|
369
|
-
if (roleIdentifier && actionIdentifier) super(`${defaultMessage}: roleIdentifier "${roleIdentifier}", actionIdentifier "${actionIdentifier}"`);
|
|
370
|
-
else if (roleIdentifier) super(`${defaultMessage}: roleIdentifier "${roleIdentifier}"`);
|
|
371
|
-
else if (actionIdentifier) super(`${defaultMessage}: actionIdentifier "${actionIdentifier}"`);
|
|
372
|
-
else super(defaultMessage);
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
/**
|
|
376
|
-
* Error thrown when a role is not found.
|
|
377
|
-
*/
|
|
378
|
-
var TemplateLockingScriptRoleNotFoundError = class extends Error {
|
|
379
|
-
constructor(lockingScriptIdentifier, roleIdentifier) {
|
|
380
|
-
super(`Template locking script role not found, lockingScriptIdentifier "${lockingScriptIdentifier}", roleIdentifier "${roleIdentifier}"`);
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
/**
|
|
384
|
-
* Error thrown when a data type is invalid.
|
|
385
|
-
*/
|
|
386
|
-
var InvalidDataTypeError = class extends Error {
|
|
387
|
-
constructor() {
|
|
388
|
-
super("Invalid data type");
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
/**
|
|
392
|
-
* Error thrown when a variable type is invalid.
|
|
393
|
-
*/
|
|
394
|
-
var InvalidVariableTypeError = class extends Error {
|
|
395
|
-
constructor(variableKey, expectedType, actualType) {
|
|
396
|
-
super(`Invalid variable type: variableKey "${variableKey}", expected ${expectedType}, got ${actualType}`);
|
|
397
|
-
}
|
|
398
|
-
};
|
|
399
|
-
/**
|
|
400
|
-
* Error thrown when a required variable is missing.
|
|
401
|
-
*/
|
|
402
|
-
var MissingRequiredVariableError = class extends Error {
|
|
403
|
-
constructor(variableNames) {
|
|
404
|
-
const defaultMessage = "Missing required variable";
|
|
405
|
-
if (variableNames && variableNames.length > 0) super(`${defaultMessage}: variableNames [${variableNames.join(", ")}]`);
|
|
406
|
-
else super(defaultMessage);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
/**
|
|
410
|
-
* Error thrown when an unknown variable is provided.
|
|
411
|
-
*/
|
|
412
|
-
var UnknownVariableError = class extends Error {
|
|
413
|
-
constructor(variableKey) {
|
|
414
|
-
const defaultMessage = "Unknown variable provided";
|
|
415
|
-
super(variableKey ? `${defaultMessage}: variableKey "${variableKey}"` : defaultMessage);
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
/**
|
|
419
|
-
* Error thrown when a variable is required but not provided.
|
|
420
|
-
*/
|
|
421
|
-
var VariableRequiredError = class extends Error {
|
|
422
|
-
constructor(variableKey) {
|
|
423
|
-
const defaultMessage = "Variable required";
|
|
424
|
-
super(variableKey ? `${defaultMessage}: variableKey "${variableKey}"` : defaultMessage);
|
|
425
|
-
}
|
|
426
|
-
};
|
|
427
|
-
/**
|
|
428
|
-
* Error thrown when a number variable value is not a valid integer for CashAssembly compilation.
|
|
429
|
-
*/
|
|
430
|
-
var CashAssemblyVariableNotIntegerError = class extends Error {
|
|
431
|
-
constructor(variableName, value) {
|
|
432
|
-
super(`CashAssembly variable value is not a valid integer: variableName "${variableName}", got ${value}`);
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
/**
|
|
436
|
-
* Error thrown when variable validation fails.
|
|
437
|
-
*/
|
|
438
|
-
var VariableValidationFailedError = class extends Error {
|
|
439
|
-
constructor(variableNames) {
|
|
440
|
-
const defaultMessage = "Variable validation failed";
|
|
441
|
-
if (variableNames && variableNames.length > 0) super(`${defaultMessage}: variableNames [${variableNames.join(", ")}]`);
|
|
442
|
-
else super(defaultMessage);
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
/**
|
|
446
|
-
* Error thrown when an action is invalid for an invitation.
|
|
447
|
-
*/
|
|
448
|
-
var InvalidActionError = class extends Error {
|
|
449
|
-
constructor(actionIdentifier) {
|
|
450
|
-
const defaultMessage = "Invalid action for invitation";
|
|
451
|
-
super(actionIdentifier ? `${defaultMessage}: actionIdentifier "${actionIdentifier}"` : defaultMessage);
|
|
452
|
-
}
|
|
453
|
-
};
|
|
454
|
-
/**
|
|
455
|
-
* Error thrown when cash assembly compilation fails.
|
|
456
|
-
*/
|
|
457
|
-
var CashAssemblyCompilationFailedError = class extends Error {
|
|
458
|
-
constructor(message) {
|
|
459
|
-
const defaultMessage = "Cash assembly compilation failed";
|
|
460
|
-
super(message ? `${defaultMessage}: ${message}` : defaultMessage);
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
/**
|
|
464
254
|
* Error thrown when locking bytecode generation fails.
|
|
465
255
|
*/
|
|
466
256
|
var LockingBytecodeGenerationFailedError = class extends Error {
|
|
@@ -485,30 +275,6 @@ var TransactionGenerationFailedError = class extends Error {
|
|
|
485
275
|
}
|
|
486
276
|
};
|
|
487
277
|
/**
|
|
488
|
-
* Error thrown when the created by identifier is already set.
|
|
489
|
-
*/
|
|
490
|
-
var CreatedByIdentifierAlreadySetError = class extends Error {
|
|
491
|
-
constructor() {
|
|
492
|
-
super("Created by identifier already set");
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
/**
|
|
496
|
-
* Error thrown when the created by identifier signature is already set.
|
|
497
|
-
*/
|
|
498
|
-
var CreatedByIdentifierSignatureAlreadySetError = class extends Error {
|
|
499
|
-
constructor() {
|
|
500
|
-
super("Created by identifier signature already set");
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
/**
|
|
504
|
-
* Error thrown when data is not found in the template.
|
|
505
|
-
*/
|
|
506
|
-
var DataNotFoundError = class extends Error {
|
|
507
|
-
constructor() {
|
|
508
|
-
super("Data not found in template");
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
/**
|
|
512
278
|
* Error thrown when input is invalid.
|
|
513
279
|
*/
|
|
514
280
|
var InvalidInputError = class extends Error {
|
|
@@ -517,60 +283,6 @@ var InvalidInputError = class extends Error {
|
|
|
517
283
|
}
|
|
518
284
|
};
|
|
519
285
|
/**
|
|
520
|
-
* Error thrown when required data is missing.
|
|
521
|
-
*/
|
|
522
|
-
var MissingRequiredDataError = class extends Error {
|
|
523
|
-
constructor() {
|
|
524
|
-
super("Missing required data");
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
/**
|
|
528
|
-
* Error thrown when a required field is missing.
|
|
529
|
-
*/
|
|
530
|
-
var MissingRequiredFieldError = class extends Error {
|
|
531
|
-
constructor(fieldName) {
|
|
532
|
-
const defaultMessage = "Missing required field";
|
|
533
|
-
super(fieldName ? `${defaultMessage}: fieldName "${fieldName}"` : defaultMessage);
|
|
534
|
-
}
|
|
535
|
-
};
|
|
536
|
-
/**
|
|
537
|
-
* Error thrown when a required role for an entity is missing.
|
|
538
|
-
*/
|
|
539
|
-
var MissingRequiredRoleForEntityError = class extends Error {
|
|
540
|
-
constructor(roleIdentifier, entityIdentifier) {
|
|
541
|
-
const defaultMessage = "Missing required role for entity";
|
|
542
|
-
if (roleIdentifier && entityIdentifier) super(`${defaultMessage}: roleIdentifier "${roleIdentifier}", entityIdentifier "${entityIdentifier}"`);
|
|
543
|
-
else if (roleIdentifier) super(`${defaultMessage}: roleIdentifier "${roleIdentifier}"`);
|
|
544
|
-
else if (entityIdentifier) super(`${defaultMessage}: entityIdentifier "${entityIdentifier}"`);
|
|
545
|
-
else super(defaultMessage);
|
|
546
|
-
}
|
|
547
|
-
};
|
|
548
|
-
/**
|
|
549
|
-
* Error thrown when a required signature by an entity is missing.
|
|
550
|
-
*/
|
|
551
|
-
var MissingRequiredSignatureByEntityError = class extends Error {
|
|
552
|
-
constructor(entityIdentifier) {
|
|
553
|
-
const defaultMessage = "Missing required signature by entity";
|
|
554
|
-
super(entityIdentifier ? `${defaultMessage}: entityIdentifier "${entityIdentifier}"` : defaultMessage);
|
|
555
|
-
}
|
|
556
|
-
};
|
|
557
|
-
/**
|
|
558
|
-
* Error thrown when resource reservation fails.
|
|
559
|
-
*/
|
|
560
|
-
var ReserveResourcesFailedError = class extends Error {
|
|
561
|
-
constructor() {
|
|
562
|
-
super("Failed to reserve resources");
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
/**
|
|
566
|
-
* Error thrown when signature verification fails.
|
|
567
|
-
*/
|
|
568
|
-
var SignatureVerificationFailedError = class extends Error {
|
|
569
|
-
constructor() {
|
|
570
|
-
super("Signature verification failed");
|
|
571
|
-
}
|
|
572
|
-
};
|
|
573
|
-
/**
|
|
574
286
|
* Error thrown when a variable definition is invalid.
|
|
575
287
|
*/
|
|
576
288
|
var InvalidVariableDefinitionError = class extends Error {
|
|
@@ -579,14 +291,6 @@ var InvalidVariableDefinitionError = class extends Error {
|
|
|
579
291
|
}
|
|
580
292
|
};
|
|
581
293
|
/**
|
|
582
|
-
* Error thrown when an output does not have a locking script.
|
|
583
|
-
*/
|
|
584
|
-
var TemplateLockingScriptInOutputNotFoundError = class extends Error {
|
|
585
|
-
constructor(outputIdentifier) {
|
|
586
|
-
super(`Template locking script in output not found, outputIdentifier "${outputIdentifier}"`);
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
/**
|
|
590
294
|
* Error thrown when a locking script has an invalid type.
|
|
591
295
|
*/
|
|
592
296
|
var InvalidLockingScriptTypeError = class extends Error {
|
|
@@ -603,165 +307,37 @@ var InvalidLockingTypeError = class extends Error {
|
|
|
603
307
|
}
|
|
604
308
|
};
|
|
605
309
|
/**
|
|
606
|
-
* Error thrown when
|
|
310
|
+
* Error thrown when an electrum client is not found.
|
|
607
311
|
*/
|
|
608
|
-
var
|
|
609
|
-
constructor(
|
|
610
|
-
super(
|
|
312
|
+
var ElectrumClientNotFoundError = class extends Error {
|
|
313
|
+
constructor() {
|
|
314
|
+
super("No electrum client found");
|
|
611
315
|
}
|
|
612
316
|
};
|
|
613
317
|
/**
|
|
614
|
-
* Error thrown when
|
|
318
|
+
* Error thrown when fallback locking parameters are missing for locking bytecode generation.
|
|
615
319
|
*/
|
|
616
|
-
var
|
|
617
|
-
constructor(
|
|
618
|
-
super(
|
|
320
|
+
var FallbackLockingParametersMissingError = class extends Error {
|
|
321
|
+
constructor() {
|
|
322
|
+
super("Fallback locking parameters are missing for locking bytecode generation. Call Engine.updateFallbackLockingParameters(...) when other lockingbytecode generation policies are unavailable.");
|
|
619
323
|
}
|
|
620
324
|
};
|
|
621
325
|
/**
|
|
622
|
-
* Error thrown when
|
|
326
|
+
* Error thrown when a derivative index is required for a p2pkh locking type.
|
|
623
327
|
*/
|
|
624
|
-
var
|
|
328
|
+
var P2PKHDerivativeIndexMissingError = class extends Error {
|
|
625
329
|
constructor() {
|
|
626
|
-
super("
|
|
330
|
+
super("Derivative index is required for p2pkh locking type");
|
|
627
331
|
}
|
|
628
332
|
};
|
|
629
333
|
|
|
630
|
-
//#endregion
|
|
631
|
-
//#region source/defaults.ts
|
|
632
|
-
/**
|
|
633
|
-
* When an entity opts to participate in an invitation, it reserves resources (e.g., UTXOs)
|
|
634
|
-
* that would otherwise be available to other invitations. To prevent resources from remaining
|
|
635
|
-
* locked indefinitely, each reservation requires an expiration time, after which the resources
|
|
636
|
-
* can be released.
|
|
637
|
-
*
|
|
638
|
-
* By default, the expiration time is set to 1 hour.
|
|
639
|
-
*/
|
|
640
|
-
const DEFAULT_COMMIT_EXPIRATION_IN_MILLISECONDS = 3600 * 1e3;
|
|
641
|
-
/**
|
|
642
|
-
* The default storage backend is IndexedDB.
|
|
643
|
-
*/
|
|
644
|
-
const DEFAULT_STORAGE_TYPE = StorageType.INDEXEDDB;
|
|
645
|
-
/**
|
|
646
|
-
* The default sequence number is 0, which disables relative locktime on the input.
|
|
647
|
-
*/
|
|
648
|
-
const DEFAULT_SEQUENCE_NUMBER = 0;
|
|
649
|
-
/**
|
|
650
|
-
* The default locktime is 0, which means the transaction is valid in any block and has no absolute locktime constraint.
|
|
651
|
-
*/
|
|
652
|
-
const DEFAULT_LOCKTIME = 0;
|
|
653
|
-
/**
|
|
654
|
-
* The default transaction version is 2.
|
|
655
|
-
*/
|
|
656
|
-
const DEFAULT_VERSION = 2;
|
|
657
|
-
/**
|
|
658
|
-
* An empty unlocking bytecode used as a placeholder for inputs (unlockingBytecode) within an invitation.
|
|
659
|
-
* This placeholder is used when the entities responsible for signing those inputs
|
|
660
|
-
* have not yet provided their signatures for the resulting transaction.
|
|
661
|
-
*
|
|
662
|
-
* Using a placeholder unlocking bytecode enables the engine to construct transaction structures for the
|
|
663
|
-
* libauth compiler when working on template actions, even before all participant signatures are available.
|
|
664
|
-
* This is especially helpful when invitation commits are merged, as it allows the transaction to be built and it's
|
|
665
|
-
* parts can be validated before all unlocking scripts have been provided.
|
|
666
|
-
*/
|
|
667
|
-
const PLACEHOLDER_UNLOCKING_BYTECODE = new Uint8Array(0);
|
|
668
|
-
/**
|
|
669
|
-
* The default electrum application identifier.
|
|
670
|
-
*/
|
|
671
|
-
const DEFAULT_ELECTRUM_APPLICATION_IDENTIFIER = "xo";
|
|
672
|
-
/**
|
|
673
|
-
* Detects whether a string is a pure CashAssembly expression.
|
|
674
|
-
*
|
|
675
|
-
* CashAssembly expressions look like `$(<variable>)` or `$(<a> <b>)`. This pattern checks
|
|
676
|
-
* that the entire string is one such expression and nothing else. It will not match if
|
|
677
|
-
* there is other text surrounding the expression.
|
|
678
|
-
*
|
|
679
|
-
* For example:
|
|
680
|
-
* `$(<fee>)` matches (a full expression)
|
|
681
|
-
* `OP_DUP $(<fee>)` does not match (extra text before it)
|
|
682
|
-
* `$()` does not match (empty expression)
|
|
683
|
-
*/
|
|
684
|
-
const CASHASSEMBLY_EXPRESSION_PATTERN = /^\$\([^)]+\)$/;
|
|
685
|
-
/**
|
|
686
|
-
* Finds all CashAssembly evaluations embedded in a larger string.
|
|
687
|
-
*
|
|
688
|
-
* An evaluation looks like `$(...)`, for example `$(<fee>)` or `$(<a> <b>)`. This pattern
|
|
689
|
-
* locates every occurrence in the input and returns them all (global flag `g`).
|
|
690
|
-
* Empty evaluations `$()` are intentionally excluded because they reference no variables.
|
|
691
|
-
*
|
|
692
|
-
* For example, scanning `"OP_DUP <$(<pubkeyHash>)> OP_HASH160 $(<fee>)"` would return
|
|
693
|
-
* `['$(<pubkeyHash>)', '$(<fee>)']`.
|
|
694
|
-
*/
|
|
695
|
-
const CASHASSEMBLY_EVALUATION_PATTERN = /\$\([^)]+\)/g;
|
|
696
|
-
/**
|
|
697
|
-
* Extracts variable names from angle-bracket references inside a CashAssembly evaluation.
|
|
698
|
-
*
|
|
699
|
-
* Inside an evaluation like `$(<pubkeyHash> <fee>)`, variables are referenced as `<name>`.
|
|
700
|
-
* This pattern captures the name between the brackets. The global flag `g` allows iterating
|
|
701
|
-
* over every variable reference in a single evaluation string.
|
|
702
|
-
*
|
|
703
|
-
* For example, running this against `$(<pubkeyHash> <fee>)` would return the variable names
|
|
704
|
-
* `["pubkeyHash", "fee"]`.
|
|
705
|
-
*/
|
|
706
|
-
const CASHASSEMBLY_VARIABLE_PATTERN = /<([^>]+)>/g;
|
|
707
|
-
/**
|
|
708
|
-
* Identifies CashAssembly literal tokens that appear inside angle-bracket push statements.
|
|
709
|
-
*
|
|
710
|
-
* Inside an evaluation, not everything between `<` and `>` is a variable name. Literals are
|
|
711
|
-
* also valid push contents: numeric literals (e.g. `<0>`, `<32>`), hex literals (`<0x02>`),
|
|
712
|
-
* binary literals (`<0b1010>`), and string literals (`<"minting">`, `<'hello'>`). This pattern
|
|
713
|
-
* matches any captured token that starts with a digit or a quote character.
|
|
714
|
-
*/
|
|
715
|
-
const CASHASSEMBLY_LITERAL_TOKEN_PATTERN = /^[0-9"']/;
|
|
716
|
-
|
|
717
334
|
//#endregion
|
|
718
335
|
//#region source/utils/libauth-utils.ts
|
|
719
336
|
/**
|
|
720
|
-
* Generates bytecode for a specific CashAssembly evaluation using given variable values and a prepared compiler.
|
|
721
|
-
* Returns libauth result or error string.
|
|
722
|
-
*
|
|
723
|
-
* @param compiler - The libauth compiler.
|
|
724
|
-
* @param evaluation - The specific evaluation string to compile.
|
|
725
|
-
* @param variables - A record mapping variable names to their values.
|
|
726
|
-
* @returns Bytecode result object on success, otherwise an error string.
|
|
727
|
-
*/
|
|
728
|
-
const generateCashAssemblyBytecode = (compiler, evaluation, variables) => {
|
|
729
|
-
const variableNames = extractVariablesFromEvaluations([evaluation]);
|
|
730
|
-
const missingVariables = variableNames.filter((name) => !Object.hasOwn(variables, name));
|
|
731
|
-
if (missingVariables.length > 0) throw new MissingRequiredVariableError(missingVariables);
|
|
732
|
-
const bytecode = {};
|
|
733
|
-
for (const variableName of variableNames) {
|
|
734
|
-
const value = variables[variableName];
|
|
735
|
-
if (!(value instanceof Uint8Array)) throw new InvalidVariableTypeError(variableName, "Uint8Array", typeof value);
|
|
736
|
-
bytecode[variableName] = value;
|
|
737
|
-
}
|
|
738
|
-
const compiledBytecode = compiler.generateBytecode({
|
|
739
|
-
data: { bytecode },
|
|
740
|
-
scriptId: evaluation
|
|
741
|
-
});
|
|
742
|
-
if (!compiledBytecode.success) throw new CashAssemblyCompilationFailedError("errors" in compiledBytecode ? JSON.stringify(compiledBytecode.errors) : JSON.stringify(compiledBytecode));
|
|
743
|
-
return compiledBytecode.bytecode;
|
|
744
|
-
};
|
|
745
|
-
/**
|
|
746
|
-
* Prepares a compiler for the provided CashAssembly evaluations, setting required variables as 'WalletData'.
|
|
747
|
-
*
|
|
748
|
-
* @param evaluations - Array of evaluation strings (e.g., ['$(<var1>)', '$(<var2> <var3>)'])
|
|
749
|
-
* @returns A Libauth compiler instance for use with these evaluations.
|
|
750
|
-
*/
|
|
751
|
-
const compileCashAssemblyEvaluations = (evaluations) => {
|
|
752
|
-
const scripts = {};
|
|
753
|
-
for (const evaluation of evaluations) scripts[evaluation] = evaluation;
|
|
754
|
-
const variableNames = extractVariablesFromEvaluations(evaluations);
|
|
755
|
-
return createCompilerBch({
|
|
756
|
-
scripts,
|
|
757
|
-
variables: Object.fromEntries(variableNames.map((variableName) => [variableName, { type: "WalletData" }]))
|
|
758
|
-
});
|
|
759
|
-
};
|
|
760
|
-
/**
|
|
761
337
|
* Creates a map of variable identifiers to role identifiers for the libauth compiler configuration.
|
|
762
338
|
*
|
|
763
|
-
* @param template - The template used to gather the variable identifiers.
|
|
764
|
-
* @param role - The role identifier to assign to the variables.
|
|
339
|
+
* @param {XOTemplate} template - The template used to gather the variable identifiers.
|
|
340
|
+
* @param {XOTemplateRoleIdentifier} role - The role identifier to assign to the variables.
|
|
765
341
|
* @returns {Record<string, string>} A map of variable identifiers to role identifiers.
|
|
766
342
|
*/
|
|
767
343
|
const entityOwnershipForLibauthCompiler = (template, role) => {
|
|
@@ -773,7 +349,7 @@ const entityOwnershipForLibauthCompiler = (template, role) => {
|
|
|
773
349
|
/**
|
|
774
350
|
* Creates a map of script identifiers to script definitions for the libauth compiler configuration.
|
|
775
351
|
*
|
|
776
|
-
* @param template - The template used to gather the scripts.
|
|
352
|
+
* @param {XOTemplate} template - The template used to gather the scripts.
|
|
777
353
|
* @returns {CompilerConfiguration['scripts']} A map of script identifiers to script definitions.
|
|
778
354
|
*/
|
|
779
355
|
const scriptsForLibauthCompiler = (template) => {
|
|
@@ -785,8 +361,8 @@ const scriptsForLibauthCompiler = (template) => {
|
|
|
785
361
|
/**
|
|
786
362
|
* Converts template variables into libauth variables with types.
|
|
787
363
|
*
|
|
788
|
-
* @param template - The template to assign types to the variables.
|
|
789
|
-
* @returns CompilerConfiguration['variables']
|
|
364
|
+
* @param {XOTemplate} template - The template to assign types to the variables.
|
|
365
|
+
* @returns {CompilerConfiguration['variables']} Variables mapped for the libauth compiler.
|
|
790
366
|
*/
|
|
791
367
|
const assignTypeToVariablesForLibauthCompiler = (template) => {
|
|
792
368
|
const variablesWithTypes = {};
|
|
@@ -795,7 +371,6 @@ const assignTypeToVariablesForLibauthCompiler = (template) => {
|
|
|
795
371
|
if (!variableDefinition || typeof variableDefinition !== "object") throw new InvalidVariableDefinitionError(variableIdentifier);
|
|
796
372
|
let variableType;
|
|
797
373
|
if ("hint" in variableDefinition) if (variableDefinition.hint === "private_key") variableType = "Key";
|
|
798
|
-
else if (variableDefinition.hint === "script") variableType = "AddressData";
|
|
799
374
|
else variableType = "WalletData";
|
|
800
375
|
else variableType = "WalletData";
|
|
801
376
|
variablesWithTypes[variableIdentifier] = {
|
|
@@ -816,8 +391,8 @@ const XO_LOCKING_TYPE_TO_LIBAUTH_MAP = {
|
|
|
816
391
|
/**
|
|
817
392
|
* Converts an XO template locking type to the corresponding libauth compiler locking script type.
|
|
818
393
|
*
|
|
819
|
-
* @param lockingType - The XO template locking type to convert.
|
|
820
|
-
* @returns The corresponding libauth compiler locking script type
|
|
394
|
+
* @param {XOTemplateLockingType} lockingType - The XO template locking type to convert.
|
|
395
|
+
* @returns {NonNullable<CompilerConfiguration['lockingScriptTypes']>[string]} The corresponding libauth compiler locking script type.
|
|
821
396
|
*/
|
|
822
397
|
const convertXOLockingTypeToLibauth = (lockingType) => {
|
|
823
398
|
return XO_LOCKING_TYPE_TO_LIBAUTH_MAP[lockingType];
|
|
@@ -825,17 +400,12 @@ const convertXOLockingTypeToLibauth = (lockingType) => {
|
|
|
825
400
|
/**
|
|
826
401
|
* Creates the libauth compatible compiler data for a unspent output.
|
|
827
402
|
*
|
|
828
|
-
* @param template - The template used to generate the compiler data for.
|
|
829
|
-
* @param unspentOutput - The unspent output to create the compiler data for.
|
|
830
|
-
* @
|
|
831
|
-
* @returns {Promise<{
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
* valueSatoshis: bigint;
|
|
835
|
-
* }>} The compiler data for the unspent output.
|
|
836
|
-
*/
|
|
837
|
-
const createLibauthCompilerDataFromUnspentOutput = async (template, unspentOutput) => {
|
|
838
|
-
const { outputIdentifier } = unspentOutput;
|
|
403
|
+
* @param {Template} template - The template used to generate the compiler data for.
|
|
404
|
+
* @param {UnspentOutputData} unspentOutput - The unspent output to create the compiler data for.
|
|
405
|
+
* @param {XOTemplateOutputIdentifier} outputIdentifier - The template output identifier for the unspent output.
|
|
406
|
+
* @returns {Promise<{ compiler: CompilerBch; script: string; valueSatoshis: bigint; }>} The compiler data for the unspent output.
|
|
407
|
+
*/
|
|
408
|
+
const createLibauthCompilerDataFromUnspentOutput = async (template, unspentOutput, outputIdentifier) => {
|
|
839
409
|
const output = template.getOutputOrThrow(outputIdentifier);
|
|
840
410
|
const lockingScriptForOutput = template.getLockingScriptForOutputOrThrow(outputIdentifier);
|
|
841
411
|
if (lockingScriptForOutput.lockingBytecode === void 0) throw new TemplateLockingScriptInOutputNotFoundError(outputIdentifier);
|
|
@@ -870,151 +440,6 @@ const createLibauthCompilerDataFromUnspentOutput = async (template, unspentOutpu
|
|
|
870
440
|
};
|
|
871
441
|
};
|
|
872
442
|
|
|
873
|
-
//#endregion
|
|
874
|
-
//#region source/utils/cashassembly.ts
|
|
875
|
-
/**
|
|
876
|
-
* Utilities for parsing, extracting, and compiling CashAssembly expressions.
|
|
877
|
-
*
|
|
878
|
-
* CashAssembly is the scripting language used by Bitauth templates to describe Bitcoin Cash
|
|
879
|
-
* locking and unlocking scripts.
|
|
880
|
-
*
|
|
881
|
-
* ## Syntax
|
|
882
|
-
*
|
|
883
|
-
* `<expression>` — a push statement. Compiles the contents and pushes the result onto the VM stack.
|
|
884
|
-
* `<someKey.public_key>` pushes the 33-byte compressed public key. `<1>` pushes the integer 1.
|
|
885
|
-
*
|
|
886
|
-
* `$(<expression>)` — an evaluation. Runs the inner script in the VM and inserts the top stack
|
|
887
|
-
* item as VM bytecode.
|
|
888
|
-
* `$(<someKey.public_key> OP_HASH160)` inserts the HASH160 of the public key.
|
|
889
|
-
*
|
|
890
|
-
* `<$(<expression>)>` — a push of an evaluation result. Combines the two above: evaluates first,
|
|
891
|
-
* then pushes. Example: P2PKH locking script:
|
|
892
|
-
* `OP_DUP OP_HASH160 <$(<someKey.public_key> OP_HASH160)> OP_EQUALVERIFY OP_CHECKSIG`.
|
|
893
|
-
*
|
|
894
|
-
* `variableId.operation` — a variable with a compiler-resolved operation. `someKey.public_key`
|
|
895
|
-
* produces the public key bytes. `someKey.schnorr_signature.all_outputs` produces a Schnorr
|
|
896
|
-
* signature.
|
|
897
|
-
*
|
|
898
|
-
* Opcodes (`OP_DUP`, `OP_HASH160`, etc.) are inserted as their bytecode equivalent directly.
|
|
899
|
-
*
|
|
900
|
-
* ## Name resolution priority
|
|
901
|
-
*
|
|
902
|
-
* When the compiler encounters an identifier it resolves it in this order:
|
|
903
|
-
* 1. Opcode — always wins. Naming a variable or script `OP_ADD` will not shadow it.
|
|
904
|
-
* 2. Variable — shadows scripts of the same name.
|
|
905
|
-
* 3. Script — the script's bytecode.
|
|
906
|
-
*
|
|
907
|
-
* NOTE: In future development of the XO, there will be XO_CODE taking the highest priority.
|
|
908
|
-
*
|
|
909
|
-
* @see playground/examples/cashassembly-evaluations.js for runnable examples covering all syntax forms,
|
|
910
|
-
* VM evaluation, and name resolution priority.
|
|
911
|
-
*/
|
|
912
|
-
/**
|
|
913
|
-
* Checks if the expression is a CashAssembly expression.
|
|
914
|
-
*
|
|
915
|
-
* @param expression - The expression to check.
|
|
916
|
-
* @returns True if the expression is a CashAssembly expression, false otherwise.
|
|
917
|
-
*/
|
|
918
|
-
const isCashAssemblyExpression = (expression) => {
|
|
919
|
-
return typeof expression === "string" && CASHASSEMBLY_EXPRESSION_PATTERN.test(expression);
|
|
920
|
-
};
|
|
921
|
-
/**
|
|
922
|
-
* Extracts all CashAssembly evaluations (i.e., substrings like $(...)) from the input text.
|
|
923
|
-
*
|
|
924
|
-
* @param text - The input string to scan for CashAssembly evaluations.
|
|
925
|
-
* @returns An array of evaluation strings found in the input.
|
|
926
|
-
*
|
|
927
|
-
* @example
|
|
928
|
-
* extractCashAssemblyEvaluations("OP_DUP <$(<foo>)> OP_HASH160 $(<bar>)");
|
|
929
|
-
* // returns ['$(<foo>)', '$(<bar>)']
|
|
930
|
-
*/
|
|
931
|
-
const extractCashAssemblyEvaluations = (text) => {
|
|
932
|
-
return text.match(CASHASSEMBLY_EVALUATION_PATTERN) ?? [];
|
|
933
|
-
};
|
|
934
|
-
/**
|
|
935
|
-
* Extracts unique variable identifiers enclosed in angle brackets from each evaluation string.
|
|
936
|
-
*
|
|
937
|
-
* CashAssembly literal tokens such as hex bytes, numbers, and quoted strings are excluded.
|
|
938
|
-
* For example, `<0x02>` and `<"minting">` are not returned.
|
|
939
|
-
*
|
|
940
|
-
* @param evaluations - An array of evaluation strings from which to extract variable names.
|
|
941
|
-
* @returns An array of variable names
|
|
942
|
-
*/
|
|
943
|
-
const extractVariablesFromEvaluations = (evaluations) => {
|
|
944
|
-
const uniqueVariables = /* @__PURE__ */ new Set();
|
|
945
|
-
for (const evaluation of evaluations) for (const match of evaluation.matchAll(CASHASSEMBLY_VARIABLE_PATTERN)) {
|
|
946
|
-
const extractedIdentifier = match[1];
|
|
947
|
-
if (CASHASSEMBLY_LITERAL_TOKEN_PATTERN.test(extractedIdentifier)) continue;
|
|
948
|
-
uniqueVariables.add(extractedIdentifier);
|
|
949
|
-
}
|
|
950
|
-
return [...uniqueVariables];
|
|
951
|
-
};
|
|
952
|
-
/**
|
|
953
|
-
* Converts a variable value to its byte representation.
|
|
954
|
-
*
|
|
955
|
-
* @param value - The variable value to convert.
|
|
956
|
-
* @param variableName - The name of the variable, used in error messages.
|
|
957
|
-
* @returns The byte representation of the value.
|
|
958
|
-
* @throws {CashAssemblyVariableNotIntegerError} If a number value is not a valid integer.
|
|
959
|
-
*/
|
|
960
|
-
const convertVariableToBytes = (value, variableName) => {
|
|
961
|
-
if (value instanceof Uint8Array) return value;
|
|
962
|
-
if (typeof value === "bigint") return bigIntToVmNumber(value);
|
|
963
|
-
if (typeof value === "number") {
|
|
964
|
-
if (!Number.isInteger(value)) throw new CashAssemblyVariableNotIntegerError(variableName, value);
|
|
965
|
-
return bigIntToVmNumber(BigInt(value));
|
|
966
|
-
}
|
|
967
|
-
if (typeof value === "boolean") return new Uint8Array(value ? [1] : []);
|
|
968
|
-
return utf8ToBin(value);
|
|
969
|
-
};
|
|
970
|
-
/**
|
|
971
|
-
* Decodes compiled CashAssembly evaluation bytes into a string representation.
|
|
972
|
-
*
|
|
973
|
-
* 'evaluationDecodeMode' determines how the evaluation bytes are interpreted and presented.
|
|
974
|
-
* Choose based on what the evaluation is expected to produce:
|
|
975
|
-
* `bigint` for numeric values like satoshis, `utf8` for text labels, `hex` for binary data such
|
|
976
|
-
* as hashes, and `boolean` to represent boolean values.
|
|
977
|
-
*
|
|
978
|
-
* @param compiledResult - The compiled evaluation bytecode.
|
|
979
|
-
* @param evaluationDecodeMode - The decode mode used to convert bytes to text.
|
|
980
|
-
* @returns The decoded value as a string suitable for inline replacement.
|
|
981
|
-
*/
|
|
982
|
-
const decodeCompiledCashAssemblyEvaluation = (compiledResult, evaluationDecodeMode = "utf8") => {
|
|
983
|
-
if (evaluationDecodeMode === "uint8array") return String(compiledResult);
|
|
984
|
-
if (evaluationDecodeMode === "boolean") return compiledResult.length === 0 ? "false" : "true";
|
|
985
|
-
if (evaluationDecodeMode === "hex") return binToHex(compiledResult);
|
|
986
|
-
if (evaluationDecodeMode === "bigint") {
|
|
987
|
-
const vmNumberResult = vmNumberToBigInt(compiledResult);
|
|
988
|
-
if (typeof vmNumberResult === "bigint") return vmNumberResult.toString();
|
|
989
|
-
}
|
|
990
|
-
return binToUtf8(compiledResult);
|
|
991
|
-
};
|
|
992
|
-
/**
|
|
993
|
-
* Compiles all CashAssembly evaluations in a text string and replaces each evaluation
|
|
994
|
-
* with a decoded string representation.
|
|
995
|
-
*
|
|
996
|
-
* @param cashAssemblyText - The string with CashAssembly evaluations.
|
|
997
|
-
* @param variables - Object mapping variable names to values for compilation.
|
|
998
|
-
* @param evaluationDecodeMode - The decode mode used after each evaluation is compiled.
|
|
999
|
-
* @returns Compiled text with all evaluations replaced by decoded string values.
|
|
1000
|
-
* @throws VariableValidationFailedError when a required variable is not present in the variables map.
|
|
1001
|
-
*/
|
|
1002
|
-
const compileCashAssemblyString = (parameters) => {
|
|
1003
|
-
const { cashAssemblyText, variables, evaluationDecodeMode = "utf8" } = parameters;
|
|
1004
|
-
let compiledText = cashAssemblyText;
|
|
1005
|
-
const evaluations = extractCashAssemblyEvaluations(cashAssemblyText);
|
|
1006
|
-
for (const evaluation of evaluations) {
|
|
1007
|
-
const variableNames = extractVariablesFromEvaluations([evaluation]);
|
|
1008
|
-
const missingVariables = variableNames.filter((variableName) => !Object.hasOwn(variables, variableName));
|
|
1009
|
-
if (missingVariables.length > 0) throw new VariableValidationFailedError(missingVariables);
|
|
1010
|
-
const convertedVariables = {};
|
|
1011
|
-
for (const variableName of variableNames) convertedVariables[variableName] = convertVariableToBytes(variables[variableName], variableName);
|
|
1012
|
-
const decodedResult = decodeCompiledCashAssemblyEvaluation(generateCashAssemblyBytecode(compileCashAssemblyEvaluations([evaluation]), evaluation, convertedVariables), evaluationDecodeMode);
|
|
1013
|
-
compiledText = compiledText.replace(evaluation, decodedResult);
|
|
1014
|
-
}
|
|
1015
|
-
return compiledText;
|
|
1016
|
-
};
|
|
1017
|
-
|
|
1018
443
|
//#endregion
|
|
1019
444
|
//#region source/utils/store-utils.ts
|
|
1020
445
|
/**
|
|
@@ -1027,10 +452,11 @@ const compileCashAssemblyString = (parameters) => {
|
|
|
1027
452
|
* 4. Adds any new unspent outputs found on the blockchain but missing from the state to the state.
|
|
1028
453
|
* 5. Removes any unspent outputs present in the state but no longer in the blockchain.
|
|
1029
454
|
*
|
|
1030
|
-
* @param state - The State instance to use for data operations.
|
|
1031
|
-
* @param scriptHash - The script hash for which to update the stored unspent outputs.
|
|
1032
|
-
* @param
|
|
1033
|
-
*
|
|
455
|
+
* @param {State} state - The State instance to use for data operations.
|
|
456
|
+
* @param {string} scriptHash - The script hash for which to update the stored unspent outputs.
|
|
457
|
+
* @param {(scriptHash: string) => Promise<ScriptHashListUnspentResponse>} fetchUnspentOutputsCallback
|
|
458
|
+
* Callback to fetch unspent outputs from the blockchain.
|
|
459
|
+
* @throws {ScriptHashDataNotFoundError} If the script hash is not found in the state.
|
|
1034
460
|
*/
|
|
1035
461
|
const updateUnspentOutputsForScriptHashInStore = async (state, scriptHash, fetchUnspentOutputsCallback) => {
|
|
1036
462
|
const scriptHashData = await state.getScriptHashData(scriptHash);
|
|
@@ -1057,7 +483,6 @@ const updateUnspentOutputsForScriptHashInStore = async (state, scriptHash, fetch
|
|
|
1057
483
|
selectable: true,
|
|
1058
484
|
privacy: false,
|
|
1059
485
|
templateIdentifier: scriptHashData.templateIdentifier,
|
|
1060
|
-
outputIdentifier: scriptHashData.outputIdentifier,
|
|
1061
486
|
outpointIndex: unspentOutput.tx_pos,
|
|
1062
487
|
outpointTransactionHash: unspentOutput.tx_hash,
|
|
1063
488
|
minedAtHeight: unspentOutput.height,
|
|
@@ -1081,18 +506,18 @@ var BlockchainMonitor = class {
|
|
|
1081
506
|
/**
|
|
1082
507
|
* The state instance for data persistence.
|
|
1083
508
|
*/
|
|
1084
|
-
state;
|
|
509
|
+
#state;
|
|
1085
510
|
/**
|
|
1086
511
|
* Flag indicating whether event listeners have been initialized.
|
|
1087
512
|
*/
|
|
1088
|
-
eventListenersInitialized = false;
|
|
513
|
+
#eventListenersInitialized = false;
|
|
1089
514
|
/**
|
|
1090
515
|
* Creates a new instance of BlockchainMonitor.
|
|
1091
516
|
*
|
|
1092
517
|
* @param {State} state - The State instance for data persistence.
|
|
1093
518
|
*/
|
|
1094
519
|
constructor(state) {
|
|
1095
|
-
this
|
|
520
|
+
this.#state = state;
|
|
1096
521
|
}
|
|
1097
522
|
/**
|
|
1098
523
|
* Initializes event listeners for electrum application events.
|
|
@@ -1104,7 +529,7 @@ var BlockchainMonitor = class {
|
|
|
1104
529
|
* @returns {void}
|
|
1105
530
|
*/
|
|
1106
531
|
initializeEventListeners() {
|
|
1107
|
-
if (this
|
|
532
|
+
if (this.#eventListenersInitialized) return;
|
|
1108
533
|
electrumEvents.on("SystemFailure", (_error) => {
|
|
1109
534
|
this.stop().catch(() => {});
|
|
1110
535
|
});
|
|
@@ -1128,7 +553,7 @@ var BlockchainMonitor = class {
|
|
|
1128
553
|
electrumEvents.on("ScriptHashUpdate", async (data) => {
|
|
1129
554
|
await this.onScriptHashUpdate(data);
|
|
1130
555
|
});
|
|
1131
|
-
this
|
|
556
|
+
this.#eventListenersInitialized = true;
|
|
1132
557
|
}
|
|
1133
558
|
/**
|
|
1134
559
|
* Stops the blockchain monitor and releases resources.
|
|
@@ -1136,7 +561,7 @@ var BlockchainMonitor = class {
|
|
|
1136
561
|
*/
|
|
1137
562
|
async stop() {
|
|
1138
563
|
await stopElectrumNetwork();
|
|
1139
|
-
this
|
|
564
|
+
this.#eventListenersInitialized = false;
|
|
1140
565
|
}
|
|
1141
566
|
/**
|
|
1142
567
|
* Handles the creation of a new unspent output by storing it in the state.
|
|
@@ -1145,7 +570,7 @@ var BlockchainMonitor = class {
|
|
|
1145
570
|
* @returns {Promise<void>} Resolves when the unspent output data is stored.
|
|
1146
571
|
*/
|
|
1147
572
|
async handleUnspentOutputCreated(data) {
|
|
1148
|
-
await this
|
|
573
|
+
await this.#state.storeUnspentOutputData(data);
|
|
1149
574
|
}
|
|
1150
575
|
/**
|
|
1151
576
|
* Handles script hash update notifications from the electrum protocol.
|
|
@@ -1156,31 +581,74 @@ var BlockchainMonitor = class {
|
|
|
1156
581
|
*/
|
|
1157
582
|
async onScriptHashUpdate(data) {
|
|
1158
583
|
const { scriptHash } = data;
|
|
1159
|
-
const scriptHashDataList = await this
|
|
584
|
+
const scriptHashDataList = await this.#state.listScriptHashData({ scriptHash });
|
|
1160
585
|
const [firstElectrumClient] = electrumClients.values();
|
|
1161
|
-
for (const scriptHashData of scriptHashDataList) await updateUnspentOutputsForScriptHashInStore(this
|
|
586
|
+
for (const scriptHashData of scriptHashDataList) await updateUnspentOutputsForScriptHashInStore(this.#state, scriptHashData.scriptHash, async () => {
|
|
1162
587
|
return fetchScriptHashUnspentTransactionOutputs(firstElectrumClient, scriptHashData.scriptHash);
|
|
1163
588
|
});
|
|
1164
589
|
}
|
|
1165
590
|
};
|
|
1166
591
|
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region source/defaults.ts
|
|
594
|
+
/**
|
|
595
|
+
* When an entity opts to participate in an invitation, it reserves resources (e.g., UTXOs)
|
|
596
|
+
* that would otherwise be available to other invitations. To prevent resources from remaining
|
|
597
|
+
* locked indefinitely, each reservation requires an expiration time, after which the resources
|
|
598
|
+
* can be released.
|
|
599
|
+
*
|
|
600
|
+
* By default, the expiration time is set to 1 hour.
|
|
601
|
+
*/
|
|
602
|
+
const DEFAULT_COMMIT_EXPIRATION_IN_MILLISECONDS = 3600 * 1e3;
|
|
603
|
+
/**
|
|
604
|
+
* The default storage backend is IndexedDB.
|
|
605
|
+
*/
|
|
606
|
+
const DEFAULT_STORAGE_TYPE = StorageType.INDEXEDDB;
|
|
607
|
+
/**
|
|
608
|
+
* The default sequence number is 0, which disables relative locktime on the input.
|
|
609
|
+
*/
|
|
610
|
+
const DEFAULT_SEQUENCE_NUMBER = 0;
|
|
611
|
+
/**
|
|
612
|
+
* The default locktime is 0, which means the transaction is valid in any block and has no absolute locktime constraint.
|
|
613
|
+
*/
|
|
614
|
+
const DEFAULT_LOCKTIME = 0;
|
|
615
|
+
/**
|
|
616
|
+
* The default transaction version is 2.
|
|
617
|
+
*/
|
|
618
|
+
const DEFAULT_VERSION = 2;
|
|
619
|
+
/**
|
|
620
|
+
* An empty unlocking bytecode used as a placeholder for inputs (unlockingBytecode) within an invitation.
|
|
621
|
+
* This placeholder is used when the entities responsible for signing those inputs
|
|
622
|
+
* have not yet provided their signatures for the resulting transaction.
|
|
623
|
+
*
|
|
624
|
+
* Using a placeholder unlocking bytecode enables the engine to construct transaction structures for the
|
|
625
|
+
* libauth compiler when working on template actions, even before all participant signatures are available.
|
|
626
|
+
* This is especially helpful when invitation commits are merged, as it allows the transaction to be built and it's
|
|
627
|
+
* parts can be validated before all unlocking scripts have been provided.
|
|
628
|
+
*/
|
|
629
|
+
const PLACEHOLDER_UNLOCKING_BYTECODE = new Uint8Array(0);
|
|
630
|
+
/**
|
|
631
|
+
* The default electrum application identifier.
|
|
632
|
+
*/
|
|
633
|
+
const DEFAULT_ELECTRUM_APPLICATION_IDENTIFIER = "xo";
|
|
634
|
+
|
|
1167
635
|
//#endregion
|
|
1168
636
|
//#region source/invitation/commit.ts
|
|
1169
637
|
/**
|
|
1170
|
-
* Generates a random
|
|
638
|
+
* Generates a random commit identifier.
|
|
1171
639
|
*
|
|
1172
|
-
* @returns {
|
|
640
|
+
* @returns {XOInvitationCommitIdentifier} The generated commit identifier as a hex string.
|
|
1173
641
|
*/
|
|
1174
642
|
const generateCommitIdentifier = () => {
|
|
1175
|
-
return binToHex(generateRandomBytes(
|
|
643
|
+
return binToHex(generateRandomBytes(COMMIT_IDENTIFIER_BYTE_LENGTH));
|
|
1176
644
|
};
|
|
1177
645
|
/**
|
|
1178
646
|
* Finds the root commit identifier for a given entity in an invitation. The root commit is
|
|
1179
647
|
* the first commit the entity added to the invitation.
|
|
1180
648
|
*
|
|
1181
|
-
* @param {XOInvitation} invitation The invitation whose commits are searched.
|
|
1182
|
-
* @param {
|
|
1183
|
-
* @returns {
|
|
649
|
+
* @param {XOInvitation} invitation - The invitation whose commits are searched.
|
|
650
|
+
* @param {XOInvitationEntityIdentifier} entityIdentifier - The entity identifier to search for among the invitation commits.
|
|
651
|
+
* @returns {XOInvitationCommitIdentifier | undefined} The root commit identifier if the entity has a commit in the invitation, otherwise undefined.
|
|
1184
652
|
*/
|
|
1185
653
|
const findRootCommitIdentifierInInvitation = (invitation, entityIdentifier) => {
|
|
1186
654
|
for (const commit of invitation.commits) if (commit.entityIdentifier === entityIdentifier) return commit.commitIdentifier;
|
|
@@ -1212,26 +680,27 @@ var InvitationBuilder = class InvitationBuilder {
|
|
|
1212
680
|
* The timestamp when the invitation is created.
|
|
1213
681
|
*
|
|
1214
682
|
*/
|
|
1215
|
-
createdAtTimestamp;
|
|
683
|
+
#createdAtTimestamp;
|
|
1216
684
|
/**
|
|
1217
685
|
* The commits in the invitation.
|
|
1218
686
|
*/
|
|
1219
|
-
commits;
|
|
687
|
+
#commits;
|
|
1220
688
|
/**
|
|
1221
689
|
* Create a new InvitationBuilder.
|
|
1222
|
-
* @param invitation Context for invitation creation.
|
|
690
|
+
* @param {InvitationCreationParameters} invitation - Context for invitation creation.
|
|
1223
691
|
*/
|
|
1224
692
|
constructor(invitation) {
|
|
1225
693
|
const { templateIdentifier, actionIdentifier } = invitation;
|
|
1226
694
|
this.templateIdentifier = templateIdentifier;
|
|
1227
695
|
this.actionIdentifier = actionIdentifier;
|
|
1228
|
-
this
|
|
1229
|
-
this.invitationIdentifier = binToHex(generateRandomBytes(
|
|
1230
|
-
this
|
|
696
|
+
this.#commits = [];
|
|
697
|
+
this.invitationIdentifier = binToHex(generateRandomBytes(INVITATION_IDENTIFIER_BYTE_LENGTH));
|
|
698
|
+
this.#createdAtTimestamp = Date.now();
|
|
1231
699
|
}
|
|
1232
700
|
/**
|
|
1233
701
|
* Create a builder from an existing invitation.
|
|
1234
|
-
* @param invitation An existing
|
|
702
|
+
* @param {XOInvitation} invitation - An existing invitation.
|
|
703
|
+
* @returns {InvitationBuilder} A builder initialized from the invitation.
|
|
1235
704
|
*/
|
|
1236
705
|
static fromInvitation(invitation) {
|
|
1237
706
|
const builder = new InvitationBuilder({
|
|
@@ -1239,27 +708,28 @@ var InvitationBuilder = class InvitationBuilder {
|
|
|
1239
708
|
actionIdentifier: invitation.actionIdentifier
|
|
1240
709
|
});
|
|
1241
710
|
builder.invitationIdentifier = invitation.invitationIdentifier;
|
|
1242
|
-
builder
|
|
1243
|
-
builder
|
|
711
|
+
builder.#createdAtTimestamp = invitation.createdAtTimestamp;
|
|
712
|
+
builder.#commits = [...invitation.commits];
|
|
1244
713
|
return builder;
|
|
1245
714
|
}
|
|
1246
715
|
/**
|
|
1247
|
-
* Build the
|
|
1248
|
-
* @returns The built
|
|
716
|
+
* Build the invitation object to be shared.
|
|
717
|
+
* @returns {XOInvitation} The built invitation.
|
|
1249
718
|
*/
|
|
1250
719
|
build() {
|
|
1251
720
|
return {
|
|
1252
721
|
invitationIdentifier: this.invitationIdentifier,
|
|
1253
722
|
templateIdentifier: this.templateIdentifier,
|
|
1254
723
|
actionIdentifier: this.actionIdentifier,
|
|
1255
|
-
createdAtTimestamp: this
|
|
1256
|
-
commits: this
|
|
724
|
+
createdAtTimestamp: this.#createdAtTimestamp,
|
|
725
|
+
commits: this.#commits
|
|
1257
726
|
};
|
|
1258
727
|
}
|
|
1259
728
|
/**
|
|
1260
|
-
* Fully validates a given
|
|
729
|
+
* Fully validates a given invitation.
|
|
1261
730
|
*
|
|
1262
|
-
* @param template - The template to validate against.
|
|
731
|
+
* @param {XOTemplate} template - The template to validate against.
|
|
732
|
+
* @returns {this} The builder instance after validation.
|
|
1263
733
|
* @throws {InvitationExpiredError} If the invitation has expired.
|
|
1264
734
|
* @throws {InvalidEntityIdentifierError} If the entity identifiers are invalid.
|
|
1265
735
|
* @throws {InvalidRequirementsError} If the requirements are invalid.
|
|
@@ -1278,47 +748,50 @@ var InvitationBuilder = class InvitationBuilder {
|
|
|
1278
748
|
}
|
|
1279
749
|
/**
|
|
1280
750
|
* Add a commit to the invitation.
|
|
1281
|
-
* @param commit The commit to add.
|
|
1282
|
-
* @returns The builder instance.
|
|
751
|
+
* @param {XOInvitationCommit} commit - The commit to add.
|
|
752
|
+
* @returns {this} The builder instance.
|
|
753
|
+
* @throws {CommitNotSignedError} If the commit has no signature.
|
|
754
|
+
* @throws {CommitEntityIdentifierNotFoundError} If the commit has no entity identifier.
|
|
1283
755
|
*/
|
|
1284
756
|
addCommit(commit) {
|
|
1285
757
|
if (!commit.signature) throw new CommitNotSignedError(commit.commitIdentifier);
|
|
1286
758
|
if (!commit.entityIdentifier) throw new CommitEntityIdentifierNotFoundError(commit.commitIdentifier);
|
|
1287
|
-
this
|
|
759
|
+
this.#commits.push(commit);
|
|
1288
760
|
return this;
|
|
1289
761
|
}
|
|
1290
762
|
/**
|
|
1291
|
-
* Returns the
|
|
1292
|
-
* @returns The
|
|
763
|
+
* Returns the commit identifier of the last commit in the invitation.
|
|
764
|
+
* @returns {XOInvitationCommitIdentifier | undefined} The last commit identifier, or undefined if there are no commits.
|
|
1293
765
|
*/
|
|
1294
766
|
getLastCommitIdentifier() {
|
|
1295
|
-
return this
|
|
767
|
+
return this.#commits[this.#commits.length - 1]?.commitIdentifier;
|
|
1296
768
|
}
|
|
1297
769
|
/**
|
|
1298
|
-
* Returns the last
|
|
1299
|
-
* @returns The last
|
|
770
|
+
* Returns the last commit in the invitation.
|
|
771
|
+
* @returns {XOInvitationCommit | undefined} The last commit, or undefined if there are no commits.
|
|
1300
772
|
*/
|
|
1301
773
|
getLastCommit() {
|
|
1302
|
-
return this
|
|
774
|
+
return this.#commits[this.#commits.length - 1];
|
|
1303
775
|
}
|
|
1304
776
|
/**
|
|
1305
777
|
* Creates an unsigned commit without adding it to the invitation.
|
|
1306
778
|
* This allows for atomic commit creation with signature.
|
|
1307
779
|
*
|
|
1308
|
-
* @param params - Parameters for creating the commit
|
|
1309
|
-
* @param params.inputs - Optional array of inputs to add
|
|
1310
|
-
* @param params.outputs - Optional array of outputs to add
|
|
1311
|
-
* @param params.variables - Optional array of variables to add
|
|
1312
|
-
* @param params.transactionVersion - Optional transaction version
|
|
1313
|
-
* @param params.transactionLocktime - Optional transaction locktime
|
|
1314
|
-
* @param params.expiresAtTimestamp - Optional expiration timestamp
|
|
1315
|
-
*
|
|
780
|
+
* @param {object} params - Parameters for creating the commit.
|
|
781
|
+
* @param {XOInvitationInput[]} [params.inputs] - Optional array of inputs to add.
|
|
782
|
+
* @param {XOInvitationOutput[]} [params.outputs] - Optional array of outputs to add.
|
|
783
|
+
* @param {XOInvitationVariable[]} [params.variables] - Optional array of variables to add.
|
|
784
|
+
* @param {number} [params.transactionVersion] - Optional transaction version.
|
|
785
|
+
* @param {number} [params.transactionLocktime] - Optional transaction locktime.
|
|
786
|
+
* @param {number} [params.expiresAtTimestamp] - Optional expiration timestamp.
|
|
787
|
+
* Defaults to now plus {@link DEFAULT_COMMIT_EXPIRATION_IN_MILLISECONDS}.
|
|
788
|
+
* @returns {Omit<XOInvitationCommit, 'signature' | 'entityIdentifier'>} The unsigned commit without signature.
|
|
1316
789
|
*/
|
|
1317
790
|
createUnsignedCommit(params) {
|
|
1318
791
|
const { inputs, outputs, variables, transactionVersion, transactionLocktime, expiresAtTimestamp } = params;
|
|
1319
|
-
if (inputs) this
|
|
1320
|
-
if (outputs) this
|
|
1321
|
-
return this
|
|
792
|
+
if (inputs) this.#validateMergesWithReferences(inputs);
|
|
793
|
+
if (outputs) this.#validateMergesWithReferences(outputs);
|
|
794
|
+
return this.#createCommit({
|
|
1322
795
|
previousCommitIdentifier: this.getLastCommitIdentifier(),
|
|
1323
796
|
inputs,
|
|
1324
797
|
outputs,
|
|
@@ -1332,7 +805,7 @@ var InvitationBuilder = class InvitationBuilder {
|
|
|
1332
805
|
* Creates a new commit from the provided data.
|
|
1333
806
|
* This is a private helper method that initializes the commit building logic.
|
|
1334
807
|
*/
|
|
1335
|
-
createCommit(params) {
|
|
808
|
+
#createCommit(params) {
|
|
1336
809
|
let variables;
|
|
1337
810
|
if (params.variables !== void 0 && params.variables.length > 0) variables = params.variables;
|
|
1338
811
|
let inputs;
|
|
@@ -1349,6 +822,7 @@ var InvitationBuilder = class InvitationBuilder {
|
|
|
1349
822
|
return {
|
|
1350
823
|
commitIdentifier: generateCommitIdentifier(),
|
|
1351
824
|
previousCommitIdentifier: params.previousCommitIdentifier,
|
|
825
|
+
createdAtTimestamp: Date.now(),
|
|
1352
826
|
data,
|
|
1353
827
|
expiresAtTimestamp: params.expiresAtTimestamp
|
|
1354
828
|
};
|
|
@@ -1357,9 +831,9 @@ var InvitationBuilder = class InvitationBuilder {
|
|
|
1357
831
|
* Validates mergesWith references for inputs and outputs.
|
|
1358
832
|
* @private
|
|
1359
833
|
*/
|
|
1360
|
-
validateMergesWithReferences(items) {
|
|
834
|
+
#validateMergesWithReferences(items) {
|
|
1361
835
|
for (const item of items) if (item.mergesWith) {
|
|
1362
|
-
if (!this
|
|
836
|
+
if (!this.#commits.find((commit) => commit.commitIdentifier === item.mergesWith?.commitIdentifier)) throw new ParentCommitNotFoundInInvitationError(item.mergesWith.commitIdentifier, this.invitationIdentifier);
|
|
1363
837
|
}
|
|
1364
838
|
}
|
|
1365
839
|
};
|
|
@@ -1373,38 +847,42 @@ var Template = class {
|
|
|
1373
847
|
/**
|
|
1374
848
|
* The template object.
|
|
1375
849
|
*/
|
|
1376
|
-
template;
|
|
850
|
+
#template;
|
|
1377
851
|
/**
|
|
1378
852
|
* Creates a Template class from a template object.
|
|
1379
853
|
*/
|
|
1380
854
|
constructor(template) {
|
|
1381
|
-
this
|
|
855
|
+
this.#template = template;
|
|
1382
856
|
}
|
|
1383
857
|
/**
|
|
1384
858
|
* Gets the underlying template object.
|
|
1385
859
|
*/
|
|
1386
860
|
getTemplate() {
|
|
1387
|
-
return this
|
|
861
|
+
return this.#template;
|
|
1388
862
|
}
|
|
1389
863
|
/**
|
|
1390
864
|
* Gets the starting actions for the template.
|
|
1391
865
|
* @returns {Array<XOTemplateActionIntent>} The list of starting actions.
|
|
1392
866
|
*/
|
|
1393
867
|
getStartingActions() {
|
|
1394
|
-
return this
|
|
868
|
+
return this.#template.start ?? [];
|
|
1395
869
|
}
|
|
1396
870
|
/**
|
|
1397
871
|
* Retrieves an output definition by identifier.
|
|
872
|
+
* @param {XOTemplateOutputIdentifier} outputIdentifier - The output identifier.
|
|
873
|
+
* @returns {XOTemplateOutput} The output definition associated with the specified output identifier.
|
|
1398
874
|
* @throws {TemplateOutputNotFoundError} If the output is not found.
|
|
1399
875
|
*/
|
|
1400
876
|
getOutputOrThrow(outputIdentifier) {
|
|
1401
|
-
const output = this
|
|
877
|
+
const output = this.#template.outputs[outputIdentifier];
|
|
1402
878
|
if (output === void 0) throw new TemplateOutputNotFoundError(outputIdentifier);
|
|
1403
879
|
if (!output.lockingScript) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
1404
880
|
return output;
|
|
1405
881
|
}
|
|
1406
882
|
/**
|
|
1407
883
|
* Retrieves the locking script identifier from an output.
|
|
884
|
+
* @param {XOTemplateOutputIdentifier} outputIdentifier - The output identifier.
|
|
885
|
+
* @returns {XOTemplateLockingScriptIdentifier} The locking script identifier associated with the specified output.
|
|
1408
886
|
* @throws {TemplateOutputNotFoundError} If the output is not found.
|
|
1409
887
|
* @throws {TemplateLockingScriptInOutputNotFoundError} If the output has no locking script reference.
|
|
1410
888
|
*/
|
|
@@ -1415,16 +893,20 @@ var Template = class {
|
|
|
1415
893
|
}
|
|
1416
894
|
/**
|
|
1417
895
|
* Retrieves a locking script definition by identifier.
|
|
896
|
+
* @param {XOTemplateLockingScriptIdentifier} lockingScriptIdentifier - The locking script identifier.
|
|
897
|
+
* @returns {XOTemplateLockingScript} The locking script definition associated with the specified locking script identifier.
|
|
1418
898
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1419
899
|
*/
|
|
1420
900
|
getLockingScriptOrThrow(lockingScriptIdentifier) {
|
|
1421
|
-
const lockingScript = this
|
|
901
|
+
const lockingScript = this.#template.lockingScripts[lockingScriptIdentifier];
|
|
1422
902
|
if (lockingScript === void 0) throw new TemplateLockingScriptNotFoundError(lockingScriptIdentifier);
|
|
1423
903
|
if (lockingScript.lockingType === void 0) throw new TemplateLockingScriptTypeNotFoundError(lockingScriptIdentifier);
|
|
1424
904
|
return lockingScript;
|
|
1425
905
|
}
|
|
1426
906
|
/**
|
|
1427
907
|
* Retrieves the locking script for a given output.
|
|
908
|
+
* @param {XOTemplateOutputIdentifier} outputIdentifier - The output identifier.
|
|
909
|
+
* @returns {XOTemplateLockingScript} The locking script definition associated with the specified output.
|
|
1428
910
|
* @throws {TemplateOutputNotFoundError} If the output is not found.
|
|
1429
911
|
* @throws {TemplateLockingScriptInOutputNotFoundError} If the output has no locking script reference.
|
|
1430
912
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script definition is not found.
|
|
@@ -1435,8 +917,9 @@ var Template = class {
|
|
|
1435
917
|
}
|
|
1436
918
|
/**
|
|
1437
919
|
* Retrieves a role within a locking script.
|
|
1438
|
-
* @param lockingScriptIdentifier - The locking script identifier.
|
|
1439
|
-
* @param roleIdentifier - The role identifier.
|
|
920
|
+
* @param {XOTemplateLockingScriptIdentifier} lockingScriptIdentifier - The locking script identifier.
|
|
921
|
+
* @param {XOTemplateRoleIdentifier} roleIdentifier - The role identifier.
|
|
922
|
+
* @returns {XOTemplateLockingScriptRole} The role definition associated with the specified locking script and role identifiers.
|
|
1440
923
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1441
924
|
* @throws {TemplateLockingScriptRoleNotFoundError} If the role is not found.
|
|
1442
925
|
*/
|
|
@@ -1447,8 +930,9 @@ var Template = class {
|
|
|
1447
930
|
}
|
|
1448
931
|
/**
|
|
1449
932
|
* Retrieves the first available role in a locking script, or throws if none exist.
|
|
1450
|
-
* @param lockingScriptIdentifier - The locking script identifier.
|
|
1451
|
-
* @returns
|
|
933
|
+
* @param {XOTemplateLockingScriptIdentifier} lockingScriptIdentifier - The locking script identifier.
|
|
934
|
+
* @returns {[XOTemplateRoleIdentifier, XOTemplateLockingScriptRole]} The first available role identifier and role definition
|
|
935
|
+
* for the specified locking script.
|
|
1452
936
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1453
937
|
* @throws {TemplateLockingScriptRoleNotFoundError} If no roles exist.
|
|
1454
938
|
*/
|
|
@@ -1459,12 +943,12 @@ var Template = class {
|
|
|
1459
943
|
return [roleKeys[0], roles[roleKeys[0]]];
|
|
1460
944
|
}
|
|
1461
945
|
/**
|
|
1462
|
-
* Retrieves
|
|
1463
|
-
* If roleIdentifier is provided, returns
|
|
1464
|
-
* Otherwise, returns
|
|
1465
|
-
* @param lockingScriptIdentifier - The locking script identifier.
|
|
1466
|
-
* @param roleIdentifier - The role identifier
|
|
1467
|
-
* @returns Array of secret identifiers.
|
|
946
|
+
* Retrieves the secret identifiers tracked for a locking script or one of its roles.
|
|
947
|
+
* If roleIdentifier is provided, returns the secret identifiers tracked for that role within the locking script.
|
|
948
|
+
* Otherwise, returns the secret identifiers tracked at the locking script level.
|
|
949
|
+
* @param {XOTemplateLockingScriptIdentifier} lockingScriptIdentifier - The locking script identifier.
|
|
950
|
+
* @param {XOTemplateRoleIdentifier} [roleIdentifier] - The role identifier. When omitted, locking script secrets are used.
|
|
951
|
+
* @returns {string[]} Array of secret identifiers.
|
|
1468
952
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1469
953
|
* @throws {TemplateLockingScriptRoleNotFoundError} If roleIdentifier is provided but the role is not found.
|
|
1470
954
|
*/
|
|
@@ -1474,27 +958,30 @@ var Template = class {
|
|
|
1474
958
|
}
|
|
1475
959
|
/**
|
|
1476
960
|
* Retrieves an action definition by identifier.
|
|
1477
|
-
* @param actionIdentifier - The identifier of the action.
|
|
961
|
+
* @param {XOTemplateActionIdentifier} actionIdentifier - The identifier of the action.
|
|
962
|
+
* @returns {XOTemplateAction} The action definition associated with the specified action identifier.
|
|
1478
963
|
* @throws {TemplateActionNotFoundError} If the action is not found.
|
|
1479
964
|
*/
|
|
1480
965
|
getActionOrThrow(actionIdentifier) {
|
|
1481
|
-
const action = this
|
|
966
|
+
const action = this.#template.actions[actionIdentifier];
|
|
1482
967
|
if (action === void 0) throw new TemplateActionNotFoundError(actionIdentifier);
|
|
1483
968
|
return action;
|
|
1484
969
|
}
|
|
1485
970
|
/**
|
|
1486
971
|
* Retrieves a transaction definition by identifier.
|
|
1487
|
-
* @param transactionIdentifier - The identifier of the transaction.
|
|
972
|
+
* @param {XOTemplateTransactionIdentifier} transactionIdentifier - The identifier of the transaction.
|
|
973
|
+
* @returns {XOTemplateTransaction} The transaction definition associated with the specified transaction identifier.
|
|
1488
974
|
* @throws {TemplateTransactionNotFoundError} If the transaction is not found.
|
|
1489
975
|
*/
|
|
1490
976
|
getTransactionOrThrow(transactionIdentifier) {
|
|
1491
|
-
const transaction = this
|
|
977
|
+
const transaction = this.#template.transactions?.[transactionIdentifier];
|
|
1492
978
|
if (transaction === void 0) throw new TemplateTransactionNotFoundError(transactionIdentifier);
|
|
1493
979
|
return transaction;
|
|
1494
980
|
}
|
|
1495
981
|
/**
|
|
1496
982
|
* Retrieves the transaction for a given action.
|
|
1497
|
-
* @param actionIdentifier - The identifier of the action.
|
|
983
|
+
* @param {XOTemplateActionIdentifier} actionIdentifier - The identifier of the action.
|
|
984
|
+
* @returns {XOTemplateTransaction} The transaction definition associated with the specified action.
|
|
1498
985
|
* @throws {TemplateActionNotFoundError} If the action is not found.
|
|
1499
986
|
* @throws {TemplateTransactionNotFoundError} If the action has no transaction defined or the transaction is not found.
|
|
1500
987
|
*/
|
|
@@ -1530,8 +1017,8 @@ var Template = class {
|
|
|
1530
1017
|
* Converts an array of XOInvitationVariable objects into an object,
|
|
1531
1018
|
* where each key is the variable's identifier and the value is the variable's value.
|
|
1532
1019
|
*
|
|
1533
|
-
* @param variables - Array of
|
|
1534
|
-
* @returns An object mapping each variable identifier to its corresponding value.
|
|
1020
|
+
* @param {XOInvitationVariable[]} variables - Array of invitation variables to convert.
|
|
1021
|
+
* @returns {Record<string, XOInvitationVariableValue>} An object mapping each variable identifier to its corresponding value.
|
|
1535
1022
|
*/
|
|
1536
1023
|
const convertCommitVariablesIntoObject = (variables) => {
|
|
1537
1024
|
const variablesObject = {};
|
|
@@ -1542,7 +1029,7 @@ const convertCommitVariablesIntoObject = (variables) => {
|
|
|
1542
1029
|
* Validates that all commit references exist.
|
|
1543
1030
|
*/
|
|
1544
1031
|
const validateCommitReferences = (commits, commitsMap) => {
|
|
1545
|
-
for (const commit of commits) if (commit.previousCommitIdentifier && !commitsMap.has(commit.previousCommitIdentifier)) throw new
|
|
1032
|
+
for (const commit of commits) if (commit.previousCommitIdentifier && !commitsMap.has(commit.previousCommitIdentifier)) throw new ParentCommitNotFoundInInvitationError(commit.previousCommitIdentifier, commit.commitIdentifier);
|
|
1546
1033
|
};
|
|
1547
1034
|
/**
|
|
1548
1035
|
* Merges simple data types (variables, transactionVersion, transactionLocktime).
|
|
@@ -1592,15 +1079,15 @@ const mergeItemsWithMergesWith = (context) => {
|
|
|
1592
1079
|
const resolveItemMerges = (item, currentCommitIdentifier, visitedPath = /* @__PURE__ */ new Set()) => {
|
|
1593
1080
|
const mergedItem = { ...item };
|
|
1594
1081
|
if (item.mergesWith) {
|
|
1595
|
-
const mergeKey = `${item.mergesWith.commitIdentifier}:${item.mergesWith.
|
|
1082
|
+
const mergeKey = `${item.mergesWith.commitIdentifier}:${item.mergesWith.itemIndex}`;
|
|
1596
1083
|
if (visitedPath.has(mergeKey)) throw new CircularMergeReferenceError(itemType, mergeKey, currentCommitIdentifier);
|
|
1597
1084
|
visitedPath.add(mergeKey);
|
|
1598
1085
|
const mergesWithCommit = commitsMap.get(item.mergesWith.commitIdentifier);
|
|
1599
1086
|
if (!mergesWithCommit) throw new MergesWithCommitNotFoundError(item.mergesWith.commitIdentifier, currentCommitIdentifier);
|
|
1600
1087
|
const parentItems = getItemsFromCommit(mergesWithCommit);
|
|
1601
1088
|
if (parentItems) {
|
|
1602
|
-
const parentItem = parentItems[item.mergesWith.
|
|
1603
|
-
if (!parentItem) throw new ItemNotFoundInCommitError(itemType, item.mergesWith.
|
|
1089
|
+
const parentItem = parentItems[item.mergesWith.itemIndex];
|
|
1090
|
+
if (!parentItem) throw new ItemNotFoundInCommitError(itemType, item.mergesWith.itemIndex, item.mergesWith.commitIdentifier);
|
|
1604
1091
|
visitedCommits.add(item.mergesWith.commitIdentifier);
|
|
1605
1092
|
const resolvedParent = resolveItemMerges(parentItem, item.mergesWith.commitIdentifier, visitedPath);
|
|
1606
1093
|
Object.assign(mergedItem, resolvedParent);
|
|
@@ -1675,7 +1162,8 @@ const resolveOutputFromTemplate = (output, variables, templateInstance) => {
|
|
|
1675
1162
|
const compiledResult = compileCashAssemblyString({
|
|
1676
1163
|
cashAssemblyText: templateOutput.valueSatoshis,
|
|
1677
1164
|
variables: variablesObject,
|
|
1678
|
-
evaluationDecodeMode: "bigint"
|
|
1165
|
+
evaluationDecodeMode: "bigint",
|
|
1166
|
+
templateVariables: templateInstance.getTemplate().variables
|
|
1679
1167
|
});
|
|
1680
1168
|
resolvedOutput.valueSatoshis = BigInt(compiledResult);
|
|
1681
1169
|
}
|
|
@@ -1686,7 +1174,8 @@ const resolveOutputFromTemplate = (output, variables, templateInstance) => {
|
|
|
1686
1174
|
resolvedOutput.lockingBytecode = hexToBin(compileCashAssemblyString({
|
|
1687
1175
|
cashAssemblyText: templateOutput.lockingScript,
|
|
1688
1176
|
variables: variablesObject,
|
|
1689
|
-
evaluationDecodeMode: "hex"
|
|
1177
|
+
evaluationDecodeMode: "hex",
|
|
1178
|
+
templateVariables: templateInstance.getTemplate().variables
|
|
1690
1179
|
}));
|
|
1691
1180
|
}
|
|
1692
1181
|
}
|
|
@@ -1728,9 +1217,9 @@ const mergeOutputs = (commits, commitsMap, template, variables) => {
|
|
|
1728
1217
|
/**
|
|
1729
1218
|
* Merges invitation commits and transforms it into MergedInvitation structure.
|
|
1730
1219
|
*
|
|
1731
|
-
* @param invitation - The invitation containing commits to merge.
|
|
1732
|
-
* @param template - The template used for resolving output identifiers.
|
|
1733
|
-
* @returns MergedInvitation structure, or null if no commits exist.
|
|
1220
|
+
* @param {XOInvitation} invitation - The invitation containing commits to merge.
|
|
1221
|
+
* @param {XOTemplate} template - The template used for resolving output identifiers.
|
|
1222
|
+
* @returns {MergedInvitation | null} Merged invitation structure, or null if no commits exist.
|
|
1734
1223
|
* @throws {Error} If merge conflicts or missing references are detected.
|
|
1735
1224
|
*/
|
|
1736
1225
|
const mergeInvitationCommits = (invitation, template) => {
|
|
@@ -1762,8 +1251,8 @@ const INVITATION_REQUIREMENT_FIELD_NAMES = [
|
|
|
1762
1251
|
];
|
|
1763
1252
|
/**
|
|
1764
1253
|
* Checks if a role requirement has any items of the specified types.
|
|
1765
|
-
* @param roleRequirement - The role requirement object to check for any populated requirement fields.
|
|
1766
|
-
* @returns
|
|
1254
|
+
* @param {InvitationRoleRequirementIdentifierLists} roleRequirement - The role requirement object to check for any populated requirement fields.
|
|
1255
|
+
* @returns {boolean} True if the role has any requirements (variables, inputs, or outputs), false otherwise.
|
|
1767
1256
|
*/
|
|
1768
1257
|
const hasRoleRequirements = (roleRequirement) => {
|
|
1769
1258
|
return INVITATION_REQUIREMENT_FIELD_NAMES.some((requirementFieldName) => {
|
|
@@ -1774,8 +1263,8 @@ const hasRoleRequirements = (roleRequirement) => {
|
|
|
1774
1263
|
/**
|
|
1775
1264
|
* Filters role requirements to only include those with actual populated requirements.
|
|
1776
1265
|
* Removes any roles that have no variables, inputs, or outputs defined.
|
|
1777
|
-
* @param roleRequirements - Record mapping role identifiers to their requirement objects.
|
|
1778
|
-
* @returns Filtered record containing only roles that have at least one requirement field populated.
|
|
1266
|
+
* @param {RoleRequirementIdentifierMap} roleRequirements - Record mapping role identifiers to their requirement objects.
|
|
1267
|
+
* @returns {RoleRequirementIdentifierMap} Filtered record containing only roles that have at least one requirement field populated.
|
|
1779
1268
|
*/
|
|
1780
1269
|
const filterRolesWithRequirements = (roleRequirements) => {
|
|
1781
1270
|
const filteredRoleRequirements = {};
|
|
@@ -1784,9 +1273,9 @@ const filterRolesWithRequirements = (roleRequirements) => {
|
|
|
1784
1273
|
};
|
|
1785
1274
|
/**
|
|
1786
1275
|
* Finds requirement identifiers that are required but not yet populated.
|
|
1787
|
-
* @param requiredIdentifiers - Array of requirement identifiers that are expected
|
|
1788
|
-
* @param populatedIdentifiers - Array of requirement identifiers that have already been provided.
|
|
1789
|
-
* @returns Array of identifiers that are required but missing from the populated list.
|
|
1276
|
+
* @param {string[]} [requiredIdentifiers] - Array of requirement identifiers that are expected.
|
|
1277
|
+
* @param {string[]} [populatedIdentifiers] - Array of requirement identifiers that have already been provided.
|
|
1278
|
+
* @returns {string[]} Array of identifiers that are required but missing from the populated list.
|
|
1790
1279
|
*/
|
|
1791
1280
|
const findMissingRequirementIdentifiers = (requiredIdentifiers = [], populatedIdentifiers = []) => {
|
|
1792
1281
|
return requiredIdentifiers.filter((identifier) => !populatedIdentifiers.includes(identifier));
|
|
@@ -1794,11 +1283,13 @@ const findMissingRequirementIdentifiers = (requiredIdentifiers = [], populatedId
|
|
|
1794
1283
|
/**
|
|
1795
1284
|
* Processes and identifies missing requirements by comparing template requirements with populated requirements.
|
|
1796
1285
|
* Analyzes both top-level requirements and role-specific requirements to determine what is still needed.
|
|
1797
|
-
* @param requirementsFromTemplate - Requirements expected from the template definition.
|
|
1798
|
-
* @param populatedRequirements - Requirements that have already been populated in the invitation.
|
|
1799
|
-
* @returns
|
|
1800
|
-
*
|
|
1801
|
-
*
|
|
1286
|
+
* @param {InvitationRequirementIdentifierLists} requirementsFromTemplate - Requirements expected from the template definition.
|
|
1287
|
+
* @param {InvitationRequirementIdentifierLists} populatedRequirements - Requirements that have already been populated in the invitation.
|
|
1288
|
+
* @returns {{
|
|
1289
|
+
* missingRequirements: InvitationRequirementIdentifierLists,
|
|
1290
|
+
* missingRoleRequirements: Record<string, InvitationRoleRequirementIdentifierLists>,
|
|
1291
|
+
* }}
|
|
1292
|
+
* Missing top-level requirements and role-specific missing requirements keyed by role identifier.
|
|
1802
1293
|
*/
|
|
1803
1294
|
const processMissingRequirements = (requirementsFromTemplate, populatedRequirements) => {
|
|
1804
1295
|
const missingRequirements = {};
|
|
@@ -1827,9 +1318,9 @@ const processMissingRequirements = (requirementsFromTemplate, populatedRequireme
|
|
|
1827
1318
|
* Analyzes the template action to determine what variables, inputs, outputs, and role-specific
|
|
1828
1319
|
* requirements are needed to fulfill the action.
|
|
1829
1320
|
*
|
|
1830
|
-
* @param template - The template.
|
|
1831
|
-
* @param action - The action that has requirements.
|
|
1832
|
-
* @returns {
|
|
1321
|
+
* @param {XOTemplate} template - The template.
|
|
1322
|
+
* @param {XOTemplateAction} action - The action that has requirements.
|
|
1323
|
+
* @returns {InvitationRequirementIdentifierLists} An object containing all the requirements.
|
|
1833
1324
|
*/
|
|
1834
1325
|
const listRequirementsFromTemplate = (template, action) => {
|
|
1835
1326
|
const generalRequirements = {};
|
|
@@ -1880,9 +1371,9 @@ const listRequirementsFromTemplate = (template, action) => {
|
|
|
1880
1371
|
*
|
|
1881
1372
|
* TODO: Determine how an Authorized Entity will get into the invitation process.
|
|
1882
1373
|
*
|
|
1883
|
-
* @param invitation - The invitation to examine.
|
|
1884
|
-
* @param roleIdentifier - The role identifier to count filled slots for.
|
|
1885
|
-
* @returns The number of unique entities that have contributed data for the specified role.
|
|
1374
|
+
* @param {XOInvitation} invitation - The invitation to examine.
|
|
1375
|
+
* @param {XOTemplateRoleIdentifier} roleIdentifier - The role identifier to count filled slots for.
|
|
1376
|
+
* @returns {number} The number of unique entities that have contributed data for the specified role.
|
|
1886
1377
|
*/
|
|
1887
1378
|
const countFilledSlotsForRole = (invitation, roleIdentifier) => {
|
|
1888
1379
|
const entityIdentifiers = /* @__PURE__ */ new Set();
|
|
@@ -1909,10 +1400,13 @@ const countFilledSlotsForRole = (invitation, roleIdentifier) => {
|
|
|
1909
1400
|
* Variables are added to either top-level requirements or role-specific requirements based on
|
|
1910
1401
|
* whether they have a role identifier. This can be customized independently as variable handling evolves.
|
|
1911
1402
|
*
|
|
1912
|
-
* @param variables - Array of invitation variables to extract identifiers from.
|
|
1913
|
-
* @param generalRequirements - Requirements object to add top-level variable identifiers to.
|
|
1914
|
-
* @param roleRequirements -
|
|
1915
|
-
* @returns
|
|
1403
|
+
* @param {XOInvitationVariable[]} variables - Array of invitation variables to extract identifiers from.
|
|
1404
|
+
* @param {InvitationRequirementIdentifierLists} generalRequirements - Requirements object to add top-level variable identifiers to.
|
|
1405
|
+
* @param {RoleRequirementIdentifierMap} roleRequirements - Role requirements map for role-specific variable identifiers.
|
|
1406
|
+
* @returns {{
|
|
1407
|
+
* generalRequirements: InvitationRequirementIdentifierLists,
|
|
1408
|
+
* roleRequirements: RoleRequirementIdentifierMap,
|
|
1409
|
+
* }} Updated requirements objects.
|
|
1916
1410
|
*/
|
|
1917
1411
|
const addVariableIdentifiers = (variables, generalRequirements, roleRequirements) => {
|
|
1918
1412
|
const updatedGeneralRequirements = { ...generalRequirements };
|
|
@@ -1939,10 +1433,13 @@ const addVariableIdentifiers = (variables, generalRequirements, roleRequirements
|
|
|
1939
1433
|
* Inputs are added to either top-level requirements or role-specific requirements based on
|
|
1940
1434
|
* whether they have a role identifier.
|
|
1941
1435
|
*
|
|
1942
|
-
* @param inputs - Array of invitation inputs to extract identifiers from.
|
|
1943
|
-
* @param generalRequirements - Requirements object to add top-level input identifiers to.
|
|
1944
|
-
* @param roleRequirements -
|
|
1945
|
-
* @returns
|
|
1436
|
+
* @param {XOInvitationInput[]} inputs - Array of invitation inputs to extract identifiers from.
|
|
1437
|
+
* @param {InvitationRequirementIdentifierLists} generalRequirements - Requirements object to add top-level input identifiers to.
|
|
1438
|
+
* @param {RoleRequirementIdentifierMap} roleRequirements - Role requirements map for role-specific input identifiers.
|
|
1439
|
+
* @returns {{
|
|
1440
|
+
* generalRequirements: InvitationRequirementIdentifierLists,
|
|
1441
|
+
* roleRequirements: RoleRequirementIdentifierMap,
|
|
1442
|
+
* }} Updated requirements objects.
|
|
1946
1443
|
*/
|
|
1947
1444
|
const addInputIdentifiers = (inputs, generalRequirements, roleRequirements) => {
|
|
1948
1445
|
const updatedGeneralRequirements = { ...generalRequirements };
|
|
@@ -1969,10 +1466,13 @@ const addInputIdentifiers = (inputs, generalRequirements, roleRequirements) => {
|
|
|
1969
1466
|
* Outputs are added to either top-level requirements or role-specific requirements based on
|
|
1970
1467
|
* whether they have a role identifier. This can be customized independently as output handling evolves.
|
|
1971
1468
|
*
|
|
1972
|
-
* @param outputs - Array of invitation outputs to extract identifiers from.
|
|
1973
|
-
* @param generalRequirements - Requirements object to add top-level output identifiers to.
|
|
1974
|
-
* @param roleRequirements -
|
|
1975
|
-
* @returns
|
|
1469
|
+
* @param {XOInvitationOutput[]} outputs - Array of invitation outputs to extract identifiers from.
|
|
1470
|
+
* @param {InvitationRequirementIdentifierLists} generalRequirements - Requirements object to add top-level output identifiers to.
|
|
1471
|
+
* @param {RoleRequirementIdentifierMap} roleRequirements - Role requirements map for role-specific output identifiers.
|
|
1472
|
+
* @returns {{
|
|
1473
|
+
* generalRequirements: InvitationRequirementIdentifierLists,
|
|
1474
|
+
* roleRequirements: RoleRequirementIdentifierMap,
|
|
1475
|
+
* }} Updated requirements objects.
|
|
1976
1476
|
*/
|
|
1977
1477
|
const addOutputIdentifiers = (outputs, generalRequirements, roleRequirements) => {
|
|
1978
1478
|
const updatedGeneralRequirements = { ...generalRequirements };
|
|
@@ -1999,8 +1499,8 @@ const addOutputIdentifiers = (outputs, generalRequirements, roleRequirements) =>
|
|
|
1999
1499
|
* Extracts variable, input, and output identifiers from all commits in the invitation and
|
|
2000
1500
|
* organizes them into top-level and role-specific requirements.
|
|
2001
1501
|
*
|
|
2002
|
-
* @param invitation - The invitation object to analyze for populated requirements.
|
|
2003
|
-
* @returns An object containing all populated requirements found in the invitation's commits,
|
|
1502
|
+
* @param {XOInvitation} invitation - The invitation object to analyze for populated requirements.
|
|
1503
|
+
* @returns {InvitationRequirementIdentifierLists} An object containing all populated requirements found in the invitation's commits,
|
|
2004
1504
|
* including top-level requirements and role-specific requirements. Only includes roles
|
|
2005
1505
|
* that have at least one requirement populated.
|
|
2006
1506
|
*/
|
|
@@ -2037,10 +1537,10 @@ const listPopulatedRequirementsInInvitation = (invitation) => {
|
|
|
2037
1537
|
*
|
|
2038
1538
|
* Logic: `missing_requirements = requirements_from_template - populated_requirements_in_invitation`
|
|
2039
1539
|
*
|
|
2040
|
-
* @param invitation - The invitation object to check for missing requirements.
|
|
2041
|
-
* @param template - The template defining what requirements are expected.
|
|
2042
|
-
* @param action - The template action being analyzed to determine required requirements.
|
|
2043
|
-
* @returns An object containing all missing requirements, including top-level missing requirements
|
|
1540
|
+
* @param {XOInvitation} invitation - The invitation object to check for missing requirements.
|
|
1541
|
+
* @param {XOTemplate} template - The template defining what requirements are expected.
|
|
1542
|
+
* @param {XOTemplateAction} action - The template action being analyzed to determine required requirements.
|
|
1543
|
+
* @returns {InvitationRequirementIdentifierLists} An object containing all missing requirements, including top-level missing requirements
|
|
2044
1544
|
* and role-specific missing requirements. Only includes roles that have at least one missing requirement.
|
|
2045
1545
|
*/
|
|
2046
1546
|
const listMissingRequirements = (invitation, template, action) => {
|
|
@@ -2055,10 +1555,9 @@ const listMissingRequirements = (invitation, template, action) => {
|
|
|
2055
1555
|
* A role is considered available if the number of distinct entities that have filled it
|
|
2056
1556
|
* is less than the role's maximum slot count. If max is undefined, the role has unlimited capacity.
|
|
2057
1557
|
*
|
|
2058
|
-
* @param invitation - The invitation object to check for available roles.
|
|
2059
|
-
* @param
|
|
2060
|
-
* @
|
|
2061
|
-
* @returns Array of role identifier strings representing roles that can still accept participants.
|
|
1558
|
+
* @param {XOInvitation} invitation - The invitation object to check for available roles.
|
|
1559
|
+
* @param {XOTemplateAction} action - The template action being analyzed to determine which roles are expected.
|
|
1560
|
+
* @returns {XOTemplateRoleIdentifier[]} Role identifiers for roles that can still accept participants.
|
|
2062
1561
|
* Returns an empty array if no roles are defined in the action.
|
|
2063
1562
|
*/
|
|
2064
1563
|
const listAvailableRolesForInvitation = (invitation, action) => {
|
|
@@ -2079,8 +1578,8 @@ const listAvailableRolesForInvitation = (invitation, action) => {
|
|
|
2079
1578
|
/**
|
|
2080
1579
|
* Validates that the invitation has not expired for any of the commits.
|
|
2081
1580
|
*
|
|
2082
|
-
* @param invitation - The
|
|
2083
|
-
* @returns True if the invitation is expired, false if it is valid.
|
|
1581
|
+
* @param {XOInvitation} invitation - The invitation containing creation and expiration timestamps.
|
|
1582
|
+
* @returns {boolean} True if the invitation is expired, false if it is valid.
|
|
2084
1583
|
*/
|
|
2085
1584
|
const hasInvitationExpired = (invitation) => {
|
|
2086
1585
|
const { commits } = invitation;
|
|
@@ -2088,16 +1587,16 @@ const hasInvitationExpired = (invitation) => {
|
|
|
2088
1587
|
return commits.some((commit) => commit.expiresAtTimestamp < now);
|
|
2089
1588
|
};
|
|
2090
1589
|
/**
|
|
2091
|
-
* Validates that all entity identifiers in the invitation are valid public keys.
|
|
1590
|
+
* Validates that all entity identifiers in the invitation are valid extended public keys.
|
|
2092
1591
|
* Extracts unique entity identifiers from all commits.
|
|
2093
1592
|
*
|
|
2094
|
-
* @param invitation - The invitation to validate.
|
|
2095
|
-
* @returns True if all entities are valid, false otherwise.
|
|
1593
|
+
* @param {XOInvitation} invitation - The invitation to validate.
|
|
1594
|
+
* @returns {boolean} True if all entities are valid, false otherwise.
|
|
2096
1595
|
*/
|
|
2097
1596
|
const validateInvitationEntities = (invitation) => {
|
|
2098
1597
|
const entityIdentifiers = new Set(invitation.commits.map((commit) => commit.entityIdentifier));
|
|
2099
1598
|
for (const entityIdentifier of entityIdentifiers) try {
|
|
2100
|
-
new
|
|
1599
|
+
new ExtendedPublicKey(entityIdentifier);
|
|
2101
1600
|
} catch {
|
|
2102
1601
|
return false;
|
|
2103
1602
|
}
|
|
@@ -2106,10 +1605,10 @@ const validateInvitationEntities = (invitation) => {
|
|
|
2106
1605
|
/**
|
|
2107
1606
|
* Validates that all provided requirements in the invitation were expected by the template.
|
|
2108
1607
|
*
|
|
2109
|
-
* @param invitation - The invitation to validate.
|
|
2110
|
-
* @param template - The template to validate against.
|
|
2111
|
-
* @param action - The action to validate against.
|
|
2112
|
-
* @returns True if all requirements are expected, false otherwise.
|
|
1608
|
+
* @param {XOInvitation} invitation - The invitation to validate.
|
|
1609
|
+
* @param {XOTemplate} template - The template to validate against.
|
|
1610
|
+
* @param {XOTemplateAction} action - The action to validate against.
|
|
1611
|
+
* @returns {boolean} True if all requirements are expected, false otherwise.
|
|
2113
1612
|
*/
|
|
2114
1613
|
const validateRoleRequirements = (invitation, template, action) => {
|
|
2115
1614
|
const expectedRequirementsByTemplate = listRequirementsFromTemplate(template, action);
|
|
@@ -2139,13 +1638,12 @@ const validateRoleRequirements = (invitation, template, action) => {
|
|
|
2139
1638
|
* Validates commit signatures.
|
|
2140
1639
|
* Signature signs: stringify(data) + templateIdentifier + actionIdentifier + invitationIdentifier + commitIdentifier + previousCommitIdentifier
|
|
2141
1640
|
*
|
|
2142
|
-
* @param invitation - The invitation containing commits to validate.
|
|
2143
|
-
* @returns True if all commit signatures are valid, false otherwise.
|
|
1641
|
+
* @param {XOInvitation} invitation - The invitation containing commits to validate.
|
|
1642
|
+
* @returns {boolean} True if all commit signatures are valid, false otherwise.
|
|
2144
1643
|
*/
|
|
2145
1644
|
const validateInvitationCommits = (invitation) => {
|
|
2146
1645
|
const { templateIdentifier, actionIdentifier, invitationIdentifier, commits } = invitation;
|
|
2147
1646
|
for (const commit of commits) {
|
|
2148
|
-
const entityPublicKey = commit.entityIdentifier;
|
|
2149
1647
|
const commitSigningPayload = buildInvitationCommitSigningPayload({
|
|
2150
1648
|
data: commit.data,
|
|
2151
1649
|
templateIdentifier,
|
|
@@ -2154,7 +1652,12 @@ const validateInvitationCommits = (invitation) => {
|
|
|
2154
1652
|
commitIdentifier: commit.commitIdentifier,
|
|
2155
1653
|
previousCommitIdentifier: commit.previousCommitIdentifier
|
|
2156
1654
|
});
|
|
2157
|
-
if (!
|
|
1655
|
+
if (!verifyInvitationCommitSignature({
|
|
1656
|
+
signature: commit.signature,
|
|
1657
|
+
entityIdentifier: commit.entityIdentifier,
|
|
1658
|
+
commitIdentifier: commit.commitIdentifier,
|
|
1659
|
+
message: commitSigningPayload
|
|
1660
|
+
})) return false;
|
|
2158
1661
|
}
|
|
2159
1662
|
return true;
|
|
2160
1663
|
};
|
|
@@ -2168,27 +1671,15 @@ var Engine = class Engine {
|
|
|
2168
1671
|
/**
|
|
2169
1672
|
* The crypto instance.
|
|
2170
1673
|
*/
|
|
2171
|
-
crypto;
|
|
1674
|
+
#crypto;
|
|
2172
1675
|
/**
|
|
2173
1676
|
* The state instance.
|
|
2174
1677
|
*/
|
|
2175
|
-
state;
|
|
1678
|
+
#state;
|
|
2176
1679
|
/**
|
|
2177
1680
|
* The blockchain monitor instance.
|
|
2178
1681
|
*/
|
|
2179
|
-
blockchainMonitor;
|
|
2180
|
-
/**
|
|
2181
|
-
* The default template identifier.
|
|
2182
|
-
*/
|
|
2183
|
-
defaultTemplateIdentifier;
|
|
2184
|
-
/**
|
|
2185
|
-
* The default role identifier.
|
|
2186
|
-
*/
|
|
2187
|
-
defaultRoleIdentifier;
|
|
2188
|
-
/**
|
|
2189
|
-
* The default output identifier.
|
|
2190
|
-
*/
|
|
2191
|
-
defaultOutputIdentifier;
|
|
1682
|
+
#blockchainMonitor;
|
|
2192
1683
|
/**
|
|
2193
1684
|
* Constructs a new instance of XO Engine.
|
|
2194
1685
|
* @private
|
|
@@ -2197,9 +1688,9 @@ var Engine = class Engine {
|
|
|
2197
1688
|
* @param {BlockchainMonitor} blockchainMonitor - Blockchain monitor instance.
|
|
2198
1689
|
*/
|
|
2199
1690
|
constructor(mnemonic, state, blockchainMonitor) {
|
|
2200
|
-
this
|
|
2201
|
-
this
|
|
2202
|
-
this
|
|
1691
|
+
this.#crypto = new Crypto(convertMnemonicToSeedBytes(mnemonic));
|
|
1692
|
+
this.#state = state;
|
|
1693
|
+
this.#blockchainMonitor = blockchainMonitor;
|
|
2203
1694
|
}
|
|
2204
1695
|
/**
|
|
2205
1696
|
* Initializes and creates a new XO Engine instance.
|
|
@@ -2214,7 +1705,7 @@ var Engine = class Engine {
|
|
|
2214
1705
|
electrumServers,
|
|
2215
1706
|
...options
|
|
2216
1707
|
});
|
|
2217
|
-
const accountHash =
|
|
1708
|
+
const accountHash = computeAccountHash(mnemonic);
|
|
2218
1709
|
const stateInstance = new State(await createStorageAdapter({
|
|
2219
1710
|
storageType: options.storageType ?? DEFAULT_STORAGE_TYPE,
|
|
2220
1711
|
databasePath: options.databasePath,
|
|
@@ -2232,7 +1723,7 @@ var Engine = class Engine {
|
|
|
2232
1723
|
* @returns {Promise<void>} Resolves when the engine is stopped.
|
|
2233
1724
|
*/
|
|
2234
1725
|
async stop() {
|
|
2235
|
-
await this
|
|
1726
|
+
await this.#blockchainMonitor.stop();
|
|
2236
1727
|
}
|
|
2237
1728
|
/**
|
|
2238
1729
|
* Retrieves the current chain status from the electrum application.
|
|
@@ -2243,21 +1734,21 @@ var Engine = class Engine {
|
|
|
2243
1734
|
}
|
|
2244
1735
|
/**
|
|
2245
1736
|
* Retrieves a template by its identifier from the state.
|
|
2246
|
-
* @param {
|
|
1737
|
+
* @param {XOTemplateIdentifier} templateIdentifier - Template identifier.
|
|
2247
1738
|
* @returns {Promise<XOTemplate | undefined>} The template object.
|
|
2248
1739
|
*/
|
|
2249
1740
|
async getTemplate(templateIdentifier) {
|
|
2250
|
-
return this
|
|
1741
|
+
return this.#state.getTemplate(templateIdentifier);
|
|
2251
1742
|
}
|
|
2252
1743
|
/**
|
|
2253
1744
|
* Imports a template into the Engine after sanitization and validation.
|
|
2254
1745
|
* @param {string | XOTemplate} untrustedJsonOrObject - JSON or object containing the untrusted template to import.
|
|
2255
|
-
* @returns {Promise<
|
|
1746
|
+
* @returns {Promise<{ template: XOTemplate; templateIdentifier: XOTemplateIdentifier }>} The imported template and its identifier.
|
|
2256
1747
|
* @throws {TemplateImportFailedError} If the template is invalid.
|
|
2257
1748
|
*/
|
|
2258
1749
|
async importTemplate(untrustedJsonOrObject) {
|
|
2259
1750
|
const template = parseTemplate$1(untrustedJsonOrObject);
|
|
2260
|
-
await this
|
|
1751
|
+
await this.#state.storeTemplate(template);
|
|
2261
1752
|
return {
|
|
2262
1753
|
template,
|
|
2263
1754
|
templateIdentifier: generateTemplateIdentifier$1(template)
|
|
@@ -2268,11 +1759,11 @@ var Engine = class Engine {
|
|
|
2268
1759
|
* @returns {Promise<XOTemplate[]>} The list of imported templates.
|
|
2269
1760
|
*/
|
|
2270
1761
|
async listImportedTemplates() {
|
|
2271
|
-
return this
|
|
1762
|
+
return this.#state.listTemplates();
|
|
2272
1763
|
}
|
|
2273
1764
|
/**
|
|
2274
1765
|
* Lists the starting actions available in a specified template.
|
|
2275
|
-
* @param {
|
|
1766
|
+
* @param {XOTemplateIdentifier} templateIdentifier - Template identifier.
|
|
2276
1767
|
* @returns {Promise<Array<XOTemplateActionIntent>>} The list of starting actions.
|
|
2277
1768
|
*/
|
|
2278
1769
|
async listStartingActions(templateIdentifier) {
|
|
@@ -2283,7 +1774,7 @@ var Engine = class Engine {
|
|
|
2283
1774
|
*
|
|
2284
1775
|
* TODO: Make this an private/internal method.
|
|
2285
1776
|
*
|
|
2286
|
-
* @param {
|
|
1777
|
+
* @param {XOTemplateIdentifier} templateIdentifier - Template identifier.
|
|
2287
1778
|
* @returns {Promise<void>} Resolves when the unspent outputs are updated.
|
|
2288
1779
|
*/
|
|
2289
1780
|
async updateUnspentOutputsForTemplate(templateIdentifier) {
|
|
@@ -2296,7 +1787,7 @@ var Engine = class Engine {
|
|
|
2296
1787
|
* TODO: Make this a private/internal method. We may change how/where this is defined in the future.
|
|
2297
1788
|
* We'd like to ensure events are propagated to the sync server with an ACK and that the states match between remote and local.
|
|
2298
1789
|
*
|
|
2299
|
-
* @param {
|
|
1790
|
+
* @param {XOTemplateIdentifier} templateIdentifier - Template identifier.
|
|
2300
1791
|
* @returns {Promise<void>} Resolves when the script hashes are subscribed.
|
|
2301
1792
|
*/
|
|
2302
1793
|
async subscribeToScriptHashForTemplate(templateIdentifier) {
|
|
@@ -2306,22 +1797,27 @@ var Engine = class Engine {
|
|
|
2306
1797
|
for (const scriptHashData of scriptHashDataList) await subscribeToScriptHashUpdates(firstElectrumClient, scriptHashData.scriptHash);
|
|
2307
1798
|
}
|
|
2308
1799
|
/**
|
|
2309
|
-
*
|
|
1800
|
+
* Sets the fallback locking parameters used for change and locking bytecode generation.
|
|
2310
1801
|
*
|
|
2311
|
-
*
|
|
2312
|
-
*
|
|
2313
|
-
* unlocking conditions are unclear. This is likely to change in the future.
|
|
1802
|
+
* Resolution order is override, then template defaults, then these fallbacks.
|
|
1803
|
+
* If generation is required and no fallback exists, {@link FallbackLockingParametersMissingError} is thrown.
|
|
2314
1804
|
*
|
|
2315
|
-
* @param {
|
|
2316
|
-
* @
|
|
2317
|
-
* @
|
|
2318
|
-
* @
|
|
2319
|
-
* @throws {
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
1805
|
+
* @param {FallbackLockingParameters} fallbackLockingParameters - Template, output, and role identifiers set together.
|
|
1806
|
+
* @returns {Promise<void>} Resolves when the fallback parameters have been set.
|
|
1807
|
+
* @throws {TemplateIdentifierInvalidInputTypeError} If the template identifier input type is invalid.
|
|
1808
|
+
* @throws {TemplateIdentifierByteLengthInvalidError} If the template identifier byte length is invalid.
|
|
1809
|
+
* @throws {TemplateIdentifierHexLengthInvalidError} If the template identifier hex length is invalid.
|
|
1810
|
+
* @throws {TemplateIdentifierHexFormatInvalidError} If the template identifier hex format is invalid.
|
|
1811
|
+
* @throws {TemplateIdentifierAllZeroesError} If the template identifier is all zeroes.
|
|
1812
|
+
* @throws {TemplateIdentifierMaximumError} If the template identifier is all 0xFF bytes.
|
|
1813
|
+
*/
|
|
1814
|
+
async updateFallbackLockingParameters(fallbackLockingParameters) {
|
|
1815
|
+
const templateIdentifier = new TemplateIdentifier(fallbackLockingParameters.templateIdentifier).toHex();
|
|
1816
|
+
return this.#state.updateWalletSettings({ fallbackLockingParameters: {
|
|
1817
|
+
templateIdentifier,
|
|
1818
|
+
outputIdentifier: fallbackLockingParameters.outputIdentifier,
|
|
1819
|
+
roleIdentifier: fallbackLockingParameters.roleIdentifier
|
|
1820
|
+
} });
|
|
2325
1821
|
}
|
|
2326
1822
|
/**
|
|
2327
1823
|
* TODO: Implement this.
|
|
@@ -2342,10 +1838,10 @@ var Engine = class Engine {
|
|
|
2342
1838
|
* xo template are done using invitations.
|
|
2343
1839
|
*
|
|
2344
1840
|
* @param {InvitationCreationParameters} params - The parameters for invitation creation.
|
|
2345
|
-
* @param {
|
|
2346
|
-
* @param {
|
|
1841
|
+
* @param {XOTemplateIdentifier} params.templateIdentifier - Identifier for the template to use.
|
|
1842
|
+
* @param {XOTemplateActionIdentifier} params.actionIdentifier - Identifier for the action to initiate.
|
|
2347
1843
|
* @returns {Promise<XOInvitation>} A promise that resolves to the constructed invitation.
|
|
2348
|
-
* @throws {
|
|
1844
|
+
* @throws {TemplateNotFoundInStoreError} If the template is not found.
|
|
2349
1845
|
* @throws {TemplateActionNotFoundError} If the action is not found.
|
|
2350
1846
|
*
|
|
2351
1847
|
* TODO: Add all the throws that happen during the execution of the method.
|
|
@@ -2355,7 +1851,7 @@ var Engine = class Engine {
|
|
|
2355
1851
|
const invitationBuilder = new InvitationBuilder(params);
|
|
2356
1852
|
const invitationCommit = invitationBuilder.createUnsignedCommit({});
|
|
2357
1853
|
const rootCommitIdentifier = invitationCommit.commitIdentifier;
|
|
2358
|
-
const signedCommit = this
|
|
1854
|
+
const signedCommit = this.#crypto.signInvitationCommitWithSchnorr({
|
|
2359
1855
|
invitationCommit,
|
|
2360
1856
|
invitationIdentifier: invitationBuilder.invitationIdentifier,
|
|
2361
1857
|
rootCommitIdentifier,
|
|
@@ -2364,7 +1860,7 @@ var Engine = class Engine {
|
|
|
2364
1860
|
});
|
|
2365
1861
|
invitationBuilder.addCommit(signedCommit);
|
|
2366
1862
|
const invitation = invitationBuilder.build();
|
|
2367
|
-
await this
|
|
1863
|
+
await this.#state.storeNewInvitation(invitation.invitationIdentifier, {
|
|
2368
1864
|
...invitation,
|
|
2369
1865
|
entityIdentifier: signedCommit.entityIdentifier
|
|
2370
1866
|
});
|
|
@@ -2376,7 +1872,7 @@ var Engine = class Engine {
|
|
|
2376
1872
|
* @param {XOInvitation} invitation - The invitation to be accepted.
|
|
2377
1873
|
* @param {AcceptInvitationParameters} params - Optional parameters for accepting the invitation.
|
|
2378
1874
|
* @returns {Promise<XOInvitation>} The updated XOInvitation instance.
|
|
2379
|
-
* @throws {
|
|
1875
|
+
* @throws {TemplateNotFoundInStoreError} If the template is not found.
|
|
2380
1876
|
* @throws {TemplateActionNotFoundError} If the action is not found.
|
|
2381
1877
|
* @throws {InvalidEntityIdentifierError} If the entity identifiers are invalid.
|
|
2382
1878
|
* @throws {InvalidRequirementsError} If the requirements are invalid.
|
|
@@ -2384,12 +1880,12 @@ var Engine = class Engine {
|
|
|
2384
1880
|
*/
|
|
2385
1881
|
async acceptInvitation(invitation, params) {
|
|
2386
1882
|
new Template(await this.getTemplateOrThrow(invitation.templateIdentifier)).getActionOrThrow(invitation.actionIdentifier);
|
|
2387
|
-
const invitationFromStore = await this
|
|
1883
|
+
const invitationFromStore = await this.#state.getInvitation(invitation.invitationIdentifier);
|
|
2388
1884
|
if (invitationFromStore !== void 0) return invitationFromStore;
|
|
2389
1885
|
const invitationBuilder = InvitationBuilder.fromInvitation(invitation);
|
|
2390
1886
|
const invitationCommit = invitationBuilder.createUnsignedCommit({ ...params });
|
|
2391
1887
|
const rootCommitIdentifier = invitationCommit.commitIdentifier;
|
|
2392
|
-
const signedCommit = this
|
|
1888
|
+
const signedCommit = this.#crypto.signInvitationCommitWithSchnorr({
|
|
2393
1889
|
invitationCommit,
|
|
2394
1890
|
invitationIdentifier: invitationBuilder.invitationIdentifier,
|
|
2395
1891
|
rootCommitIdentifier,
|
|
@@ -2398,7 +1894,7 @@ var Engine = class Engine {
|
|
|
2398
1894
|
});
|
|
2399
1895
|
invitationBuilder.addCommit(signedCommit);
|
|
2400
1896
|
const acceptedInvitation = invitationBuilder.build();
|
|
2401
|
-
await this
|
|
1897
|
+
await this.#state.storeNewInvitation(acceptedInvitation.invitationIdentifier, {
|
|
2402
1898
|
...acceptedInvitation,
|
|
2403
1899
|
entityIdentifier: signedCommit.entityIdentifier
|
|
2404
1900
|
});
|
|
@@ -2407,21 +1903,23 @@ var Engine = class Engine {
|
|
|
2407
1903
|
/**
|
|
2408
1904
|
* Appends the invitation with the new information.
|
|
2409
1905
|
*
|
|
2410
|
-
* @param {
|
|
1906
|
+
* @param {XOInvitationIdentifier} invitationIdentifier - The identifier of the invitation to append.
|
|
1907
|
+
* The invitation must already exist in the store.
|
|
2411
1908
|
* @param {AppendInvitationParameters} params - The parameters to append the invitation with.
|
|
2412
1909
|
* @returns {Promise<XOInvitation>} The appended invitation.
|
|
2413
1910
|
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2414
1911
|
*/
|
|
2415
1912
|
async appendInvitation(invitationIdentifier, params) {
|
|
2416
1913
|
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2417
|
-
return this
|
|
1914
|
+
return this.#applyInvitationAppend(invitationFromStore, params);
|
|
2418
1915
|
}
|
|
2419
1916
|
/**
|
|
2420
1917
|
* Signs the provided invitation once all the resources are reserved and the invitation is complete.
|
|
2421
1918
|
* This method is used to sign own inputs and provide unlocking data for them, this appends the
|
|
2422
1919
|
* invitation and adds new commit with unlocking data.
|
|
2423
1920
|
*
|
|
2424
|
-
* @param {
|
|
1921
|
+
* @param {XOInvitationIdentifier} invitationIdentifier - The identifier of the invitation to sign.
|
|
1922
|
+
* The invitation must already exist in the store.
|
|
2425
1923
|
* @returns {Promise<XOInvitation>} Resolves when signing is complete.
|
|
2426
1924
|
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2427
1925
|
*
|
|
@@ -2431,7 +1929,7 @@ var Engine = class Engine {
|
|
|
2431
1929
|
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2432
1930
|
const template = await this.getTemplateOrThrow(invitationFromStore.templateIdentifier);
|
|
2433
1931
|
const mergedCommit = mergeInvitationCommits(invitationFromStore, template);
|
|
2434
|
-
if (mergedCommit ===
|
|
1932
|
+
if (mergedCommit === null) throw new MergedCommitNotFoundError();
|
|
2435
1933
|
const transactionResult = generateTransaction({
|
|
2436
1934
|
version: mergedCommit.transactionVersion,
|
|
2437
1935
|
locktime: mergedCommit.transactionLocktime,
|
|
@@ -2439,7 +1937,7 @@ var Engine = class Engine {
|
|
|
2439
1937
|
outputs: mergedCommit.outputs
|
|
2440
1938
|
});
|
|
2441
1939
|
if (transactionResult.success === false) throw new TransactionGenerationFailedError();
|
|
2442
|
-
const unspentOutputs = await this
|
|
1940
|
+
const unspentOutputs = await this.#state.listUnspentOutputsData({ reservedBy: invitationFromStore.invitationIdentifier });
|
|
2443
1941
|
for (const [index, input] of transactionResult.transaction.inputs.entries()) {
|
|
2444
1942
|
let unspentOutput;
|
|
2445
1943
|
for (const sourceOutput of unspentOutputs) if (sourceOutput.outpointTransactionHash === binToHex(input.outpointTransactionHash) && sourceOutput.outpointIndex === input.outpointIndex && sourceOutput.reservedBy) {
|
|
@@ -2447,27 +1945,28 @@ var Engine = class Engine {
|
|
|
2447
1945
|
break;
|
|
2448
1946
|
}
|
|
2449
1947
|
if (!unspentOutput) continue;
|
|
2450
|
-
const scriptHashData = await this
|
|
1948
|
+
const scriptHashData = await this.#state.getScriptHashData(unspentOutput.scriptHash);
|
|
2451
1949
|
if (scriptHashData === void 0) throw new ScriptHashDataNotFoundError(unspentOutput.scriptHash);
|
|
2452
1950
|
let compilerPrivateKeyRecord;
|
|
2453
|
-
if (scriptHashData.
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
1951
|
+
if (scriptHashData.secrets !== void 0) {
|
|
1952
|
+
const derivedSecretRecord = Object.values(scriptHashData.secrets).find((secretRecord) => secretRecord.derivativeIndex !== void 0);
|
|
1953
|
+
if (derivedSecretRecord?.lockingType === XOTemplateLockingTypes.P2PKH) {
|
|
1954
|
+
const templateDataForInput = await this.#state.getTemplate(scriptHashData.templateIdentifier);
|
|
1955
|
+
if (templateDataForInput === void 0) throw new TemplateNotFoundInStoreError(scriptHashData.templateIdentifier);
|
|
2457
1956
|
const templateInstanceForInput = new Template(templateDataForInput);
|
|
2458
1957
|
const lockingScriptIdentifier = templateInstanceForInput.getLockingScriptIdentifierOrThrow(scriptHashData.outputIdentifier);
|
|
2459
1958
|
const secretIdentifiers = templateInstanceForInput.getSecrets(lockingScriptIdentifier, scriptHashData.roleIdentifier);
|
|
2460
|
-
const { derivativeIndex } =
|
|
2461
|
-
if (
|
|
2462
|
-
compilerPrivateKeyRecord = this
|
|
1959
|
+
const { derivativeIndex } = derivedSecretRecord;
|
|
1960
|
+
if (derivativeIndex === void 0) throw new P2PKHDerivativeIndexMissingError();
|
|
1961
|
+
compilerPrivateKeyRecord = this.#crypto.derivePrivateKeyRecordForCompiler({
|
|
2463
1962
|
compilerKeyIdentifiers: secretIdentifiers,
|
|
2464
1963
|
templateIdentifier: scriptHashData.templateIdentifier,
|
|
2465
|
-
templateKeyDerivationIndex:
|
|
1964
|
+
templateKeyDerivationIndex: derivativeIndex
|
|
2466
1965
|
});
|
|
2467
1966
|
}
|
|
2468
1967
|
}
|
|
2469
1968
|
if (compilerPrivateKeyRecord === void 0) throw new LockingDataNotFoundError();
|
|
2470
|
-
const compilerData = await createLibauthCompilerDataFromUnspentOutput(new Template(template), unspentOutput);
|
|
1969
|
+
const compilerData = await createLibauthCompilerDataFromUnspentOutput(new Template(template), unspentOutput, scriptHashData.outputIdentifier);
|
|
2471
1970
|
transactionResult.transaction.inputs[index] = {
|
|
2472
1971
|
...input,
|
|
2473
1972
|
unlockingBytecode: {
|
|
@@ -2490,7 +1989,7 @@ var Engine = class Engine {
|
|
|
2490
1989
|
unspentOutput = sourceOutput;
|
|
2491
1990
|
break;
|
|
2492
1991
|
}
|
|
2493
|
-
if (unspentOutput === void 0) throw new
|
|
1992
|
+
if (unspentOutput === void 0) throw new UnspentOutputNotFoundInStoreError(`${binToHex(input.outpointTransactionHash)}:${String(input.outpointIndex)}`);
|
|
2494
1993
|
let commitToExtend;
|
|
2495
1994
|
let indexToExtend;
|
|
2496
1995
|
for (const commit of currentStoredInvitation.commits) for (const [index, commitInput] of (commit.data.inputs ?? []).entries()) if (commitInput.outpointTransactionHash && binToHex(commitInput.outpointTransactionHash) === binToHex(input.outpointTransactionHash) && Number(commitInput.outpointIndex) === Number(input.outpointIndex)) {
|
|
@@ -2499,10 +1998,10 @@ var Engine = class Engine {
|
|
|
2499
1998
|
}
|
|
2500
1999
|
if (commitToExtend === void 0 || indexToExtend === void 0) throw new CommitNotFoundInInvitationError();
|
|
2501
2000
|
currentStoredInvitation = {
|
|
2502
|
-
...await this
|
|
2001
|
+
...await this.#applyInvitationAppend(currentStoredInvitation, { inputs: [{
|
|
2503
2002
|
mergesWith: {
|
|
2504
2003
|
commitIdentifier: commitToExtend.commitIdentifier,
|
|
2505
|
-
|
|
2004
|
+
itemIndex: indexToExtend
|
|
2506
2005
|
},
|
|
2507
2006
|
unlockingBytecode: input.unlockingBytecode
|
|
2508
2007
|
}] }),
|
|
@@ -2513,7 +2012,7 @@ var Engine = class Engine {
|
|
|
2513
2012
|
}
|
|
2514
2013
|
/**
|
|
2515
2014
|
* Executes an action for a given invitation.
|
|
2516
|
-
* @param {
|
|
2015
|
+
* @param {XOInvitationIdentifier} invitationIdentifier - The identifier of the invitation whose action to execute.
|
|
2517
2016
|
* The invitation must already exist in the store.
|
|
2518
2017
|
* @param {ExecuteActionParameters} params - Parameters for executing the action.
|
|
2519
2018
|
* @returns {Promise<TransactionHash>} Resolves with the transaction hash when the action is executed.
|
|
@@ -2525,7 +2024,7 @@ var Engine = class Engine {
|
|
|
2525
2024
|
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2526
2025
|
const template = await this.getTemplateOrThrow(invitationFromStore.templateIdentifier);
|
|
2527
2026
|
const mergedCommit = mergeInvitationCommits(invitationFromStore, template);
|
|
2528
|
-
if (mergedCommit ===
|
|
2027
|
+
if (mergedCommit === null) throw new MergedCommitNotFoundError();
|
|
2529
2028
|
if (new Template(template).getActionOrThrow(invitationFromStore.actionIdentifier).transaction === void 0) throw new TemplateTransactionNotFoundError(invitationFromStore.actionIdentifier);
|
|
2530
2029
|
const resultTransaction = generateTransaction({
|
|
2531
2030
|
version: mergedCommit.transactionVersion,
|
|
@@ -2543,8 +2042,8 @@ var Engine = class Engine {
|
|
|
2543
2042
|
* Lists available roles for the given invitation.
|
|
2544
2043
|
*
|
|
2545
2044
|
* @param {XOInvitation} invitation - The invitation to examine.
|
|
2546
|
-
* @returns {Promise<
|
|
2547
|
-
* @throws {
|
|
2045
|
+
* @returns {Promise<XOTemplateRoleIdentifier[]>} The list of roles that still require data.
|
|
2046
|
+
* @throws {TemplateNotFoundInStoreError} If the template is not found.
|
|
2548
2047
|
* @throws {TemplateActionNotFoundError} If the action is not found.
|
|
2549
2048
|
*/
|
|
2550
2049
|
async listAvailableRoles(invitation) {
|
|
@@ -2554,7 +2053,7 @@ var Engine = class Engine {
|
|
|
2554
2053
|
* Lists requirements for the provided invitation.
|
|
2555
2054
|
*
|
|
2556
2055
|
* @param {XOInvitation} invitation - The invitation to analyze requirements for.
|
|
2557
|
-
* @returns {Promise<
|
|
2056
|
+
* @returns {Promise<InvitationRequirementIdentifierLists>} The requirements information.
|
|
2558
2057
|
*
|
|
2559
2058
|
*/
|
|
2560
2059
|
async listRequirements(invitation) {
|
|
@@ -2564,8 +2063,9 @@ var Engine = class Engine {
|
|
|
2564
2063
|
/**
|
|
2565
2064
|
* Lists missing requirements for the provided invitation.
|
|
2566
2065
|
*
|
|
2567
|
-
* @param {
|
|
2568
|
-
*
|
|
2066
|
+
* @param {XOInvitationIdentifier} invitationIdentifier - The identifier of the invitation to analyze.
|
|
2067
|
+
* The invitation must already exist in the store.
|
|
2068
|
+
* @returns {Promise<InvitationRequirementIdentifierLists>} The missing requirements.
|
|
2569
2069
|
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2570
2070
|
*/
|
|
2571
2071
|
async listMissingRequirements(invitationIdentifier) {
|
|
@@ -2579,7 +2079,7 @@ var Engine = class Engine {
|
|
|
2579
2079
|
* @returns {Promise<UnspentOutputData[]>} The list of unspent outputs.
|
|
2580
2080
|
*/
|
|
2581
2081
|
async listUnspentOutputsData() {
|
|
2582
|
-
return this
|
|
2082
|
+
return this.#state.listUnspentOutputsData();
|
|
2583
2083
|
}
|
|
2584
2084
|
/**
|
|
2585
2085
|
* Finds suitable resources for an invitation.
|
|
@@ -2590,19 +2090,30 @@ var Engine = class Engine {
|
|
|
2590
2090
|
* @param {XOInvitation} invitation - The invitation to find resources for.
|
|
2591
2091
|
* @param {GetSpendableResourcesParameters} options - Parameters for finding suitable resources.
|
|
2592
2092
|
* @returns {Promise<{ unspentOutputs: UnspentOutputData[] }>} The list of suitable resources.
|
|
2093
|
+
* @throws {FallbackLockingParametersMissingError} If options are omitted and fallback locking parameters are unset.
|
|
2593
2094
|
*/
|
|
2594
2095
|
async getSpendableResources(invitation, options) {
|
|
2595
2096
|
const { templateIdentifier } = invitation;
|
|
2596
|
-
if (await this
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2097
|
+
if (await this.#state.getTemplate(templateIdentifier) === void 0) throw new TemplateNotFoundInStoreError(templateIdentifier);
|
|
2098
|
+
let sourceTemplateIdentifier;
|
|
2099
|
+
let sourceOutputIdentifier;
|
|
2100
|
+
if (options !== void 0) {
|
|
2101
|
+
sourceTemplateIdentifier = options.templateIdentifier;
|
|
2102
|
+
sourceOutputIdentifier = options.outputIdentifier;
|
|
2103
|
+
} else {
|
|
2104
|
+
const walletSettings = await this.#state.getWalletSettings();
|
|
2105
|
+
if (walletSettings.fallbackLockingParameters !== void 0) {
|
|
2106
|
+
sourceTemplateIdentifier = walletSettings.fallbackLockingParameters.templateIdentifier;
|
|
2107
|
+
sourceOutputIdentifier = walletSettings.fallbackLockingParameters.outputIdentifier;
|
|
2108
|
+
} else throw new FallbackLockingParametersMissingError();
|
|
2109
|
+
}
|
|
2110
|
+
const rawSourceTemplate = await this.#state.getTemplate(sourceTemplateIdentifier);
|
|
2111
|
+
if (rawSourceTemplate === void 0) throw new TemplateNotFoundInStoreError(sourceTemplateIdentifier);
|
|
2601
2112
|
new Template(rawSourceTemplate).getOutputOrThrow(sourceOutputIdentifier);
|
|
2602
|
-
const scriptHashDataList = await this
|
|
2113
|
+
const scriptHashDataList = await this.#state.listScriptHashData({ templateIdentifier: sourceTemplateIdentifier });
|
|
2603
2114
|
const unspentOutputs = [];
|
|
2604
2115
|
for (const scriptHashData of scriptHashDataList) {
|
|
2605
|
-
const outputsByScriptHash = await this
|
|
2116
|
+
const outputsByScriptHash = await this.#state.listUnspentOutputsData({ scriptHash: scriptHashData.scriptHash });
|
|
2606
2117
|
const unspentOutputsByScriptHash = [];
|
|
2607
2118
|
for (const output of outputsByScriptHash) if (output.reservedBy === void 0) unspentOutputsByScriptHash.push(output);
|
|
2608
2119
|
unspentOutputs.push(...unspentOutputsByScriptHash);
|
|
@@ -2612,11 +2123,11 @@ var Engine = class Engine {
|
|
|
2612
2123
|
/**
|
|
2613
2124
|
* Retrieves a list of script hash data stored for a given template identifier.
|
|
2614
2125
|
*
|
|
2615
|
-
* @param {
|
|
2126
|
+
* @param {XOTemplateIdentifier} templateIdentifier - The identifier of the template.
|
|
2616
2127
|
* @returns {Promise<ScriptHashData[]>} An array of script hash data objects.
|
|
2617
2128
|
*/
|
|
2618
2129
|
async listScriptHashesForTemplate(templateIdentifier) {
|
|
2619
|
-
return this
|
|
2130
|
+
return this.#state.listScriptHashData({ templateIdentifier });
|
|
2620
2131
|
}
|
|
2621
2132
|
/**
|
|
2622
2133
|
* Gets all the script hashes from all the templates.
|
|
@@ -2624,7 +2135,7 @@ var Engine = class Engine {
|
|
|
2624
2135
|
* @returns {Promise<ScriptHashData[]>} An array of script hash data objects.
|
|
2625
2136
|
*/
|
|
2626
2137
|
async listScriptHashes() {
|
|
2627
|
-
const scriptHashDataPromises = (await this
|
|
2138
|
+
const scriptHashDataPromises = (await this.#state.listTemplates()).map((template) => generateTemplateIdentifier$1(template)).map((templateIdentifier) => this.listScriptHashesForTemplate(templateIdentifier));
|
|
2628
2139
|
return (await Promise.all(scriptHashDataPromises)).flat();
|
|
2629
2140
|
}
|
|
2630
2141
|
/**
|
|
@@ -2655,9 +2166,9 @@ var Engine = class Engine {
|
|
|
2655
2166
|
* TODO: If important to make this process faster, consider emitting an event and pushing process to the event loop.
|
|
2656
2167
|
* TODO: Encrypt the new information, update the encrypted state servers.
|
|
2657
2168
|
*
|
|
2658
|
-
* @param {
|
|
2659
|
-
* @param {
|
|
2660
|
-
* @param {
|
|
2169
|
+
* @param {XOTemplateIdentifier} templateIdentifier - Identifier of the template.
|
|
2170
|
+
* @param {XOTemplateOutputIdentifier} outputIdentifier - Identifier of the output (from the template).
|
|
2171
|
+
* @param {XOTemplateRoleIdentifier} roleIdentifier - Role identifier (corresponding to a role in the output's script).
|
|
2661
2172
|
* @returns {Promise<string>} The generated locking bytecode as a hexadecimal string.
|
|
2662
2173
|
* @throws {Error} If any required template, output, or configuration is not found, or if compilation fails.
|
|
2663
2174
|
*/
|
|
@@ -2674,6 +2185,7 @@ var Engine = class Engine {
|
|
|
2674
2185
|
if (lockingScriptForOutput.unlockingBytecode === void 0 || lockingScriptForOutput.lockingBytecode === void 0) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
2675
2186
|
const unlockingScripts = { [lockingScriptForOutput.unlockingBytecode]: lockingScriptForOutput.lockingBytecode };
|
|
2676
2187
|
const { lockingType } = lockingScriptForOutput;
|
|
2188
|
+
if (lockingType === void 0) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
2677
2189
|
const compiler = createCompilerBch({
|
|
2678
2190
|
entityOwnership,
|
|
2679
2191
|
scripts,
|
|
@@ -2681,16 +2193,18 @@ var Engine = class Engine {
|
|
|
2681
2193
|
unlockingScripts,
|
|
2682
2194
|
lockingScriptTypes: { lock: convertXOLockingTypeToLibauth(lockingType) }
|
|
2683
2195
|
});
|
|
2684
|
-
let lockingBytecodeMetadata = { lockingType: lockingScriptForOutput.lockingType };
|
|
2685
2196
|
let lockingData;
|
|
2197
|
+
let scriptHashSecrets;
|
|
2686
2198
|
switch (lockingScriptForOutput.lockingType) {
|
|
2687
|
-
case
|
|
2688
|
-
const { lockingData: standardLockingData, derivativeIndex: standardDerivativeIndex } = await this.generateLockingDataForStandardP2PKH(templateIdentifier,
|
|
2199
|
+
case XOTemplateLockingTypes.P2PKH: {
|
|
2200
|
+
const { lockingData: standardLockingData, derivativeIndex: standardDerivativeIndex, secretIdentifiers } = await this.generateLockingDataForStandardP2PKH(templateIdentifier, finalRoleIdentifier, lockingScriptForOutput);
|
|
2689
2201
|
lockingData = standardLockingData;
|
|
2690
|
-
|
|
2691
|
-
|
|
2202
|
+
const secrets = {};
|
|
2203
|
+
for (const secretIdentifier of secretIdentifiers) secrets[secretIdentifier] = {
|
|
2204
|
+
lockingType: lockingScriptForOutput.lockingType,
|
|
2692
2205
|
derivativeIndex: standardDerivativeIndex
|
|
2693
2206
|
};
|
|
2207
|
+
scriptHashSecrets = secrets;
|
|
2694
2208
|
break;
|
|
2695
2209
|
}
|
|
2696
2210
|
default: throw new TemplateLockingScriptTypeNotFoundError(output.lockingScript ?? outputIdentifier);
|
|
@@ -2704,14 +2218,14 @@ var Engine = class Engine {
|
|
|
2704
2218
|
if (!resultLockingBytecode.success) throw new LockingBytecodeGenerationFailedError();
|
|
2705
2219
|
const lockingBytecode = binToHex(resultLockingBytecode.bytecode);
|
|
2706
2220
|
const scriptHash = scriptToScriptHash(hexToBin(lockingBytecode));
|
|
2707
|
-
const existingScriptHashData = await this
|
|
2221
|
+
const existingScriptHashData = await this.#state.getScriptHashData(scriptHash);
|
|
2708
2222
|
if (existingScriptHashData) return existingScriptHashData.lockingBytecode;
|
|
2709
2223
|
const [firstElectrumClient] = electrumClients.values();
|
|
2710
2224
|
if (!firstElectrumClient) throw new ElectrumClientNotFoundError();
|
|
2711
2225
|
await subscribeToScriptHashUpdates(firstElectrumClient, scriptHash);
|
|
2712
|
-
await this
|
|
2226
|
+
await this.#state.storeScriptHashData({
|
|
2713
2227
|
lockingBytecode,
|
|
2714
|
-
|
|
2228
|
+
secrets: scriptHashSecrets,
|
|
2715
2229
|
scriptHash,
|
|
2716
2230
|
templateIdentifier,
|
|
2717
2231
|
outputIdentifier,
|
|
@@ -2721,49 +2235,52 @@ var Engine = class Engine {
|
|
|
2721
2235
|
return lockingBytecode;
|
|
2722
2236
|
}
|
|
2723
2237
|
/**
|
|
2724
|
-
* Generates a locking bytecode using the engine's
|
|
2238
|
+
* Generates a locking bytecode using the engine's fallback locking parameters.
|
|
2725
2239
|
*
|
|
2726
|
-
* @returns
|
|
2240
|
+
* @returns {Promise<string>} The generated locking bytecode as a hexadecimal string.
|
|
2241
|
+
* @throws {FallbackLockingParametersMissingError} If fallback locking parameters have not been set.
|
|
2727
2242
|
*/
|
|
2728
|
-
async
|
|
2729
|
-
|
|
2243
|
+
async generateLockingBytecodeFromFallbackParameters() {
|
|
2244
|
+
const walletSettings = await this.#state.getWalletSettings();
|
|
2245
|
+
if (walletSettings.fallbackLockingParameters === void 0) throw new FallbackLockingParametersMissingError();
|
|
2246
|
+
return this.generateLockingBytecode(walletSettings.fallbackLockingParameters.templateIdentifier, walletSettings.fallbackLockingParameters.outputIdentifier, walletSettings.fallbackLockingParameters.roleIdentifier);
|
|
2730
2247
|
}
|
|
2731
2248
|
/**
|
|
2732
2249
|
* Generate locking data and derivative index for P2PKH.
|
|
2733
2250
|
*/
|
|
2734
|
-
async generateLockingDataForStandardP2PKH(templateIdentifier,
|
|
2735
|
-
const
|
|
2736
|
-
const derivativeIndex = lastDerivativeIndex === null ? "0" : (parseInt(lastDerivativeIndex, 10) + 1).toString();
|
|
2251
|
+
async generateLockingDataForStandardP2PKH(templateIdentifier, roleIdentifier, lockingScriptForOutput) {
|
|
2252
|
+
const derivativeIndex = await this.#state.generateNextDerivativeIndex(templateIdentifier);
|
|
2737
2253
|
const privateKeyIdentifiers = (lockingScriptForOutput.roles?.[roleIdentifier])?.state?.secrets ?? [];
|
|
2738
2254
|
return {
|
|
2739
|
-
lockingData: this
|
|
2255
|
+
lockingData: this.#crypto.derivePrivateKeyRecordForCompiler({
|
|
2740
2256
|
compilerKeyIdentifiers: privateKeyIdentifiers,
|
|
2741
2257
|
templateIdentifier,
|
|
2742
|
-
templateKeyDerivationIndex:
|
|
2258
|
+
templateKeyDerivationIndex: derivativeIndex
|
|
2743
2259
|
}),
|
|
2744
|
-
derivativeIndex
|
|
2260
|
+
derivativeIndex,
|
|
2261
|
+
secretIdentifiers: privateKeyIdentifiers
|
|
2745
2262
|
};
|
|
2746
2263
|
}
|
|
2747
2264
|
/**
|
|
2748
2265
|
* Fetches a stored invitation by identifier
|
|
2749
2266
|
*
|
|
2750
|
-
* @param {
|
|
2267
|
+
* @param {XOInvitationIdentifier} invitationIdentifier - The identifier to look up.
|
|
2751
2268
|
* @returns {Promise<StoredInvitation | undefined>} The stored invitation, or undefined.
|
|
2752
2269
|
*/
|
|
2753
2270
|
async getInvitationOrThrow(invitationIdentifier) {
|
|
2754
|
-
const invitation = await this
|
|
2271
|
+
const invitation = await this.#state.getInvitation(invitationIdentifier);
|
|
2755
2272
|
if (invitation === void 0) throw new InvitationNotFoundInStoreError(invitationIdentifier);
|
|
2756
2273
|
return invitation;
|
|
2757
2274
|
}
|
|
2758
2275
|
/**
|
|
2759
2276
|
* Fetches a stored template by identifier
|
|
2760
2277
|
*
|
|
2761
|
-
* @param {
|
|
2278
|
+
* @param {XOTemplateIdentifier} templateIdentifier - The identifier to look up.
|
|
2762
2279
|
* @returns {Promise<XOTemplate>} The stored template.
|
|
2763
2280
|
*/
|
|
2764
2281
|
async getTemplateOrThrow(templateIdentifier) {
|
|
2765
|
-
const template = await this
|
|
2766
|
-
if (template === void 0) throw new
|
|
2282
|
+
const template = await this.#state.getTemplate(templateIdentifier);
|
|
2283
|
+
if (template === void 0) throw new TemplateNotFoundInStoreError(templateIdentifier);
|
|
2767
2284
|
return template;
|
|
2768
2285
|
}
|
|
2769
2286
|
/**
|
|
@@ -2777,13 +2294,13 @@ var Engine = class Engine {
|
|
|
2777
2294
|
* 5. Adds any new unspent outputs found on the blockchain but missing from the store to the store.
|
|
2778
2295
|
* 6. Removes any unspent outputs present in the store but no longer in the blockchain.
|
|
2779
2296
|
*
|
|
2780
|
-
* @param scriptHash - The script hash for which to update the stored unspent outputs.
|
|
2781
|
-
* @throws If the script hash is not found in the store.
|
|
2297
|
+
* @param {string} scriptHash - The script hash for which to update the stored unspent outputs.
|
|
2298
|
+
* @throws {ScriptHashDataNotFoundError} If the script hash is not found in the store.
|
|
2782
2299
|
*/
|
|
2783
2300
|
async updateUnspentOutputsForScriptHashInStore(scriptHash) {
|
|
2784
2301
|
const [firstElectrumClient] = electrumClients.values();
|
|
2785
2302
|
if (!firstElectrumClient) throw new ElectrumClientNotFoundError();
|
|
2786
|
-
await updateUnspentOutputsForScriptHashInStore(this
|
|
2303
|
+
await updateUnspentOutputsForScriptHashInStore(this.#state, scriptHash, async (scripthash) => {
|
|
2787
2304
|
return fetchScriptHashUnspentTransactionOutputs(firstElectrumClient, scripthash);
|
|
2788
2305
|
});
|
|
2789
2306
|
}
|
|
@@ -2795,7 +2312,7 @@ var Engine = class Engine {
|
|
|
2795
2312
|
* @param {XOInvitationInput[]} inputs - The inputs to find resources for.
|
|
2796
2313
|
* @returns {Promise<XOInvitationInput[]>} The list of own inputs for reservation.
|
|
2797
2314
|
*/
|
|
2798
|
-
async findOwnResources(inputs) {
|
|
2315
|
+
async #findOwnResources(inputs) {
|
|
2799
2316
|
const inputsForReservation = [];
|
|
2800
2317
|
if (inputs) for (const input of inputs) {
|
|
2801
2318
|
if (input.mergesWith) continue;
|
|
@@ -2817,15 +2334,16 @@ var Engine = class Engine {
|
|
|
2817
2334
|
* @param {AppendInvitationParameters} params - The parameters to append.
|
|
2818
2335
|
* @returns {Promise<XOInvitation>} The updated invitation after the new commit is applied.
|
|
2819
2336
|
*/
|
|
2820
|
-
async applyInvitationAppend(storedInvitation, params) {
|
|
2337
|
+
async #applyInvitationAppend(storedInvitation, params) {
|
|
2338
|
+
if (storedInvitation.entityIdentifier === void 0) throw new EntityIdentifierNotFoundInInvitationError(storedInvitation.invitationIdentifier);
|
|
2821
2339
|
const rootCommitIdentifier = findRootCommitIdentifierInInvitation(storedInvitation, storedInvitation.entityIdentifier);
|
|
2822
|
-
if (rootCommitIdentifier === void 0) throw new
|
|
2340
|
+
if (rootCommitIdentifier === void 0) throw new RootCommitNotFoundInInvitationError(storedInvitation.invitationIdentifier);
|
|
2823
2341
|
if (params.outputs !== void 0) for (const output of params.outputs) {
|
|
2824
2342
|
if (output.mergesWith !== void 0 || output.roleIdentifier !== void 0 || output.lockingBytecode !== void 0) continue;
|
|
2825
|
-
output.lockingBytecode = hexToBin(await this.
|
|
2343
|
+
output.lockingBytecode = hexToBin(await this.generateLockingBytecodeFromFallbackParameters());
|
|
2826
2344
|
}
|
|
2827
2345
|
const invitationBuilder = InvitationBuilder.fromInvitation(storedInvitation);
|
|
2828
|
-
const signedCommit = this
|
|
2346
|
+
const signedCommit = this.#crypto.signInvitationCommitWithSchnorr({
|
|
2829
2347
|
invitationCommit: invitationBuilder.createUnsignedCommit({ ...params }),
|
|
2830
2348
|
invitationIdentifier: invitationBuilder.invitationIdentifier,
|
|
2831
2349
|
rootCommitIdentifier,
|
|
@@ -2833,23 +2351,20 @@ var Engine = class Engine {
|
|
|
2833
2351
|
actionIdentifier: invitationBuilder.actionIdentifier
|
|
2834
2352
|
});
|
|
2835
2353
|
invitationBuilder.addCommit(signedCommit);
|
|
2836
|
-
const inputsForReservation = await this
|
|
2837
|
-
if (inputsForReservation.length > 0) await this
|
|
2354
|
+
const inputsForReservation = await this.#findOwnResources(params.inputs ?? []);
|
|
2355
|
+
if (inputsForReservation.length > 0) await this.#reserveResources(inputsForReservation, invitationBuilder.invitationIdentifier);
|
|
2838
2356
|
const updatedInvitation = invitationBuilder.build();
|
|
2839
|
-
await this
|
|
2840
|
-
...updatedInvitation,
|
|
2841
|
-
entityIdentifier: storedInvitation.entityIdentifier
|
|
2842
|
-
});
|
|
2357
|
+
await this.#state.updateInvitation(updatedInvitation.invitationIdentifier, updatedInvitation);
|
|
2843
2358
|
return updatedInvitation;
|
|
2844
2359
|
}
|
|
2845
2360
|
/**
|
|
2846
2361
|
* Reserves utxos and store information in the storage.
|
|
2847
2362
|
*
|
|
2848
2363
|
* @param {XOInvitationInput[]} inputs - The inputs to reserve.
|
|
2849
|
-
* @param {
|
|
2364
|
+
* @param {XOInvitationIdentifier} invitationIdentifier - The identifier of the invitation for which the resources are being reserved.
|
|
2850
2365
|
* @returns {Promise<void>} Resolves when the reservation is complete.
|
|
2851
2366
|
*/
|
|
2852
|
-
async reserveResources(inputs, invitationIdentifier) {
|
|
2367
|
+
async #reserveResources(inputs, invitationIdentifier) {
|
|
2853
2368
|
const outpoints = [];
|
|
2854
2369
|
for (const input of inputs) {
|
|
2855
2370
|
if (!input.outpointTransactionHash || input.outpointIndex === void 0) throw new InvalidInputError();
|
|
@@ -2858,10 +2373,10 @@ var Engine = class Engine {
|
|
|
2858
2373
|
outpointIndex: input.outpointIndex
|
|
2859
2374
|
});
|
|
2860
2375
|
}
|
|
2861
|
-
await this
|
|
2376
|
+
await this.#state.executeBulkUnspentOutputReservation(outpoints, true, invitationIdentifier);
|
|
2862
2377
|
}
|
|
2863
2378
|
};
|
|
2864
2379
|
|
|
2865
2380
|
//#endregion
|
|
2866
|
-
export { BlockchainMonitor,
|
|
2381
|
+
export { BlockchainMonitor, CircularMergeReferenceError, CommitEntityIdentifierNotFoundError, CommitNotFoundInInvitationError, CommitNotSignedError, DuplicateTransactionIndexError, DuplicateTransactionLocktimeError, DuplicateTransactionVersionError, ElectrumClientNotFoundError, Engine, EntityIdentifierNotFoundInInvitationError, FallbackLockingParametersMissingError, InvalidCommitSignaturesError, InvalidEntityIdentifierError, InvalidInputError, InvalidLockingScriptTypeError, InvalidLockingTypeError, InvalidRequirementsError, InvalidVariableDefinitionError, InvitationBuilder, InvitationExpiredError, InvitationTemplateMismatchError, ItemNotFoundInCommitError, LockingBytecodeGenerationFailedError, LockingBytecodeNotFoundForOutputError, LockingDataNotFoundError, MergedCommitNotFoundError, MergesWithCommitNotFoundError, P2PKHDerivativeIndexMissingError, ParentCommitNotFoundInInvitationError, RootCommitNotFoundInInvitationError, ScriptHashDataNotFoundError, Template, TemplateActionNotFoundError, TemplateLockingScriptInOutputNotFoundError, TemplateLockingScriptNotFoundError, TemplateLockingScriptRoleNotFoundError, TemplateLockingScriptTypeNotFoundError, TemplateOutputNotFoundError, TemplateTransactionNotFoundError, TransactionGenerationFailedError, UnlockingScriptNotFoundError, assignTypeToVariablesForLibauthCompiler, convertCommitVariablesIntoObject, convertXOLockingTypeToLibauth, createLibauthCompilerDataFromUnspentOutput, entityOwnershipForLibauthCompiler, findRootCommitIdentifierInInvitation, generateCommitIdentifier, generateTemplateIdentifier, hasInvitationExpired, listAvailableRolesForInvitation, listMissingRequirements, listPopulatedRequirementsInInvitation, listRequirementsFromTemplate, mergeInvitationCommits, parseTemplate, scriptsForLibauthCompiler, updateUnspentOutputsForScriptHashInStore, validateInvitationCommits, validateInvitationEntities, validateRoleRequirements };
|
|
2867
2382
|
//# sourceMappingURL=index.mjs.map
|