aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Account.svelte
blob: 33c769de21dfa8f0c460a37eb930d95f0cbedb31 (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
<script>
  import { hex } from "@scure/base";
  import WalletPass from "$comp/WalletPass.svelte";
  import { run } from "svelte/legacy";
  import { onMount } from "svelte";
  import { goto } from "$app/navigation";
  import Icon from "$comp/Icon.svelte";
  import Balance from "$comp/Balance.svelte";
  import { t } from "$lib/translations";
  import { arkkey } from "$lib/ark";

  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 setAccount = (event, url) => {
    event.preventDefault();
    document.cookie = `aid=${id}; path=/; max-age=86400`;
    if (isArk) {
      pendingUrl = url;
      if ($arkkey) goto(pendingUrl);
      else passwordPrompt = true;
    } else {
      $arkkey = undefined;
      goto(url);
    }
  };

  let submitPassword = async () => {
    const { decrypt } = await import("nostr-tools/nip49");
    $arkkey = hex.encode(decrypt(seed, password));
    passwordPrompt = false;
    if (pendingUrl) {
      goto(pendingUrl);
      pendingUrl = undefined;
    }
  };

  let displayType = $derived(
    accountType === "ark" ? "Ark" : seed ? "Bitcoin" : "Custodial",
  );
  let isArk = $derived(accountType === "ark");
</script>

<div class="rounded-2xl bg-base-200 p-5 shadow space-y-4" aria-label="Payments">
  <div class="flex items-center gap-3">
    {#if isArk}
      <img src="/images/ark.png" class="w-8 h-8 rounded-full object-cover" alt="Ark" />
    {:else if seed}
      <iconify-icon noobserver icon="cryptocurrency-color:btc" width="32"></iconify-icon>
    {:else}
      <img src="/images/icon.png" class="w-8 h-8" />
    {/if}
    <span class="font-bold text-lg">{displayType}</span>
    {#if id !== user.id}
      <a
        href={`/account/${id}`}
        class="ml-auto opacity-40 hover:opacity-100 transition-opacity"
        aria-label="Settings"
      >
        <iconify-icon
          noobserver
          icon="ph:gear-bold"
          width="24"
        ></iconify-icon>
      </a>
    {/if}
  </div>

  <Balance {balance} {user} {rate} {id} />

  <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">
        <iconify-icon
          noobserver
          icon="ph:hand-coins-bold"
          width="32"
          flip="horizontal"
        ></iconify-icon>
        <div class="my-auto">{$t("user.dashboard.receive")}</div>
      </button>
    </a>

    <a
      href={"/payments"}
      class="contents"
      onclick={(e) => setAccount(e, "/payments")}
    >
      <button class="btn !w-auto flex-1">
        <iconify-icon
          noobserver
          icon="ph:clock-bold"
          width="32"
        ></iconify-icon>
        <div class="my-auto">{$t("user.dashboard.history")}</div>
      </button>
    </a>

    <a
      href={`/send`}
      class="contents"
      onclick={(e) => setAccount(e, "/send")}
    >
      <button type="button" class="btn !w-auto flex-1">
        <iconify-icon noobserver icon="ph:paper-plane-right-bold" width="32"
        ></iconify-icon>
        <div class="my-auto">{$t("user.dashboard.send")}</div>
      </button>
    </a>
  </div>
</div>

{#if passwordPrompt}
  <WalletPass bind:password bind:cancel submit={submitPassword} />
{/if}