aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-12-30 21:57:14 -0800
committerAdam Soltys <[email protected]>2024-12-30 21:57:14 -0800
commitbec6c516f63b84720192a9a3909c09fccfce7143 (patch)
tree55bedf969606def5e0b623ae559f7a598033b110
parented4538ca280c7f4a0f0221ae214626234a1a4974 (diff)
downloadcoinos-server-bec6c516f63b84720192a9a3909c09fccfce7143.tar.gz
coinos-server-bec6c516f63b84720192a9a3909c09fccfce7143.zip
fix pubkey registration
-rw-r--r--lib/register.ts19
-rw-r--r--routes/users.ts13
2 files changed, 19 insertions, 13 deletions
diff --git a/lib/register.ts b/lib/register.ts
index 7fd1f680..36b9d443 100644
--- a/lib/register.ts
+++ b/lib/register.ts
@@ -1,22 +1,18 @@
-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";
-
import { bytesToHex, randomBytes } from "@noble/hashes/utils";
-import { getPublicKey } from "nostr";
+import { got } from "got";
+import { getPublicKey, nip19 } from "nostr-tools";
import { encrypt as nip49encrypt } from "nostr-tools/nip49";
-
-import { nip19 } from "nostr-tools";
+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;
+ let { password, username } = user;
l("registering", username);
if (!username) fail("Username required");
@@ -53,14 +49,15 @@ export default async (user, ip) => {
}
const sk = randomBytes(32);
- user.pubkey ||= bytesToHex(getPublicKey(sk));
+ const pubkey = getPublicKey(sk);
+ user.pubkey = pubkey;
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));
diff --git a/routes/users.ts b/routes/users.ts
index 3f453141..151bd534 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -13,9 +13,11 @@ import upload from "$lib/upload";
import { bail, fail, getUser, pick } from "$lib/utils";
import whitelist from "$lib/whitelist";
import rpc from "@coinos/rpc";
+import { randomBytes } from "@noble/hashes/utils";
import { $ } from "bun";
import jwt from "jsonwebtoken";
-import { nip19, verifyEvent } from "nostr-tools";
+import { getPublicKey, nip19, verifyEvent } from "nostr-tools";
+import { encrypt as nip49encrypt } from "nostr-tools/nip49";
import { authenticator } from "otplib";
import { v4 } from "uuid";
@@ -167,6 +169,7 @@ export default {
res.send({ ...pick(user, whitelist), token });
} catch (e) {
+ console.log(e);
err("problem registering", e.message);
res.code(500).send(e.message);
}
@@ -538,9 +541,15 @@ export default {
try {
user.pin = null;
- user.pubkey = null;
user.cipher = null;
user.salt = null;
+
+ if (user.nsec) {
+ const sk = randomBytes(32);
+ user.pubkey = getPublicKey(sk);
+ user.nsec = nip49encrypt(sk, password);
+ }
+
user.password = await Bun.password.hash(password, {
algorithm: "bcrypt",
cost: 4,