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,187 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { BaseError, setErrorConfig } from "./base.js";
|
|
3
|
+
|
|
4
|
+
test("BaseError", () => {
|
|
5
|
+
expect(new BaseError("An error occurred.")).toMatchInlineSnapshot(`
|
|
6
|
+
[BaseError: An error occurred.]
|
|
7
|
+
`);
|
|
8
|
+
|
|
9
|
+
expect(
|
|
10
|
+
new BaseError("An error occurred.", { details: "details" }),
|
|
11
|
+
).toMatchInlineSnapshot(`
|
|
12
|
+
[BaseError: An error occurred.
|
|
13
|
+
Details: details]
|
|
14
|
+
`);
|
|
15
|
+
|
|
16
|
+
expect(new BaseError("", { details: "details" })).toMatchInlineSnapshot(`
|
|
17
|
+
[BaseError: An error occurred.
|
|
18
|
+
Details: details]
|
|
19
|
+
`);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("BaseError (w/ docsPath)", () => {
|
|
23
|
+
expect(
|
|
24
|
+
new BaseError("An error occurred.", {
|
|
25
|
+
details: "details",
|
|
26
|
+
docsPath: "/lol",
|
|
27
|
+
}),
|
|
28
|
+
).toMatchInlineSnapshot(`
|
|
29
|
+
[BaseError: An error occurred.
|
|
30
|
+
Docs: https://viem.sh/lol
|
|
31
|
+
Details: details]
|
|
32
|
+
`);
|
|
33
|
+
expect(
|
|
34
|
+
new BaseError("An error occurred.", {
|
|
35
|
+
cause: new BaseError("error", { docsPath: "/docs" }),
|
|
36
|
+
}),
|
|
37
|
+
).toMatchInlineSnapshot(`
|
|
38
|
+
[BaseError: An error occurred.
|
|
39
|
+
Docs: https://viem.sh/docs]
|
|
40
|
+
`);
|
|
41
|
+
expect(
|
|
42
|
+
new BaseError("An error occurred.", {
|
|
43
|
+
cause: new BaseError("error"),
|
|
44
|
+
docsPath: "/lol",
|
|
45
|
+
}),
|
|
46
|
+
).toMatchInlineSnapshot(`
|
|
47
|
+
[BaseError: An error occurred.
|
|
48
|
+
Docs: https://viem.sh/lol]
|
|
49
|
+
`);
|
|
50
|
+
expect(
|
|
51
|
+
new BaseError("An error occurred.", {
|
|
52
|
+
details: "details",
|
|
53
|
+
docsPath: "/lol",
|
|
54
|
+
}),
|
|
55
|
+
).toMatchInlineSnapshot(`
|
|
56
|
+
[BaseError: An error occurred.
|
|
57
|
+
Docs: https://viem.sh/lol
|
|
58
|
+
Details: details]
|
|
59
|
+
`);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("BaseError (w/ docsBaseUrl)", () => {
|
|
63
|
+
expect(
|
|
64
|
+
new BaseError("An error occurred.", {
|
|
65
|
+
docsBaseUrl: "https://test",
|
|
66
|
+
details: "details",
|
|
67
|
+
docsPath: "/lol",
|
|
68
|
+
}),
|
|
69
|
+
).toMatchInlineSnapshot(`
|
|
70
|
+
[BaseError: An error occurred.
|
|
71
|
+
Docs: https://test/lol
|
|
72
|
+
Details: details]
|
|
73
|
+
`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("BaseError (w/ metaMessages)", () => {
|
|
77
|
+
expect(
|
|
78
|
+
new BaseError("An error occurred.", {
|
|
79
|
+
details: "details",
|
|
80
|
+
metaMessages: ["Reason: idk", "Cause: lol"],
|
|
81
|
+
}),
|
|
82
|
+
).toMatchInlineSnapshot(`
|
|
83
|
+
[BaseError: An error occurred.
|
|
84
|
+
Reason: idk
|
|
85
|
+
Cause: lol
|
|
86
|
+
Details: details]
|
|
87
|
+
`);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("inherited BaseError", () => {
|
|
91
|
+
const err = new BaseError("An error occurred.", {
|
|
92
|
+
details: "details",
|
|
93
|
+
docsPath: "/lol",
|
|
94
|
+
});
|
|
95
|
+
expect(
|
|
96
|
+
new BaseError("An internal error occurred.", {
|
|
97
|
+
cause: err,
|
|
98
|
+
}),
|
|
99
|
+
).toMatchInlineSnapshot(`
|
|
100
|
+
[BaseError: An internal error occurred.
|
|
101
|
+
Docs: https://viem.sh/lol
|
|
102
|
+
Details: details]
|
|
103
|
+
`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("inherited Error", () => {
|
|
107
|
+
const err = new Error("details");
|
|
108
|
+
expect(
|
|
109
|
+
new BaseError("An internal error occurred.", {
|
|
110
|
+
cause: err,
|
|
111
|
+
docsPath: "/lol",
|
|
112
|
+
}),
|
|
113
|
+
).toMatchInlineSnapshot(`
|
|
114
|
+
[BaseError: An internal error occurred.
|
|
115
|
+
Docs: https://viem.sh/lol
|
|
116
|
+
Details: details]
|
|
117
|
+
`);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("walk: no predicate fn (walks to leaf)", () => {
|
|
121
|
+
class FooError extends BaseError {}
|
|
122
|
+
class BarError extends BaseError {}
|
|
123
|
+
|
|
124
|
+
const err = new BaseError("test1", {
|
|
125
|
+
cause: new FooError("test2", { cause: new BarError("test3") }),
|
|
126
|
+
});
|
|
127
|
+
expect(err.walk()).toMatchInlineSnapshot(`
|
|
128
|
+
[BaseError: test3]
|
|
129
|
+
`);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("walk: predicate fn", () => {
|
|
133
|
+
class FooError extends BaseError {}
|
|
134
|
+
class BarError extends BaseError {}
|
|
135
|
+
|
|
136
|
+
const err = new BaseError("test1", {
|
|
137
|
+
cause: new FooError("test2", { cause: new BarError("test3") }),
|
|
138
|
+
});
|
|
139
|
+
expect(err.walk((err) => err instanceof FooError)).toMatchInlineSnapshot(`
|
|
140
|
+
[BaseError: test2]
|
|
141
|
+
`);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("walk: predicate fn (no match)", () => {
|
|
145
|
+
class FooError extends BaseError {}
|
|
146
|
+
class BarError extends BaseError {}
|
|
147
|
+
|
|
148
|
+
const err = new BaseError("test1", {
|
|
149
|
+
cause: new Error("test2", { cause: new BarError("test3") }),
|
|
150
|
+
});
|
|
151
|
+
expect(err.walk((err) => err instanceof FooError)).toBeNull();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("walk: undefined cause", () => {
|
|
155
|
+
const withCauseUndefined = new Error("Cuase undefined", { cause: undefined });
|
|
156
|
+
|
|
157
|
+
const err = new BaseError("test1", {
|
|
158
|
+
cause: withCauseUndefined,
|
|
159
|
+
});
|
|
160
|
+
expect(err.walk()).toBe(withCauseUndefined);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("setErrorConfig", () => {
|
|
164
|
+
class FooError extends BaseError {
|
|
165
|
+
constructor() {
|
|
166
|
+
super("An error occurred", {
|
|
167
|
+
name: "FooError",
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
setErrorConfig({
|
|
173
|
+
getDocsUrl({ name }) {
|
|
174
|
+
if (name === "FooError") return "https://sweetlib.com/xyz";
|
|
175
|
+
return undefined;
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
expect(new BaseError("An error occurred.")).toMatchInlineSnapshot(`
|
|
180
|
+
[BaseError: An error occurred.]
|
|
181
|
+
`);
|
|
182
|
+
|
|
183
|
+
expect(new FooError()).toMatchInlineSnapshot(`
|
|
184
|
+
[FooError: An error occurred
|
|
185
|
+
Docs: https://sweetlib.com/xyz]
|
|
186
|
+
`);
|
|
187
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
type ErrorConfig = {
|
|
2
|
+
getDocsUrl?: ((args: BaseErrorParameters) => string | undefined) | undefined;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
let errorConfig: ErrorConfig = {
|
|
6
|
+
getDocsUrl: ({
|
|
7
|
+
docsBaseUrl,
|
|
8
|
+
docsPath = "",
|
|
9
|
+
docsSlug,
|
|
10
|
+
}: BaseErrorParameters) =>
|
|
11
|
+
docsPath
|
|
12
|
+
? `${docsBaseUrl ?? "https://viem.sh"}${docsPath}${
|
|
13
|
+
docsSlug ? `#${docsSlug}` : ""
|
|
14
|
+
}`
|
|
15
|
+
: undefined,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function setErrorConfig(config: ErrorConfig) {
|
|
19
|
+
errorConfig = config;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type BaseErrorParameters = {
|
|
23
|
+
cause?: BaseError | Error | undefined;
|
|
24
|
+
details?: string | undefined;
|
|
25
|
+
docsBaseUrl?: string | undefined;
|
|
26
|
+
docsPath?: string | undefined;
|
|
27
|
+
docsSlug?: string | undefined;
|
|
28
|
+
metaMessages?: string[] | undefined;
|
|
29
|
+
name?: string | undefined;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type BaseErrorType = BaseError & { name: "BaseError" };
|
|
33
|
+
export class BaseError extends Error {
|
|
34
|
+
details: string;
|
|
35
|
+
docsPath?: string | undefined;
|
|
36
|
+
metaMessages?: string[] | undefined;
|
|
37
|
+
shortMessage: string;
|
|
38
|
+
|
|
39
|
+
override name = "BaseError";
|
|
40
|
+
|
|
41
|
+
constructor(shortMessage: string, args: BaseErrorParameters = {}) {
|
|
42
|
+
const details = (() => {
|
|
43
|
+
if (args.cause instanceof BaseError) return args.cause.details;
|
|
44
|
+
if (args.cause?.message) return args.cause.message;
|
|
45
|
+
return args.details!;
|
|
46
|
+
})();
|
|
47
|
+
const docsPath = (() => {
|
|
48
|
+
if (args.cause instanceof BaseError)
|
|
49
|
+
return args.cause.docsPath || args.docsPath;
|
|
50
|
+
return args.docsPath;
|
|
51
|
+
})();
|
|
52
|
+
const docsUrl = errorConfig.getDocsUrl?.({ ...args, docsPath });
|
|
53
|
+
|
|
54
|
+
const message = [
|
|
55
|
+
shortMessage || "An error occurred.",
|
|
56
|
+
"",
|
|
57
|
+
...(args.metaMessages ? [...args.metaMessages, ""] : []),
|
|
58
|
+
...(docsUrl ? [`Docs: ${docsUrl}`] : []),
|
|
59
|
+
...(details ? [`Details: ${details}`] : []),
|
|
60
|
+
]
|
|
61
|
+
.filter((data) => data !== "")
|
|
62
|
+
.join("\n")
|
|
63
|
+
.replace(/\n$/, "");
|
|
64
|
+
|
|
65
|
+
super(message, args.cause ? { cause: args.cause } : undefined);
|
|
66
|
+
|
|
67
|
+
this.details = details;
|
|
68
|
+
this.docsPath = docsPath;
|
|
69
|
+
this.metaMessages = args.metaMessages;
|
|
70
|
+
this.name = args.name ?? this.name;
|
|
71
|
+
this.shortMessage = shortMessage;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
walk(): Error;
|
|
75
|
+
walk(fn: (err: unknown) => boolean): Error | null;
|
|
76
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
77
|
+
walk(fn?: any): any {
|
|
78
|
+
return walk(this, fn);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function walk(
|
|
83
|
+
err: unknown,
|
|
84
|
+
fn?: ((err: unknown) => boolean) | undefined,
|
|
85
|
+
): unknown {
|
|
86
|
+
if (fn?.(err)) return err;
|
|
87
|
+
if (
|
|
88
|
+
err &&
|
|
89
|
+
typeof err === "object" &&
|
|
90
|
+
"cause" in err &&
|
|
91
|
+
err.cause !== undefined
|
|
92
|
+
)
|
|
93
|
+
return walk(err.cause, fn);
|
|
94
|
+
return fn ? null : err;
|
|
95
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseError } from "./base.js";
|
|
2
|
+
|
|
3
|
+
export type SiwsInvalidMessageFieldErrorType = SiwsInvalidMessageFieldError & {
|
|
4
|
+
name: "SiwsInvalidMessageFieldError";
|
|
5
|
+
};
|
|
6
|
+
export class SiwsInvalidMessageFieldError extends BaseError {
|
|
7
|
+
constructor({
|
|
8
|
+
field,
|
|
9
|
+
metaMessages,
|
|
10
|
+
}: {
|
|
11
|
+
field: string;
|
|
12
|
+
metaMessages?: string[] | undefined;
|
|
13
|
+
}) {
|
|
14
|
+
super(`Invalid Sign-In with Stacks message field "${field}".`, {
|
|
15
|
+
metaMessages,
|
|
16
|
+
name: "SiwsInvalidMessageFieldError",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const size = 256;
|
|
2
|
+
let index = size;
|
|
3
|
+
let buffer: string;
|
|
4
|
+
|
|
5
|
+
function uid(length = 11) {
|
|
6
|
+
if (!buffer || index + length > size * 2) {
|
|
7
|
+
buffer = "";
|
|
8
|
+
index = 0;
|
|
9
|
+
for (let i = 0; i < size; i++) {
|
|
10
|
+
buffer += ((256 + Math.random() * 256) | 0).toString(16).substring(1);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return buffer.substring(index, index++ + length);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @description Generates random SIP-X nonce.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const nonce = generateNonce()
|
|
21
|
+
*
|
|
22
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
23
|
+
*
|
|
24
|
+
* @returns A randomly generated SIP-X nonce.
|
|
25
|
+
*/
|
|
26
|
+
export function generateSiwsNonce(): string {
|
|
27
|
+
return uid(96);
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createSiwsMessage,
|
|
3
|
+
type CreateSiwsMessageParameters,
|
|
4
|
+
type CreateSiwsMessageReturnType,
|
|
5
|
+
type CreateSiwsMessageErrorType,
|
|
6
|
+
} from "./createSiwsMessage.js";
|
|
7
|
+
export { generateSiwsNonce } from "./generateSiwsNonce.js";
|
|
8
|
+
export {
|
|
9
|
+
validateSiwsMessage,
|
|
10
|
+
type ValidateSiwsMessageParameters,
|
|
11
|
+
type ValidateSiwsMessageReturnType,
|
|
12
|
+
} from "./validateSiwsMessage.js";
|
|
13
|
+
export {
|
|
14
|
+
verifySiwsMessage,
|
|
15
|
+
type VerifySiwsMessageParameters,
|
|
16
|
+
type VerifySiwsMessageReturnType,
|
|
17
|
+
} from "./verifySiwsMessage.js";
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { parseSiwsMessage } from "./parseSiwsMessage.js";
|
|
3
|
+
|
|
4
|
+
test("default", () => {
|
|
5
|
+
const message = `example.com wants you to sign in with your Stacks account:
|
|
6
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
7
|
+
|
|
8
|
+
I accept the ExampleOrg Terms of Service: https://example.com/tos
|
|
9
|
+
|
|
10
|
+
URI: https://example.com/path
|
|
11
|
+
Version: 1
|
|
12
|
+
Chain ID: 1
|
|
13
|
+
Nonce: foobarbaz
|
|
14
|
+
Issued At: 2023-02-01T00:00:00.000Z`;
|
|
15
|
+
const parsed = parseSiwsMessage(message);
|
|
16
|
+
expect(parsed).toMatchInlineSnapshot(`
|
|
17
|
+
{
|
|
18
|
+
"address": "SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173",
|
|
19
|
+
"chainId": 1,
|
|
20
|
+
"domain": "example.com",
|
|
21
|
+
"issuedAt": 2023-02-01T00:00:00.000Z,
|
|
22
|
+
"nonce": "foobarbaz",
|
|
23
|
+
"statement": "I accept the ExampleOrg Terms of Service: https://example.com/tos",
|
|
24
|
+
"uri": "https://example.com/path",
|
|
25
|
+
"version": "1",
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("behavior: with scheme", () => {
|
|
31
|
+
const message = `https://example.com wants you to sign in with your Stacks account:
|
|
32
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
33
|
+
|
|
34
|
+
URI: https://example.com/path
|
|
35
|
+
Version: 1
|
|
36
|
+
Chain ID: 1
|
|
37
|
+
Nonce: foobarbaz
|
|
38
|
+
Issued At: 2023-02-01T00:00:00.000Z`;
|
|
39
|
+
const parsed = parseSiwsMessage(message);
|
|
40
|
+
expect(parsed.scheme).toMatchInlineSnapshot(`"https"`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("behavior: domain with port", () => {
|
|
44
|
+
const message = `example.com:8080 wants you to sign in with your Stacks account:
|
|
45
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
46
|
+
|
|
47
|
+
URI: https://example.com/path
|
|
48
|
+
Version: 1
|
|
49
|
+
Chain ID: 1
|
|
50
|
+
Nonce: foobarbaz
|
|
51
|
+
Issued At: 2023-02-01T00:00:00.000Z`;
|
|
52
|
+
const parsed = parseSiwsMessage(message);
|
|
53
|
+
expect(parsed.domain).toMatchInlineSnapshot(`"example.com:8080"`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("behavior: with statement", () => {
|
|
57
|
+
const message = `example.com wants you to sign in with your Stacks account:
|
|
58
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
59
|
+
|
|
60
|
+
I accept the ExampleOrg Terms of Service: https://example.com/tos
|
|
61
|
+
|
|
62
|
+
URI: https://example.com/path
|
|
63
|
+
Version: 1
|
|
64
|
+
Chain ID: 1
|
|
65
|
+
Nonce: foobarbaz
|
|
66
|
+
Issued At: 2023-02-01T00:00:00.000Z`;
|
|
67
|
+
const parsed = parseSiwsMessage(message);
|
|
68
|
+
expect(parsed.statement).toMatchInlineSnapshot(
|
|
69
|
+
`"I accept the ExampleOrg Terms of Service: https://example.com/tos"`,
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("behavior: with expirationTime", () => {
|
|
74
|
+
const message = `https://example.com wants you to sign in with your Stacks account:
|
|
75
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
76
|
+
|
|
77
|
+
URI: https://example.com/path
|
|
78
|
+
Version: 1
|
|
79
|
+
Chain ID: 1
|
|
80
|
+
Nonce: foobarbaz
|
|
81
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
82
|
+
Expiration Time: 2022-02-04T00:00:00.000Z`;
|
|
83
|
+
const parsed = parseSiwsMessage(message);
|
|
84
|
+
expect(parsed.expirationTime).toMatchInlineSnapshot(
|
|
85
|
+
"2022-02-04T00:00:00.000Z",
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("behavior: with notBefore", () => {
|
|
90
|
+
const message = `https://example.com wants you to sign in with your Stacks account:
|
|
91
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
92
|
+
|
|
93
|
+
URI: https://example.com/path
|
|
94
|
+
Version: 1
|
|
95
|
+
Chain ID: 1
|
|
96
|
+
Nonce: foobarbaz
|
|
97
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
98
|
+
Not Before: 2022-02-04T00:00:00.000Z`;
|
|
99
|
+
const parsed = parseSiwsMessage(message);
|
|
100
|
+
expect(parsed.notBefore).toMatchInlineSnapshot("2022-02-04T00:00:00.000Z");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("behavior: with requestId", () => {
|
|
104
|
+
const message = `https://example.com wants you to sign in with your Stacks account:
|
|
105
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
106
|
+
|
|
107
|
+
URI: https://example.com/path
|
|
108
|
+
Version: 1
|
|
109
|
+
Chain ID: 1
|
|
110
|
+
Nonce: foobarbaz
|
|
111
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
112
|
+
Request ID: 123e4567-e89b-12d3-a456-426614174000`;
|
|
113
|
+
const parsed = parseSiwsMessage(message);
|
|
114
|
+
expect(parsed.requestId).toMatchInlineSnapshot(
|
|
115
|
+
`"123e4567-e89b-12d3-a456-426614174000"`,
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("behavior: with resources", () => {
|
|
120
|
+
const message = `https://example.com wants you to sign in with your Stacks account:
|
|
121
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
122
|
+
|
|
123
|
+
URI: https://example.com/path
|
|
124
|
+
Version: 1
|
|
125
|
+
Chain ID: 1
|
|
126
|
+
Nonce: foobarbaz
|
|
127
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
128
|
+
Resources:
|
|
129
|
+
- https://example.com/foo
|
|
130
|
+
- https://example.com/bar
|
|
131
|
+
- https://example.com/baz`;
|
|
132
|
+
const parsed = parseSiwsMessage(message);
|
|
133
|
+
expect(parsed.resources).toMatchInlineSnapshot(`
|
|
134
|
+
[
|
|
135
|
+
"https://example.com/foo",
|
|
136
|
+
"https://example.com/bar",
|
|
137
|
+
"https://example.com/baz",
|
|
138
|
+
]
|
|
139
|
+
`);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("behavior: no suffix", () => {
|
|
143
|
+
const message = `https://example.com wants you to sign in with your Stacks account:
|
|
144
|
+
SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173
|
|
145
|
+
|
|
146
|
+
`;
|
|
147
|
+
const parsed = parseSiwsMessage(message);
|
|
148
|
+
expect(parsed).toMatchInlineSnapshot(`
|
|
149
|
+
{
|
|
150
|
+
"address": "SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173",
|
|
151
|
+
"domain": "example.com",
|
|
152
|
+
"scheme": "https",
|
|
153
|
+
}
|
|
154
|
+
`);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("behavior: no prefix", () => {
|
|
158
|
+
const message = `URI: https://example.com/path
|
|
159
|
+
Version: 1
|
|
160
|
+
Chain ID: 1
|
|
161
|
+
Nonce: foobarbaz
|
|
162
|
+
Issued At: 2023-02-01T00:00:00.000Z
|
|
163
|
+
Request ID: 123e4567-e89b-12d3-a456-426614174000`;
|
|
164
|
+
const parsed = parseSiwsMessage(message);
|
|
165
|
+
expect(parsed).toMatchInlineSnapshot(`
|
|
166
|
+
{
|
|
167
|
+
"chainId": 1,
|
|
168
|
+
"issuedAt": 2023-02-01T00:00:00.000Z,
|
|
169
|
+
"nonce": "foobarbaz",
|
|
170
|
+
"requestId": "123e4567-e89b-12d3-a456-426614174000",
|
|
171
|
+
"uri": "https://example.com/path",
|
|
172
|
+
"version": "1",
|
|
173
|
+
}
|
|
174
|
+
`);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("behavior: bogus message", () => {
|
|
178
|
+
const message = "foobarbaz";
|
|
179
|
+
const parsed = parseSiwsMessage(message);
|
|
180
|
+
expect(parsed).toMatchInlineSnapshot("{}");
|
|
181
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ExactPartial, Prettify, SiwsMessage } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description Parses SIP-X formatted message into message fields object.
|
|
5
|
+
*
|
|
6
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
7
|
+
*
|
|
8
|
+
* @returns SIP-X fields object
|
|
9
|
+
*/
|
|
10
|
+
export function parseSiwsMessage(
|
|
11
|
+
message: string,
|
|
12
|
+
): Prettify<ExactPartial<SiwsMessage>> {
|
|
13
|
+
const { scheme, statement, ...prefix } = (message.match(prefixRegex)
|
|
14
|
+
?.groups ?? {}) as {
|
|
15
|
+
address: string;
|
|
16
|
+
domain: string;
|
|
17
|
+
scheme?: string;
|
|
18
|
+
statement?: string;
|
|
19
|
+
};
|
|
20
|
+
const { chainId, expirationTime, issuedAt, notBefore, requestId, ...suffix } =
|
|
21
|
+
(message.match(suffixRegex)?.groups ?? {}) as {
|
|
22
|
+
chainId: string;
|
|
23
|
+
expirationTime?: string;
|
|
24
|
+
issuedAt?: string;
|
|
25
|
+
nonce: string;
|
|
26
|
+
notBefore?: string;
|
|
27
|
+
requestId?: string;
|
|
28
|
+
uri: string;
|
|
29
|
+
version: "1";
|
|
30
|
+
};
|
|
31
|
+
const resources = message.split("Resources:")[1]?.split("\n- ").slice(1);
|
|
32
|
+
return {
|
|
33
|
+
...prefix,
|
|
34
|
+
...suffix,
|
|
35
|
+
...(chainId ? { chainId: Number(chainId) } : {}),
|
|
36
|
+
...(expirationTime ? { expirationTime: new Date(expirationTime) } : {}),
|
|
37
|
+
...(issuedAt ? { issuedAt: new Date(issuedAt) } : {}),
|
|
38
|
+
...(notBefore ? { notBefore: new Date(notBefore) } : {}),
|
|
39
|
+
...(requestId ? { requestId } : {}),
|
|
40
|
+
...(resources ? { resources } : {}),
|
|
41
|
+
...(scheme ? { scheme } : {}),
|
|
42
|
+
...(statement ? { statement } : {}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// https://regexr.com/80gdj
|
|
47
|
+
const prefixRegex =
|
|
48
|
+
/^(?:(?<scheme>[a-zA-Z][a-zA-Z0-9+-.]*):\/\/)?(?<domain>[a-zA-Z0-9+-.]*(?::[0-9]{1,5})?) (?:wants you to sign in with your Stacks account:\n)(?<address>S[A-Z0-9]{39,40})\n\n(?:(?<statement>.*)\n\n)?/;
|
|
49
|
+
|
|
50
|
+
// https://regexr.com/80gf9
|
|
51
|
+
const suffixRegex =
|
|
52
|
+
/(?:URI: (?<uri>.+))\n(?:Version: (?<version>.+))\n(?:Chain ID: (?<chainId>\d+))\n(?:Nonce: (?<nonce>[a-zA-Z0-9]+))\n(?:Issued At: (?<issuedAt>.+))(?:\nExpiration Time: (?<expirationTime>.+))?(?:\nNot Before: (?<notBefore>.+))?(?:\nRequest ID: (?<requestId>.+))?/;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Combines members of an intersection into a readable type.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://twitter.com/mattpocockuk/status/1622730173446557697?s=20&t=NdpAcmEFXY01xkqU3KO0Mg}
|
|
5
|
+
* @example
|
|
6
|
+
* Prettify<{ a: string } & { b: string } & { c: number, d: bigint }>
|
|
7
|
+
* => { a: string, b: string, c: number, d: bigint }
|
|
8
|
+
*/
|
|
9
|
+
export type Prettify<T> = {
|
|
10
|
+
[K in keyof T]: T[K];
|
|
11
|
+
} & {};
|
|
12
|
+
|
|
13
|
+
export type ExactPartial<type> = {
|
|
14
|
+
[key in keyof type]?: type[key] | undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @description SIP-X message fields
|
|
19
|
+
*
|
|
20
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
21
|
+
*/
|
|
22
|
+
export type SiwsMessage = {
|
|
23
|
+
/**
|
|
24
|
+
* The Stacks address performing the signing.
|
|
25
|
+
*/
|
|
26
|
+
address: string;
|
|
27
|
+
/**
|
|
28
|
+
* The [SIP-005](https://github.com/stacksgov/sips/blob/main/sips/sip-005/sip-005-blocks-and-transactions.md) Chain ID to which the session is bound,
|
|
29
|
+
*/
|
|
30
|
+
chainId: number;
|
|
31
|
+
/**
|
|
32
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority that is requesting the signing.
|
|
33
|
+
*/
|
|
34
|
+
domain: string;
|
|
35
|
+
/**
|
|
36
|
+
* Time when the signed authentication message is no longer valid.
|
|
37
|
+
*/
|
|
38
|
+
expirationTime?: Date | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Time when the message was generated, typically the current time.
|
|
41
|
+
*/
|
|
42
|
+
issuedAt?: Date | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* A random string typically chosen by the relying party and used to prevent replay attacks.
|
|
45
|
+
*/
|
|
46
|
+
nonce: string;
|
|
47
|
+
/**
|
|
48
|
+
* Time when the signed authentication message will become valid.
|
|
49
|
+
*/
|
|
50
|
+
notBefore?: Date | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* A system-specific identifier that may be used to uniquely refer to the sign-in request.
|
|
53
|
+
*/
|
|
54
|
+
requestId?: string | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* A list of information or references to information the user wishes to have resolved as part of authentication by the relying party.
|
|
57
|
+
*/
|
|
58
|
+
resources?: string[] | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request.
|
|
61
|
+
*/
|
|
62
|
+
scheme?: string | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* A human-readable ASCII assertion that the user will sign.
|
|
65
|
+
*/
|
|
66
|
+
statement?: string | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI referring to the resource that is the subject of the signing (as in the subject of a claim).
|
|
69
|
+
*/
|
|
70
|
+
uri: string;
|
|
71
|
+
/**
|
|
72
|
+
* The current version of the SIWS Message.
|
|
73
|
+
*/
|
|
74
|
+
version: "1";
|
|
75
|
+
};
|