forked from fybx/zkl-roadhog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
59 lines (49 loc) · 1.55 KB
/
user.js
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
import { Key } from "./zkl-kds/key.js";
import { generateKeypair, generateMnemonic } from "./zkl-kds/key-derivation.js";
const endpoint = "http://localhost:3000";
export const createUser = async function (name, networkType, walletAddress) {
const authToken = localStorage.getItem("authToken");
const mnemonic = generateMnemonic();
const { publicKey, privateKey } = await generateKeypair(mnemonic);
const response = await fetch(`${endpoint}/users`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify({
profileName: name,
networkType,
publicKey: publicKey.asHexString,
walletAddress,
}),
});
if (response.ok) return;
console.error("Backend error:", (await response.json()).error);
};
export const updateUser = async function (publicKey, updates = {}) {
const allowedParams = ["profileName", "networkType", "address"];
const body = {};
for (const key of allowedParams) {
if (key in updates) {
body[key] = updates[key];
}
}
if (Object.keys(body).length === 0) {
console.error("No valid fields to update");
return;
}
const response = await fetch(`${endpoint}/users/${publicKey}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (response.ok) {
const updatedUser = await response.json();
console.log("User updated successfully:", updatedUser);
} else {
console.error((await response.json()).error);
}
};