Formatting
This commit is contained in:
+64
-30
@@ -3,7 +3,13 @@ import type {
|
||||
Engine,
|
||||
GetSpendableResourcesParameters,
|
||||
} from "@xo-cash/engine";
|
||||
import { generateTemplateIdentifier, hasInvitationExpired, mergeInvitationCommits, serializeInvitation, deserializeInvitation } from "@xo-cash/engine";
|
||||
import {
|
||||
generateTemplateIdentifier,
|
||||
hasInvitationExpired,
|
||||
mergeInvitationCommits,
|
||||
serializeInvitation,
|
||||
deserializeInvitation,
|
||||
} from "@xo-cash/engine";
|
||||
import type {
|
||||
XOInvitation,
|
||||
XOInvitationCommit,
|
||||
@@ -92,7 +98,9 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
}
|
||||
|
||||
// engine invitation (I have no idea if this is required)
|
||||
const engineInvitation = await dependencies.engine.importInvitation(serializeInvitation(invitation));
|
||||
const engineInvitation = await dependencies.engine.importInvitation(
|
||||
serializeInvitation(invitation),
|
||||
);
|
||||
|
||||
// Create the invitation
|
||||
const invitationInstance = new Invitation(engineInvitation, dependencies);
|
||||
@@ -287,7 +295,9 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
return payload;
|
||||
}
|
||||
|
||||
private unwrapLegacyInvitationUpdatedPayload(payload: unknown): unknown | null {
|
||||
private unwrapLegacyInvitationUpdatedPayload(
|
||||
payload: unknown,
|
||||
): unknown | null {
|
||||
if (
|
||||
payload &&
|
||||
typeof payload === "object" &&
|
||||
@@ -308,7 +318,10 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
invitation: XOInvitation = this.data,
|
||||
): Promise<void> {
|
||||
this.syncServer.publishInvitation(invitation).catch((error) => {
|
||||
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
||||
this.emit(
|
||||
"error",
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -362,7 +375,9 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
private async computeStatusInternal(): Promise<string> {
|
||||
let missingReqs;
|
||||
try {
|
||||
const missingRequirements = await this.engine.listMissingRequirements(this.data.invitationIdentifier);
|
||||
const missingRequirements = await this.engine.listMissingRequirements(
|
||||
this.data.invitationIdentifier,
|
||||
);
|
||||
missingReqs = missingRequirements.templateRequirements;
|
||||
} catch {
|
||||
return "unknown";
|
||||
@@ -454,13 +469,18 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
* Update the status of the invitation and emit the new single-word status.
|
||||
*/
|
||||
private async updateStatus(): Promise<void> {
|
||||
this.computeStatus().then(status => {
|
||||
this.status = status;
|
||||
this.emit("invitation-status-changed", status);
|
||||
}).catch((error) => {
|
||||
this.status = `error (${error instanceof Error ? error.message : String(error)})`;
|
||||
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
||||
});
|
||||
this.computeStatus()
|
||||
.then((status) => {
|
||||
this.status = status;
|
||||
this.emit("invitation-status-changed", status);
|
||||
})
|
||||
.catch((error) => {
|
||||
this.status = `error (${error instanceof Error ? error.message : String(error)})`;
|
||||
this.emit(
|
||||
"error",
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -499,7 +519,9 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
*/
|
||||
async sign(): Promise<void> {
|
||||
// Sign the invitation
|
||||
const signedInvitation = await this.engine.signInvitation(this.data.invitationIdentifier);
|
||||
const signedInvitation = await this.engine.signInvitation(
|
||||
this.data.invitationIdentifier,
|
||||
);
|
||||
|
||||
// Publish the signed invitation to the sync server
|
||||
this.publishInvitation(signedInvitation);
|
||||
@@ -518,9 +540,12 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
* @returns The transaction hash returned by the network after broadcast.
|
||||
*/
|
||||
async broadcast(): Promise<string> {
|
||||
const txHash = await this.engine.executeAction(this.data.invitationIdentifier, {
|
||||
broadcastTransaction: true,
|
||||
});
|
||||
const txHash = await this.engine.executeAction(
|
||||
this.data.invitationIdentifier,
|
||||
{
|
||||
broadcastTransaction: true,
|
||||
},
|
||||
);
|
||||
|
||||
await this.updateStatus();
|
||||
|
||||
@@ -538,7 +563,10 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
await this.ensureAccepted();
|
||||
|
||||
// Append the commit to the invitation
|
||||
this.data = await this.engine.appendInvitation(this.data.invitationIdentifier, data);
|
||||
this.data = await this.engine.appendInvitation(
|
||||
this.data.invitationIdentifier,
|
||||
data,
|
||||
);
|
||||
|
||||
// Sync the invitation to the sync server
|
||||
await this.publishInvitation(this.data);
|
||||
@@ -617,8 +645,8 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
const templates = await this.engine.listImportedTemplates();
|
||||
|
||||
// For each template, we need to create a 2d array of all the outputs
|
||||
const outputs = templates.map(template => {
|
||||
return Object.keys(template.outputs).map(output => {
|
||||
const outputs = templates.map((template) => {
|
||||
return Object.keys(template.outputs).map((output) => {
|
||||
const templateIdentifier = generateTemplateIdentifier(template);
|
||||
|
||||
return {
|
||||
@@ -629,14 +657,18 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
});
|
||||
|
||||
// then, for each output, we need to get the spendable resources
|
||||
const spendableResources = await Promise.all(outputs.flat().map(output => {
|
||||
return this.engine.getSpendableResources(this.data, {
|
||||
templateIdentifier: output.templateIdentifier,
|
||||
outputIdentifier: output.outputIdentifier,
|
||||
});
|
||||
}));
|
||||
const spendableResources = await Promise.all(
|
||||
outputs.flat().map((output) => {
|
||||
return this.engine.getSpendableResources(this.data, {
|
||||
templateIdentifier: output.templateIdentifier,
|
||||
outputIdentifier: output.outputIdentifier,
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const unspentOutputs = spendableResources.flatMap(resource => resource.unspentOutputs);
|
||||
const unspentOutputs = spendableResources.flatMap(
|
||||
(resource) => resource.unspentOutputs,
|
||||
);
|
||||
|
||||
// Update the status of the invitation
|
||||
await this.updateStatus();
|
||||
@@ -738,9 +770,11 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
);
|
||||
|
||||
// Compile the CashAssembly expression to get the value satoshis (It handles the variable replacement for us)
|
||||
const valueSatoshis = compileCashAssemblyString(
|
||||
{ cashAssemblyText: String(valueSatoshisExpression), variables: formattedVariables, evaluationDecodeMode: 'bigint' },
|
||||
);
|
||||
const valueSatoshis = compileCashAssemblyString({
|
||||
cashAssemblyText: String(valueSatoshisExpression),
|
||||
variables: formattedVariables,
|
||||
evaluationDecodeMode: "bigint",
|
||||
});
|
||||
|
||||
// Return the value satoshis as a bigint
|
||||
// TODO: Check this of a vulnerability or crash - I assume there might be one if someone made the `valueSatoshis` a malicious expression
|
||||
@@ -796,7 +830,7 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
for (const output of outputs) {
|
||||
if (typeof output === "string") {
|
||||
const sats = await this.getSatsOut(output);
|
||||
totalSats += sats
|
||||
totalSats += sats;
|
||||
} else {
|
||||
const sats = await this.getSatsOut(output.output);
|
||||
totalSats += sats;
|
||||
|
||||
Reference in New Issue
Block a user