@xo-cash/engine 0.0.1-development.14146115040 → 0.0.1-development.14148570389
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 +34 -24
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +122 -110
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -836,16 +836,15 @@ const convertXOLockingTypeToLibauth = (lockingType) => {
|
|
|
836
836
|
*/
|
|
837
837
|
const createLibauthCompilerDataFromUnspentOutput = async (template, unspentOutput) => {
|
|
838
838
|
const { outputIdentifier } = unspentOutput;
|
|
839
|
-
const output = template.
|
|
840
|
-
const lockingScriptForOutput = template.
|
|
839
|
+
const output = template.getOutputOrThrow(outputIdentifier);
|
|
840
|
+
const lockingScriptForOutput = template.getLockingScriptForOutputOrThrow(outputIdentifier);
|
|
841
841
|
if (lockingScriptForOutput.lockingBytecode === void 0) throw new TemplateLockingScriptInOutputNotFoundError(outputIdentifier);
|
|
842
|
-
template.validateLockingScript(lockingScriptForOutput, output.lockingScript);
|
|
843
842
|
const lockingScriptIdentifier = output.lockingScript;
|
|
844
843
|
let roleIdentifier = "owner";
|
|
845
844
|
try {
|
|
846
|
-
template.
|
|
845
|
+
template.getRoleOrThrow(lockingScriptIdentifier, roleIdentifier);
|
|
847
846
|
} catch {
|
|
848
|
-
const [firstRoleId] = template.
|
|
847
|
+
const [firstRoleId] = template.getFirstRoleOrThrow(lockingScriptIdentifier);
|
|
849
848
|
roleIdentifier = firstRoleId;
|
|
850
849
|
}
|
|
851
850
|
const entityOwnership = entityOwnershipForLibauthCompiler(template.getTemplate(), roleIdentifier);
|
|
@@ -1398,9 +1397,10 @@ var Template = class {
|
|
|
1398
1397
|
* Retrieves an output definition by identifier.
|
|
1399
1398
|
* @throws {TemplateOutputNotFoundError} If the output is not found.
|
|
1400
1399
|
*/
|
|
1401
|
-
|
|
1400
|
+
getOutputOrThrow(outputIdentifier) {
|
|
1402
1401
|
const output = this.template.outputs[outputIdentifier];
|
|
1403
1402
|
if (output === void 0) throw new TemplateOutputNotFoundError(outputIdentifier);
|
|
1403
|
+
if (!output.lockingScript) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
1404
1404
|
return output;
|
|
1405
1405
|
}
|
|
1406
1406
|
/**
|
|
@@ -1408,8 +1408,8 @@ var Template = class {
|
|
|
1408
1408
|
* @throws {TemplateOutputNotFoundError} If the output is not found.
|
|
1409
1409
|
* @throws {TemplateLockingScriptInOutputNotFoundError} If the output has no locking script reference.
|
|
1410
1410
|
*/
|
|
1411
|
-
|
|
1412
|
-
const output = this.
|
|
1411
|
+
getLockingScriptIdentifierOrThrow(outputIdentifier) {
|
|
1412
|
+
const output = this.getOutputOrThrow(outputIdentifier);
|
|
1413
1413
|
if (output.lockingScript === void 0) throw new TemplateLockingScriptInOutputNotFoundError(outputIdentifier);
|
|
1414
1414
|
return output.lockingScript;
|
|
1415
1415
|
}
|
|
@@ -1417,9 +1417,10 @@ var Template = class {
|
|
|
1417
1417
|
* Retrieves a locking script definition by identifier.
|
|
1418
1418
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1419
1419
|
*/
|
|
1420
|
-
|
|
1420
|
+
getLockingScriptOrThrow(lockingScriptIdentifier) {
|
|
1421
1421
|
const lockingScript = this.template.lockingScripts[lockingScriptIdentifier];
|
|
1422
1422
|
if (lockingScript === void 0) throw new TemplateLockingScriptNotFoundError(lockingScriptIdentifier);
|
|
1423
|
+
if (lockingScript.lockingType === void 0) throw new TemplateLockingScriptTypeNotFoundError(lockingScriptIdentifier);
|
|
1423
1424
|
return lockingScript;
|
|
1424
1425
|
}
|
|
1425
1426
|
/**
|
|
@@ -1428,18 +1429,9 @@ var Template = class {
|
|
|
1428
1429
|
* @throws {TemplateLockingScriptInOutputNotFoundError} If the output has no locking script reference.
|
|
1429
1430
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script definition is not found.
|
|
1430
1431
|
*/
|
|
1431
|
-
|
|
1432
|
-
const lockingScriptIdentifier = this.
|
|
1433
|
-
return this.
|
|
1434
|
-
}
|
|
1435
|
-
/**
|
|
1436
|
-
* Validates that a locking script has required unlocking script and locking type.
|
|
1437
|
-
* @param lockingScript - The locking script to validate.
|
|
1438
|
-
* @param lockingScriptIdentifier - The identifier of the locking script (for error messages).
|
|
1439
|
-
* @throws {TemplateLockingScriptTypeNotFoundError} If lockingType is missing.
|
|
1440
|
-
*/
|
|
1441
|
-
validateLockingScript(lockingScript, lockingScriptIdentifier) {
|
|
1442
|
-
if (lockingScript.lockingType === void 0) throw new TemplateLockingScriptTypeNotFoundError(lockingScriptIdentifier);
|
|
1432
|
+
getLockingScriptForOutputOrThrow(outputIdentifier) {
|
|
1433
|
+
const lockingScriptIdentifier = this.getLockingScriptIdentifierOrThrow(outputIdentifier);
|
|
1434
|
+
return this.getLockingScriptOrThrow(lockingScriptIdentifier);
|
|
1443
1435
|
}
|
|
1444
1436
|
/**
|
|
1445
1437
|
* Retrieves a role within a locking script.
|
|
@@ -1448,8 +1440,8 @@ var Template = class {
|
|
|
1448
1440
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1449
1441
|
* @throws {TemplateLockingScriptRoleNotFoundError} If the role is not found.
|
|
1450
1442
|
*/
|
|
1451
|
-
|
|
1452
|
-
const role = this.
|
|
1443
|
+
getRoleOrThrow(lockingScriptIdentifier, roleIdentifier) {
|
|
1444
|
+
const role = this.getLockingScriptOrThrow(lockingScriptIdentifier).roles?.[roleIdentifier];
|
|
1453
1445
|
if (role === void 0) throw new TemplateLockingScriptRoleNotFoundError(lockingScriptIdentifier, roleIdentifier);
|
|
1454
1446
|
return role;
|
|
1455
1447
|
}
|
|
@@ -1460,8 +1452,8 @@ var Template = class {
|
|
|
1460
1452
|
* @throws {TemplateLockingScriptNotFoundError} If the locking script is not found.
|
|
1461
1453
|
* @throws {TemplateLockingScriptRoleNotFoundError} If no roles exist.
|
|
1462
1454
|
*/
|
|
1463
|
-
|
|
1464
|
-
const roles = this.
|
|
1455
|
+
getFirstRoleOrThrow(lockingScriptIdentifier) {
|
|
1456
|
+
const roles = this.getLockingScriptOrThrow(lockingScriptIdentifier).roles ?? {};
|
|
1465
1457
|
const roleKeys = Object.keys(roles);
|
|
1466
1458
|
if (roleKeys.length === 0) throw new TemplateLockingScriptRoleNotFoundError(lockingScriptIdentifier, "");
|
|
1467
1459
|
return [roleKeys[0], roles[roleKeys[0]]];
|
|
@@ -1477,8 +1469,8 @@ var Template = class {
|
|
|
1477
1469
|
* @throws {TemplateLockingScriptRoleNotFoundError} If roleIdentifier is provided but the role is not found.
|
|
1478
1470
|
*/
|
|
1479
1471
|
getSecrets(lockingScriptIdentifier, roleIdentifier) {
|
|
1480
|
-
if (roleIdentifier !== void 0) return this.
|
|
1481
|
-
return this.
|
|
1472
|
+
if (roleIdentifier !== void 0) return this.getRoleOrThrow(lockingScriptIdentifier, roleIdentifier).state?.secrets ?? [];
|
|
1473
|
+
return this.getLockingScriptOrThrow(lockingScriptIdentifier).state?.secrets ?? [];
|
|
1482
1474
|
}
|
|
1483
1475
|
/**
|
|
1484
1476
|
* Retrieves an action definition by identifier.
|
|
@@ -1495,7 +1487,7 @@ var Template = class {
|
|
|
1495
1487
|
* @param transactionIdentifier - The identifier of the transaction.
|
|
1496
1488
|
* @throws {TemplateTransactionNotFoundError} If the transaction is not found.
|
|
1497
1489
|
*/
|
|
1498
|
-
|
|
1490
|
+
getTransactionOrThrow(transactionIdentifier) {
|
|
1499
1491
|
const transaction = this.template.transactions?.[transactionIdentifier];
|
|
1500
1492
|
if (transaction === void 0) throw new TemplateTransactionNotFoundError(transactionIdentifier);
|
|
1501
1493
|
return transaction;
|
|
@@ -1506,10 +1498,10 @@ var Template = class {
|
|
|
1506
1498
|
* @throws {TemplateActionNotFoundError} If the action is not found.
|
|
1507
1499
|
* @throws {TemplateTransactionNotFoundError} If the action has no transaction defined or the transaction is not found.
|
|
1508
1500
|
*/
|
|
1509
|
-
|
|
1501
|
+
getTransactionForActionOrThrow(actionIdentifier) {
|
|
1510
1502
|
const action = this.getActionOrThrow(actionIdentifier);
|
|
1511
1503
|
if (action.transaction === void 0) throw new TemplateTransactionNotFoundError(actionIdentifier);
|
|
1512
|
-
return this.
|
|
1504
|
+
return this.getTransactionOrThrow(action.transaction);
|
|
1513
1505
|
}
|
|
1514
1506
|
};
|
|
1515
1507
|
|
|
@@ -1676,7 +1668,7 @@ const resolveOutputFromTemplate = (output, variables, templateInstance) => {
|
|
|
1676
1668
|
if (output.lockingBytecode !== void 0) resolvedOutput.lockingBytecode = typeof output.lockingBytecode === "string" ? hexToBin(output.lockingBytecode) : output.lockingBytecode;
|
|
1677
1669
|
return resolvedOutput;
|
|
1678
1670
|
}
|
|
1679
|
-
const templateOutput = templateInstance.
|
|
1671
|
+
const templateOutput = templateInstance.getOutputOrThrow(output.outputIdentifier);
|
|
1680
1672
|
if (resolvedOutput.valueSatoshis === void 0) {
|
|
1681
1673
|
if (templateOutput.valueSatoshis && isCashAssemblyExpression(templateOutput.valueSatoshis)) {
|
|
1682
1674
|
const variablesObject = convertCommitVariablesIntoObject(variables);
|
|
@@ -1855,7 +1847,7 @@ const listRequirementsFromTemplate = (template, action) => {
|
|
|
1855
1847
|
if (roleVariables) roleRequirements[roleIdentifier].variables = roleVariables;
|
|
1856
1848
|
}
|
|
1857
1849
|
if (action.transaction) {
|
|
1858
|
-
const transactionDefinition = new Template(template).
|
|
1850
|
+
const transactionDefinition = new Template(template).getTransactionOrThrow(action.transaction);
|
|
1859
1851
|
if (transactionDefinition.roles) for (const [roleIdentifier, transactionRoleRequirement] of Object.entries(transactionDefinition.roles)) {
|
|
1860
1852
|
if (!roleRequirements[roleIdentifier]) roleRequirements[roleIdentifier] = {};
|
|
1861
1853
|
if (transactionRoleRequirement.inputs) {
|
|
@@ -2340,7 +2332,10 @@ var Engine = class Engine {
|
|
|
2340
2332
|
* Initializes the state sync process.
|
|
2341
2333
|
* @returns {Promise<void>} Resolves when the state sync is initialized.
|
|
2342
2334
|
*/
|
|
2343
|
-
async initializeStateSync() {
|
|
2335
|
+
async initializeStateSync() {
|
|
2336
|
+
const scriptHashDataList = await this.listScriptHashes();
|
|
2337
|
+
for (const scriptHashData of scriptHashDataList) await this.updateUnspentOutputsForScriptHashInStore(scriptHashData.scriptHash);
|
|
2338
|
+
}
|
|
2344
2339
|
/**
|
|
2345
2340
|
* Creates an invitation that can be shared with other entities, user, application, etc.
|
|
2346
2341
|
* Invitation is the fundamental communication unit in XO, all the interactions done using the
|
|
@@ -2412,92 +2407,72 @@ var Engine = class Engine {
|
|
|
2412
2407
|
/**
|
|
2413
2408
|
* Appends the invitation with the new information.
|
|
2414
2409
|
*
|
|
2415
|
-
* @param {
|
|
2410
|
+
* @param {string} invitationIdentifier - The identifier of the invitation to append. The invitation must already exist in the store.
|
|
2416
2411
|
* @param {AppendInvitationParameters} params - The parameters to append the invitation with.
|
|
2417
2412
|
* @returns {Promise<XOInvitation>} The appended invitation.
|
|
2413
|
+
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2418
2414
|
*/
|
|
2419
|
-
async appendInvitation(
|
|
2420
|
-
const invitationFromStore = await this.getInvitationOrThrow(
|
|
2421
|
-
|
|
2422
|
-
if (rootCommitIdentifier === void 0) throw new RootCommitNotFoundError(invitationFromStore.entityIdentifier);
|
|
2423
|
-
const invitationBuilder = InvitationBuilder.fromInvitation(invitation);
|
|
2424
|
-
if (params.outputs) for (const output of params.outputs) {
|
|
2425
|
-
if (output.mergesWith !== void 0 || output.roleIdentifier !== void 0 || output.lockingBytecode !== void 0) continue;
|
|
2426
|
-
output.lockingBytecode = hexToBin(await this.generateLockingBytecodeForDefaultParameters());
|
|
2427
|
-
}
|
|
2428
|
-
const signedCommit = this.crypto.signInvitationCommitWithSchnorr({
|
|
2429
|
-
invitationCommit: invitationBuilder.createUnsignedCommit({ ...params }),
|
|
2430
|
-
invitationIdentifier: invitationBuilder.invitationIdentifier,
|
|
2431
|
-
rootCommitIdentifier,
|
|
2432
|
-
templateIdentifier: invitationBuilder.templateIdentifier,
|
|
2433
|
-
actionIdentifier: invitationBuilder.actionIdentifier
|
|
2434
|
-
});
|
|
2435
|
-
invitationBuilder.addCommit(signedCommit);
|
|
2436
|
-
const inputsForReservation = await this.findOwnResources(params.inputs ?? []);
|
|
2437
|
-
if (inputsForReservation.length > 0) await this.reserveResources(inputsForReservation, invitationBuilder.invitationIdentifier);
|
|
2438
|
-
const updatedInvitation = invitationBuilder.build();
|
|
2439
|
-
await this.state.storeInvitation(updatedInvitation.invitationIdentifier, {
|
|
2440
|
-
...updatedInvitation,
|
|
2441
|
-
entityIdentifier: invitationFromStore.entityIdentifier
|
|
2442
|
-
});
|
|
2443
|
-
return updatedInvitation;
|
|
2415
|
+
async appendInvitation(invitationIdentifier, params) {
|
|
2416
|
+
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2417
|
+
return this.applyInvitationAppend(invitationFromStore, params);
|
|
2444
2418
|
}
|
|
2445
2419
|
/**
|
|
2446
2420
|
* Signs the provided invitation once all the resources are reserved and the invitation is complete.
|
|
2447
2421
|
* This method is used to sign own inputs and provide unlocking data for them, this appends the
|
|
2448
2422
|
* invitation and adds new commit with unlocking data.
|
|
2449
2423
|
*
|
|
2450
|
-
* @param {
|
|
2424
|
+
* @param {string} invitationIdentifier - The identifier of the invitation to sign. The invitation must already exist in the store.
|
|
2451
2425
|
* @returns {Promise<XOInvitation>} Resolves when signing is complete.
|
|
2426
|
+
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2452
2427
|
*
|
|
2453
2428
|
* TODO: Add all the throws that happen during the execution of the method.
|
|
2454
2429
|
*/
|
|
2455
|
-
async signInvitation(
|
|
2456
|
-
const invitationFromStore = await this.getInvitationOrThrow(
|
|
2430
|
+
async signInvitation(invitationIdentifier) {
|
|
2431
|
+
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2457
2432
|
const template = await this.getTemplateOrThrow(invitationFromStore.templateIdentifier);
|
|
2458
2433
|
const mergedCommit = mergeInvitationCommits(invitationFromStore, template);
|
|
2459
|
-
if (
|
|
2434
|
+
if (mergedCommit === void 0) throw new MergedCommitNotFoundError();
|
|
2460
2435
|
const transactionResult = generateTransaction({
|
|
2461
2436
|
version: mergedCommit.transactionVersion,
|
|
2462
2437
|
locktime: mergedCommit.transactionLocktime,
|
|
2463
2438
|
inputs: mergedCommit.inputs,
|
|
2464
2439
|
outputs: mergedCommit.outputs
|
|
2465
2440
|
});
|
|
2466
|
-
if (
|
|
2441
|
+
if (transactionResult.success === false) throw new TransactionGenerationFailedError();
|
|
2467
2442
|
const unspentOutputs = await this.state.listUnspentOutputsData({ reservedBy: invitationFromStore.invitationIdentifier });
|
|
2468
2443
|
for (const [index, input] of transactionResult.transaction.inputs.entries()) {
|
|
2469
2444
|
let unspentOutput;
|
|
2470
|
-
for (const sourceOutput of unspentOutputs) if (sourceOutput.outpointTransactionHash === binToHex(input.outpointTransactionHash) && sourceOutput.outpointIndex === input.outpointIndex && sourceOutput.reservedBy
|
|
2445
|
+
for (const sourceOutput of unspentOutputs) if (sourceOutput.outpointTransactionHash === binToHex(input.outpointTransactionHash) && sourceOutput.outpointIndex === input.outpointIndex && sourceOutput.reservedBy) {
|
|
2471
2446
|
unspentOutput = sourceOutput;
|
|
2472
2447
|
break;
|
|
2473
2448
|
}
|
|
2474
2449
|
if (!unspentOutput) continue;
|
|
2475
2450
|
const scriptHashData = await this.state.getScriptHashData(unspentOutput.scriptHash);
|
|
2476
2451
|
if (scriptHashData === void 0) throw new ScriptHashDataNotFoundError(unspentOutput.scriptHash);
|
|
2477
|
-
let
|
|
2478
|
-
if (scriptHashData.derivationSource) {
|
|
2479
|
-
if (scriptHashData.derivationSource.lockingType === "
|
|
2452
|
+
let compilerPrivateKeyRecord;
|
|
2453
|
+
if (scriptHashData.derivationSource !== void 0) {
|
|
2454
|
+
if (scriptHashData.derivationSource.lockingType === "p2pkh") {
|
|
2480
2455
|
const templateDataForInput = await this.state.getTemplate(scriptHashData.templateIdentifier);
|
|
2481
|
-
if (
|
|
2456
|
+
if (templateDataForInput === void 0) throw new TemplateNotFoundError(scriptHashData.templateIdentifier);
|
|
2482
2457
|
const templateInstanceForInput = new Template(templateDataForInput);
|
|
2483
|
-
const lockingScriptIdentifier = templateInstanceForInput.
|
|
2458
|
+
const lockingScriptIdentifier = templateInstanceForInput.getLockingScriptIdentifierOrThrow(scriptHashData.outputIdentifier);
|
|
2484
2459
|
const secretIdentifiers = templateInstanceForInput.getSecrets(lockingScriptIdentifier, scriptHashData.roleIdentifier);
|
|
2485
2460
|
const { derivativeIndex } = scriptHashData.derivationSource;
|
|
2486
2461
|
if (typeof derivativeIndex !== "string") throw new InvalidDerivativeIndexTypeError("string", typeof derivativeIndex);
|
|
2487
|
-
|
|
2462
|
+
compilerPrivateKeyRecord = this.crypto.derivePrivateKeyRecordForCompiler({
|
|
2488
2463
|
compilerKeyIdentifiers: secretIdentifiers,
|
|
2489
2464
|
templateIdentifier: scriptHashData.templateIdentifier,
|
|
2490
2465
|
templateKeyDerivationIndex: Number(derivativeIndex)
|
|
2491
2466
|
});
|
|
2492
2467
|
}
|
|
2493
2468
|
}
|
|
2494
|
-
if (
|
|
2469
|
+
if (compilerPrivateKeyRecord === void 0) throw new LockingDataNotFoundError();
|
|
2495
2470
|
const compilerData = await createLibauthCompilerDataFromUnspentOutput(new Template(template), unspentOutput);
|
|
2496
2471
|
transactionResult.transaction.inputs[index] = {
|
|
2497
2472
|
...input,
|
|
2498
2473
|
unlockingBytecode: {
|
|
2499
2474
|
...compilerData,
|
|
2500
|
-
data
|
|
2475
|
+
data: compilerPrivateKeyRecord
|
|
2501
2476
|
}
|
|
2502
2477
|
};
|
|
2503
2478
|
}
|
|
@@ -2507,47 +2482,51 @@ var Engine = class Engine {
|
|
|
2507
2482
|
inputs: transactionResult.transaction.inputs,
|
|
2508
2483
|
outputs: transactionResult.transaction.outputs
|
|
2509
2484
|
});
|
|
2510
|
-
if (
|
|
2511
|
-
let
|
|
2485
|
+
if (signedTransaction.success === false) throw new TransactionGenerationFailedError();
|
|
2486
|
+
let currentStoredInvitation = invitationFromStore;
|
|
2512
2487
|
for (const input of signedTransaction.transaction.inputs.values()) {
|
|
2513
2488
|
let unspentOutput;
|
|
2514
2489
|
for (const sourceOutput of unspentOutputs) if (sourceOutput.outpointTransactionHash === binToHex(input.outpointTransactionHash) && sourceOutput.outpointIndex === input.outpointIndex) {
|
|
2515
2490
|
unspentOutput = sourceOutput;
|
|
2516
2491
|
break;
|
|
2517
2492
|
}
|
|
2518
|
-
if (
|
|
2493
|
+
if (unspentOutput === void 0) throw new UnspentOutputNotFoundError();
|
|
2519
2494
|
let commitToExtend;
|
|
2520
2495
|
let indexToExtend;
|
|
2521
|
-
for (const commit of
|
|
2496
|
+
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)) {
|
|
2522
2497
|
commitToExtend = commit;
|
|
2523
2498
|
indexToExtend = index;
|
|
2524
2499
|
}
|
|
2525
2500
|
if (commitToExtend === void 0 || indexToExtend === void 0) throw new CommitNotFoundInInvitationError();
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2501
|
+
currentStoredInvitation = {
|
|
2502
|
+
...await this.applyInvitationAppend(currentStoredInvitation, { inputs: [{
|
|
2503
|
+
mergesWith: {
|
|
2504
|
+
commitIdentifier: commitToExtend.commitIdentifier,
|
|
2505
|
+
index: indexToExtend
|
|
2506
|
+
},
|
|
2507
|
+
unlockingBytecode: input.unlockingBytecode
|
|
2508
|
+
}] }),
|
|
2509
|
+
entityIdentifier: invitationFromStore.entityIdentifier
|
|
2510
|
+
};
|
|
2533
2511
|
}
|
|
2534
|
-
return
|
|
2512
|
+
return currentStoredInvitation;
|
|
2535
2513
|
}
|
|
2536
2514
|
/**
|
|
2537
2515
|
* Executes an action for a given invitation.
|
|
2538
|
-
* @param {
|
|
2516
|
+
* @param {string} invitationIdentifier - The identifier of the invitation whose action to execute.
|
|
2517
|
+
* The invitation must already exist in the store.
|
|
2539
2518
|
* @param {ExecuteActionParameters} params - Parameters for executing the action.
|
|
2540
|
-
* @returns {Promise<
|
|
2519
|
+
* @returns {Promise<TransactionHash>} Resolves with the transaction hash when the action is executed.
|
|
2520
|
+
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2541
2521
|
*
|
|
2542
2522
|
* TODO: Add all the throws that happen during the execution of the method.
|
|
2543
2523
|
*/
|
|
2544
|
-
async executeAction(
|
|
2545
|
-
const
|
|
2546
|
-
const
|
|
2547
|
-
const
|
|
2548
|
-
|
|
2549
|
-
if (
|
|
2550
|
-
if (action.transaction === void 0) throw new TemplateTransactionNotFoundError(builder.actionIdentifier);
|
|
2524
|
+
async executeAction(invitationIdentifier, params) {
|
|
2525
|
+
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2526
|
+
const template = await this.getTemplateOrThrow(invitationFromStore.templateIdentifier);
|
|
2527
|
+
const mergedCommit = mergeInvitationCommits(invitationFromStore, template);
|
|
2528
|
+
if (mergedCommit === void 0) throw new MergedCommitNotFoundError();
|
|
2529
|
+
if (new Template(template).getActionOrThrow(invitationFromStore.actionIdentifier).transaction === void 0) throw new TemplateTransactionNotFoundError(invitationFromStore.actionIdentifier);
|
|
2551
2530
|
const resultTransaction = generateTransaction({
|
|
2552
2531
|
version: mergedCommit.transactionVersion,
|
|
2553
2532
|
locktime: mergedCommit.transactionLocktime,
|
|
@@ -2585,12 +2564,14 @@ var Engine = class Engine {
|
|
|
2585
2564
|
/**
|
|
2586
2565
|
* Lists missing requirements for the provided invitation.
|
|
2587
2566
|
*
|
|
2588
|
-
* @param {
|
|
2567
|
+
* @param {string} invitationIdentifier - The identifier of the invitation to analyze. The invitation must already exist in the store.
|
|
2589
2568
|
* @returns {Promise<XOInvitationRequirements>} The missing requirements.
|
|
2569
|
+
* @throws {InvitationNotFoundInStoreError} If the invitation is not found in the store.
|
|
2590
2570
|
*/
|
|
2591
|
-
async listMissingRequirements(
|
|
2592
|
-
const
|
|
2593
|
-
|
|
2571
|
+
async listMissingRequirements(invitationIdentifier) {
|
|
2572
|
+
const invitationFromStore = await this.getInvitationOrThrow(invitationIdentifier);
|
|
2573
|
+
const template = await this.getTemplateOrThrow(invitationFromStore.templateIdentifier);
|
|
2574
|
+
return listMissingRequirements(invitationFromStore, template, new Template(template).getActionOrThrow(invitationFromStore.actionIdentifier));
|
|
2594
2575
|
}
|
|
2595
2576
|
/**
|
|
2596
2577
|
* Lists all the unspent outputs.
|
|
@@ -2617,7 +2598,7 @@ var Engine = class Engine {
|
|
|
2617
2598
|
const sourceOutputIdentifier = options?.outputIdentifier ?? this.defaultOutputIdentifier;
|
|
2618
2599
|
const rawSourceTemplate = await this.state.getTemplate(sourceTemplateIdentifier);
|
|
2619
2600
|
if (rawSourceTemplate === void 0) throw new TemplateNotFoundError(sourceTemplateIdentifier);
|
|
2620
|
-
new Template(rawSourceTemplate).
|
|
2601
|
+
new Template(rawSourceTemplate).getOutputOrThrow(sourceOutputIdentifier);
|
|
2621
2602
|
const scriptHashDataList = await this.state.listScriptHashData({ templateIdentifier: sourceTemplateIdentifier });
|
|
2622
2603
|
const unspentOutputs = [];
|
|
2623
2604
|
for (const scriptHashData of scriptHashDataList) {
|
|
@@ -2681,15 +2662,11 @@ var Engine = class Engine {
|
|
|
2681
2662
|
* @throws {Error} If any required template, output, or configuration is not found, or if compilation fails.
|
|
2682
2663
|
*/
|
|
2683
2664
|
async generateLockingBytecode(templateIdentifier, outputIdentifier, roleIdentifier) {
|
|
2684
|
-
const
|
|
2685
|
-
|
|
2686
|
-
const
|
|
2687
|
-
const
|
|
2688
|
-
|
|
2689
|
-
if (!output.lockingScript) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
2690
|
-
template.validateLockingScript(lockingScriptForOutput, output.lockingScript);
|
|
2691
|
-
const selectedRoleId = roleIdentifier ?? template.getFirstRole(output.lockingScript)[0];
|
|
2692
|
-
if (roleIdentifier) template.getRole(output.lockingScript, roleIdentifier);
|
|
2665
|
+
const template = new Template(await this.getTemplateOrThrow(templateIdentifier));
|
|
2666
|
+
const output = template.getOutputOrThrow(outputIdentifier);
|
|
2667
|
+
const lockingScriptForOutput = template.getLockingScriptForOutputOrThrow(outputIdentifier);
|
|
2668
|
+
const selectedRoleId = roleIdentifier ?? template.getFirstRoleOrThrow(output.lockingScript)[0];
|
|
2669
|
+
if (roleIdentifier) template.getRoleOrThrow(output.lockingScript, roleIdentifier);
|
|
2693
2670
|
const finalRoleIdentifier = selectedRoleId;
|
|
2694
2671
|
const entityOwnership = entityOwnershipForLibauthCompiler(template.getTemplate(), finalRoleIdentifier);
|
|
2695
2672
|
const scripts = scriptsForLibauthCompiler(template.getTemplate());
|
|
@@ -2697,7 +2674,6 @@ var Engine = class Engine {
|
|
|
2697
2674
|
if (lockingScriptForOutput.unlockingBytecode === void 0 || lockingScriptForOutput.lockingBytecode === void 0) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
2698
2675
|
const unlockingScripts = { [lockingScriptForOutput.unlockingBytecode]: lockingScriptForOutput.lockingBytecode };
|
|
2699
2676
|
const { lockingType } = lockingScriptForOutput;
|
|
2700
|
-
if (!lockingType) throw new TemplateLockingScriptTypeNotFoundError(outputIdentifier);
|
|
2701
2677
|
const compiler = createCompilerBch({
|
|
2702
2678
|
entityOwnership,
|
|
2703
2679
|
scripts,
|
|
@@ -2741,7 +2717,7 @@ var Engine = class Engine {
|
|
|
2741
2717
|
outputIdentifier,
|
|
2742
2718
|
roleIdentifier: finalRoleIdentifier
|
|
2743
2719
|
});
|
|
2744
|
-
await this.updateUnspentOutputsForScriptHashInStore(
|
|
2720
|
+
await this.updateUnspentOutputsForScriptHashInStore(scriptHash);
|
|
2745
2721
|
return lockingBytecode;
|
|
2746
2722
|
}
|
|
2747
2723
|
/**
|
|
@@ -2831,6 +2807,42 @@ var Engine = class Engine {
|
|
|
2831
2807
|
return inputsForReservation;
|
|
2832
2808
|
}
|
|
2833
2809
|
/**
|
|
2810
|
+
* Applies an invitation append operation to a stored invitation. Signs a new commit using the
|
|
2811
|
+
* engine's own entity, persists the updated invitation to the store, and returns the result.
|
|
2812
|
+
*
|
|
2813
|
+
* This is the shared implementation used by both the public {@link appendInvitation} method
|
|
2814
|
+
* and internal callers that already hold the stored invitation in memory.
|
|
2815
|
+
*
|
|
2816
|
+
* @param {StoredInvitation} storedInvitation - The stored invitation to append to.
|
|
2817
|
+
* @param {AppendInvitationParameters} params - The parameters to append.
|
|
2818
|
+
* @returns {Promise<XOInvitation>} The updated invitation after the new commit is applied.
|
|
2819
|
+
*/
|
|
2820
|
+
async applyInvitationAppend(storedInvitation, params) {
|
|
2821
|
+
const rootCommitIdentifier = findRootCommitIdentifierInInvitation(storedInvitation, storedInvitation.entityIdentifier);
|
|
2822
|
+
if (rootCommitIdentifier === void 0) throw new RootCommitNotFoundError(storedInvitation.entityIdentifier);
|
|
2823
|
+
if (params.outputs !== void 0) for (const output of params.outputs) {
|
|
2824
|
+
if (output.mergesWith !== void 0 || output.roleIdentifier !== void 0 || output.lockingBytecode !== void 0) continue;
|
|
2825
|
+
output.lockingBytecode = hexToBin(await this.generateLockingBytecodeForDefaultParameters());
|
|
2826
|
+
}
|
|
2827
|
+
const invitationBuilder = InvitationBuilder.fromInvitation(storedInvitation);
|
|
2828
|
+
const signedCommit = this.crypto.signInvitationCommitWithSchnorr({
|
|
2829
|
+
invitationCommit: invitationBuilder.createUnsignedCommit({ ...params }),
|
|
2830
|
+
invitationIdentifier: invitationBuilder.invitationIdentifier,
|
|
2831
|
+
rootCommitIdentifier,
|
|
2832
|
+
templateIdentifier: invitationBuilder.templateIdentifier,
|
|
2833
|
+
actionIdentifier: invitationBuilder.actionIdentifier
|
|
2834
|
+
});
|
|
2835
|
+
invitationBuilder.addCommit(signedCommit);
|
|
2836
|
+
const inputsForReservation = await this.findOwnResources(params.inputs ?? []);
|
|
2837
|
+
if (inputsForReservation.length > 0) await this.reserveResources(inputsForReservation, invitationBuilder.invitationIdentifier);
|
|
2838
|
+
const updatedInvitation = invitationBuilder.build();
|
|
2839
|
+
await this.state.storeInvitation(updatedInvitation.invitationIdentifier, {
|
|
2840
|
+
...updatedInvitation,
|
|
2841
|
+
entityIdentifier: storedInvitation.entityIdentifier
|
|
2842
|
+
});
|
|
2843
|
+
return updatedInvitation;
|
|
2844
|
+
}
|
|
2845
|
+
/**
|
|
2834
2846
|
* Reserves utxos and store information in the storage.
|
|
2835
2847
|
*
|
|
2836
2848
|
* @param {XOInvitationInput[]} inputs - The inputs to reserve.
|