Formatting and reduce explicit typing
This commit is contained in:
@@ -22,24 +22,6 @@ import type {
|
||||
XOTemplateVariable,
|
||||
} from "@xo-cash/types";
|
||||
|
||||
/**
|
||||
* View metadata copied from a template definition onto a resolved invitation item.
|
||||
*/
|
||||
export interface TemplateViewMetadata {
|
||||
name?: string;
|
||||
description?: string;
|
||||
icon?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Role-specific view metadata from a template output definition.
|
||||
*/
|
||||
export interface ResolvedInvitationOutputRoleMetadata {
|
||||
name?: string;
|
||||
description?: string;
|
||||
icon?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A variable from invitation commits enriched with its template definition.
|
||||
*/
|
||||
@@ -74,7 +56,10 @@ export type ResolvedInvitationOutput = XOInvitationOutput & {
|
||||
name?: string;
|
||||
description?: string;
|
||||
icon?: string;
|
||||
roles?: Record<string, ResolvedInvitationOutputRoleMetadata>;
|
||||
roles?: Record<
|
||||
string,
|
||||
{ name?: string; description?: string; icon?: string }
|
||||
>;
|
||||
lockingScript?: string;
|
||||
};
|
||||
|
||||
@@ -91,24 +76,13 @@ export interface ResolvedInvitationData {
|
||||
}
|
||||
|
||||
/**
|
||||
* Template display metadata layered onto a committed output.
|
||||
* Picks human-readable view fields from a template definition.
|
||||
*/
|
||||
export interface TemplateOutputMetadata {
|
||||
export const pickTemplateViewMetadata = (definition?: {
|
||||
name?: string;
|
||||
description?: string;
|
||||
icon?: string;
|
||||
roles?: Record<string, ResolvedInvitationOutputRoleMetadata>;
|
||||
lockingScript?: string;
|
||||
valueSatoshis?: bigint | string;
|
||||
token?: XOTemplateOutput["token"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks human-readable view fields from a template definition.
|
||||
*/
|
||||
export const pickTemplateViewMetadata = (
|
||||
definition: TemplateViewMetadata | undefined,
|
||||
): TemplateViewMetadata => {
|
||||
}) => {
|
||||
if (!definition) return {};
|
||||
|
||||
return {
|
||||
@@ -124,8 +98,8 @@ export const pickTemplateViewMetadata = (
|
||||
* Picks variable metadata from a template variable definition.
|
||||
*/
|
||||
export const pickTemplateVariableMetadata = (
|
||||
definition: XOTemplateVariable | undefined,
|
||||
): Pick<ResolvedInvitationVariable, "name" | "description" | "type" | "hint"> => {
|
||||
definition?: XOTemplateVariable,
|
||||
) => {
|
||||
if (!definition) return {};
|
||||
|
||||
return {
|
||||
@@ -138,12 +112,7 @@ export const pickTemplateVariableMetadata = (
|
||||
/**
|
||||
* Picks input metadata from a template input definition.
|
||||
*/
|
||||
export const pickTemplateInputMetadata = (
|
||||
definition: XOTemplateInput | undefined,
|
||||
): Pick<
|
||||
ResolvedInvitationInput,
|
||||
"name" | "description" | "icon" | "unlockingScript" | "omitChangeAmounts"
|
||||
> => {
|
||||
export const pickTemplateInputMetadata = (definition?: XOTemplateInput) => {
|
||||
if (!definition) return {};
|
||||
|
||||
return {
|
||||
@@ -164,9 +133,7 @@ export const pickTemplateInputMetadata = (
|
||||
* defaults; display-oriented fields like name, description, and template
|
||||
* valueSatoshis expressions are layered on for UI rendering.
|
||||
*/
|
||||
export const pickTemplateOutputMetadata = (
|
||||
definition: XOTemplateOutput | undefined,
|
||||
): TemplateOutputMetadata => {
|
||||
export const pickTemplateOutputMetadata = (definition?: XOTemplateOutput) => {
|
||||
if (!definition) return {};
|
||||
|
||||
const roles = definition.roles
|
||||
@@ -198,19 +165,17 @@ export const resolveVariable = (
|
||||
variable: XOInvitationVariable,
|
||||
entityIdentifier: string,
|
||||
template: XOTemplate,
|
||||
): ResolvedInvitationVariable => {
|
||||
const definition = template.variables?.[variable.variableIdentifier];
|
||||
|
||||
return {
|
||||
entityIdentifier,
|
||||
variableIdentifier: variable.variableIdentifier,
|
||||
...(variable.roleIdentifier !== undefined && {
|
||||
roleIdentifier: variable.roleIdentifier,
|
||||
}),
|
||||
value: variable.value,
|
||||
...pickTemplateVariableMetadata(definition),
|
||||
};
|
||||
};
|
||||
): ResolvedInvitationVariable => ({
|
||||
entityIdentifier,
|
||||
variableIdentifier: variable.variableIdentifier,
|
||||
...(variable.roleIdentifier !== undefined && {
|
||||
roleIdentifier: variable.roleIdentifier,
|
||||
}),
|
||||
value: variable.value,
|
||||
...pickTemplateVariableMetadata(
|
||||
template.variables?.[variable.variableIdentifier],
|
||||
),
|
||||
});
|
||||
|
||||
/**
|
||||
* Enriches a committed input with its template definition when an identifier is present.
|
||||
@@ -219,17 +184,15 @@ export const resolveInput = (
|
||||
input: XOInvitationInput,
|
||||
entityIdentifier: string,
|
||||
template: XOTemplate,
|
||||
): ResolvedInvitationInput => {
|
||||
const definition = input.inputIdentifier
|
||||
? template.inputs?.[input.inputIdentifier]
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
entityIdentifier,
|
||||
...input,
|
||||
...pickTemplateInputMetadata(definition),
|
||||
};
|
||||
};
|
||||
): ResolvedInvitationInput => ({
|
||||
entityIdentifier,
|
||||
...input,
|
||||
...pickTemplateInputMetadata(
|
||||
input.inputIdentifier
|
||||
? template.inputs?.[input.inputIdentifier]
|
||||
: undefined,
|
||||
),
|
||||
});
|
||||
|
||||
/**
|
||||
* Enriches a committed output with its template definition when an identifier is present.
|
||||
@@ -238,25 +201,21 @@ export const resolveOutput = (
|
||||
output: XOInvitationOutput,
|
||||
entityIdentifier: string,
|
||||
template: XOTemplate,
|
||||
): ResolvedInvitationOutput => {
|
||||
const definition = output.outputIdentifier
|
||||
? template.outputs?.[output.outputIdentifier]
|
||||
: undefined;
|
||||
const templateMetadata = pickTemplateOutputMetadata(definition);
|
||||
|
||||
return {
|
||||
): ResolvedInvitationOutput =>
|
||||
({
|
||||
entityIdentifier,
|
||||
...output,
|
||||
...templateMetadata,
|
||||
} as ResolvedInvitationOutput;
|
||||
};
|
||||
...pickTemplateOutputMetadata(
|
||||
output.outputIdentifier
|
||||
? template.outputs?.[output.outputIdentifier]
|
||||
: undefined,
|
||||
),
|
||||
}) as ResolvedInvitationOutput;
|
||||
|
||||
/**
|
||||
* Converts hex or binary invitation bytecode fields to hex strings for display.
|
||||
*/
|
||||
export const hexOrBinToHex = (
|
||||
value: string | Uint8Array | undefined,
|
||||
): string | undefined => {
|
||||
export const hexOrBinToHex = (value?: string | Uint8Array) => {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -267,10 +226,8 @@ export const hexOrBinToHex = (
|
||||
/**
|
||||
* Normalizes a merged input row for UI display (hex strings, no encoding placeholders).
|
||||
*/
|
||||
export const normalizeMergedInputForDisplay = (
|
||||
input: XOInvitationInput,
|
||||
): XOInvitationInput => {
|
||||
const normalized: XOInvitationInput = { ...input };
|
||||
export const normalizeMergedInputForDisplay = (input: XOInvitationInput) => {
|
||||
const normalized = { ...input };
|
||||
|
||||
if (input.outpointTransactionHash !== undefined) {
|
||||
normalized.outpointTransactionHash = hexOrBinToHex(
|
||||
@@ -302,10 +259,8 @@ export const normalizeMergedInputForDisplay = (
|
||||
/**
|
||||
* Normalizes a merged output row for UI display (hex strings).
|
||||
*/
|
||||
export const normalizeMergedOutputForDisplay = (
|
||||
output: XOInvitationOutput,
|
||||
): XOInvitationOutput => {
|
||||
const normalized: XOInvitationOutput = { ...output };
|
||||
export const normalizeMergedOutputForDisplay = (output: XOInvitationOutput) => {
|
||||
const normalized = { ...output };
|
||||
|
||||
if (output.lockingBytecode !== undefined) {
|
||||
normalized.lockingBytecode = hexOrBinToHex(
|
||||
@@ -323,7 +278,7 @@ export const normalizeMergedOutputForDisplay = (
|
||||
export const findOutputIdentifierForMergedOutput = (
|
||||
commit: XOInvitationCommit | undefined,
|
||||
mergedOutput: XOInvitationOutput,
|
||||
): string | undefined => {
|
||||
) => {
|
||||
const outputs = commit?.data?.outputs ?? [];
|
||||
const mergedBytecodeHex = hexOrBinToHex(mergedOutput.lockingBytecode);
|
||||
|
||||
@@ -348,8 +303,7 @@ export const findOutputIdentifierForMergedOutput = (
|
||||
);
|
||||
|
||||
if (outputsWithIdentifier.length === 1) {
|
||||
const soleIdentifiedOutput = outputsWithIdentifier[0];
|
||||
return soleIdentifiedOutput?.outputIdentifier;
|
||||
return outputsWithIdentifier[0]?.outputIdentifier;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
@@ -361,12 +315,9 @@ export const findOutputIdentifierForMergedOutput = (
|
||||
export const matchesInvitationVariable = (
|
||||
left: XOInvitationVariable,
|
||||
right: XOInvitationVariable,
|
||||
): boolean => {
|
||||
return (
|
||||
left.variableIdentifier === right.variableIdentifier &&
|
||||
left.roleIdentifier === right.roleIdentifier
|
||||
);
|
||||
};
|
||||
) =>
|
||||
left.variableIdentifier === right.variableIdentifier &&
|
||||
left.roleIdentifier === right.roleIdentifier;
|
||||
|
||||
/**
|
||||
* Finds the entity that authored a merged variable by scanning invitation commits.
|
||||
@@ -376,7 +327,7 @@ export const matchesInvitationVariable = (
|
||||
export const findVariableEntityIdentifier = (
|
||||
variable: XOInvitationVariable,
|
||||
commits: XOInvitationCommit[],
|
||||
): string => {
|
||||
) => {
|
||||
let entityIdentifier = "";
|
||||
|
||||
for (const commit of commits) {
|
||||
@@ -397,10 +348,6 @@ export const findVariableEntityIdentifier = (
|
||||
* extensions and transaction indices are resolved. Variables come from the merged
|
||||
* result and are enriched with template metadata. Commit ordering is delegated to
|
||||
* the engine merger.
|
||||
*
|
||||
* @param invitation - The raw invitation in standard XO format.
|
||||
* @param template - The template referenced by the invitation.
|
||||
* @returns Resolved invitation data ready for display.
|
||||
*/
|
||||
export const resolveCommitReferences = (
|
||||
invitation: XOInvitation,
|
||||
@@ -436,8 +383,9 @@ export const resolveCommitReferences = (
|
||||
);
|
||||
|
||||
const inputs = merged.inputs.map((mergedInput) => {
|
||||
const commit = commitsMap.get(mergedInput.sourceCommitIdentifier);
|
||||
const entityIdentifier = commit?.entityIdentifier ?? "";
|
||||
const entityIdentifier =
|
||||
commitsMap.get(mergedInput.sourceCommitIdentifier)?.entityIdentifier ??
|
||||
"";
|
||||
const {
|
||||
sourceCommitIdentifier: _sourceCommitIdentifier,
|
||||
mergesWith: _mergesWith,
|
||||
@@ -459,7 +407,10 @@ export const resolveCommitReferences = (
|
||||
mergesWith: _mergesWith,
|
||||
...output
|
||||
} = mergedOutput;
|
||||
const outputIdentifier = findOutputIdentifierForMergedOutput(commit, output);
|
||||
const outputIdentifier = findOutputIdentifierForMergedOutput(
|
||||
commit,
|
||||
output,
|
||||
);
|
||||
const outputForDisplay = normalizeMergedOutputForDisplay(
|
||||
outputIdentifier !== undefined ? { ...output, outputIdentifier } : output,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user