aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
blob: 1ff24c67d640ad7701b5fa15416c4a6fa15f1d44 (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
import db from "$db";
import sequelize from "@sequelize/core";
const { Op } = sequelize;

export const getUser = async (username, transaction) => {
  let params = {
    include: [
      {
        model: db.Account,
        as: "account"
      }
    ],
    where: {
      username: { [Op.or]: [username, username.replace(/ /g, "")] }
    }
  };

  if (transaction) {
    params.lock = transaction.LOCK.UPDATE;
    params.transaction = transaction;
  }

  let user = await db.User.findOne(params);
  if (!user) throw new Error(`User ${username} not found`);

  return user;
};

export const getUserById = async (id, transaction) => {
  let params = {
    include: [
      {
        model: db.Account,
        as: "account"
      }
    ],
    where: { id }
  };

  if (transaction) {
    params.lock = transaction.LOCK.UPDATE;
    params.transaction = transaction;
  }

  let user = await db.User.findOne(params);
  if (!user) throw new Error("User not found");

  return user;
};

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

export const SATS = 100000000;
export const toSats = n => Math.round(n * SATS);

export const getBlockHeight = message => {
  let buffer = Buffer.from(message, "hex");
  if (buffer.length < 80) throw new Error("Buffer too small (< 80 bytes)");

  let offset = 0;
  const readSlice = n => {
    offset += n;
    return buffer.slice(offset - n, offset);
  };

  const readUInt32 = () => {
    const i = buffer.readUInt32LE(offset);
    offset += 4;
    return i;
  };

  const readUInt8 = () => {
    const i = buffer.readUInt8(offset);
    offset += 1;
    return i;
  };

  const readVarInt = () => {
    const vi = varuint.decode(buffer, offset);
    offset += varuint.decode.bytes;
    return vi;
  };

  readUInt32();
  readSlice(32);
  readSlice(32);
  readUInt32();
  return readUInt32();
};