diff options
| author | Adam Soltys <[email protected]> | 2024-12-03 14:34:23 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-12-03 14:34:23 -0800 |
| commit | 0a318bf04d7691b47f65e1aa431a618d9bd935c0 (patch) | |
| tree | 01d4aeeaa1c238c19c214f9daae12d892ad18d01 | |
| parent | f011ed304da9d72549ec0d967a7500cfe487765f (diff) | |
| download | coinos-server-0a318bf04d7691b47f65e1aa431a618d9bd935c0.tar.gz coinos-server-0a318bf04d7691b47f65e1aa431a618d9bd935c0.zip | |
return nwc and token on registration
| -rw-r--r-- | lib/register.ts | 13 | ||||
| -rw-r--r-- | routes/users.ts | 10 |
2 files changed, 21 insertions, 2 deletions
diff --git a/lib/register.ts b/lib/register.ts index 139aefcf..7fd1f680 100644 --- a/lib/register.ts +++ b/lib/register.ts @@ -8,6 +8,12 @@ import { db } from "$lib/db"; import { l, warn } from "$lib/logging"; import { fail, getUser } from "$lib/utils"; +import { bytesToHex, randomBytes } from "@noble/hashes/utils"; +import { getPublicKey } from "nostr"; +import { encrypt as nip49encrypt } from "nostr-tools/nip49"; + +import { nip19 } from "nostr-tools"; + const valid = /^[\p{L}\p{N}]{2,24}$/u; export default async (user, ip) => { let { password, pubkey, username } = user; @@ -46,11 +52,17 @@ export default async (user, ip) => { } } + const sk = randomBytes(32); + user.pubkey ||= bytesToHex(getPublicKey(sk)); + user.nsec = nip49encrypt(sk, password); + user.currencies = [...new Set([user.currency, "CAD", "USD"])]; user.fiat = false; user.otpsecret = authenticator.generateSecret(); user.migrated = true; user.locktime = 300; + user.npub = nip19.npubEncode(user.pubkey); + user.nwc = bytesToHex(randomBytes(32)); const account = JSON.stringify({ id, @@ -59,6 +71,7 @@ export default async (user, ip) => { }); db.multi() + .set(getPublicKey(user.nwc), user.id) .set(`user:${id}`, JSON.stringify(user)) .set(`user:${username}`, id) .set(`user:${pubkey}`, id) diff --git a/routes/users.ts b/routes/users.ts index a2b3b407..9588727f 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -156,10 +156,16 @@ export default { "salt", ]; - user = await register(pick(user, fields), ip, false); + user = await register(pick(user, fields), ip); + + const payload = { id: user.id }; + const token = jwt.sign(payload, config.jwt); + l("registered new user", user.username); - res.send(pick(user, whitelist)); + + res.send({ ...pick(user, whitelist), token }); } catch (e) { + console.log(e); res.code(500).send(e.message); } }, |
