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,8 @@
|
|
|
1
|
+
# Changesets
|
|
2
|
+
|
|
3
|
+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
|
|
4
|
+
with multi-package repos, or single-package repos to help you version and publish your code. You can
|
|
5
|
+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
|
|
6
|
+
|
|
7
|
+
We have a quick list of common questions to get you started engaging with this project in
|
|
8
|
+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json",
|
|
3
|
+
"changelog": "@changesets/cli/changelog",
|
|
4
|
+
"commit": false,
|
|
5
|
+
"fixed": [],
|
|
6
|
+
"linked": [],
|
|
7
|
+
"access": "restricted",
|
|
8
|
+
"baseBranch": "main",
|
|
9
|
+
"updateInternalDependencies": "patch",
|
|
10
|
+
"ignore": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: Changesets
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
env:
|
|
9
|
+
CI: true
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
version:
|
|
13
|
+
timeout-minutes: 15
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
permissions:
|
|
17
|
+
contents: write
|
|
18
|
+
pull-requests: write
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- name: Checkout code repository
|
|
22
|
+
uses: actions/checkout@v4
|
|
23
|
+
|
|
24
|
+
- name: Setup pnpm
|
|
25
|
+
uses: pnpm/action-setup@v4
|
|
26
|
+
|
|
27
|
+
- name: Setup node.js
|
|
28
|
+
uses: actions/setup-node@v4
|
|
29
|
+
with:
|
|
30
|
+
node-version: 22
|
|
31
|
+
cache: "pnpm"
|
|
32
|
+
|
|
33
|
+
- name: Install dependencies
|
|
34
|
+
run: pnpm install
|
|
35
|
+
|
|
36
|
+
- name: Create and publish versions
|
|
37
|
+
uses: changesets/action@v1
|
|
38
|
+
with:
|
|
39
|
+
commit: "chore: update versions"
|
|
40
|
+
title: "chore: update versions"
|
|
41
|
+
publish: pnpm release
|
|
42
|
+
env:
|
|
43
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
44
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Node.js CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["main"]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: ["main"]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build:
|
|
11
|
+
name: Build and Test
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
matrix:
|
|
15
|
+
node-version: [20.x, 22.x]
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
|
|
20
|
+
- uses: pnpm/action-setup@v4
|
|
21
|
+
|
|
22
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
23
|
+
uses: actions/setup-node@v4
|
|
24
|
+
with:
|
|
25
|
+
node-version: ${{ matrix.node-version }}
|
|
26
|
+
cache: "pnpm"
|
|
27
|
+
|
|
28
|
+
- name: Install dependencies
|
|
29
|
+
run: pnpm install
|
|
30
|
+
|
|
31
|
+
- name: Build
|
|
32
|
+
run: pnpm build
|
|
33
|
+
|
|
34
|
+
- name: Test
|
|
35
|
+
run: pnpm test
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Léo Pradel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# sign-in-with-stacks
|
|
2
|
+
|
|
3
|
+
Sign-in with Stacks is a library for creating and verifying Sign-In with Stacks messages.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install sign-in-with-stacks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
On the client create a message and sign it with the user's wallet.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createSiwsMessage, generateSiwsNonce } from 'sign-in-with-stacks'
|
|
17
|
+
import { STACKS_MAINNET } from '@stacks/network';
|
|
18
|
+
import { openSignatureRequestPopup } from '@stacks/connect';
|
|
19
|
+
|
|
20
|
+
const message = createSiwsMessage({
|
|
21
|
+
address: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM',
|
|
22
|
+
chainId: STACKS_MAINNET.chainId,
|
|
23
|
+
domain: 'example.com',
|
|
24
|
+
nonce: generateSiwsNonce(),
|
|
25
|
+
uri: 'https://example.com/path',
|
|
26
|
+
version: '1',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
await openSignatureRequestPopup({
|
|
30
|
+
message,
|
|
31
|
+
onFinish: async ({ signature }) => {
|
|
32
|
+
// Pass the signature to your backend
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
On the backend verify the signature, can be used with any framework, next-auth, better-auth, express, etc.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { verifySiwsMessage } from 'sign-in-with-stacks'
|
|
41
|
+
import { STACKS_MAINNET } from '@stacks/network';
|
|
42
|
+
|
|
43
|
+
const valid = verifySiwsMessage({
|
|
44
|
+
message,
|
|
45
|
+
signature,
|
|
46
|
+
nonce: 'your-nonce',
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Credits
|
|
51
|
+
|
|
52
|
+
The first version of this library is a port of the view siwe implementation.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
//#region src/errors/base.d.ts
|
|
2
|
+
|
|
3
|
+
type BaseErrorParameters = {
|
|
4
|
+
cause?: BaseError | Error | undefined;
|
|
5
|
+
details?: string | undefined;
|
|
6
|
+
docsBaseUrl?: string | undefined;
|
|
7
|
+
docsPath?: string | undefined;
|
|
8
|
+
docsSlug?: string | undefined;
|
|
9
|
+
metaMessages?: string[] | undefined;
|
|
10
|
+
name?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
declare class BaseError extends Error {
|
|
13
|
+
details: string;
|
|
14
|
+
docsPath?: string | undefined;
|
|
15
|
+
metaMessages?: string[] | undefined;
|
|
16
|
+
shortMessage: string;
|
|
17
|
+
name: string;
|
|
18
|
+
constructor(shortMessage: string, args?: BaseErrorParameters);
|
|
19
|
+
walk(): Error;
|
|
20
|
+
walk(fn: (err: unknown) => boolean): Error | null;
|
|
21
|
+
} //#endregion
|
|
22
|
+
//#region src/errors/address.d.ts
|
|
23
|
+
type InvalidAddressErrorType = InvalidAddressError & {
|
|
24
|
+
name: "InvalidAddressError";
|
|
25
|
+
};
|
|
26
|
+
declare class InvalidAddressError extends BaseError {
|
|
27
|
+
constructor({
|
|
28
|
+
address
|
|
29
|
+
}: {
|
|
30
|
+
address: string;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/errors/siws.d.ts
|
|
36
|
+
type SiwsInvalidMessageFieldErrorType = SiwsInvalidMessageFieldError & {
|
|
37
|
+
name: "SiwsInvalidMessageFieldError";
|
|
38
|
+
};
|
|
39
|
+
declare class SiwsInvalidMessageFieldError extends BaseError {
|
|
40
|
+
constructor({
|
|
41
|
+
field,
|
|
42
|
+
metaMessages
|
|
43
|
+
}: {
|
|
44
|
+
field: string;
|
|
45
|
+
metaMessages?: string[] | undefined;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/types.d.ts
|
|
51
|
+
/**
|
|
52
|
+
* @description Combines members of an intersection into a readable type.
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://twitter.com/mattpocockuk/status/1622730173446557697?s=20&t=NdpAcmEFXY01xkqU3KO0Mg}
|
|
55
|
+
* @example
|
|
56
|
+
* Prettify<{ a: string } & { b: string } & { c: number, d: bigint }>
|
|
57
|
+
* => { a: string, b: string, c: number, d: bigint }
|
|
58
|
+
*/
|
|
59
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
60
|
+
type ExactPartial<type> = { [key in keyof type]?: type[key] | undefined };
|
|
61
|
+
/**
|
|
62
|
+
* @description SIP-X message fields
|
|
63
|
+
*
|
|
64
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
65
|
+
*/
|
|
66
|
+
type SiwsMessage = {
|
|
67
|
+
/**
|
|
68
|
+
* The Stacks address performing the signing.
|
|
69
|
+
*/
|
|
70
|
+
address: string;
|
|
71
|
+
/**
|
|
72
|
+
* 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,
|
|
73
|
+
*/
|
|
74
|
+
chainId: number;
|
|
75
|
+
/**
|
|
76
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority that is requesting the signing.
|
|
77
|
+
*/
|
|
78
|
+
domain: string;
|
|
79
|
+
/**
|
|
80
|
+
* Time when the signed authentication message is no longer valid.
|
|
81
|
+
*/
|
|
82
|
+
expirationTime?: Date | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Time when the message was generated, typically the current time.
|
|
85
|
+
*/
|
|
86
|
+
issuedAt?: Date | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* A random string typically chosen by the relying party and used to prevent replay attacks.
|
|
89
|
+
*/
|
|
90
|
+
nonce: string;
|
|
91
|
+
/**
|
|
92
|
+
* Time when the signed authentication message will become valid.
|
|
93
|
+
*/
|
|
94
|
+
notBefore?: Date | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* A system-specific identifier that may be used to uniquely refer to the sign-in request.
|
|
97
|
+
*/
|
|
98
|
+
requestId?: string | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* A list of information or references to information the user wishes to have resolved as part of authentication by the relying party.
|
|
101
|
+
*/
|
|
102
|
+
resources?: string[] | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request.
|
|
105
|
+
*/
|
|
106
|
+
scheme?: string | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* A human-readable ASCII assertion that the user will sign.
|
|
109
|
+
*/
|
|
110
|
+
statement?: string | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* [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).
|
|
113
|
+
*/
|
|
114
|
+
uri: string;
|
|
115
|
+
/**
|
|
116
|
+
* The current version of the SIWS Message.
|
|
117
|
+
*/
|
|
118
|
+
version: "1";
|
|
119
|
+
}; //#endregion
|
|
120
|
+
//#region src/createSiwsMessage.d.ts
|
|
121
|
+
type CreateSiwsMessageParameters = SiwsMessage;
|
|
122
|
+
type CreateSiwsMessageReturnType = string;
|
|
123
|
+
type CreateSiwsMessageErrorType = InvalidAddressErrorType | SiwsInvalidMessageFieldErrorType;
|
|
124
|
+
/**
|
|
125
|
+
* @description Creates SIP-X formatted message.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const message = createMessage({
|
|
129
|
+
* address: 'SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173',
|
|
130
|
+
* chainId: 1,
|
|
131
|
+
* domain: 'example.com',
|
|
132
|
+
* nonce: 'foobarbaz',
|
|
133
|
+
* uri: 'https://example.com/path',
|
|
134
|
+
* version: '1',
|
|
135
|
+
* })
|
|
136
|
+
*
|
|
137
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
138
|
+
*/
|
|
139
|
+
declare function createSiwsMessage(parameters: CreateSiwsMessageParameters): CreateSiwsMessageReturnType;
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/generateSiwsNonce.d.ts
|
|
143
|
+
/**
|
|
144
|
+
* @description Generates random SIP-X nonce.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* const nonce = generateNonce()
|
|
148
|
+
*
|
|
149
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
150
|
+
*
|
|
151
|
+
* @returns A randomly generated SIP-X nonce.
|
|
152
|
+
*/
|
|
153
|
+
declare function generateSiwsNonce(): string;
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/validateSiwsMessage.d.ts
|
|
157
|
+
type ValidateSiwsMessageParameters = {
|
|
158
|
+
/**
|
|
159
|
+
* Stacks address to check against.
|
|
160
|
+
*/
|
|
161
|
+
address?: string | undefined;
|
|
162
|
+
/**
|
|
163
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority to check against.
|
|
164
|
+
*/
|
|
165
|
+
domain?: string | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* SIP-X message fields.
|
|
168
|
+
*/
|
|
169
|
+
message: ExactPartial<SiwsMessage>;
|
|
170
|
+
/**
|
|
171
|
+
* Random string to check against.
|
|
172
|
+
*/
|
|
173
|
+
nonce?: string | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme to check against.
|
|
176
|
+
*/
|
|
177
|
+
scheme?: string | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* Current time to check optional `expirationTime` and `notBefore` fields.
|
|
180
|
+
*
|
|
181
|
+
* @default new Date()
|
|
182
|
+
*/
|
|
183
|
+
time?: Date | undefined;
|
|
184
|
+
};
|
|
185
|
+
type ValidateSiwsMessageReturnType = boolean;
|
|
186
|
+
/**
|
|
187
|
+
* @description Validates SIP-X message.
|
|
188
|
+
*
|
|
189
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
190
|
+
*/
|
|
191
|
+
declare function validateSiwsMessage(parameters: ValidateSiwsMessageParameters): ValidateSiwsMessageReturnType;
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/verifySiwsMessage.d.ts
|
|
195
|
+
type VerifySiwsMessageParameters = Prettify<Pick<ValidateSiwsMessageParameters, "address" | "domain" | "nonce" | "scheme" | "time"> & {
|
|
196
|
+
/**
|
|
197
|
+
* SIP-X formatted message.
|
|
198
|
+
*/
|
|
199
|
+
message: string;
|
|
200
|
+
/**
|
|
201
|
+
* Signature to check against.
|
|
202
|
+
*/
|
|
203
|
+
signature: string;
|
|
204
|
+
}>;
|
|
205
|
+
type VerifySiwsMessageReturnType = boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Verifies [SIP-X](https://github.com/stacksgov/sips/pull/70) formatted message was signed.
|
|
208
|
+
*
|
|
209
|
+
* @param parameters - {@link VerifySiwsMessageParameters}
|
|
210
|
+
* @returns Whether or not the signature is valid. {@link VerifySiwsMessageReturnType}
|
|
211
|
+
*/
|
|
212
|
+
declare function verifySiwsMessage(parameters: VerifySiwsMessageParameters): VerifySiwsMessageReturnType;
|
|
213
|
+
|
|
214
|
+
//#endregion
|
|
215
|
+
export { CreateSiwsMessageErrorType, CreateSiwsMessageParameters, CreateSiwsMessageReturnType, ValidateSiwsMessageParameters, ValidateSiwsMessageReturnType, VerifySiwsMessageParameters, VerifySiwsMessageReturnType, createSiwsMessage, generateSiwsNonce, validateSiwsMessage, verifySiwsMessage };
|
|
216
|
+
//# sourceMappingURL=index.d.ts.map
|