From 3aab57df2063d913bd16b953187ced242421f66a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 26 Nov 2025 21:44:55 -0800 Subject: ark stuff --- src/lib/ark.ts | 97 +++----------------------------------------------------- src/lib/parse.ts | 4 ++- 2 files changed, 7 insertions(+), 94 deletions(-) (limited to 'src/lib') diff --git a/src/lib/ark.ts b/src/lib/ark.ts index 7649a6bf..487898ae 100644 --- a/src/lib/ark.ts +++ b/src/lib/ark.ts @@ -1,54 +1,13 @@ import { persistLocal } from "$lib/store"; -import { get } from "svelte/store"; import { SingleKey, Wallet } from "@arkade-os/sdk"; -import { bytesToHex, randomBytes } from "@noble/hashes/utils"; -import { nip19 } from "nostr-tools"; -import { hex } from "@scure/base"; +import { get } from "svelte/store"; export const arkServerUrl = "http://localhost:7070"; export const arkkey = persistLocal("arkkey", ""); -export const generateKey = (): string => { - const privateKey = bytesToHex(randomBytes(32)); - arkkey.set(privateKey); - return privateKey; -}; - -export const getKey = (): string => { - let key = get(arkkey); - if (!key) { - key = generateKey(); - } - return key; -}; - -export const hasKey = (): boolean => { - return !!get(arkkey); -}; - -export const clearKey = (): void => { - arkkey.set(""); -}; - -export const keyToNsec = (key?: string): string => { - const k = key || getKey(); - return nip19.nsecEncode(hex.decode(k)); -}; - -export const nsecToKey = (nsec: string): string => { - const { type, data } = nip19.decode(nsec); - if (type !== "nsec") throw new Error("Invalid nsec format"); - return bytesToHex(data); -}; - -export const restoreFromNsec = (nsec: string): string => { - const key = nsecToKey(nsec); - arkkey.set(key); - return key; -}; - export const getWallet = async () => { - const key = getKey(); + const key = get(arkkey); + if (!key) return; const identity = SingleKey.fromHex(key); return Wallet.create({ @@ -62,55 +21,7 @@ export const getAddress = async (): Promise => { return wallet.getAddress(); }; -export const getBalance = async (): Promise<{ available: number; pending: number }> => { +export const getBalance = async (): Promise<{ available: number }> => { const wallet = await getWallet(); return wallet.getBalance(); }; - -export const syncArkAddress = async ( - currentAddress: string | undefined, - updateFn: (arkAddress: string) => Promise -): Promise => { - const address = await getAddress(); - if (address !== currentAddress) { - await updateFn(address); - } - return address; -}; - -export const sendArk = async (address: string, amount: number): Promise => { - const wallet = await getWallet(); - const vtxos = await wallet.getVtxos(); - - // Sort by expiry (spend oldest first) - const sorted = vtxos.sort( - (a, b) => (a.virtualStatus.batchExpiry ?? 0) - (b.virtualStatus.batchExpiry ?? 0) - ); - - // Select inputs - const inputs = []; - let selectedAmount = 0; - for (const vtxo of sorted) { - if (selectedAmount >= amount) break; - inputs.push(vtxo); - selectedAmount += vtxo.value; - } - - if (selectedAmount < amount) { - throw new Error(`Insufficient funds: have ${selectedAmount}, need ${amount}`); - } - - // Build outputs - const outputs: { address: string; amount: bigint }[] = [ - { address, amount: BigInt(amount) } - ]; - - // Add change output if needed - const change = selectedAmount - amount; - if (change > 0) { - const changeAddress = await wallet.getAddress(); - outputs.push({ address: changeAddress, amount: BigInt(change) }); - } - - return wallet.settle({ inputs, outputs }); -}; diff --git a/src/lib/parse.ts b/src/lib/parse.ts index c69032d1..bbcab429 100644 --- a/src/lib/parse.ts +++ b/src/lib/parse.ts @@ -11,7 +11,9 @@ export default async (s, host, cookies) => { let invoice; let user; - if (t.startsWith("ark")) redirect(307, `/send/ark/${t}`); + if (t.startsWith("ark") || t.startsWith("tark")) + redirect(307, `/send/ark/${t}`); + if (t.startsWith("http")) redirect(307, t); if (t.startsWith(host)) redirect(307, `http://${t}`); -- cgit v1.2.3