aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Pin.svelte
blob: fb17c612b037a8c38a1f207388e5aee1ef7cf24a (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
<script>
  import { t } from "$lib/translations";
  import { post, success, fail, focus, setCookie } from "$lib/utils";
  import Pinpad from "$comp/Pinpad.svelte";
  import { pin } from "$lib/store";
  import { onMount } from "svelte";

  export let value = "";
  export let title = $t("user.settings.verifyPIN");
  export let cancel = () => (p = "");
  export let notify = true;

  let p = "";

  $: update(value);
  let update = (value) => (p = value);

  $: p.toString().length > 5 && (value = p.toString());

  let loaded;
  onMount(() => setTimeout(() => (loaded = true), 500));

  let locktime = 30 * 60;

  $: loaded && checkPin(p);
  let checkPin = async (p) => {
    if (p?.length !== 6) return;
    let result;
    try {
      result = await post("/pin", { pin: p });
    } catch (e) {
      console.log("Pin check failed", e);
    }

    if (result) {
      if (notify) success("Pin confirmed");
      if (locktime) setCookie("pin", p, locktime);
      $pin = p;
    } else {
      fail("Invalid pin");
      value = "";
    }
  };
</script>

{#if loaded}
  <div
    class="fixed bg-base-100 bg-opacity-90 inset-0 h-full w-full z-50 cursor-default"
    on:click|preventDefault
  >
    <div
      class="mx-auto p-5 border shadow-lg rounded-md bg-base-100 space-y-5 max-w-lg"
    >
      <h1 class="text-center text-2xl font-semibold">{title}</h1>
      <Pinpad bind:v={p} {cancel} />

      <div>
        <label for="locktime" class="font-bold"
          >{$t("user.settings.rememberFor")}</label
        >
        <select
          name="locktime"
          bind:value={locktime}
        >
          <option value={30}>30 {$t("user.settings.seconds")}</option>
          <option value={5 * 60}>5 {$t("user.settings.minutes")}</option>
          <option value={10 * 60}>10 {$t("user.settings.minutes")}</option>
          <option value={30 * 60}>30 {$t("user.settings.minutes")}</option>
          <option value={60 * 60}>1 {$t("user.settings.hour")}</option>
          <option value={8 * 60 * 60}>8 {$t("user.settings.hours")}</option>
        </select>
      </div>

      <div class="w-full flex">
        <button class="btn" on:click|preventDefault={cancel}>
          <div class="my-auto">Cancel</div>
        </button>
      </div>
    </div>
  </div>
{/if}
/span>from "nostr-tools/nip49"; import { authenticator } from "otplib"; import { v4 } from "uuid"; const valid = /^[\p{L}\p{N}]{2,24}$/u; export default async (user, ip) => { let { password, pubkey, username } = user; l("registering", username); const reserved = ["ecash"]; if (!username) fail("Username required"); username = username.toLowerCase().replace(/\s/g, ""); if (!valid.test(username)) fail("Usernames can only have letters and numbers"); if (reserved.includes(username)) fail("Invalid username"); if (username.includes("undefined")) fail("Invalid username"); const id = v4(); user.id = id; const exists = await db.exists(`user:${username}`); if (exists) fail(`Username ${username} taken`); if (password) { user.password = await Bun.password.hash(password, { algorithm: "bcrypt", cost: 12, }); } user.created = Date.now(); user.currency = "USD"; if (config.ipregistry) { try { const { location: { country: { code } }, }: any = await got( `https://api.ipregistry.co/${ip}?key=${config.ipregistry}&fields=location.country.code`, ).json(); user.currency = countries[code]; user.country = code; // stored for analytics (signup geography) } catch (e) { warn("unable to detect country from IP", username); } } user.currencies = [...new Set([user.currency, "CAD", "USD"])]; user.fiat = false; user.otpsecret = authenticator.generateSecret(); user.migrated = true; user.locktime = 300; let sk; if (!pubkey) { sk = randomBytes(32); pubkey = getPublicKey(sk); user.pubkey = pubkey; user.nsec = nip49encrypt(sk, password); } user.npub = nip19.npubEncode(pubkey); const account = JSON.stringify({ id, type: "ecash", name: "Spending", }); const bytes = randomBytes(32); const secret = bytesToHex(bytes); const app = { uid: id, secret, pubkey: getPublicKey(bytes), max_amount: 1000000, budget_renewal: "weekly", name: username, created: Date.now(), }; await s(`app:${app.pubkey}`, app); await db.sAdd(`${id}:apps`, app.pubkey); db.multi() .set(`user:${id}`, JSON.stringify(user)) .set(`user:${username}`, id) .set(`user:${pubkey}`, id) .set(`balance:${id}`, 0) .set(`account:${id}`, account) .set(`${pubkey}:follows:n`, 0) .set(`${pubkey}:followers:n`, 0) .set(`${pubkey}:pubkeys`, "[]") .lPush(`${id}:accounts`, id) .exec(); l("new user", username); if (sk) user.sk = bytesToHex(sk); return user; };