import { readFile } from "node:fs/promises";
import { makeError } from "./errors.js";
import { ok, err } from "./result.js";
export async function loadPaymentWallet(keyfilePath, walletService) {
    const keysResult = await walletService.exportKeys();
    if (!keysResult.ok)
        return keysResult;
    const { publicKey, address } = keysResult.value;
    let raw;
    try {
        raw = JSON.parse(await readFile(keyfilePath, "utf8"));
    }
    catch (error) {
        const nodeError = error;
        if (nodeError.code === "ENOENT") {
            return err(makeError("WALLET_NOT_FOUND", "No wallet keyfile found"));
        }
        return err(makeError("WALLET_LOAD_FAILED", `Failed to read keyfile: ${error.message}`));
    }
    if (!raw || typeof raw !== "object" || typeof raw.privateKey !== "string") {
        return err(makeError("WALLET_LOAD_FAILED", "Keyfile is missing a readable privateKey"));
    }
    return ok({ privateKey: raw.privateKey, publicKey, address });
}
//# sourceMappingURL=payment-wallet.js.map