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
|
<script>
import { hex } from "@scure/base";
import { bytesToHex } from "@noble/hashes/utils.js";
import WalletPass from "$comp/WalletPass.svelte";
import { run } from "svelte/legacy";
import { onMount } from "svelte";
import { goto } from "$app/navigation"; import Balance from "$comp/Balance.svelte";
import { t } from "$lib/translations";
import { versions } from "$lib/utils";
import { arkkey } from "$lib/ark";
import {
getRememberedWalletPassword,
forgetWalletPassword,
} from "$lib/passwordCache";
let { user, rate, account } = $props();
let {
name,
seed,
balance,
id,
type: accountType,
arkAddress,
} = $derived(account);
let arkBalance = $state(0);
let arkLoading = $state(false);
let passwordPrompt = $state(false);
let password = $state();
let pendingUrl = $state();
let cancel = $state(() => {
passwordPrompt = false;
pendingUrl = undefined;
});
let tryUnlockArk = async () => {
const cached = getRememberedWalletPassword();
if (!cached) return false;
try {
const { decrypt } = await import("nostr-tools/nip49");
if (seed) {
// Legacy: per-account seed is raw hex key
$arkkey = hex.encode(decrypt(seed, cached));
} else if (user.seed) {
// Master seed: derive m/86'/0'/0'/0/0
const [
{ HDKey },
{ entropyToMnemonic, mnemonicToSeed },
{ wordlist },
] = await Promise.all([
import("@scure/bip32"),
import("@scure/bip39"),
import("@scure/bip39/wordlists/english.js"),
]);
let entropy = await decrypt(user.seed, cached);
let mnemonic = entropyToMnemonic(entropy, wordlist);
let s = await mnemonicToSeed(mnemonic, cached);
let master = HDKey.fromMasterSeed(s, versions);
let arkChild = master.derive("m/86'/0'/0'/0/0");
$arkkey = bytesToHex(arkChild.privateKey);
} else {
return false;
}
return true;
} catch (e) {
forgetWalletPassword();
return false;
}
};
let setAccount = async (event, url) => {
event.preventDefault();
event.stopPropagation();
document.cookie = `aid=${id}; path=/; max-age=86400`;
if (isArk) {
pendingUrl = url;
if ($arkkey) {
goto(pendingUrl);
} else if (await tryUnlockArk()) {
goto(pendingUrl);
} else {
passwordPrompt = true;
}
} else {
$arkkey = undefined;
goto(url);
}
};
let submitPassword = async () => {
const { decrypt } = await import("nostr-tools/nip49");
if (seed) {
$arkkey = hex.encode(decrypt(seed, password));
} else if (user.seed) {
const [
{ HDKey },
{ entropyToMnemonic, mnemonicToSeed },
{ wordlist },
] = await Promise.all([
import("@scure/bip32"),
import("@scure/bip39"),
import("@scure/bip39/wordlists/english.js"),
]);
let entropy = await decrypt(user.seed, password);
let mnemonic = entropyToMnemonic(entropy, wordlist);
let s = await mnemonicToSeed(mnemonic, password);
let master = HDKey.fromMasterSeed(s, versions);
let arkChild = master.derive("m/86'/0'/0'/0/0");
$arkkey = bytesToHex(arkChild.privateKey);
}
passwordPrompt = false;
if (pendingUrl) {
goto(pendingUrl);
pendingUrl = undefined;
}
};
let displayType = $derived(
accountType === "ark"
? $t("accounts.ark")
: seed || (user.seed && accountType === "bitcoin")
? $t("accounts.bitcoin")
: $t("accounts.custodial"),
);
let displayName = $derived(name || displayType);
let isArk = $derived(accountType === "ark");
let goPayments = (e) => {
e.preventDefault();
setAccount(e, "/payments");
};
let goSettings = (e) => {
e.preventDefault();
e.stopPropagation();
document.cookie = `aid=${id}; path=/; max-age=86400`;
goto(`/account/${id}`);
};
</script>
<div
class="shadow-lg space-y-4 p-4 cursor-pointer hover:bg-base-200"
aria-label="Payments"
role="button"
tabindex="0"
onclick={goPayments}
onkeydown={(e) => (e.key === "Enter" || e.key === " ") && goPayments(e)}
>
<div class="flex items-start justify-between gap-4">
<Balance {balance} {user} {rate} {id} />
<a
href={`/account/${id}`}
class="font-bold text-lg opacity-60 hover:opacity-100 transition-opacity flex items-center gap-2 shrink-0"
aria-label="Account settings"
onclick={goSettings}
>
{#if isArk}
<img
src="/images/ark.png"
class="w-8 h-8 rounded-full object-cover"
alt="Ark"
/>
{:else if seed || (user.seed && accountType === "bitcoin")}
<iconify-icon
noobserver
icon="cryptocurrency-color:btc"
width="32"
></iconify-icon>
{:else}
<img src="/images/icon.png" class="w-8 h-8" />
{/if}
{displayName}
</a>
</div>
<div class="flex w-full text-xl gap-2">
<a
href={"/invoice"}
class="contents"
onclick={(e) => setAccount(e, "/invoice")}
>
<button class="btn !w-auto flex-1 !bg-base-300">
<iconify-icon
noobserver
icon="ph:hand-coins-bold"
width="32"
flip="horizontal"
></iconify-icon>
<div class="my-auto hidden sm:block">{$t("user.dashboard.receive")}</div>
</button>
</a>
<a
href={`/send`}
class="contents"
onclick={(e) => setAccount(e, "/send")}
>
<button type="button" class="btn !w-auto flex-1 !bg-base-300">
<iconify-icon noobserver icon="ph:paper-plane-right-bold" width="32"
></iconify-icon>
<div class="my-auto hidden sm:block">{$t("user.dashboard.send")}</div>
</button>
</a>
</div>
</div>
{#if passwordPrompt}
<WalletPass bind:password bind:cancel submit={submitPassword} />
{/if}
|