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
|
import { got } from "got";
import { authenticator } from "otplib";
import { v4 } from "uuid";
import config from "$config";
import countries from "$lib/countries";
import { db } from "$lib/db";
import { l, warn } from "$lib/logging";
import { fail, getUser } from "$lib/utils";
const valid = /^[\p{L}\p{N}]{2,24}$/u;
export default async (user, ip) => {
let { password, pubkey, username } = user;
l("registering", username);
if (!username) fail("Username required");
username = username.toLowerCase();
if (!valid.test(username))
fail("Usernames can only have letters and numbers");
const id = v4();
user.id = id;
const exists = await getUser(username);
if (exists) fail(`Username ${username} taken`);
if (password) {
user.password = await Bun.password.hash(password, {
algorithm: "bcrypt",
cost: 4,
});
}
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];
} 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;
const account = JSON.stringify({
id,
type: "ecash",
name: "Spending",
});
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);
return user;
};
|