sign-in-with-stacks 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.changeset/funny-ants-hide.md +5 -0
- package/.github/workflows/changesets.yml +44 -0
- package/.github/workflows/node.js.yml +35 -0
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/dist/index.d.ts +216 -0
- package/dist/index.js +345 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
- package/src/createSiwsMessage.test.ts +378 -0
- package/src/createSiwsMessage.ts +176 -0
- package/src/errors/address.ts +12 -0
- package/src/errors/base.test.ts +187 -0
- package/src/errors/base.ts +95 -0
- package/src/errors/siws.ts +19 -0
- package/src/generateSiwsNonce.test.ts +7 -0
- package/src/generateSiwsNonce.ts +28 -0
- package/src/index.ts +17 -0
- package/src/parseSiwsMessage.test.ts +181 -0
- package/src/parseSiwsMessage.ts +52 -0
- package/src/types.ts +75 -0
- package/src/utils.test.ts +41 -0
- package/src/utils.ts +66 -0
- package/src/validateSiwsMessage.test.ts +108 -0
- package/src/validateSiwsMessage.ts +67 -0
- package/src/verifySiwsMessage.test.ts +98 -0
- package/src/verifySiwsMessage.ts +86 -0
- package/test/constants.ts +7 -0
- package/tsconfig.json +11 -0
- package/tsdown.config.ts +9 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { STACKS_MAINNET } from "@stacks/network";
|
|
2
|
+
import { expect, test, vi } from "vitest";
|
|
3
|
+
import { createSiwsMessage } from "./createSiwsMessage.js";
|
|
4
|
+
import type { SiwsMessage } from "./types.js";
|
|
5
|
+
|
|
6
|
+
const message = {
|
|
7
|
+
address: "SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173",
|
|
8
|
+
chainId: STACKS_MAINNET.chainId,
|
|
9
|
+
domain: "example.com",
|
|
10
|
+
nonce: "foobarbaz",
|
|
11
|
+
uri: "https://example.com/path",
|
|
12
|
+
version: "1",
|
|
13
|
+
} satisfies SiwsMessage;
|
|
14
|
+
|
|
15
|
+
test("default", () => {
|
|
16
|
+
vi.useFakeTimers();
|
|
17
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
18
|
+
|
|
19
|
+
expect(createSiwsMessage(message)).toMatchInlineSnapshot(`
|
|
20
|
+
"example.com wants you to sign in with your Stacks account:
|
|
21
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
URI: https://example.com/path
|
|
25
|
+
Version: 1
|
|
26
|
+
Chain ID: 1
|
|
27
|
+
Nonce: foobarbaz
|
|
28
|
+
Issued At: 2023-02-01T00:00:00.000Z"
|
|
29
|
+
`);
|
|
30
|
+
|
|
31
|
+
vi.useRealTimers();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("parameters: domain", () => {
|
|
35
|
+
vi.useFakeTimers();
|
|
36
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
37
|
+
|
|
38
|
+
expect(
|
|
39
|
+
createSiwsMessage({
|
|
40
|
+
...message,
|
|
41
|
+
domain: "foo.example.com",
|
|
42
|
+
}),
|
|
43
|
+
).toMatchInlineSnapshot(`
|
|
44
|
+
"foo.example.com wants you to sign in with your Stacks account:
|
|
45
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
URI: https://example.com/path
|
|
49
|
+
Version: 1
|
|
50
|
+
Chain ID: 1
|
|
51
|
+
Nonce: foobarbaz
|
|
52
|
+
Issued At: 2023-02-01T00:00:00.000Z"
|
|
53
|
+
`);
|
|
54
|
+
|
|
55
|
+
expect(
|
|
56
|
+
createSiwsMessage({
|
|
57
|
+
...message,
|
|
58
|
+
domain: "example.co.uk",
|
|
59
|
+
}),
|
|
60
|
+
).toMatchInlineSnapshot(`
|
|
61
|
+
"example.co.uk wants you to sign in with your Stacks account:
|
|
62
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
URI: https://example.com/path
|
|
66
|
+
Version: 1
|
|
67
|
+
Chain ID: 1
|
|
68
|
+
Nonce: foobarbaz
|
|
69
|
+
Issued At: 2023-02-01T00:00:00.000Z"
|
|
70
|
+
`);
|
|
71
|
+
|
|
72
|
+
vi.useRealTimers();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("parameters: scheme", () => {
|
|
76
|
+
vi.useFakeTimers();
|
|
77
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
78
|
+
|
|
79
|
+
expect(
|
|
80
|
+
createSiwsMessage({
|
|
81
|
+
...message,
|
|
82
|
+
scheme: "https",
|
|
83
|
+
}),
|
|
84
|
+
).toMatchInlineSnapshot(`
|
|
85
|
+
"https://example.com wants you to sign in with your Stacks account:
|
|
86
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
URI: https://example.com/path
|
|
90
|
+
Version: 1
|
|
91
|
+
Chain ID: 1
|
|
92
|
+
Nonce: foobarbaz
|
|
93
|
+
Issued At: 2023-02-01T00:00:00.000Z"
|
|
94
|
+
`);
|
|
95
|
+
|
|
96
|
+
vi.useRealTimers();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("parameters: statement", () => {
|
|
100
|
+
vi.useFakeTimers();
|
|
101
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
102
|
+
|
|
103
|
+
expect(
|
|
104
|
+
createSiwsMessage({
|
|
105
|
+
...message,
|
|
106
|
+
statement:
|
|
107
|
+
"I accept the ExampleOrg Terms of Service: https://example.com/tos",
|
|
108
|
+
}),
|
|
109
|
+
).toMatchInlineSnapshot(`
|
|
110
|
+
"example.com wants you to sign in with your Stacks account:
|
|
111
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
112
|
+
|
|
113
|
+
I accept the ExampleOrg Terms of Service: https://example.com/tos
|
|
114
|
+
|
|
115
|
+
URI: https://example.com/path
|
|
116
|
+
Version: 1
|
|
117
|
+
Chain ID: 1
|
|
118
|
+
Nonce: foobarbaz
|
|
119
|
+
Issued At: 2023-02-01T00:00:00.000Z"
|
|
120
|
+
`);
|
|
121
|
+
|
|
122
|
+
vi.useRealTimers();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("parameters: issuedAt", () => {
|
|
126
|
+
const issuedAt = new Date(Date.UTC(2022, 1, 4));
|
|
127
|
+
expect(createSiwsMessage({ ...message, issuedAt })).toMatchInlineSnapshot(`
|
|
128
|
+
"example.com wants you to sign in with your Stacks account:
|
|
129
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
URI: https://example.com/path
|
|
133
|
+
Version: 1
|
|
134
|
+
Chain ID: 1
|
|
135
|
+
Nonce: foobarbaz
|
|
136
|
+
Issued At: 2022-02-04T00:00:00.000Z"
|
|
137
|
+
`);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("parameters: expirationTime", () => {
|
|
141
|
+
vi.useFakeTimers();
|
|
142
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
143
|
+
|
|
144
|
+
expect(
|
|
145
|
+
createSiwsMessage({
|
|
146
|
+
...message,
|
|
147
|
+
expirationTime: new Date(Date.UTC(2022, 1, 4)),
|
|
148
|
+
}),
|
|
149
|
+
).toMatchInlineSnapshot(`
|
|
150
|
+
"example.com wants you to sign in with your Stacks account:
|
|
151
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
URI: https://example.com/path
|
|
155
|
+
Version: 1
|
|
156
|
+
Chain ID: 1
|
|
157
|
+
Nonce: foobarbaz
|
|
158
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
159
|
+
Expiration Time: 2022-02-04T00:00:00.000Z"
|
|
160
|
+
`);
|
|
161
|
+
|
|
162
|
+
vi.useRealTimers();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("parameters: notBefore", () => {
|
|
166
|
+
vi.useFakeTimers();
|
|
167
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
168
|
+
|
|
169
|
+
expect(
|
|
170
|
+
createSiwsMessage({
|
|
171
|
+
...message,
|
|
172
|
+
notBefore: new Date(Date.UTC(2022, 1, 4)),
|
|
173
|
+
}),
|
|
174
|
+
).toMatchInlineSnapshot(`
|
|
175
|
+
"example.com wants you to sign in with your Stacks account:
|
|
176
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
URI: https://example.com/path
|
|
180
|
+
Version: 1
|
|
181
|
+
Chain ID: 1
|
|
182
|
+
Nonce: foobarbaz
|
|
183
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
184
|
+
Not Before: 2022-02-04T00:00:00.000Z"
|
|
185
|
+
`);
|
|
186
|
+
|
|
187
|
+
vi.useRealTimers();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("parameters: requestId", () => {
|
|
191
|
+
vi.useFakeTimers();
|
|
192
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
193
|
+
|
|
194
|
+
expect(
|
|
195
|
+
createSiwsMessage({
|
|
196
|
+
...message,
|
|
197
|
+
requestId: "123e4567-e89b-12d3-a456-426614174000",
|
|
198
|
+
}),
|
|
199
|
+
).toMatchInlineSnapshot(`
|
|
200
|
+
"example.com wants you to sign in with your Stacks account:
|
|
201
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
URI: https://example.com/path
|
|
205
|
+
Version: 1
|
|
206
|
+
Chain ID: 1
|
|
207
|
+
Nonce: foobarbaz
|
|
208
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
209
|
+
Request ID: 123e4567-e89b-12d3-a456-426614174000"
|
|
210
|
+
`);
|
|
211
|
+
|
|
212
|
+
vi.useRealTimers();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("parameters: resources", () => {
|
|
216
|
+
vi.useFakeTimers();
|
|
217
|
+
vi.setSystemTime(new Date(Date.UTC(2023, 1, 1)));
|
|
218
|
+
|
|
219
|
+
expect(
|
|
220
|
+
createSiwsMessage({
|
|
221
|
+
...message,
|
|
222
|
+
resources: [
|
|
223
|
+
"https://example.com/foo",
|
|
224
|
+
"https://example.com/bar",
|
|
225
|
+
"https://example.com/baz",
|
|
226
|
+
],
|
|
227
|
+
}),
|
|
228
|
+
).toMatchInlineSnapshot(`
|
|
229
|
+
"example.com wants you to sign in with your Stacks account:
|
|
230
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
URI: https://example.com/path
|
|
234
|
+
Version: 1
|
|
235
|
+
Chain ID: 1
|
|
236
|
+
Nonce: foobarbaz
|
|
237
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
238
|
+
Resources:
|
|
239
|
+
- https://example.com/foo
|
|
240
|
+
- https://example.com/bar
|
|
241
|
+
- https://example.com/baz"
|
|
242
|
+
`);
|
|
243
|
+
|
|
244
|
+
vi.useRealTimers();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("behavior: invalid address", () => {
|
|
248
|
+
expect(() =>
|
|
249
|
+
createSiwsMessage({ ...message, address: "0xfoobarbaz" }),
|
|
250
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
251
|
+
[InvalidAddressError: Address "0xfoobarbaz" is invalid.]
|
|
252
|
+
`);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("behavior: invalid chainId", () => {
|
|
256
|
+
expect(() =>
|
|
257
|
+
createSiwsMessage({ ...message, chainId: 1.1 }),
|
|
258
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
259
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "chainId".
|
|
260
|
+
- Chain ID must be a SIP-005 chain ID.
|
|
261
|
+
- See https://github.com/stacksgov/sips/blob/main/sips/sip-005/sip-005-blocks-and-transactions.md
|
|
262
|
+
Provided value: 1.1]
|
|
263
|
+
`);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("behavior: invalid domain", () => {
|
|
267
|
+
expect(() =>
|
|
268
|
+
createSiwsMessage({ ...message, domain: "#foo" }),
|
|
269
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
270
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "domain".
|
|
271
|
+
- Domain must be an RFC 3986 authority.
|
|
272
|
+
- See https://www.rfc-editor.org/rfc/rfc3986
|
|
273
|
+
Provided value: #foo]
|
|
274
|
+
`);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("behavior: invalid nonce", () => {
|
|
278
|
+
expect(() =>
|
|
279
|
+
createSiwsMessage({ ...message, nonce: "#foo" }),
|
|
280
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
281
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "nonce".
|
|
282
|
+
- Nonce must be at least 8 characters.
|
|
283
|
+
- Nonce must be alphanumeric.
|
|
284
|
+
Provided value: #foo]
|
|
285
|
+
`);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test("behavior: invalid uri", () => {
|
|
289
|
+
expect(() =>
|
|
290
|
+
createSiwsMessage({ ...message, uri: "#foo" }),
|
|
291
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
292
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "uri".
|
|
293
|
+
- URI must be a RFC 3986 URI referring to the resource that is the subject of the signing.
|
|
294
|
+
- See https://www.rfc-editor.org/rfc/rfc3986
|
|
295
|
+
Provided value: #foo]
|
|
296
|
+
`);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test("behavior: invalid version", () => {
|
|
300
|
+
expect(() =>
|
|
301
|
+
// @ts-expect-error
|
|
302
|
+
createSiwsMessage({ ...message, version: "2" }),
|
|
303
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
304
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "version".
|
|
305
|
+
- Version must be '1'.
|
|
306
|
+
Provided value: 2]
|
|
307
|
+
`);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("behavior: invalid scheme", () => {
|
|
311
|
+
expect(() =>
|
|
312
|
+
createSiwsMessage({ ...message, scheme: "foo_bar" }),
|
|
313
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
314
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "scheme".
|
|
315
|
+
- Scheme must be an RFC 3986 URI scheme.
|
|
316
|
+
- See https://www.rfc-editor.org/rfc/rfc3986#section-3.1
|
|
317
|
+
Provided value: foo_bar]
|
|
318
|
+
`);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("behavior: invalid statement", () => {
|
|
322
|
+
expect(() =>
|
|
323
|
+
createSiwsMessage({ ...message, statement: "foo\nbar" }),
|
|
324
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
325
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "statement".
|
|
326
|
+
- Statement must not include '\\n'.
|
|
327
|
+
Provided value: foo
|
|
328
|
+
bar]
|
|
329
|
+
`);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test("behavior: invalid resources", () => {
|
|
333
|
+
expect(() =>
|
|
334
|
+
createSiwsMessage({
|
|
335
|
+
...message,
|
|
336
|
+
resources: ["https://example.com", "foo"],
|
|
337
|
+
}),
|
|
338
|
+
).toThrowErrorMatchingInlineSnapshot(`
|
|
339
|
+
[SiwsInvalidMessageFieldError: Invalid Sign-In with Stacks message field "resources".
|
|
340
|
+
- Every resource must be a RFC 3986 URI.
|
|
341
|
+
- See https://www.rfc-editor.org/rfc/rfc3986
|
|
342
|
+
Provided value: foo]
|
|
343
|
+
`);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test.each([
|
|
347
|
+
"example.com",
|
|
348
|
+
"localhost",
|
|
349
|
+
"127.0.0.1",
|
|
350
|
+
"example.com:3000",
|
|
351
|
+
"localhost:3000",
|
|
352
|
+
"127.0.0.1:3000",
|
|
353
|
+
])("valid domain `%s`", (domain) => {
|
|
354
|
+
expect(
|
|
355
|
+
createSiwsMessage({
|
|
356
|
+
...message,
|
|
357
|
+
domain,
|
|
358
|
+
}),
|
|
359
|
+
).toBeTypeOf("string");
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test.each([
|
|
363
|
+
"http://example.com",
|
|
364
|
+
"http://localhost",
|
|
365
|
+
"http://127.0.0.1",
|
|
366
|
+
"http://example.com:3000",
|
|
367
|
+
"http://localhost:3000",
|
|
368
|
+
"http://127.0.0.1:3000",
|
|
369
|
+
"foobarbaz",
|
|
370
|
+
"-example.com",
|
|
371
|
+
])("invalid domain `%s`", (domain) => {
|
|
372
|
+
expect(() =>
|
|
373
|
+
createSiwsMessage({
|
|
374
|
+
...message,
|
|
375
|
+
domain,
|
|
376
|
+
}),
|
|
377
|
+
).toThrowError();
|
|
378
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { InvalidAddressErrorType } from "./errors/address.js";
|
|
2
|
+
import {
|
|
3
|
+
SiwsInvalidMessageFieldError,
|
|
4
|
+
type SiwsInvalidMessageFieldErrorType,
|
|
5
|
+
} from "./errors/siws.js";
|
|
6
|
+
import type { SiwsMessage } from "./types.js";
|
|
7
|
+
import { getAddress, isUri } from "./utils.js";
|
|
8
|
+
|
|
9
|
+
export type CreateSiwsMessageParameters = SiwsMessage;
|
|
10
|
+
|
|
11
|
+
export type CreateSiwsMessageReturnType = string;
|
|
12
|
+
|
|
13
|
+
export type CreateSiwsMessageErrorType =
|
|
14
|
+
| InvalidAddressErrorType
|
|
15
|
+
| SiwsInvalidMessageFieldErrorType;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @description Creates SIP-X formatted message.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const message = createMessage({
|
|
22
|
+
* address: 'SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173',
|
|
23
|
+
* chainId: 1,
|
|
24
|
+
* domain: 'example.com',
|
|
25
|
+
* nonce: 'foobarbaz',
|
|
26
|
+
* uri: 'https://example.com/path',
|
|
27
|
+
* version: '1',
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
31
|
+
*/
|
|
32
|
+
export function createSiwsMessage(
|
|
33
|
+
parameters: CreateSiwsMessageParameters,
|
|
34
|
+
): CreateSiwsMessageReturnType {
|
|
35
|
+
const {
|
|
36
|
+
chainId,
|
|
37
|
+
domain,
|
|
38
|
+
expirationTime,
|
|
39
|
+
issuedAt = new Date(),
|
|
40
|
+
nonce,
|
|
41
|
+
notBefore,
|
|
42
|
+
requestId,
|
|
43
|
+
resources,
|
|
44
|
+
scheme,
|
|
45
|
+
uri,
|
|
46
|
+
version,
|
|
47
|
+
} = parameters;
|
|
48
|
+
|
|
49
|
+
// Validate fields
|
|
50
|
+
{
|
|
51
|
+
// Required fields
|
|
52
|
+
if (chainId !== Math.floor(chainId))
|
|
53
|
+
throw new SiwsInvalidMessageFieldError({
|
|
54
|
+
field: "chainId",
|
|
55
|
+
metaMessages: [
|
|
56
|
+
"- Chain ID must be a SIP-005 chain ID.",
|
|
57
|
+
"- See https://github.com/stacksgov/sips/blob/main/sips/sip-005/sip-005-blocks-and-transactions.md",
|
|
58
|
+
"",
|
|
59
|
+
`Provided value: ${chainId}`,
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
if (
|
|
63
|
+
!(
|
|
64
|
+
domainRegex.test(domain) ||
|
|
65
|
+
ipRegex.test(domain) ||
|
|
66
|
+
localhostRegex.test(domain)
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
throw new SiwsInvalidMessageFieldError({
|
|
70
|
+
field: "domain",
|
|
71
|
+
metaMessages: [
|
|
72
|
+
"- Domain must be an RFC 3986 authority.",
|
|
73
|
+
"- See https://www.rfc-editor.org/rfc/rfc3986",
|
|
74
|
+
"",
|
|
75
|
+
`Provided value: ${domain}`,
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
if (!nonceRegex.test(nonce))
|
|
79
|
+
throw new SiwsInvalidMessageFieldError({
|
|
80
|
+
field: "nonce",
|
|
81
|
+
metaMessages: [
|
|
82
|
+
"- Nonce must be at least 8 characters.",
|
|
83
|
+
"- Nonce must be alphanumeric.",
|
|
84
|
+
"",
|
|
85
|
+
`Provided value: ${nonce}`,
|
|
86
|
+
],
|
|
87
|
+
});
|
|
88
|
+
if (!isUri(uri))
|
|
89
|
+
throw new SiwsInvalidMessageFieldError({
|
|
90
|
+
field: "uri",
|
|
91
|
+
metaMessages: [
|
|
92
|
+
"- URI must be a RFC 3986 URI referring to the resource that is the subject of the signing.",
|
|
93
|
+
"- See https://www.rfc-editor.org/rfc/rfc3986",
|
|
94
|
+
"",
|
|
95
|
+
`Provided value: ${uri}`,
|
|
96
|
+
],
|
|
97
|
+
});
|
|
98
|
+
if (version !== "1")
|
|
99
|
+
throw new SiwsInvalidMessageFieldError({
|
|
100
|
+
field: "version",
|
|
101
|
+
metaMessages: [
|
|
102
|
+
"- Version must be '1'.",
|
|
103
|
+
"",
|
|
104
|
+
`Provided value: ${version}`,
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Optional fields
|
|
109
|
+
if (scheme && !schemeRegex.test(scheme))
|
|
110
|
+
throw new SiwsInvalidMessageFieldError({
|
|
111
|
+
field: "scheme",
|
|
112
|
+
metaMessages: [
|
|
113
|
+
"- Scheme must be an RFC 3986 URI scheme.",
|
|
114
|
+
"- See https://www.rfc-editor.org/rfc/rfc3986#section-3.1",
|
|
115
|
+
"",
|
|
116
|
+
`Provided value: ${scheme}`,
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
const statement = parameters.statement;
|
|
120
|
+
if (statement?.includes("\n"))
|
|
121
|
+
throw new SiwsInvalidMessageFieldError({
|
|
122
|
+
field: "statement",
|
|
123
|
+
metaMessages: [
|
|
124
|
+
"- Statement must not include '\\n'.",
|
|
125
|
+
"",
|
|
126
|
+
`Provided value: ${statement}`,
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Construct message
|
|
132
|
+
const address = getAddress(parameters.address);
|
|
133
|
+
const origin = (() => {
|
|
134
|
+
if (scheme) return `${scheme}://${domain}`;
|
|
135
|
+
return domain;
|
|
136
|
+
})();
|
|
137
|
+
const statement = (() => {
|
|
138
|
+
if (!parameters.statement) return "";
|
|
139
|
+
return `${parameters.statement}\n`;
|
|
140
|
+
})();
|
|
141
|
+
const prefix = `${origin} wants you to sign in with your Stacks account:\n${address}\n\n${statement}`;
|
|
142
|
+
|
|
143
|
+
let suffix = `URI: ${uri}\nVersion: ${version}\nChain ID: ${chainId}\nNonce: ${nonce}\nIssued At: ${issuedAt.toISOString()}`;
|
|
144
|
+
|
|
145
|
+
if (expirationTime)
|
|
146
|
+
suffix += `\nExpiration Time: ${expirationTime.toISOString()}`;
|
|
147
|
+
if (notBefore) suffix += `\nNot Before: ${notBefore.toISOString()}`;
|
|
148
|
+
if (requestId) suffix += `\nRequest ID: ${requestId}`;
|
|
149
|
+
if (resources) {
|
|
150
|
+
let content = "\nResources:";
|
|
151
|
+
for (const resource of resources) {
|
|
152
|
+
if (!isUri(resource))
|
|
153
|
+
throw new SiwsInvalidMessageFieldError({
|
|
154
|
+
field: "resources",
|
|
155
|
+
metaMessages: [
|
|
156
|
+
"- Every resource must be a RFC 3986 URI.",
|
|
157
|
+
"- See https://www.rfc-editor.org/rfc/rfc3986",
|
|
158
|
+
"",
|
|
159
|
+
`Provided value: ${resource}`,
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
content += `\n- ${resource}`;
|
|
163
|
+
}
|
|
164
|
+
suffix += content;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return `${prefix}\n${suffix}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const domainRegex =
|
|
171
|
+
/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$/;
|
|
172
|
+
const ipRegex =
|
|
173
|
+
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/;
|
|
174
|
+
const localhostRegex = /^localhost(:[0-9]{1,5})?$/;
|
|
175
|
+
const nonceRegex = /^[a-zA-Z0-9]{8,}$/;
|
|
176
|
+
const schemeRegex = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseError } from "./base.js";
|
|
2
|
+
|
|
3
|
+
export type InvalidAddressErrorType = InvalidAddressError & {
|
|
4
|
+
name: "InvalidAddressError";
|
|
5
|
+
};
|
|
6
|
+
export class InvalidAddressError extends BaseError {
|
|
7
|
+
constructor({ address }: { address: string }) {
|
|
8
|
+
super(`Address "${address}" is invalid.`, {
|
|
9
|
+
name: "InvalidAddressError",
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|