Compare commits
1 Commits
def261b568
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
d45f1ddeb3
|
2
package-lock.json
generated
2
package-lock.json
generated
@@ -51,7 +51,7 @@
|
||||
"@electrum-cash/network": "^4.2.2",
|
||||
"@electrum-cash/protocol": "^2.3.1",
|
||||
"@electrum-cash/servers": "^3.1.0",
|
||||
"@xo-cash/crypto": "^0.0.1",
|
||||
"@xo-cash/crypto": "file:../crypto",
|
||||
"@xo-cash/primitives": "0.0.1",
|
||||
"@xo-cash/state": "0.0.2",
|
||||
"@xo-cash/templates": "0.0.1",
|
||||
|
||||
@@ -57,7 +57,7 @@ npx tsx src/index.ts # TUI
|
||||
xo-cli mnemonic create
|
||||
|
||||
# Import an existing mnemonic seed phrase
|
||||
xo-cli mnemonic import page pencil stock planet limb cluster assault speak off joke private pioneer
|
||||
xo-cli mnemonic import oven crop same above under tower promote decrease vocal pretty require slow
|
||||
|
||||
# List mnemonic basenames (use with -m)
|
||||
xo-cli mnemonic list
|
||||
|
||||
@@ -7,9 +7,9 @@ import { z } from "zod";
|
||||
|
||||
/**
|
||||
* Converts the CLI args to a key-value object and return the options object along with the other arguments still in the array.\
|
||||
* eg: `xo-cli mnemonic create page pencil stock planet limb cluster assault speak off joke private pioneer -v -o mnemonic.txt` will return:
|
||||
* eg: `xo-cli mnemonic create oven crop same above under tower promote decrease vocal pretty require slow -v -o mnemonic.txt` will return:
|
||||
* {
|
||||
* args: ["mnemonic", "create", "page", "pencil", "stock", "planet", "limb", "cluster", "assault", "speak", "off", "joke", "private", "pioneer"],
|
||||
* args: ["mnemonic", "create", "oven", "crop", "same", "above", "under", "tower", "promote", "decrease", "vocal", "pretty", "require", "slow"],
|
||||
* options: {
|
||||
* output: "mnemonic.txt",
|
||||
* verbose: "true",
|
||||
|
||||
@@ -34,8 +34,8 @@ import { homedir } from "node:os";
|
||||
*
|
||||
* IMPORTANT: Keep this in sync with actual switch statements in command handlers:
|
||||
* - mnemonic.ts: create, import, list, expose
|
||||
* - template.ts: import, list, inspect, export, set-default
|
||||
* - invitation.ts: create, append, sign, broadcast, requirements, import, export, inspect, list
|
||||
* - template.ts: import, list, inspect, set-default
|
||||
* - invitation.ts: create, append, sign, broadcast, requirements, import, inspect, list
|
||||
* - resource.ts: list, unreserve, unreserve-all
|
||||
* - settings.ts: show, get, set
|
||||
*/
|
||||
@@ -43,7 +43,7 @@ import { homedir } from "node:os";
|
||||
/** Subcommands for the mnemonic command */
|
||||
const MNEMONIC_SUBS = ["create", "import", "list", "expose"];
|
||||
/** Subcommands for the template command */
|
||||
const TEMPLATE_SUBS = ["import", "list", "inspect", "export", "set-default"];
|
||||
const TEMPLATE_SUBS = ["import", "list", "inspect", "set-default"];
|
||||
/** Subcommands for the invitation command */
|
||||
const INVITATION_SUBS = [
|
||||
"create",
|
||||
@@ -52,7 +52,6 @@ const INVITATION_SUBS = [
|
||||
"broadcast",
|
||||
"requirements",
|
||||
"import",
|
||||
"export",
|
||||
"inspect",
|
||||
"list",
|
||||
];
|
||||
|
||||
@@ -8,7 +8,11 @@
|
||||
* and instead constructs the engine directly with an in-memory blockchain provider.
|
||||
*/
|
||||
|
||||
import { BlockchainMonitor, Engine } from "@xo-cash/engine";
|
||||
import {
|
||||
BlockchainMonitor,
|
||||
Engine,
|
||||
InMemoryBlockchainProvider,
|
||||
} from "@xo-cash/engine";
|
||||
import { createStorageAdapter, State, StorageType } from "@xo-cash/state";
|
||||
import { convertMnemonicToSeedBytes } from "@xo-cash/crypto";
|
||||
import { binToHex, hash256 } from "@bitauth/libauth";
|
||||
@@ -63,21 +67,18 @@ export async function createOfflineEngine(
|
||||
// Create the state instance
|
||||
const state = new State(storageAdapter);
|
||||
|
||||
// Create a minimal blockchain monitor (no electrum initialization)
|
||||
const blockchainMonitor = new BlockchainMonitor(state);
|
||||
// Use in-memory blockchain provider (no network connections)
|
||||
const blockchainProvider = new InMemoryBlockchainProvider();
|
||||
await blockchainProvider.initialize({
|
||||
applicationIdentifier: "xo-cli-completions",
|
||||
electrumOptions: {},
|
||||
});
|
||||
|
||||
// Engine constructor is private; bypass for offline read-only completions.
|
||||
type EngineConstructor = new (
|
||||
mnemonic: string,
|
||||
state: State,
|
||||
blockchainMonitor: BlockchainMonitor,
|
||||
) => Engine;
|
||||
// Create a minimal blockchain monitor
|
||||
const blockchainMonitor = new BlockchainMonitor(state, blockchainProvider);
|
||||
|
||||
const engine = new (Engine as unknown as EngineConstructor)(
|
||||
seed,
|
||||
state,
|
||||
blockchainMonitor,
|
||||
);
|
||||
// Construct engine directly without state sync
|
||||
const engine = new Engine(seed, state, blockchainMonitor, blockchainProvider);
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ _{{FUNC_NAME}}_completions() {
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
append|sign|broadcast|requirements|export|inspect)
|
||||
append|sign|broadcast|requirements|inspect)
|
||||
# These subcommands expect an invitation identifier as first arg.
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
|
||||
@@ -70,7 +70,6 @@ complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from sign; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from broadcast; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from requirements; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from export; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from inspect; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
|
||||
# invitation import <path>
|
||||
|
||||
@@ -145,7 +145,7 @@ _{{FUNC_NAME}}_completions() {
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
append|sign|broadcast|requirements|export|inspect)
|
||||
append|sign|broadcast|requirements|inspect)
|
||||
# These subcommands take invitation ID as first argument.
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
|
||||
@@ -11,21 +11,13 @@ import {
|
||||
resolveProvidedLockingBytecodeHex,
|
||||
mapUnspentOutputsToSelectable,
|
||||
autoSelectGreedyUtxos,
|
||||
hasMissingRequirements,
|
||||
} from "../../utils/invitation-flow.js";
|
||||
import { deserializeInvitation, serializeInvitation } from "@xo-cash/engine";
|
||||
import type { XOInvitation } from "@xo-cash/types";
|
||||
import { encodeExtendedJson } from "../../utils/ext-json.js";
|
||||
import { resolveTemplate } from "../utils.js";
|
||||
|
||||
const DEFAULT_FEE = 500n;
|
||||
const DUST_THRESHOLD = 546n;
|
||||
|
||||
/**
|
||||
* Serializes an invitation to pretty-printed JSON for file export.
|
||||
*/
|
||||
const formatInvitationForFile = (invitation: XOInvitation, indent = 2): string =>
|
||||
JSON.stringify(JSON.parse(serializeInvitation(invitation)), null, indent);
|
||||
|
||||
/**
|
||||
* Result of parsing CLI options into inputs and outputs for an append call.
|
||||
* A `null` return signals a fatal error that was already logged to stderr.
|
||||
@@ -294,13 +286,9 @@ ${bold("Sub-commands:")}
|
||||
- broadcast <invitation-id> ${dim("Broadcast an invitation")}
|
||||
- requirements <invitation-id> ${dim("Show requirements for an invitation")}
|
||||
- import <invitation-file> ${dim("Import an invitation from a file")}
|
||||
- export <invitation-id> [output-file] ${dim("Export an invitation to stdout or a file")}
|
||||
- inspect <invitation-id | invitation-file> ${dim("Inspect an invitation")}
|
||||
- list ${dim("List all invitations")}
|
||||
|
||||
${bold("Export options:")}
|
||||
-o --output <output-filename> ${dim("Output filename for the exported invitation")}
|
||||
|
||||
${bold("Create / Append options:")}
|
||||
-var-<name> <value> ${dim("Set a variable (e.g. -var-requested-satoshis 1000)")}
|
||||
--add-input <txhash:vout> ${dim("Add UTXO input(s), comma-separated (e.g. abc123:0,def456:1)")}
|
||||
@@ -323,7 +311,6 @@ export type InvitationCommandResult = {
|
||||
invitationIdentifier?: string;
|
||||
txHash?: string;
|
||||
count?: number;
|
||||
outputFile?: string;
|
||||
templateName?: string;
|
||||
actionIdentifier?: string;
|
||||
status?: string;
|
||||
@@ -333,66 +320,6 @@ export type InvitationCommandResult = {
|
||||
variables?: unknown[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the invitation export command.
|
||||
* Throws CommandError on failure, returns result data on success.
|
||||
* @param deps - The command dependencies.
|
||||
* @param args - Positional args after "export", e.g. ["invitation-id"] or ["invitation-id", "invitation.json"].
|
||||
* @param options - Parsed option flags.
|
||||
*/
|
||||
export const handleInvitationExportCommand = async (
|
||||
deps: CommandDependencies,
|
||||
args: string[],
|
||||
options: Record<string, string>,
|
||||
): Promise<{ invitationIdentifier?: string; outputFile?: string }> => {
|
||||
const invitationIdentifier = args[0];
|
||||
deps.io.verbose(`Invitation identifier: ${invitationIdentifier}`);
|
||||
|
||||
if (!invitationIdentifier) {
|
||||
deps.io.verbose("No invitation identifier provided");
|
||||
printInvitationHelp(deps.io);
|
||||
throw new CommandError(
|
||||
"invitation.export.identifier_missing",
|
||||
"No invitation identifier provided",
|
||||
);
|
||||
}
|
||||
|
||||
const invitation = deps.app.invitations.find(
|
||||
(candidate) =>
|
||||
candidate.data.invitationIdentifier === invitationIdentifier,
|
||||
);
|
||||
|
||||
if (!invitation) {
|
||||
deps.io.err(`Invitation not found: ${invitationIdentifier}`);
|
||||
throw new CommandError(
|
||||
"invitation.export.not_found",
|
||||
`Invitation not found: ${invitationIdentifier}`,
|
||||
);
|
||||
}
|
||||
|
||||
const serializedInvitation = serializeInvitation(invitation.data);
|
||||
|
||||
const outputFile = options["output"] ?? args[1];
|
||||
|
||||
if (!outputFile) {
|
||||
deps.io.out(serializedInvitation);
|
||||
return { invitationIdentifier };
|
||||
}
|
||||
|
||||
const outputPath = path.resolve(process.cwd(), outputFile);
|
||||
try {
|
||||
writeFileSync(outputPath, serializedInvitation);
|
||||
} catch (error) {
|
||||
throw new CommandError(
|
||||
"invitation.export.write_failed",
|
||||
`Failed to export invitation to file: ${outputPath} (${error instanceof Error ? error.message : "unknown error"})`,
|
||||
);
|
||||
}
|
||||
|
||||
deps.io.out(`Invitation exported to: ${outputPath}`);
|
||||
return { invitationIdentifier, outputFile: outputPath };
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the invitation command.
|
||||
* Throws CommandError on failure, returns result data on success.
|
||||
@@ -484,7 +411,7 @@ export const handleInvitationCommand = async (
|
||||
deps.io.verbose(`Invitation file path: ${invitationFilePath}`);
|
||||
writeFileSync(
|
||||
invitationFilePath,
|
||||
formatInvitationForFile(invitationInstance.data),
|
||||
encodeExtendedJson(invitationInstance.data, 2),
|
||||
);
|
||||
deps.io.out(
|
||||
`Invitation created: ${path.basename(invitationFilePath)} (${invitationInstance.data.invitationIdentifier})`,
|
||||
@@ -494,8 +421,11 @@ export const handleInvitationCommand = async (
|
||||
const missingRequirements =
|
||||
await invitationInstance.getMissingRequirements();
|
||||
const hasMissing =
|
||||
hasMissingRequirements(missingRequirements.templateRequirements) ||
|
||||
missingRequirements.inputsMissingSignatures.length > 0;
|
||||
(missingRequirements.variables?.length ?? 0) > 0 ||
|
||||
(missingRequirements.inputs?.length ?? 0) > 0 ||
|
||||
(missingRequirements.outputs?.length ?? 0) > 0 ||
|
||||
(missingRequirements.roles !== undefined &&
|
||||
Object.keys(missingRequirements.roles).length > 0);
|
||||
|
||||
// If there are missing requirements, print them out
|
||||
if (hasMissing) {
|
||||
@@ -602,10 +532,7 @@ export const handleInvitationCommand = async (
|
||||
// Write the invitation to a file in the working directory
|
||||
// TODO: Support the -o flag to specify the output path
|
||||
const invitationFilePath = `${deps.paths.workingDir}/inv-${invitation.data.invitationIdentifier}.json`;
|
||||
writeFileSync(
|
||||
invitationFilePath,
|
||||
formatInvitationForFile(invitation.data),
|
||||
);
|
||||
writeFileSync(invitationFilePath, encodeExtendedJson(invitation.data, 2));
|
||||
deps.io.out(
|
||||
`Invitation updated: ${path.basename(invitationFilePath)} (${invitation.data.invitationIdentifier})`,
|
||||
);
|
||||
@@ -613,8 +540,11 @@ export const handleInvitationCommand = async (
|
||||
// Get the missing requirements for the invitation. This will tell us if we are missing any variables, inputs, outputs, or roles.
|
||||
const missingRequirements = await invitation.getMissingRequirements();
|
||||
const hasMissing =
|
||||
hasMissingRequirements(missingRequirements.templateRequirements) ||
|
||||
missingRequirements.inputsMissingSignatures.length > 0;
|
||||
(missingRequirements.variables?.length ?? 0) > 0 ||
|
||||
(missingRequirements.inputs?.length ?? 0) > 0 ||
|
||||
(missingRequirements.outputs?.length ?? 0) > 0 ||
|
||||
(missingRequirements.roles !== undefined &&
|
||||
Object.keys(missingRequirements.roles).length > 0);
|
||||
|
||||
// If there are missing requirements, print them out
|
||||
if (hasMissing) {
|
||||
@@ -791,10 +721,11 @@ export const handleInvitationCommand = async (
|
||||
}
|
||||
|
||||
// Read the invitation file (XOInvitation format, can be passed to the engine directly)
|
||||
const invitationFile = readFileSync(invitationFilePath, "utf8");
|
||||
const invitationFile = await readFileSync(invitationFilePath, "utf8");
|
||||
deps.io.verbose(`Invitation file: ${invitationFile}`);
|
||||
|
||||
const invitation = deserializeInvitation(invitationFile);
|
||||
// Parse the invitation file
|
||||
const invitation = JSON.parse(invitationFile);
|
||||
deps.io.verbose(`Invitation: ${formatObject(invitation)}`);
|
||||
|
||||
// Create the invitation instance (NOTE: Not an engine compatible invitation. This is the wrapped format)
|
||||
@@ -908,27 +839,19 @@ export const handleInvitationCommand = async (
|
||||
}
|
||||
|
||||
// Read the invitation file (XOInvitation format, can be passed to the engine directly)
|
||||
const invitationFile = readFileSync(invitationFilePath, "utf8");
|
||||
const invitationFile = await readFileSync(invitationFilePath, "utf8");
|
||||
deps.io.verbose(`Invitation file: ${invitationFile}`);
|
||||
|
||||
const invitation = deserializeInvitation(invitationFile);
|
||||
// Parse the invitation file
|
||||
const invitation = JSON.parse(invitationFile);
|
||||
deps.io.verbose(`Invitation: ${formatObject(invitation)}`);
|
||||
|
||||
const template = await deps.app.engine.getTemplate(
|
||||
invitation.templateIdentifier,
|
||||
);
|
||||
if (!template) {
|
||||
throw new CommandError(
|
||||
"invitation.import.template_not_found",
|
||||
`Template not found: ${invitation.templateIdentifier}. ` +
|
||||
`Import the matching template first with: xo-cli template import <template-file>`,
|
||||
);
|
||||
}
|
||||
// "Creates" the invitiation in the engine. This method acts as both creation or import depending on the data that is being passed in
|
||||
const xoInvitation = await deps.app.engine.createInvitation(invitation);
|
||||
deps.io.verbose(`XOInvitation: ${formatObject(xoInvitation)}`);
|
||||
|
||||
// Accept and track the invitation. Invitation.create calls acceptInvitation
|
||||
// internally — do not call engine.createInvitation here, that creates a new
|
||||
// invitation and discards the imported commits.
|
||||
const invitationInstance = await deps.app.createInvitation(invitation);
|
||||
// Create the invitation instance (NOTE: Not an engine compatible invitation. This is the wrapped format)
|
||||
const invitationInstance = await deps.app.createInvitation(xoInvitation);
|
||||
deps.io.verbose(
|
||||
`Invitation created: ${formatObject(invitationInstance.data)}`,
|
||||
);
|
||||
@@ -939,10 +862,6 @@ export const handleInvitationCommand = async (
|
||||
};
|
||||
}
|
||||
|
||||
case "export": {
|
||||
return handleInvitationExportCommand(deps, args.slice(1), options);
|
||||
}
|
||||
|
||||
case "list": {
|
||||
// List all the invitations
|
||||
const invitations = await Promise.all(
|
||||
|
||||
@@ -2,14 +2,8 @@ import { hexToBin } from "@bitauth/libauth";
|
||||
|
||||
import { bold, dim } from "../utils.js";
|
||||
import type { CommandDependencies, CommandIO } from "./types.js";
|
||||
import type { UnspentOutputData } from "@xo-cash/state";
|
||||
import { CommandError } from "./types.js";
|
||||
import type { XOTemplate } from "@xo-cash/types";
|
||||
import { generateTemplateIdentifier } from "@xo-cash/engine";
|
||||
import {
|
||||
buildScriptHashDataMap,
|
||||
enrichUnspentOutput,
|
||||
type UnspentOutputWithMetadata,
|
||||
} from "../../utils/utxo-metadata.js";
|
||||
|
||||
/**
|
||||
* Prints the help message for the resource command.
|
||||
@@ -33,12 +27,9 @@ ${bold("Sub-commands:")}
|
||||
* Formats a single UTXO for display, optionally including reservation info.
|
||||
*/
|
||||
function formatResource(
|
||||
resource: UnspentOutputWithMetadata & { template?: XOTemplate },
|
||||
resource: UnspentOutputData,
|
||||
showReserved = false,
|
||||
): string {
|
||||
// Format the template
|
||||
const template = resource.template ? dim(`[${generateTemplateIdentifier(resource.template)}]`) : "";
|
||||
|
||||
// Format the outpoint
|
||||
const outpoint = bold(
|
||||
`${resource.outpointTransactionHash}:${resource.outpointIndex}`,
|
||||
@@ -48,9 +39,7 @@ function formatResource(
|
||||
const value = dim(`${resource.valueSatoshis} sats`);
|
||||
|
||||
// Format the output
|
||||
const output = resource.outputIdentifier
|
||||
? dim(resource.outputIdentifier)
|
||||
: "";
|
||||
const output = dim(resource.outputIdentifier);
|
||||
|
||||
// Format the height
|
||||
const height = dim(`(height ${resource.minedAtHeight})`);
|
||||
@@ -58,11 +47,11 @@ function formatResource(
|
||||
// If the resource is reserved, format the reservation info
|
||||
if (showReserved && resource.reservedBy) {
|
||||
const inv = dim(`reserved for ${resource.reservedBy}`);
|
||||
return `${template} ${outpoint} ${value} ${output} ${height} ${inv}`;
|
||||
return `${outpoint} ${value} ${output} ${height} ${inv}`;
|
||||
}
|
||||
|
||||
// Otherwise, format the resource without reservation info
|
||||
return `${template} ${outpoint} ${value} ${output} ${height}`;
|
||||
return `${outpoint} ${value} ${output} ${height}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,30 +108,9 @@ export const handleResourceCommand = async (
|
||||
return { count: 0 };
|
||||
}
|
||||
|
||||
const scriptHashDataByScriptHash = await buildScriptHashDataMap(
|
||||
deps.app.engine,
|
||||
);
|
||||
|
||||
const resourcesWithTemplateInformation = await Promise.all(
|
||||
filtered.map(async (resource) => {
|
||||
const enriched = enrichUnspentOutput(
|
||||
resource,
|
||||
scriptHashDataByScriptHash,
|
||||
);
|
||||
const template = enriched.templateIdentifier
|
||||
? await deps.app.engine.getTemplate(enriched.templateIdentifier)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
...enriched,
|
||||
template,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
// Format the resources into a list of strings that we can display to the user
|
||||
const showReserved = qualifier === "all" || qualifier === "reserved";
|
||||
const formattedResources = resourcesWithTemplateInformation.map((r) =>
|
||||
const formattedResources = filtered.map((r) =>
|
||||
formatResource(r, showReserved),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import path from "path";
|
||||
import { generateTemplateIdentifier } from "@xo-cash/engine";
|
||||
import type { XOTemplate } from "@xo-cash/types";
|
||||
@@ -23,10 +23,6 @@ ${bold("Sub-commands:")}
|
||||
- list <category> <identifier> ${dim("List all options of the field type in a template")}
|
||||
- inspect <category> <identifier> <field> ${dim("Inspect a field in a template")}
|
||||
- set-default <template-file> <output-identifier> <role-identifier> ${dim("Set the default template")}
|
||||
- export <template-identifier> [output-file] ${dim("Export a template to stdout or a file")}
|
||||
|
||||
${bold("Options:")}
|
||||
-o --output <output-filename> ${dim("Output filename for the exported template")}
|
||||
`,
|
||||
);
|
||||
};
|
||||
@@ -342,71 +338,6 @@ export const handleTemplateInspectCommand = async (
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the template export command.
|
||||
* Throws CommandError on failure, returns result data on success.
|
||||
* @param deps - The command dependencies.
|
||||
* @param args - Positional args after "export", e.g. ["template-id"] or ["template-id", "template.json"].
|
||||
* @param options - Parsed option flags.
|
||||
*/
|
||||
export const handleTemplateExportCommand = async (
|
||||
deps: CommandDependencies,
|
||||
args: string[],
|
||||
options: Record<string, string>,
|
||||
): Promise<{ outputFile?: string }> => {
|
||||
// Get the template identifier from the arguments
|
||||
const templateIdentifier = args[0];
|
||||
|
||||
// If no template identifier is provided, print a message and throw an error
|
||||
if (!templateIdentifier) {
|
||||
deps.io.err("No template identifier provided");
|
||||
printTemplateHelp(deps.io);
|
||||
throw new CommandError(
|
||||
"template.export.identifier_missing",
|
||||
"No template identifier provided",
|
||||
);
|
||||
}
|
||||
|
||||
// Get the raw template from the engine.
|
||||
// Do not resolve references or pretty-print the template.
|
||||
const rawTemplate = await deps.app.engine.getTemplate(templateIdentifier);
|
||||
|
||||
// If the raw template is not found, print a message and throw an error
|
||||
if (!rawTemplate) {
|
||||
deps.io.err(`No template found: ${templateIdentifier}`);
|
||||
throw new CommandError(
|
||||
"template.export.not_found",
|
||||
`No template found: ${templateIdentifier}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Serialize the template without indentation to preserve the engine output shape.
|
||||
const serializedTemplate = JSON.stringify(rawTemplate);
|
||||
|
||||
// Resolve output file from --output (or -o), then fallback to optional positional output file
|
||||
const outputFile = options["output"] ?? args[1];
|
||||
|
||||
// If no output file is provided, print the template to stdout
|
||||
if (!outputFile) {
|
||||
deps.io.out(serializedTemplate);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Resolve output file path and write the template to disk
|
||||
const outputPath = path.resolve(process.cwd(), outputFile);
|
||||
try {
|
||||
writeFileSync(outputPath, serializedTemplate);
|
||||
} catch (error) {
|
||||
throw new CommandError(
|
||||
"template.export.write_failed",
|
||||
`Failed to export template to file: ${outputPath} (${error instanceof Error ? error.message : "unknown error"})`,
|
||||
);
|
||||
}
|
||||
|
||||
deps.io.out(`Template exported to: ${outputPath}`);
|
||||
return { outputFile: outputPath };
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the template command.
|
||||
* Throws CommandError on failure, returns result data on success.
|
||||
@@ -417,8 +348,8 @@ export const handleTemplateExportCommand = async (
|
||||
export const handleTemplateCommand = async (
|
||||
deps: CommandDependencies,
|
||||
args: string[],
|
||||
options: Record<string, string>,
|
||||
): Promise<{ templateFile?: string; count?: number; outputFile?: string }> => {
|
||||
_options: Record<string, string>,
|
||||
): Promise<{ templateFile?: string; count?: number }> => {
|
||||
// Get the sub-command from the arguments
|
||||
const subCommand = args[0];
|
||||
|
||||
@@ -483,10 +414,6 @@ export const handleTemplateCommand = async (
|
||||
// Handle the template inspect command, We offload here as it has lots of arguments and is quite long
|
||||
return handleTemplateInspectCommand(deps, args.slice(1));
|
||||
}
|
||||
case "export": {
|
||||
// Handle the template export command
|
||||
return handleTemplateExportCommand(deps, args.slice(1), options);
|
||||
}
|
||||
case "set-default": {
|
||||
// Get the template file, output identifier, and role identifier from the arguments
|
||||
const templateFile = args[1];
|
||||
|
||||
@@ -81,7 +81,22 @@ export class AppService extends EventEmitter<AppEventMap> {
|
||||
|
||||
// TODO: We *technically* dont want this here, but we also need some initial templates for the wallet, so im doing it here
|
||||
// Import the default P2PKH template
|
||||
await engine.importTemplate(p2pkhTemplate);
|
||||
const { templateIdentifier } = await engine.importTemplate(p2pkhTemplate);
|
||||
|
||||
// engine
|
||||
// .subscribeToLockingBytecodesForTemplate(templateIdentifier)
|
||||
// .catch((err) =>
|
||||
// console.error(
|
||||
// `Error subscribing to locking bytecodes for template ${templateIdentifier}: ${err}`,
|
||||
// ),
|
||||
// );
|
||||
// engine
|
||||
// .updateUnspentOutputsForTemplate(templateIdentifier)
|
||||
// .catch((err) =>
|
||||
// console.error(
|
||||
// `Error updating unspent outputs for template ${templateIdentifier}: ${err}`,
|
||||
// ),
|
||||
// );
|
||||
|
||||
// Update all the unspents for every template, and subscribe to the locking bytecodes for changes
|
||||
// TODO: Remove the above lines that do the same thing. Minimising changes for BLISS.
|
||||
@@ -89,8 +104,8 @@ export class AppService extends EventEmitter<AppEventMap> {
|
||||
const templates = await engine.listImportedTemplates();
|
||||
|
||||
templates.forEach(async (template) => {
|
||||
engine.updateUnspentOutputsForTemplate(generateTemplateIdentifier(template));
|
||||
engine.subscribeToScriptHashForTemplate(generateTemplateIdentifier(template));
|
||||
// engine.updateUnspentOutputsForTemplate(generateTemplateIdentifier(template));
|
||||
engine.subscribeToLockingBytecodesForTemplate(generateTemplateIdentifier(template));
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { binToHex, hexToBin, sha256 } from "@bitauth/libauth";
|
||||
import { compileCashAssemblyString, type Engine } from "@xo-cash/engine";
|
||||
import type { ScriptHashData, State, UnspentOutputData } from "@xo-cash/state";
|
||||
import type { ScriptHashData, UnspentOutputData } from "@xo-cash/state";
|
||||
import type {
|
||||
XOInvitation,
|
||||
XOInvitationCommit,
|
||||
@@ -146,10 +146,8 @@ export class HistoryService {
|
||||
const contexts = new Map<string, InvitationContext>();
|
||||
|
||||
for (const invitation of this.invitations) {
|
||||
const templateIdentifier = invitation.data.templateIdentifier;
|
||||
const template = templateIdentifier
|
||||
? (await this.engine.getTemplate(templateIdentifier)) ?? null
|
||||
: null;
|
||||
const template =
|
||||
(await this.engine.getTemplate(invitation.data.templateIdentifier)) ?? null;
|
||||
contexts.set(invitation.data.invitationIdentifier, {
|
||||
invitation,
|
||||
template,
|
||||
@@ -166,19 +164,11 @@ export class HistoryService {
|
||||
const scriptHashDataByScriptHash = new Map<string, ScriptHashData>();
|
||||
const templateIdentifiers = new Set<string>();
|
||||
|
||||
for (const invitation of this.invitations) {
|
||||
if (invitation.data.templateIdentifier) {
|
||||
templateIdentifiers.add(invitation.data.templateIdentifier);
|
||||
}
|
||||
for (const utxo of allUtxos) {
|
||||
templateIdentifiers.add(utxo.templateIdentifier);
|
||||
}
|
||||
|
||||
const uniqueScriptHashes = new Set(allUtxos.map((utxo) => utxo.scriptHash));
|
||||
for (const scriptHash of uniqueScriptHashes) {
|
||||
const scriptHashData = await this.getScriptHashData(scriptHash);
|
||||
if (scriptHashData === undefined) continue;
|
||||
|
||||
scriptHashDataByScriptHash.set(scriptHash, scriptHashData);
|
||||
templateIdentifiers.add(scriptHashData.templateIdentifier);
|
||||
for (const invitation of this.invitations) {
|
||||
templateIdentifiers.add(invitation.data.templateIdentifier);
|
||||
}
|
||||
|
||||
for (const templateIdentifier of templateIdentifiers) {
|
||||
@@ -196,10 +186,8 @@ export class HistoryService {
|
||||
metadataIndex: WalletMetadataIndex,
|
||||
): Promise<UtxoContext> {
|
||||
const scriptHashData = metadataIndex.scriptHashDataByScriptHash.get(utxo.scriptHash);
|
||||
const templateIdentifier = scriptHashData?.templateIdentifier;
|
||||
const template = templateIdentifier
|
||||
? (await this.engine.getTemplate(templateIdentifier)) ?? null
|
||||
: null;
|
||||
const templateIdentifier = scriptHashData?.templateIdentifier ?? utxo.templateIdentifier;
|
||||
const template = (await this.engine.getTemplate(templateIdentifier)) ?? null;
|
||||
|
||||
return {
|
||||
utxo,
|
||||
@@ -311,7 +299,7 @@ export class HistoryService {
|
||||
if (!matchingContext) continue;
|
||||
|
||||
const lockingBytecode = this.getOutputLockingBytecodeHex(output) ?? matchingContext.scriptHashData?.lockingBytecode;
|
||||
const outputIdentifier = output.outputIdentifier ?? matchingContext.scriptHashData?.outputIdentifier;
|
||||
const outputIdentifier = output.outputIdentifier ?? matchingContext.scriptHashData?.outputIdentifier ?? matchingContext.utxo.outputIdentifier;
|
||||
const role =
|
||||
output.roleIdentifier ??
|
||||
this.getFirstEntityRole(entityRoles, commit.entityIdentifier) ??
|
||||
@@ -392,21 +380,20 @@ export class HistoryService {
|
||||
if (usedUtxoIds.has(this.getUtxoId(context.utxo))) return false;
|
||||
if (scriptHash && context.utxo.scriptHash === scriptHash) return true;
|
||||
if (lockingBytecode && context.scriptHashData?.lockingBytecode === lockingBytecode) return true;
|
||||
|
||||
if (output.outputIdentifier && context.scriptHashData?.outputIdentifier === output.outputIdentifier) return true;
|
||||
if (output.outputIdentifier && context.utxo.outputIdentifier === output.outputIdentifier) return true;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private projectStandaloneUtxo(context: UtxoContext): WalletHistoryItem {
|
||||
const output = this.projectUtxoOutput(context);
|
||||
const templateIdentifier = context.scriptHashData?.templateIdentifier;
|
||||
const templateIdentifier = context.scriptHashData?.templateIdentifier ?? context.utxo.templateIdentifier;
|
||||
const role = output.role;
|
||||
|
||||
return {
|
||||
id: `utxo-${context.utxo.outpointTransactionHash}:${context.utxo.outpointIndex}`,
|
||||
source: "utxo",
|
||||
templateIdentifier: templateIdentifier ?? "",
|
||||
templateIdentifier,
|
||||
template: context.template?.name ?? "UnknownTemplate",
|
||||
roles: role ? [role] : ["unknown"],
|
||||
description: output.description,
|
||||
@@ -417,7 +404,7 @@ export class HistoryService {
|
||||
}
|
||||
|
||||
private projectUtxoOutput(context: UtxoContext): WalletHistoryOutput {
|
||||
const outputIdentifier = context.scriptHashData?.outputIdentifier;
|
||||
const outputIdentifier = context.scriptHashData?.outputIdentifier ?? context.utxo.outputIdentifier;
|
||||
const role = context.scriptHashData?.roleIdentifier;
|
||||
|
||||
return {
|
||||
@@ -570,7 +557,7 @@ export class HistoryService {
|
||||
variables: Record<string, XOInvitationVariableValue>,
|
||||
): string {
|
||||
try {
|
||||
return compileCashAssemblyString({ cashAssemblyText: description, variables, evaluationDecodeMode: 'utf8' });
|
||||
return compileCashAssemblyString(description, variables);
|
||||
} catch {
|
||||
return this.interpolateSimpleCashAssemblyVariables(description, variables);
|
||||
}
|
||||
@@ -604,10 +591,6 @@ export class HistoryService {
|
||||
: binToHex(output.lockingBytecode);
|
||||
}
|
||||
|
||||
private async getScriptHashData(scriptHash: string): Promise<ScriptHashData | undefined> {
|
||||
return (this.engine as unknown as { state: State }).state.getScriptHashData(scriptHash);
|
||||
}
|
||||
|
||||
private getOutpointKey(txid: string, index: number): string {
|
||||
return `${txid}:${index}`;
|
||||
}
|
||||
|
||||
@@ -15,10 +15,6 @@ import type {
|
||||
} from "@xo-cash/types";
|
||||
import type { UnspentOutputData } from "@xo-cash/state";
|
||||
import {
|
||||
bigIntToBinUint64LE,
|
||||
bigIntToBinUintBE,
|
||||
bigIntToBinUintLE,
|
||||
bigIntToVmNumber,
|
||||
binToHex,
|
||||
encodeTransaction,
|
||||
generateTransaction,
|
||||
@@ -154,10 +150,6 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
* Start the invitation - Connect sync server and download latest invitation data.
|
||||
*/
|
||||
async start(): Promise<void> {
|
||||
// Persist immediately so imports survive sync-server outages and appear in the TUI
|
||||
// after a CLI import or app restart.
|
||||
await this.storage.set(this.data.invitationIdentifier, this.data);
|
||||
|
||||
try {
|
||||
// Connect to the sync server and get the invitation (in parallel)
|
||||
const [_, invitation] = await Promise.all([
|
||||
@@ -287,8 +279,7 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
private async computeStatusInternal(): Promise<string> {
|
||||
let missingReqs;
|
||||
try {
|
||||
const missingRequirements = await this.engine.listMissingRequirements(this.data.invitationIdentifier);
|
||||
missingReqs = missingRequirements.templateRequirements;
|
||||
missingReqs = await this.engine.listMissingRequirements(this.data);
|
||||
} catch {
|
||||
return "unknown";
|
||||
}
|
||||
@@ -403,7 +394,7 @@ 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);
|
||||
|
||||
// Publish the signed invitation to the sync server
|
||||
this.publishInvitation(signedInvitation);
|
||||
@@ -422,7 +413,7 @@ 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, {
|
||||
const txHash = await this.engine.executeAction(this.data, {
|
||||
broadcastTransaction: true,
|
||||
});
|
||||
|
||||
@@ -440,7 +431,7 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
*/
|
||||
async append(data: AppendInvitationParameters): Promise<void> {
|
||||
// Append the commit to the invitation
|
||||
this.data = await this.engine.appendInvitation(this.data.invitationIdentifier, data);
|
||||
this.data = await this.engine.appendInvitation(this.data, data);
|
||||
|
||||
// Sync the invitation to the sync server
|
||||
await this.publishInvitation(this.data);
|
||||
@@ -555,7 +546,7 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
* Get the missing requirements for the invitation
|
||||
*/
|
||||
async getMissingRequirements() {
|
||||
return this.engine.listMissingRequirements(this.data.invitationIdentifier);
|
||||
return this.engine.listMissingRequirements(this.data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -617,41 +608,33 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
);
|
||||
}
|
||||
|
||||
const valueSatoshisExpression = output.valueSatoshis;
|
||||
if (!valueSatoshisExpression) {
|
||||
const valueSatoshisIdentifier = output.valueSatoshis;
|
||||
if (!valueSatoshisIdentifier) {
|
||||
throw new Error(
|
||||
`Value satoshis identifier not found: ${outputIdentifier} in template: ${this.data.templateIdentifier}`,
|
||||
);
|
||||
}
|
||||
|
||||
console.dir(this.data, { depth: null });
|
||||
|
||||
// Create a list of all the variables from the commits
|
||||
const variables = this.data.commits.flatMap(
|
||||
(c) => c.data?.variables ?? [],
|
||||
);
|
||||
console.dir(variables, { depth: null });
|
||||
|
||||
// Create a dictionary of the variables
|
||||
const formattedVariables = variables.reduce(
|
||||
(acc, v) => {
|
||||
const { variableIdentifier, value } = v;
|
||||
console.log(typeof value);
|
||||
acc[variableIdentifier ?? ""] = value;
|
||||
acc[v.variableIdentifier ?? ""] = v.value;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, XOInvitationVariableValue>,
|
||||
);
|
||||
|
||||
console.dir(formattedVariables, { depth: null });
|
||||
|
||||
// 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 = await compileCashAssemblyString(
|
||||
String(valueSatoshisIdentifier),
|
||||
formattedVariables,
|
||||
);
|
||||
|
||||
console.dir(valueSatoshis, { depth: null });
|
||||
|
||||
// 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
|
||||
return BigInt(valueSatoshis);
|
||||
@@ -705,13 +688,9 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
// Iterate through the outputs and sum the valueSatoshis
|
||||
for (const output of outputs) {
|
||||
if (typeof output === "string") {
|
||||
const sats = await this.getSatsOut(output);
|
||||
console.log(`Sats for output: ${output} is ${sats}`);
|
||||
totalSats += sats
|
||||
totalSats += await this.getSatsOut(output);
|
||||
} else {
|
||||
const sats = await this.getSatsOut(output.output);
|
||||
console.log(`Sats for output: ${output.output} is ${sats}`);
|
||||
totalSats += sats;
|
||||
totalSats += await this.getSatsOut(output.output);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { deserializeInvitation, serializeInvitation } from "@xo-cash/engine";
|
||||
import type { XOInvitation } from "@xo-cash/types";
|
||||
import { decodeExtendedJson, encodeExtendedJson } from "../utils/ext-json.js";
|
||||
|
||||
/**
|
||||
* This is not an actual storage adapter that we want to make use of. This storage adapter is a stop-gap while the engine is under development.
|
||||
@@ -57,8 +56,9 @@ export class Storage extends BaseStorage {
|
||||
return fullKey.startsWith(prefix) ? fullKey.slice(prefix.length) : fullKey;
|
||||
}
|
||||
|
||||
async set(key: string, value: XOInvitation): Promise<void> {
|
||||
const encodedValue = serializeInvitation(value);
|
||||
async set(key: string, value: any): Promise<void> {
|
||||
// Encode the extended json object
|
||||
const encodedValue = encodeExtendedJson(value);
|
||||
|
||||
// Insert or replace the value into the database with full key (including basePath)
|
||||
const fullKey = this.getFullKey(key);
|
||||
@@ -93,10 +93,10 @@ export class Storage extends BaseStorage {
|
||||
return !strippedKey.includes(".");
|
||||
});
|
||||
|
||||
// Deserialize invitations and strip basePath from keys
|
||||
// Decode the extended json objects and strip basePath from keys
|
||||
return filteredRows.map((row) => ({
|
||||
key: this.stripBasePath(row.key),
|
||||
value: deserializeInvitation(row.value),
|
||||
value: decodeExtendedJson(row.value),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ export class Storage extends BaseStorage {
|
||||
if (!row) return null;
|
||||
|
||||
// Decode the extended json object
|
||||
return deserializeInvitation(row.value);
|
||||
return decodeExtendedJson(row.value);
|
||||
}
|
||||
|
||||
async remove(key: string): Promise<void> {
|
||||
@@ -174,9 +174,9 @@ export class InMemoryStorage extends BaseStorage {
|
||||
return fullKey.startsWith(prefix) ? fullKey.slice(prefix.length) : fullKey;
|
||||
}
|
||||
|
||||
async set(key: string, value: XOInvitation): Promise<void> {
|
||||
async set(key: string, value: any): Promise<void> {
|
||||
const fullKey = this.getFullKey(key);
|
||||
const encodedValue = serializeInvitation(value);
|
||||
const encodedValue = encodeExtendedJson(value);
|
||||
this.store.set(fullKey, encodedValue);
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ export class InMemoryStorage extends BaseStorage {
|
||||
|
||||
return filteredRows.map((row) => ({
|
||||
key: this.stripBasePath(row.key),
|
||||
value: deserializeInvitation(row.value),
|
||||
value: decodeExtendedJson(row.value),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ export class InMemoryStorage extends BaseStorage {
|
||||
const encodedValue = this.store.get(fullKey);
|
||||
if (encodedValue === undefined) return null;
|
||||
|
||||
return deserializeInvitation(encodedValue);
|
||||
return decodeExtendedJson(encodedValue);
|
||||
}
|
||||
|
||||
async remove(key: string): Promise<void> {
|
||||
|
||||
@@ -24,7 +24,6 @@ import {
|
||||
formatActionListItem,
|
||||
getTemplateRoles,
|
||||
} from '../../utils/template-utils.js';
|
||||
import { buildScriptHashDataMap } from '../../utils/utxo-metadata.js';
|
||||
|
||||
/**
|
||||
* Template item with metadata.
|
||||
@@ -84,21 +83,12 @@ export function TemplateListScreen(): React.ReactElement {
|
||||
|
||||
const templateList = await appService.engine.listImportedTemplates();
|
||||
const allUtxos = await appService.engine.listUnspentOutputsData();
|
||||
const scriptHashDataByScriptHash =
|
||||
await buildScriptHashDataMap(appService.engine);
|
||||
|
||||
const ownedOutputsByTemplate = new Map<string, Set<string>>();
|
||||
for (const utxo of allUtxos) {
|
||||
const scriptRow = scriptHashDataByScriptHash.get(utxo.scriptHash);
|
||||
if (scriptRow === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const existing =
|
||||
ownedOutputsByTemplate.get(scriptRow.templateIdentifier) ??
|
||||
new Set<string>();
|
||||
existing.add(scriptRow.outputIdentifier);
|
||||
ownedOutputsByTemplate.set(scriptRow.templateIdentifier, existing);
|
||||
const existing = ownedOutputsByTemplate.get(utxo.templateIdentifier) ?? new Set<string>();
|
||||
existing.add(utxo.outputIdentifier);
|
||||
ownedOutputsByTemplate.set(utxo.templateIdentifier, existing);
|
||||
}
|
||||
|
||||
const loadedTemplates = await Promise.all(
|
||||
|
||||
@@ -221,7 +221,7 @@ export function InvitationScreen(): React.ReactElement {
|
||||
|
||||
let isCurrent = true;
|
||||
|
||||
appService.engine.findOwnCommits(selectedInvitation.data.invitationIdentifier)
|
||||
appService.engine.getOwnCommits(selectedInvitation.data)
|
||||
.then((ownCommits) => {
|
||||
if (!isCurrent) return;
|
||||
|
||||
@@ -723,14 +723,10 @@ export function InvitationScreen(): React.ReactElement {
|
||||
{outputTemplate?.name ?? output.outputIdentifier ?? `Output ${idx}`}
|
||||
|
||||
{/* Output description */}
|
||||
{outputTemplate?.description && ' - ' + compileCashAssemblyString({
|
||||
cashAssemblyText: outputTemplate?.description,
|
||||
variables: variables.reduce((acc, variable) => {
|
||||
acc[variable.variableIdentifier] = variable.value as XOInvitationVariableValue;
|
||||
|
||||
return acc;
|
||||
}, {} as Record<string, XOInvitationVariableValue>)
|
||||
})}
|
||||
{outputTemplate?.description && ' - ' + compileCashAssemblyString(outputTemplate?.description ?? '', variables.reduce((acc, variable) => {
|
||||
acc[variable.variableIdentifier] = variable.value as XOInvitationVariableValue;
|
||||
return acc;
|
||||
}, {} as Record<string, XOInvitationVariableValue>))}
|
||||
|
||||
{/* Output value */}
|
||||
{outputSatoshis !== null && ` (${formatSatoshis(outputSatoshis)}${getFiatSuffix(outputSatoshis)})`}
|
||||
|
||||
@@ -30,7 +30,7 @@ export const isInvitationRequirementsComplete = async (
|
||||
invitation: Invitation,
|
||||
): Promise<boolean> => {
|
||||
const missingRequirements = await invitation.getMissingRequirements();
|
||||
return !hasMissingRequirements(missingRequirements.templateRequirements);
|
||||
return !hasMissingRequirements(missingRequirements);
|
||||
};
|
||||
|
||||
// TODO: Move to engine in templates.ts
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { XOInvitation } from "@xo-cash/types";
|
||||
import { EventEmitter } from "./event-emitter.js";
|
||||
import { SSESession, type SSEvent } from "./sse-client.js";
|
||||
import { deserializeInvitation, serializeInvitation } from "@xo-cash/engine";
|
||||
import { decodeExtendedJson, encodeExtendedJson } from "./ext-json.js";
|
||||
|
||||
export type SyncServerEventMap = {
|
||||
connected: void;
|
||||
@@ -81,7 +81,9 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
throw new Error(`Failed to get invitation: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const invitation = deserializeInvitation(await response.text());
|
||||
const invitation = decodeExtendedJson(await response.text()) as
|
||||
| XOInvitation
|
||||
| undefined;
|
||||
return invitation;
|
||||
}
|
||||
|
||||
@@ -94,7 +96,7 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
// Send a POST request to the sync server
|
||||
const response = await fetch(`${this.baseUrl}/invitations`, {
|
||||
method: "POST",
|
||||
body: serializeInvitation(invitation),
|
||||
body: encodeExtendedJson(invitation),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
@@ -107,7 +109,7 @@ export class SyncServer extends EventEmitter<SyncServerEventMap> {
|
||||
|
||||
// Read the returned JSON
|
||||
// TODO: This should use zod to verify the response
|
||||
const data = deserializeInvitation(await response.text());
|
||||
const data = decodeExtendedJson(await response.text()) as XOInvitation;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import type { Engine } from "@xo-cash/engine";
|
||||
import type { ScriptHashData, UnspentOutputData } from "@xo-cash/state";
|
||||
|
||||
/**
|
||||
* Template and output identifiers resolved from script hash storage.
|
||||
*/
|
||||
export type UnspentOutputMetadata = {
|
||||
templateIdentifier?: string;
|
||||
outputIdentifier?: string;
|
||||
};
|
||||
|
||||
export type UnspentOutputWithMetadata = UnspentOutputData & UnspentOutputMetadata;
|
||||
|
||||
/**
|
||||
* Builds a lookup map from script hash to its stored metadata.
|
||||
*/
|
||||
export const buildScriptHashDataMap = async (
|
||||
engine: Engine,
|
||||
): Promise<Map<string, ScriptHashData>> => {
|
||||
const scriptHashes = await engine.listScriptHashes();
|
||||
const scriptHashDataByScriptHash = new Map<string, ScriptHashData>();
|
||||
|
||||
for (const scriptHashRow of scriptHashes) {
|
||||
scriptHashDataByScriptHash.set(scriptHashRow.scriptHash, scriptHashRow);
|
||||
}
|
||||
|
||||
return scriptHashDataByScriptHash;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves template/output metadata for a single UTXO via its script hash.
|
||||
*/
|
||||
export const getUnspentOutputMetadata = (
|
||||
utxo: UnspentOutputData,
|
||||
scriptHashDataByScriptHash: Map<string, ScriptHashData>,
|
||||
): UnspentOutputMetadata => {
|
||||
const scriptRow = scriptHashDataByScriptHash.get(utxo.scriptHash);
|
||||
|
||||
if (scriptRow === undefined) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
templateIdentifier: scriptRow.templateIdentifier,
|
||||
outputIdentifier: scriptRow.outputIdentifier,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a UTXO enriched with template/output metadata from script hash storage.
|
||||
*/
|
||||
export const enrichUnspentOutput = (
|
||||
utxo: UnspentOutputData,
|
||||
scriptHashDataByScriptHash: Map<string, ScriptHashData>,
|
||||
): UnspentOutputWithMetadata => ({
|
||||
...utxo,
|
||||
...getUnspentOutputMetadata(utxo, scriptHashDataByScriptHash),
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { expect, test, describe, beforeEach, afterEach } from "vitest";
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
@@ -98,13 +98,6 @@ const testCases: TestCase[] = [
|
||||
shouldThrow: false,
|
||||
expectedData: {},
|
||||
},
|
||||
{
|
||||
name: "export returns raw template json to stdout",
|
||||
inputs: ["export", p2pkhTemplateIdentifier],
|
||||
shouldThrow: false,
|
||||
expectedData: {},
|
||||
logs: [{ out: "\"name\":\"Wallet (P2PKH)\"" }],
|
||||
},
|
||||
// Error cases - subcommand
|
||||
{
|
||||
name: "throws when no subcommand provided",
|
||||
@@ -131,18 +124,6 @@ const testCases: TestCase[] = [
|
||||
shouldThrow: true,
|
||||
expectedEvent: "template.import.file_not_found",
|
||||
},
|
||||
{
|
||||
name: "throws when export called without template identifier",
|
||||
inputs: ["export"],
|
||||
shouldThrow: true,
|
||||
expectedEvent: "template.export.identifier_missing",
|
||||
},
|
||||
{
|
||||
name: "throws when export called with unknown template",
|
||||
inputs: ["export", "unknown-template"],
|
||||
shouldThrow: true,
|
||||
expectedEvent: "template.export.not_found",
|
||||
},
|
||||
// Error cases - list category
|
||||
{
|
||||
name: "throws when list category called without template identifier",
|
||||
@@ -282,42 +263,4 @@ describe("template command", () => {
|
||||
process.chdir(originalCwd);
|
||||
}
|
||||
});
|
||||
|
||||
test("export prints exact engine template JSON to stdout", async () => {
|
||||
const { io, capture } = createMockIO();
|
||||
const expectedTemplate = await engine.getTemplate(p2pkhTemplateIdentifier);
|
||||
expect(expectedTemplate).toBeDefined();
|
||||
|
||||
await handleTemplateCommand(
|
||||
createCommandDeps(app, io),
|
||||
["export", p2pkhTemplateIdentifier],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capture.out[0]).toBe(JSON.stringify(expectedTemplate));
|
||||
});
|
||||
|
||||
test("export writes exact engine template JSON to file", async () => {
|
||||
const outputFile = "exported-template.json";
|
||||
const outputPath = path.join(tempDir, outputFile);
|
||||
const { io } = createMockIO();
|
||||
const expectedTemplate = await engine.getTemplate(p2pkhTemplateIdentifier);
|
||||
expect(expectedTemplate).toBeDefined();
|
||||
|
||||
const originalCwd = process.cwd();
|
||||
process.chdir(tempDir);
|
||||
try {
|
||||
const result = await handleTemplateCommand(
|
||||
createCommandDeps(app, io),
|
||||
["export", p2pkhTemplateIdentifier],
|
||||
{ output: outputFile },
|
||||
);
|
||||
|
||||
const exportedTemplate = readFileSync(outputPath, "utf8");
|
||||
expect(exportedTemplate).toBe(JSON.stringify(expectedTemplate));
|
||||
expect(result.outputFile).toBe(path.resolve(process.cwd(), outputFile));
|
||||
} finally {
|
||||
process.chdir(originalCwd);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,8 +19,7 @@ import {
|
||||
import { BCHMnemonicURL } from "../../src/utils/bch-mnemonic-url";
|
||||
|
||||
const TEST_SEED =
|
||||
"page pencil stock planet limb cluster assault speak off joke private pioneer";
|
||||
|
||||
"oven crop same above under tower promote decrease vocal pretty require slow";
|
||||
describe("mnemonic utilities", () => {
|
||||
let tempDir: string;
|
||||
|
||||
@@ -54,7 +53,7 @@ describe("mnemonic utilities", () => {
|
||||
test("creates a mnemonic file with auto-generated name", () => {
|
||||
const filename = createMnemonicFile(tempDir, TEST_SEED);
|
||||
|
||||
expect(filename).toMatch(/^mnemonic-page$/);
|
||||
expect(filename).toMatch(/^mnemonic-oven$/);
|
||||
expect(existsSync(path.join(tempDir, filename))).toBe(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import { MockRatesService } from "./rates-service";
|
||||
import { RatesService } from "../../../src/services/rates";
|
||||
|
||||
export const DEFAULT_SEED =
|
||||
"page pencil stock planet limb cluster assault speak off joke private pioneer";
|
||||
"oven crop same above under tower promote decrease vocal pretty require slow";
|
||||
|
||||
/**
|
||||
* Options for creating a fake resource (UTXO) in tests.
|
||||
|
||||
Reference in New Issue
Block a user