-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsiwe.test.js
More file actions
124 lines (114 loc) · 3.56 KB
/
siwe.test.js
File metadata and controls
124 lines (114 loc) · 3.56 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { describe, it, expect } from 'vitest';
import { parseSiweMessage } from '../../api/_lib/siwe.js';
const VALID_MSG = [
'localhost wants you to sign in with your Ethereum account:',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'URI: https://localhost/login',
'Version: 1',
'Chain ID: 1',
'Nonce: abc123xyz',
'Issued At: 2024-01-01T00:00:00.000Z',
].join('\n');
describe('parseSiweMessage', () => {
it('parses a valid SIWE message', () => {
const result = parseSiweMessage(VALID_MSG);
expect(result).not.toBeNull();
expect(result.domain).toBe('localhost');
expect(result.address).toBe('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045');
expect(result.uri).toBe('https://localhost/login');
expect(result.version).toBe('1');
expect(result.chainId).toBe(1);
expect(result.nonce).toBe('abc123xyz');
expect(result.issuedAt).toBe('2024-01-01T00:00:00.000Z');
});
it('returns null for message with too few lines', () => {
expect(parseSiweMessage('line1\nline2')).toBeNull();
});
it('returns null for missing URI', () => {
const msg = [
'localhost wants you to sign in with your Ethereum account:',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'Version: 1',
'Nonce: abc123xyz',
].join('\n');
expect(parseSiweMessage(msg)).toBeNull();
});
it('returns null for missing Nonce', () => {
const msg = [
'localhost wants you to sign in with your Ethereum account:',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'URI: https://localhost/login',
'Version: 1',
].join('\n');
expect(parseSiweMessage(msg)).toBeNull();
});
it('returns null for missing Version', () => {
const msg = [
'localhost wants you to sign in with your Ethereum account:',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'URI: https://localhost/login',
'Nonce: abc123xyz',
].join('\n');
expect(parseSiweMessage(msg)).toBeNull();
});
it('returns null for invalid Ethereum address', () => {
const msg = [
'localhost wants you to sign in with your Ethereum account:',
'not-an-address',
'',
'URI: https://localhost/login',
'Version: 1',
'Nonce: abc123xyz',
].join('\n');
expect(parseSiweMessage(msg)).toBeNull();
});
it('returns null for malformed header', () => {
const msg = [
'bad header line',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'URI: https://localhost/login',
'Version: 1',
'Nonce: abc123xyz',
].join('\n');
expect(parseSiweMessage(msg)).toBeNull();
});
it('parses optional fields when present', () => {
const msg = [
'example.com wants you to sign in with your Ethereum account:',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'URI: https://example.com/login',
'Version: 1',
'Chain ID: 137',
'Nonce: nonce42',
'Issued At: 2024-06-01T12:00:00Z',
'Expiration Time: 2024-06-01T13:00:00Z',
'Not Before: 2024-06-01T11:00:00Z',
'Request ID: req-001',
].join('\n');
const result = parseSiweMessage(msg);
expect(result.chainId).toBe(137);
expect(result.expirationTime).toBe('2024-06-01T13:00:00Z');
expect(result.notBefore).toBe('2024-06-01T11:00:00Z');
expect(result.requestId).toBe('req-001');
});
it('handles Chain ID of 0 as null', () => {
const msg = [
'localhost wants you to sign in with your Ethereum account:',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'',
'URI: https://localhost/login',
'Version: 1',
'Chain ID: 0',
'Nonce: abc123xyz',
].join('\n');
const result = parseSiweMessage(msg);
// parseInt('0', 10) is 0, which is falsy → null per the parser logic
expect(result.chainId).toBeNull();
});
});