aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
blob: 8291d1733175719a622431681ff3704dd87fa4a4 (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
import store from "$lib/store";
import { g } from "$lib/db";
import config from "$config";

export const nada = () => {};

export const sleep = n => new Promise(r => setTimeout(r, n));
export const wait = async (f, s = 300, n = 50) => {
  let i = 0;
  while (!(await f()) && i < s) (await sleep(n)) && i++;
  if (i >= s) throw new Error("timeout");
  return f();
};

export const requirePin = async ({ body, user }) => {
  if (!user || (user.pin && user.pin !== body.pin))
    throw new Error("Invalid pin");
};

export const prod = process.env.NODE_ENV === "production";
export const fail = msg => {
  throw new Error(msg);
};

export const SATS = 100000000;
export const sats = n => Math.round(n * SATS);
export const btc = n => parseFloat((n / SATS).toFixed(8));

export const uniq = (a, k) => [...new Map(a.map(x => [k(x), x])).values()];
export const pick = (O, K) =>
  K.reduce((o, k) => (typeof O[k] !== "undefined" && (o[k] = O[k]), o), {});

export const bip21 = (address, { amount, memo, tip }) => {
  let url = amount || memo ? `bitcoin:${address}?` : address;
  if (amount)
    url += `amount=${((amount + tip) / SATS).toFixed(8)}${memo ? "&" : ""}`;
  if (memo) url += `message=${memo}`;

  return url;
};

export const fields = [
  "cipher",
  "pubkey",
  "password",
  "username",
  "salt",
  "currency",
  "currencies",
  "fiat",
  "otpsecret",
  "balance",
  "ip",
  "pin",
  "display",
  "haspin",
  "profile",
  "prompt",
  "banner"
];