Skip to content

Commit c755571

Browse files
chore(web): initial-wrapper-update
1 parent 4c82235 commit c755571

File tree

4 files changed

+130
-33
lines changed

4 files changed

+130
-33
lines changed

web/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"siwe": "^2.3.1",
118118
"styled-components": "^5.3.11",
119119
"vanilla-jsoneditor": "^0.21.4",
120-
"viem": "^2.1.0",
120+
"viem": "^2.17.3",
121121
"vite": "^5.2.10",
122122
"wagmi": "^2.2.1"
123123
}

web/src/components/EnsureAuth.tsx

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useMemo, useState } from "react";
22

33
import * as jwt from "jose";
4-
import { SiweMessage } from "siwe";
4+
import { createSiweMessage } from "viem/siwe";
55
import { useAccount, useChainId, useSignMessage } from "wagmi";
66

77
import { Button } from "@kleros/ui-components-library";
@@ -41,7 +41,7 @@ export const EnsureAuth: React.FC<IEnsureAuth> = ({ children, className }) => {
4141
setIsLoading(true);
4242
if (!address) return;
4343

44-
const message = await createSiweMessage(address, "Sign In to Kleros with Ethereum.", chainId);
44+
const message = await createMessage(address, "Sign In to Kleros with Ethereum.", chainId);
4545

4646
const signature = await signMessageAsync({ message });
4747

@@ -52,9 +52,8 @@ export const EnsureAuth: React.FC<IEnsureAuth> = ({ children, className }) => {
5252
signature,
5353
message,
5454
})
55-
.then(async (res) => {
56-
const response = await res.json();
57-
setAuthToken(response["token"]);
55+
.then(async (token) => {
56+
setAuthToken(token);
5857
})
5958
.catch((err) => console.log({ err }))
6059
.finally(() => setIsLoading(false));
@@ -77,16 +76,15 @@ export const EnsureAuth: React.FC<IEnsureAuth> = ({ children, className }) => {
7776
);
7877
};
7978

80-
async function createSiweMessage(address: `0x${string}`, statement: string, chainId: number = DEFAULT_CHAIN) {
79+
async function createMessage(address: `0x${string}`, statement: string, chainId: number = DEFAULT_CHAIN) {
8180
const domain = window.location.host;
8281
const origin = window.location.origin;
83-
const response = await getNonce(address);
84-
const nonce = (await response.json()).nonce;
82+
const nonce = await getNonce(address);
8583

8684
// signature is valid only for 10 mins
87-
const expirationTime = new Date(Date.now() + 10 * 60 * 1000).toISOString();
85+
const expirationTime = new Date(Date.now() + 10 * 60 * 1000);
8886

89-
const message = new SiweMessage({
87+
const message = createSiweMessage({
9088
domain,
9189
address,
9290
statement,
@@ -96,5 +94,5 @@ async function createSiweMessage(address: `0x${string}`, statement: string, chai
9694
nonce,
9795
expirationTime,
9896
});
99-
return message.prepareMessage();
97+
return message;
10098
}

web/src/utils/authoriseUser.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toast } from "react-toastify";
2+
23
import { OPTIONS } from "utils/wrapWithToast";
34

45
type AuthoriseUserData = {
@@ -7,20 +8,25 @@ type AuthoriseUserData = {
78
message: string;
89
};
910

10-
export function authoriseUser(authData: AuthoriseUserData): Promise<Response> {
11-
return toast.promise<Response, Error>(
11+
export function authoriseUser(authData: AuthoriseUserData): Promise<string> {
12+
const query = `mutation Login {
13+
login(message: "${authData.message}", signature: "${authData.signature}")
14+
}
15+
`;
16+
17+
return toast.promise<string, Error>(
1218
fetch(`/.netlify/functions/authUser`, {
1319
method: "POST",
1420
headers: {
1521
"Content-Type": "application/json",
1622
},
17-
body: JSON.stringify(authData),
23+
body: JSON.stringify({ query }),
1824
}).then(async (response) => {
1925
if (response.status !== 200) {
2026
const error = await response.json().catch(() => ({ message: "Error signing in" }));
2127
throw new Error(error.message);
2228
}
23-
return response;
29+
return (await response.json()).data.login.access_token;
2430
}),
2531
{
2632
pending: `Signing in User...`,
@@ -35,16 +41,24 @@ export function authoriseUser(authData: AuthoriseUserData): Promise<Response> {
3541
);
3642
}
3743

38-
export function getNonce(address: string): Promise<Response> {
39-
return toast.promise<Response, Error>(
44+
export function getNonce(address: string): Promise<string> {
45+
const query = `mutation GetNonce {
46+
nonce(address: "${address}")
47+
}`;
48+
49+
return toast.promise<string, Error>(
4050
fetch(`/.netlify/functions/getNonce?address=${address}`, {
41-
method: "GET",
51+
method: "POST",
52+
headers: {
53+
"Content-Type": "application/json",
54+
},
55+
body: JSON.stringify({ query }),
4256
}).then(async (response) => {
4357
if (response.status !== 200) {
4458
const error = await response.json().catch(() => ({ message: "Error getting nonce" }));
4559
throw new Error(error.message);
4660
}
47-
return response;
61+
return (await response.json()).data.nonce;
4862
}),
4963
{
5064
error: {

yarn.lock

+98-13
Original file line numberDiff line numberDiff line change
@@ -6685,7 +6685,7 @@ __metadata:
66856685
supabase: "npm:^1.133.3"
66866686
typescript: "npm:^5.3.3"
66876687
vanilla-jsoneditor: "npm:^0.21.4"
6688-
viem: "npm:^2.1.0"
6688+
viem: "npm:^2.17.3"
66896689
vite: "npm:^5.2.10"
66906690
vite-plugin-node-polyfills: "npm:^0.21.0"
66916691
vite-plugin-svgr: "npm:^4.2.0"
@@ -7727,6 +7727,24 @@ __metadata:
77277727
languageName: node
77287728
linkType: hard
77297729

7730+
"@noble/curves@npm:1.4.0":
7731+
version: 1.4.0
7732+
resolution: "@noble/curves@npm:1.4.0"
7733+
dependencies:
7734+
"@noble/hashes": "npm:1.4.0"
7735+
checksum: b21b30a36ff02bfcc0f5e6163d245cdbaf7f640511fff97ccf83fc207ee79cfd91584b4d97977374de04cb118a55eb63a7964c82596a64162bbc42bc685ae6d9
7736+
languageName: node
7737+
linkType: hard
7738+
7739+
"@noble/curves@npm:~1.4.0":
7740+
version: 1.4.2
7741+
resolution: "@noble/curves@npm:1.4.2"
7742+
dependencies:
7743+
"@noble/hashes": "npm:1.4.0"
7744+
checksum: f433a2e8811ae345109388eadfa18ef2b0004c1f79417553241db4f0ad0d59550be6298a4f43d989c627e9f7551ffae6e402a4edf0173981e6da95fc7cab5123
7745+
languageName: node
7746+
linkType: hard
7747+
77307748
"@noble/ed25519@npm:^1.6.0":
77317749
version: 1.7.3
77327750
resolution: "@noble/ed25519@npm:1.7.3"
@@ -7769,7 +7787,7 @@ __metadata:
77697787
languageName: node
77707788
linkType: hard
77717789

7772-
"@noble/hashes@npm:^1.1.2":
7790+
"@noble/hashes@npm:1.4.0, @noble/hashes@npm:^1.1.2, @noble/hashes@npm:~1.4.0":
77737791
version: 1.4.0
77747792
resolution: "@noble/hashes@npm:1.4.0"
77757793
checksum: e156e65794c473794c52fa9d06baf1eb20903d0d96719530f523cc4450f6c721a957c544796e6efd0197b2296e7cd70efeb312f861465e17940a3e3c7e0febc6
@@ -9137,6 +9155,13 @@ __metadata:
91379155
languageName: node
91389156
linkType: hard
91399157

9158+
"@scure/base@npm:~1.1.6":
9159+
version: 1.1.7
9160+
resolution: "@scure/base@npm:1.1.7"
9161+
checksum: fc50ffaab36cb46ff9fa4dc5052a06089ab6a6707f63d596bb34aaaec76173c9a564ac312a0b981b5e7a5349d60097b8878673c75d6cbfc4da7012b63a82099b
9162+
languageName: node
9163+
linkType: hard
9164+
91409165
"@scure/bip32@npm:1.1.5":
91419166
version: 1.1.5
91429167
resolution: "@scure/bip32@npm:1.1.5"
@@ -9181,6 +9206,17 @@ __metadata:
91819206
languageName: node
91829207
linkType: hard
91839208

9209+
"@scure/bip32@npm:1.4.0":
9210+
version: 1.4.0
9211+
resolution: "@scure/bip32@npm:1.4.0"
9212+
dependencies:
9213+
"@noble/curves": "npm:~1.4.0"
9214+
"@noble/hashes": "npm:~1.4.0"
9215+
"@scure/base": "npm:~1.1.6"
9216+
checksum: 6cd5062d902564d9e970597ec8b1adacb415b2eadfbb95aee1a1a0480a52eb0de4d294d3753aa8b48548064c9795ed108d348a31a8ce3fc88785377bb12c63b9
9217+
languageName: node
9218+
linkType: hard
9219+
91849220
"@scure/bip39@npm:1.1.1":
91859221
version: 1.1.1
91869222
resolution: "@scure/bip39@npm:1.1.1"
@@ -9211,6 +9247,16 @@ __metadata:
92119247
languageName: node
92129248
linkType: hard
92139249

9250+
"@scure/bip39@npm:1.3.0":
9251+
version: 1.3.0
9252+
resolution: "@scure/bip39@npm:1.3.0"
9253+
dependencies:
9254+
"@noble/hashes": "npm:~1.4.0"
9255+
"@scure/base": "npm:~1.1.6"
9256+
checksum: 7d71fd58153de22fe8cd65b525f6958a80487bc9d0fbc32c71c328aeafe41fa259f989d2f1e0fa4fdfeaf83b8fcf9310d52ed9862987e46c2f2bfb9dd8cf9fc1
9257+
languageName: node
9258+
linkType: hard
9259+
92149260
"@selderee/plugin-htmlparser2@npm:^0.6.0":
92159261
version: 0.6.0
92169262
resolution: "@selderee/plugin-htmlparser2@npm:0.6.0"
@@ -12796,6 +12842,21 @@ __metadata:
1279612842
languageName: node
1279712843
linkType: hard
1279812844

12845+
"abitype@npm:1.0.5":
12846+
version: 1.0.5
12847+
resolution: "abitype@npm:1.0.5"
12848+
peerDependencies:
12849+
typescript: ">=5.0.4"
12850+
zod: ^3 >=3.22.0
12851+
peerDependenciesMeta:
12852+
typescript:
12853+
optional: true
12854+
zod:
12855+
optional: true
12856+
checksum: 1acd0d9687945dd78442b71bd84ff3b9dceae27d15f0d8b14b16554a0c8c9518eeb971ff8e94d507f4d9f05a8a8b91eb8fafd735eaecebac37d5c5a4aac06d8e
12857+
languageName: node
12858+
linkType: hard
12859+
1279912860
"abitype@npm:^0.10.3":
1280012861
version: 0.10.3
1280112862
resolution: "abitype@npm:0.10.3"
@@ -23846,6 +23907,15 @@ __metadata:
2384623907
languageName: node
2384723908
linkType: hard
2384823909

23910+
"isows@npm:1.0.4":
23911+
version: 1.0.4
23912+
resolution: "isows@npm:1.0.4"
23913+
peerDependencies:
23914+
ws: "*"
23915+
checksum: a3ee62e3d6216abb3adeeb2a551fe2e7835eac87b05a6ecc3e7739259bf5f8e83290501f49e26137390c8093f207fc3378d4a7653aab76ad7bbab4b2dba9c5b9
23916+
languageName: node
23917+
linkType: hard
23918+
2384923919
"isstream@npm:~0.1.2":
2385023920
version: 0.1.2
2385123921
resolution: "isstream@npm:0.1.2"
@@ -36577,24 +36647,24 @@ __metadata:
3657736647
languageName: node
3657836648
linkType: hard
3657936649

36580-
"viem@npm:^2.1.0":
36581-
version: 2.1.0
36582-
resolution: "viem@npm:2.1.0"
36650+
"viem@npm:^2.17.3":
36651+
version: 2.17.4
36652+
resolution: "viem@npm:2.17.4"
3658336653
dependencies:
3658436654
"@adraffy/ens-normalize": "npm:1.10.0"
36585-
"@noble/curves": "npm:1.2.0"
36586-
"@noble/hashes": "npm:1.3.2"
36587-
"@scure/bip32": "npm:1.3.2"
36588-
"@scure/bip39": "npm:1.2.1"
36589-
abitype: "npm:0.10.0"
36590-
isows: "npm:1.0.3"
36591-
ws: "npm:8.13.0"
36655+
"@noble/curves": "npm:1.4.0"
36656+
"@noble/hashes": "npm:1.4.0"
36657+
"@scure/bip32": "npm:1.4.0"
36658+
"@scure/bip39": "npm:1.3.0"
36659+
abitype: "npm:1.0.5"
36660+
isows: "npm:1.0.4"
36661+
ws: "npm:8.17.1"
3659236662
peerDependencies:
3659336663
typescript: ">=5.0.4"
3659436664
peerDependenciesMeta:
3659536665
typescript:
3659636666
optional: true
36597-
checksum: 594406567b7666eaffe60b4ae5a7d1b98953d12e1b455c07594ae38e8f41bf910dfa2ddab69911fa9fd092b82e5a347f64df2919e5424b3d13b2e6c5ad2f7f92
36667+
checksum: 29e2b7d9034ea0520c47ef25cd8048078f8a0bacd24e1c468ee2ec5c51f780429e1bbb914611b54b8ee19f7eb251e171af67db7f27b97209c16cf8e917b405bf
3659836668
languageName: node
3659936669
linkType: hard
3660036670

@@ -37934,6 +38004,21 @@ __metadata:
3793438004
languageName: node
3793538005
linkType: hard
3793638006

38007+
"ws@npm:8.17.1":
38008+
version: 8.17.1
38009+
resolution: "ws@npm:8.17.1"
38010+
peerDependencies:
38011+
bufferutil: ^4.0.1
38012+
utf-8-validate: ">=5.0.2"
38013+
peerDependenciesMeta:
38014+
bufferutil:
38015+
optional: true
38016+
utf-8-validate:
38017+
optional: true
38018+
checksum: 4264ae92c0b3e59c7e309001e93079b26937aab181835fb7af79f906b22cd33b6196d96556dafb4e985742dd401e99139572242e9847661fdbc96556b9e6902d
38019+
languageName: node
38020+
linkType: hard
38021+
3793738022
"ws@npm:8.5.0":
3793838023
version: 8.5.0
3793938024
resolution: "ws@npm:8.5.0"

0 commit comments

Comments
 (0)