aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ark.ts
blob: 82d1ccce23740d43b4ea6d66e593205d4a0585bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { PUBLIC_COINOS_NETWORK } from "$env/static/public";
import { persistLocal } from "$lib/store";
import { post } from "$lib/utils";
import { get } from "svelte/store";

export const arkServerUrl = {
  bitcoin: "https://arkade.computer",
  regtest: "http://localhost:7070",
}[PUBLIC_COINOS_NETWORK || "regtest"];
export const arkkey = persistLocal("arkkey", "");
export const arkaid = persistLocal("arkaid", "");

let arkSdkPromise: Promise<typeof import("@arkade-os/sdk")> | undefined;
const loadArkSdk = () => (arkSdkPromise ||= import("@arkade-os/sdk"));

let walletInstance: any;
let walletKey: string | undefined;

export const getWallet = async () => {
  const key = get(arkkey);
  if (!key) {
    walletInstance = undefined;
    walletKey = undefined;
    return;
  }
  if (walletInstance && walletKey === key) return walletInstance;

  const { SingleKey, Wallet } = await loadArkSdk();
  const identity = SingleKey.fromHex(key);

  walletInstance = await Wallet.create({
    identity,
    arkServerUrl,
  });
  walletKey = key;
  return walletInstance;
};

export const getAddress = async (): Promise<string> => {
  const wallet = await getWallet();
  return wallet.getAddress();
};

export const getBalance = async (): Promise<{ available: number }> => {
  const wallet = await getWallet();
  return wallet.getBalance();
};

export const getVtxos = async () => {
  const wallet = await getWallet();
  if (!wallet) return [];
  return wallet.getVtxos({ spendableOnly: false });
};

const _syncTransactions = async (aid: string) => {
  const wallet = await getWallet();
  if (!wallet) return;

  const walletAddr = await wallet.getAddress();
  console.log("arkSync: wallet address", walletAddr);

  const transactions: any[] = [];

  const history = await wallet.getTransactionHistory();
  const historyTxids = new Set<string>();
  console.log("arkSync: history", history.length, history);

  for (const tx of history) {
    if (tx.key.arkTxid) historyTxids.add(tx.key.arkTxid);
    if (tx.key.commitmentTxid) historyTxids.add(tx.key.commitmentTxid);
    transactions.push({
      arkTxid: tx.key.arkTxid || null,
      commitmentTxid: tx.key.commitmentTxid || null,
      amount: tx.type === "RECEIVED" ? tx.amount : -tx.amount,
      settled: tx.settled,
      createdAt: tx.createdAt,
    });
  }

  // Include preconfirmed VTXOs not yet in transaction history
  const vtxos = await wallet.getVtxos({ spendableOnly: false });
  console.log("arkSync: vtxos", vtxos.length, vtxos);
  for (const v of vtxos) {
    if (v.virtualStatus?.state !== "preconfirmed") continue;
    const id = v.arkTxId || v.txid;
    if (!id || historyTxids.has(id)) continue;
    historyTxids.add(id);
    transactions.push({
      arkTxid: v.arkTxId || null,
      commitmentTxid: v.txid || null,
      amount: Number(v.value),
      settled: false,
      createdAt: v.createdAt ? new Date(v.createdAt).getTime() : Date.now(),
    });
  }

  const bal = await wallet.getBalance();
  const balance = (bal.available || 0) + (bal.preconfirmed || 0);

  return post("/post/ark/sync", { transactions, aid, balance });
};

export const syncTransactions = async (aid: string) => {
  if (arkSending) return;
  const p = _syncTransactions(aid);
  syncPromise = p;
  try {
    return await p;
  } finally {
    if (syncPromise === p) syncPromise = null;
  }
};

export const vtxoKey = (v: { txid: string; vout: number }) => `${v.txid}:${v.vout}`;

export const getProcessedVtxos = (): Set<string> => {
  try {
    return new Set(JSON.parse(localStorage.getItem("processedArkVtxos") || "[]"));
  } catch {
    return new Set();
  }
};

export const addProcessedVtxos = (keys: string[]) => {
  const processed = getProcessedVtxos();
  for (const k of keys) processed.add(k);
  const arr = [...processed].slice(-200);
  localStorage.setItem("processedArkVtxos", JSON.stringify(arr));
};

