aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/account/pass/+page.svelte
blob: 3be7628db519ea3276e88fdff38ead51eb83b90f (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
<script lang="ts">
  import { preventDefault } from "svelte/legacy";

  import { tick, onMount } from "svelte";
  import { t } from "$lib/translations";
  import { mnemonic } from "$lib/store";
  import Spinner from "$comp/Spinner.svelte";
  import { encrypt } from "nostr-tools/nip49";
  import {
    mnemonicToSeed,
    mnemonicToEntropy,
    entropyToMnemonic,
  } from "@scure/bip39";
  import { wordlist } from "@scure/bip39/wordlists/english.js";
  import { focus, versions, fail, post } from "$lib/utils";
  import { goto } from "$app/navigation";
  import { HDKey } from "@scure/bip32";

  let { data } = $props();

  let { user } = data;
  let submitting = $state();

  let confirm = $state(),
    password = $state(),
    revealPassword = $state(),
    revealConfirm = $state();
  let type = "bitcoin";
  let name = $t("accounts.savings");

  onMount(() => {
    if (!$mnemonic) goto("/account/savings");
  });

  let submit = async () => {
    submitting = true;
    await tick();
    if (!password || password !== confirm) {
      fail($t("accounts.passwordMismatch"));
      return;
    }

    try {
      let seed = await encrypt(
        mnemonicToEntropy($mnemonic, wordlist),
        password,
      );

      let master = HDKey.fromMasterSeed(
        await mnemonicToSeed($mnemonic, password),
        versions,
      );

      let child = master.derive("m/84'/0'/0'");
      let pubkey = child.publicExtendedKey;
      let fingerprint = child.fingerprint.toString(16).padStart(8, "0");
      await post("/account", { fingerprint, pubkey, name, seed, type });
      $mnemonic = "";
      goto(`/${user.username}`);
    } catch (e) {
      console.log(e);
      fail(e.message);
    }

    submitting = false;
  };
</script>

<div class="space-y-5">
  <div>
    <h1 class="text-center text-3xl font-semibold">
      {$t("accounts.accountPassword")}
    </h1>
  </div>

  <form
    onsubmit={preventDefault(submit)}
    class="container w-full mx-auto text-lg px-4 max-w-xl space-y-5"
  >
    <p class="text-secondary mb-1">
      {$t("accounts.passwordWarning")}
    </p>

    <label
      for="password"
      class="input flex items-center justify-center gap-2 w-full"
    >
      {#if revealPassword}
        <input
          name="password"
          type="text"
          required
          bind:value={password}
          autocapitalize="none"
          class="clean"
        />
      {:else}
        <input
          use:focus
          name="password"
          type="password"
          placeholder={$t("accounts.password")}
          required
          bind:value={password}
          autocapitalize="none"
          class="clean"
        />
      {/if}

      <iconify-icon noobserver
        class="cursor-pointer ml-auto"
        onclick={() => (revealPassword = !revealPassword)}
        icon={revealPassword ? "ph:eye-bold" : "ph:eye-slash-bold"}
        width="32"
      ></iconify-icon>
    </label>

    <label
      for="confirm"
      class="input flex items-center justify-center gap-2 w-full"
    >
      {#if revealConfirm}
        <input
          name="confirm"
          type="text"
          placeholder="Confirm password"
          required
          bind:value={confirm}
          autocapitalize="none"
          class="clean"
        />
      {:else}
        <input
          type="password"
          placeholder={$t("accounts.confirmPassword")}
          required
          bind:value={confirm}
          autocapitalize="none"
          class="clean"
        />
      {/if}
      <iconify-icon noobserver
        class="cursor-pointer ml-auto"
        onclick={() => (revealConfirm = !revealConfirm)}
        icon={revealConfirm ? "ph:eye-bold" : "ph:eye-slash-bold"}
        width="32"
      ></iconify-icon>
    </label>

    <div class="flex gap-2">
      <a href={`/account/seed`} class="contents">
        <button type="button" class="btn !w-auto grow">
          {$t("accounts.back")}
        </button>
      </a>
      <button
        disabled={submitting}
        type="submit"
        class="btn btn-accent !w-auto grow"
      >
        {#if submitting}
          <Spinner />
        {:else}
          {$t("accounts.finish")}
        {/if}
      </button>
    </div>
  </form>
</div>