Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@arkade-os/boltz-swap": "^0.2.18",
"@arkade-os/sdk": "^0.3.12",
"@arkade-os/sdk": "^0.4.10",
"@lendasat/lendaswap-sdk-pure": "^0.0.2",
"@noble/hashes": "^2.0.1",
"@scure/base": "^2.0.0",
Expand Down
11 changes: 9 additions & 2 deletions cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface CashConfig {
publicKey: string;
arkServerUrl: string;
network: string;
delegatorUrl?: string;
}

const CONFIG_DIR = join(homedir(), ".clw-cash");
Expand Down Expand Up @@ -55,6 +56,10 @@ export function loadConfig(overrides?: Partial<CashConfig>): CashConfig {
process.env.CLW_NETWORK ??
fileConfig.network ??
"bitcoin",
delegatorUrl:
overrides?.delegatorUrl ??
process.env.CLW_DELEGATOR_URL ??
fileConfig.delegatorUrl,
};

return config;
Expand All @@ -69,22 +74,24 @@ export interface ConfigEntry {

export type CashConfigWithSources = Record<keyof CashConfig, ConfigEntry>;

const ENV_KEYS: Record<keyof CashConfig, string> = {
const ENV_KEYS: Record<keyof Required<CashConfig>, string> = {
apiBaseUrl: "CLW_API_URL",
sessionToken: "CLW_SESSION_TOKEN",
identityId: "CLW_IDENTITY_ID",
publicKey: "CLW_PUBLIC_KEY",
arkServerUrl: "CLW_ARK_SERVER_URL",
network: "CLW_NETWORK",
delegatorUrl: "CLW_DELEGATOR_URL",
};

const DEFAULTS: Record<keyof CashConfig, string> = {
const DEFAULTS: Record<keyof Required<CashConfig>, string> = {
apiBaseUrl: "https://api.clw.cash",
sessionToken: "",
identityId: "",
publicKey: "",
arkServerUrl: "https://arkade.computer",
network: "bitcoin",
delegatorUrl: "",
};

export function loadConfigWithSources(): CashConfigWithSources {
Expand Down
5 changes: 4 additions & 1 deletion cli/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from "node:path";
import { homedir } from "node:os";
import { mkdirSync } from "node:fs";
import { RemoteSignerIdentity } from "@clw-cash/sdk";
import { Wallet } from "@arkade-os/sdk";
import { Wallet, RestDelegatorProvider } from "@arkade-os/sdk";
import { FileSystemStorageAdapter } from "@arkade-os/sdk/adapters/fileSystem";
import {
SqliteWalletStorage,
Expand Down Expand Up @@ -46,6 +46,9 @@ export async function createContext(config: CashConfig, opts?: CreateContextOpts
identity,
arkServerUrl: config.arkServerUrl,
storage: walletStorage,
...(config.delegatorUrl
? { delegatorProvider: new RestDelegatorProvider(config.delegatorUrl) }
: {}),
});

const bitcoin = new ArkadeBitcoinSkill(wallet);
Expand Down
55 changes: 55 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import minimist from "minimist";
import type { ExtendedVirtualCoin, IncomingFunds } from "@arkade-os/sdk";
import { VtxoManager } from "@arkade-os/sdk";
import { loadConfig, validateConfig, getSessionStatus, saveConfig } from "./config.js";
import { createContext } from "./context.js";
import { outputError } from "./output.js";
Expand Down Expand Up @@ -74,6 +76,7 @@ Environment:
CLW_ARK_SERVER_URL Arkade server URL
CLW_NETWORK Network (bitcoin|testnet)
CLW_DAEMON_PORT Daemon port (default: 3457)
CLW_DELEGATOR_URL Delegator service URL (e.g. https://delegate.arkade.money)
`;

const argv = minimist(process.argv.slice(2), {
Expand Down Expand Up @@ -309,6 +312,57 @@ async function runDaemon() {
monitor.start();
console.error("[daemon] lendaswap monitor started");

// Recover swept VTXOs on startup
const wallet = ctx.bitcoin.getWallet();
const vtxoManager = new VtxoManager(wallet);
void (async () => {
try {
const recoverable = await vtxoManager.getRecoverableBalance();
if (recoverable.recoverable > 0n) {
console.error(`[daemon] recovering ${recoverable.recoverable} sats in swept vtxo(s)...`);
await vtxoManager.recoverVtxos();
console.error("[daemon] vtxo recovery complete");
}
} catch (err) {
console.error(`[daemon] vtxo recovery error: ${err instanceof Error ? err.message : err}`);
}
})();

// Auto-delegate VTXOs when delegator is configured
let stopVtxoDelegate: (() => void) | undefined;
const delegatorManager = await wallet.getDelegatorManager();
if (delegatorManager) {
const delegateSettled = async () => {
try {
const vtxos = (await wallet.getVtxos({ withRecoverable: false })).filter(
(v: ExtendedVirtualCoin) => v.virtualStatus.state === "settled"
);
if (vtxos.length === 0) return;
const address = await wallet.getAddress();
const result = await delegatorManager.delegate(vtxos, address);
if (result.delegated.length > 0) {
console.error(`[daemon] delegated ${result.delegated.length} vtxo(s) for auto-renewal`);
}
} catch (err) {
console.error(`[daemon] vtxo delegation error: ${err instanceof Error ? err.message : err}`);
}
};

// Delegate any existing settled VTXOs on startup
void delegateSettled();

// Subscribe and delegate on every incoming VTXO
wallet.notifyIncomingFunds((funds: IncomingFunds) => {
if (funds.type === "vtxo" && funds.newVtxos.length > 0) {
void delegateSettled();
}
}).then((stop: () => void) => { stopVtxoDelegate = stop; }).catch((err: unknown) => {
console.error(`[daemon] vtxo delegate subscription error: ${err instanceof Error ? err.message : err}`);
});

console.error("[daemon] vtxo auto-delegation started");
}

// Start HTTP server
server.listen(port, "127.0.0.1", () => {
saveDaemonPid(process.pid, port);
Expand All @@ -318,6 +372,7 @@ async function runDaemon() {
// Graceful shutdown
const shutdown = async () => {
console.error("[daemon] shutting down...");
stopVtxoDelegate?.();
monitor.stop();
authMonitor.stop();
await ctx.lightning.stopSwapManager();
Expand Down
94 changes: 90 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion skills/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@clw-cash/sdk": "workspace:*",
"@arkade-os/sdk": "^0.3.12",
"@arkade-os/sdk": "^0.4.10",
"@arkade-os/boltz-swap": "^0.2.18",
"@lendasat/lendaswap-sdk-pure": "^0.0.2"
},
Expand Down
Loading