// Flag to suppress sync during sends
export let arkSending = false;
let syncPromise: Promise<any> | null = null;

const withTimeout = <T>(promise: Promise<T>, ms: number, label: string) =>
  Promise.race([
    promise,
    new Promise<T>((_, reject) => setTimeout(() => reject(new Error(`${label} timed out`)), ms)),
  ]);

export const sendArk = async (address: string, amount: number) => {
  const wallet = await getWallet();
  if (!wallet) throw new Error("Ark wallet not available");

  // Suppress sync polling and wait for any in-flight sync to finish
  arkSending = true;
  if (syncPromise) await syncPromise.catch(() => {});

  try {
    // Refresh expired VTXOs before attempting to send
    const balance = await wallet.getBalance();
    if (balance.available < amount && balance.recoverable > 0) {
      console.log("ark send: refreshing expired VTXOs before send");
      await refresh();
    }

    const freshBalance = await wallet.getBalance();
    if (freshBalance.available < amount) {
      throw new Error(`Insufficient funds: ${freshBalance.available} available, need ${amount}`);
    }
    let txid;
    try {
      txid = await withTimeout(wallet.sendBitcoin({ address, amount }), 45_000, "Ark send");
    } catch (e: any) {
      if (e?.message?.includes("VTXO_ALREADY_REGISTERED")) {
        await wallet.settle();
        const vtxos = await wallet.getVtxos({ spendableOnly: false });
        if (vtxos?.length) addProcessedVtxos(vtxos.map(vtxoKey));
        txid = await withTimeout(wallet.sendBitcoin({ address, amount }), 45_000, "Ark send retry");
      } else {
        throw e;
      }
    }

    // Mark post-send VTXOs as processed so notifyIncomingFunds
    // ignores change VTXOs from this send
    const vtxos = await wallet.getVtxos({ spendableOnly: false });
    if (vtxos?.length) addProcessedVtxos(vtxos.map(vtxoKey));

    return txid;
  } finally {
    arkSending = false;
  }
};

export const sendArkViaForward = async ({
  serverArkAddress,
  amount,
  aid,
  forward,
  user,
}: {
  serverArkAddress: string;
  amount: number;
  aid: string;
  forward: string;
  user: any;
}) => {
  const txid = await sendArk(serverArkAddress, amount);

  await post("/post/ark/vault-send", { hash: txid, amount, aid });

  const inv = await post("/post/invoice", {
    invoice: { type: "ark", amount, forward, aid },
    user,
  });

  return post("/post/ark/receive", { amount, hash: txid, iid: inv.id });
};

export const refresh = async () => {
  const wallet = await getWallet();
  if (!wallet) return;

  const { VtxoManager } = await loadArkSdk();
  const balance = await wallet.getBalance();
  console.log("ark refresh: balance", balance);
  const manager = new VtxoManager(wallet);

  let acted = false;

  if (balance.recoverable > 0) {
    console.log("ark refresh: recovering", balance.recoverable, "swept sats");
    await manager.recoverVtxos();
    acted = true;
  }

  const expiring = await manager.getExpiringVtxos();
  if (expiring.length > 0) {
    console.log("ark refresh: renewing", expiring.length, "expiring vtxos");
    await manager.renewVtxos();
    acted = true;
  }

  // Mark post-recovery/renewal VTXOs as processed so notifyIncomingFunds
  // doesn't treat them as new incoming payments
  if (acted) {
    const vtxos = await wallet.getVtxos({ spendableOnly: false });
    if (vtxos?.length) addProcessedVtxos(vtxos.map(vtxoKey));
  }
};

export const settle = async () => {
  const wallet = await getWallet();
  if (!wallet) return;

  const balance = await wallet.getBalance();
  if (!balance.preconfirmed) return;

  await wallet.settle();

  // Mark post-settle VTXOs as processed so notifyIncomingFunds
  // ignores renewed VTXOs (they get new txids after an Ark round)
  const vtxos = await wallet.getVtxos({ spendableOnly: false });
  if (vtxos?.length) addProcessedVtxos(vtxos.map(vtxoKey));
};