aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ark.ts
blob: ff82393ba194068eb714d45e8c5d0249fe271a84 (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
import { persistLocal } from "$lib/store";
import { post } from "$lib/utils";
import { get } from "svelte/store";

export const arkServerUrl = "http://localhost:7070";
export const arkkey = persistLocal("arkkey", "");

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 });
};

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

	const history = await wallet.getTransactionHistory();
	if (!history.length) return;

	const transactions = history.map((tx) => ({
		arkTxid: tx.key.arkTxid || null,
		commitmentTxid: tx.key.commitmentTxid || null,
		amount: tx.type === "RECEIVED" ? tx.amount : -tx.amount,
		settled: tx.settled,
		createdAt: tx.createdAt,
	}));

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

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 notifyIncomingFunds during sends
export let arkSending = false;

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

	// Suppress incoming notifications during send to prevent
	// change VTXOs from being credited as incoming payments
	arkSending = true;

	try {
		const txid = await wallet.sendBitcoin({ address, amount });

		// 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 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));
};