@opensea/sdk 11.4.7 → 11.4.9
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/README.md +16 -1
- package/lib/api/walletAuth.d.ts +5 -1
- package/lib/auth/index.d.ts +21 -36
- package/lib/auth/index.js +179 -105
- package/lib/auth/index.js.map +1 -1
- package/lib/auth/types.d.ts +7 -4
- package/lib/scopes.d.ts +4 -0
- package/lib/scopes.js +4 -0
- package/lib/scopes.js.map +1 -1
- package/package.json +2 -2
- package/src/auth/index.ts +218 -123
- package/src/auth/types.ts +7 -4
- package/src/scopes.ts +4 -0
package/README.md
CHANGED
|
@@ -41,8 +41,23 @@ Happy seafaring!
|
|
|
41
41
|
|
|
42
42
|
### Wallet-authenticated API helpers
|
|
43
43
|
|
|
44
|
+
For a server-side EVM signer, `OpenSeaAuth` runs the current SIWE session,
|
|
45
|
+
scoped-token creation, and token-exchange flow. It re-exchanges the scoped token
|
|
46
|
+
when the JWT expires and uses the wallet session for revocation.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { OpenSeaAuth } from "@opensea/sdk"
|
|
50
|
+
|
|
51
|
+
const auth = new OpenSeaAuth()
|
|
52
|
+
const token = await auth.authenticate(signer, {
|
|
53
|
+
scopes: ["read:favorites", "write:orders"],
|
|
54
|
+
})
|
|
55
|
+
const freshToken = await auth.getValidToken()
|
|
56
|
+
await auth.revoke(freshToken.accessToken)
|
|
57
|
+
```
|
|
58
|
+
|
|
44
59
|
Pass an exchanged scoped JWT as `authToken`, then use the generated
|
|
45
|
-
`walletAuth` helpers. Server-side
|
|
60
|
+
`walletAuth` helpers. Server-side scope enforcement still applies.
|
|
46
61
|
|
|
47
62
|
```typescript
|
|
48
63
|
import { OpenSeaAPI } from "@opensea/sdk"
|
package/lib/api/walletAuth.d.ts
CHANGED
|
@@ -59,6 +59,10 @@ export declare class WalletAuthAPI {
|
|
|
59
59
|
marketCapUsd?: number | undefined;
|
|
60
60
|
volume24h?: number | undefined;
|
|
61
61
|
priceChange24h?: number | undefined;
|
|
62
|
+
holdersCount?: number | undefined;
|
|
63
|
+
isVerified: boolean;
|
|
64
|
+
createdAt?: number | undefined;
|
|
65
|
+
genesisDate?: number | undefined;
|
|
62
66
|
}[];
|
|
63
67
|
next?: string | undefined;
|
|
64
68
|
}>;
|
|
@@ -69,7 +73,7 @@ export declare class WalletAuthAPI {
|
|
|
69
73
|
symbol: string;
|
|
70
74
|
imageUrl?: string | undefined;
|
|
71
75
|
description?: string | undefined;
|
|
72
|
-
createdAt?:
|
|
76
|
+
createdAt?: number | undefined;
|
|
73
77
|
chain: string;
|
|
74
78
|
contractAddress: string;
|
|
75
79
|
maxLeverage: number;
|
package/lib/auth/index.d.ts
CHANGED
|
@@ -5,8 +5,10 @@ export type { AuthenticateOptions, AuthNonceResponse, AuthSigner, AuthToken, Aut
|
|
|
5
5
|
/**
|
|
6
6
|
* SIWE-based authentication helper for OpenSea.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Establishes a browser-style wallet session, creates a one-day scoped token,
|
|
9
|
+
* and exchanges that token for the short-lived JWT used by REST and MCP. Token
|
|
10
|
+
* management remains bound to the wallet session, while automatic JWT refresh
|
|
11
|
+
* uses the scoped token.
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
@@ -16,52 +18,35 @@ export type { AuthenticateOptions, AuthNonceResponse, AuthSigner, AuthToken, Aut
|
|
|
16
18
|
* const token = await auth.authenticate(signer, {
|
|
17
19
|
* scopes: ["read:eligibility", "write:orders"],
|
|
18
20
|
* })
|
|
19
|
-
*
|
|
20
|
-
* // Auto-refresh before expiry
|
|
21
21
|
* const freshToken = await auth.getValidToken()
|
|
22
|
-
*
|
|
23
|
-
* // Revoke
|
|
24
|
-
* await auth.revoke(token.accessToken)
|
|
22
|
+
* await auth.revoke(freshToken.accessToken)
|
|
25
23
|
* ```
|
|
26
24
|
*
|
|
27
25
|
* @category Authentication
|
|
28
26
|
*/
|
|
29
27
|
export declare class OpenSeaAuth {
|
|
30
|
-
private readonly
|
|
28
|
+
private readonly apiBaseUrl;
|
|
31
29
|
private cachedToken;
|
|
30
|
+
private scopedTokenId;
|
|
31
|
+
private sessionCookies;
|
|
32
32
|
constructor(config?: OpenSeaAuthConfig);
|
|
33
|
-
/**
|
|
34
|
-
* Run the full SIWE authentication flow:
|
|
35
|
-
* 1. Request a nonce from the auth server
|
|
36
|
-
* 2. Build an EIP-4361 SIWE message
|
|
37
|
-
* 3. Sign it with the provided signer
|
|
38
|
-
* 4. Exchange the signature for a scoped JWT token
|
|
39
|
-
*
|
|
40
|
-
* The returned token is cached in memory and will be auto-refreshed
|
|
41
|
-
* by {@link getValidToken}.
|
|
42
|
-
*/
|
|
33
|
+
/** Run the current SIWE, scoped-token creation, and token-exchange flow. */
|
|
43
34
|
authenticate(signer: AuthSigner, options?: AuthenticateOptions): Promise<AuthToken>;
|
|
44
|
-
/**
|
|
45
|
-
* Return a valid access token, refreshing automatically if the cached
|
|
46
|
-
* token is expired or about to expire.
|
|
47
|
-
*
|
|
48
|
-
* @throws If no token has been obtained yet via {@link authenticate}.
|
|
49
|
-
*/
|
|
35
|
+
/** Return a valid JWT, exchanging the stored scoped token when needed. */
|
|
50
36
|
getValidToken(): Promise<AuthToken>;
|
|
51
|
-
/**
|
|
52
|
-
* Revoke an access token.
|
|
53
|
-
*/
|
|
37
|
+
/** Revoke the scoped token backing the current JWT. */
|
|
54
38
|
revoke(accessToken: string): Promise<void>;
|
|
55
|
-
/** Request a
|
|
39
|
+
/** Request a single-use SIWE nonce. */
|
|
56
40
|
requestNonce(): Promise<AuthNonceResponse>;
|
|
57
|
-
|
|
58
|
-
private
|
|
59
|
-
|
|
60
|
-
private
|
|
41
|
+
private verifySignature;
|
|
42
|
+
private createScopedToken;
|
|
43
|
+
private exchangeScopedToken;
|
|
44
|
+
private cleanupScopedToken;
|
|
45
|
+
private refreshSession;
|
|
61
46
|
}
|
|
62
47
|
/**
|
|
63
|
-
* Build
|
|
64
|
-
*
|
|
65
|
-
*
|
|
48
|
+
* Build the SIWE message accepted by the current OpenSea wallet-auth API.
|
|
49
|
+
* The scopes argument remains for compatibility; webauth authorizes PAT scopes
|
|
50
|
+
* on token creation and does not accept them in this session proof.
|
|
66
51
|
*/
|
|
67
|
-
export declare function generateSiweMessage(address: string,
|
|
52
|
+
export declare function generateSiweMessage(address: string, _scopes: string[], nonce: string, _apiBaseUrl: string, chainId?: number): string;
|
package/lib/auth/index.js
CHANGED
|
@@ -2,20 +2,26 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenSeaAuth = exports.requestSiwxNonce = exports.parseSiwxMessage = exports.linkWalletWithSiwx = exports.createSiwxMessage = exports.chainArchToAccountType = void 0;
|
|
4
4
|
exports.generateSiweMessage = generateSiweMessage;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "
|
|
8
|
-
Object.defineProperty(exports, "
|
|
9
|
-
Object.defineProperty(exports, "
|
|
10
|
-
Object.defineProperty(exports, "
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
const siwx_1 = require("./siwx");
|
|
6
|
+
var siwx_2 = require("./siwx");
|
|
7
|
+
Object.defineProperty(exports, "chainArchToAccountType", { enumerable: true, get: function () { return siwx_2.chainArchToAccountType; } });
|
|
8
|
+
Object.defineProperty(exports, "createSiwxMessage", { enumerable: true, get: function () { return siwx_2.createSiwxMessage; } });
|
|
9
|
+
Object.defineProperty(exports, "linkWalletWithSiwx", { enumerable: true, get: function () { return siwx_2.linkWalletWithSiwx; } });
|
|
10
|
+
Object.defineProperty(exports, "parseSiwxMessage", { enumerable: true, get: function () { return siwx_2.parseSiwxMessage; } });
|
|
11
|
+
Object.defineProperty(exports, "requestSiwxNonce", { enumerable: true, get: function () { return siwx_2.requestSiwxNonce; } });
|
|
12
|
+
const DEFAULT_API_BASE_URL = "https://api.opensea.io";
|
|
13
|
+
const DEFAULT_TOKEN_TTL_SECONDS = 3600;
|
|
14
|
+
const SCOPED_TOKEN_CLEANUP_ATTEMPTS = 2;
|
|
13
15
|
const REFRESH_BUFFER_MS = 60_000;
|
|
16
|
+
const SIWE_STATEMENT = "Click to sign in and accept the OpenSea Terms of Service (https://opensea.io/tos) and Privacy Policy (https://opensea.io/privacy).";
|
|
17
|
+
const SESSION_COOKIE_NAMES = ["access_token", "refresh_token"];
|
|
14
18
|
/**
|
|
15
19
|
* SIWE-based authentication helper for OpenSea.
|
|
16
20
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
21
|
+
* Establishes a browser-style wallet session, creates a one-day scoped token,
|
|
22
|
+
* and exchanges that token for the short-lived JWT used by REST and MCP. Token
|
|
23
|
+
* management remains bound to the wallet session, while automatic JWT refresh
|
|
24
|
+
* uses the scoped token.
|
|
19
25
|
*
|
|
20
26
|
* @example
|
|
21
27
|
* ```ts
|
|
@@ -25,150 +31,218 @@ const REFRESH_BUFFER_MS = 60_000;
|
|
|
25
31
|
* const token = await auth.authenticate(signer, {
|
|
26
32
|
* scopes: ["read:eligibility", "write:orders"],
|
|
27
33
|
* })
|
|
28
|
-
*
|
|
29
|
-
* // Auto-refresh before expiry
|
|
30
34
|
* const freshToken = await auth.getValidToken()
|
|
31
|
-
*
|
|
32
|
-
* // Revoke
|
|
33
|
-
* await auth.revoke(token.accessToken)
|
|
35
|
+
* await auth.revoke(freshToken.accessToken)
|
|
34
36
|
* ```
|
|
35
37
|
*
|
|
36
38
|
* @category Authentication
|
|
37
39
|
*/
|
|
38
40
|
class OpenSeaAuth {
|
|
39
|
-
|
|
41
|
+
apiBaseUrl;
|
|
40
42
|
cachedToken;
|
|
43
|
+
scopedTokenId;
|
|
44
|
+
sessionCookies;
|
|
41
45
|
constructor(config = {}) {
|
|
42
|
-
this.
|
|
46
|
+
this.apiBaseUrl = (config.apiBaseUrl ??
|
|
47
|
+
config.authBaseUrl ??
|
|
48
|
+
DEFAULT_API_BASE_URL).replace(/\/$/, "");
|
|
43
49
|
}
|
|
44
|
-
/**
|
|
45
|
-
* Run the full SIWE authentication flow:
|
|
46
|
-
* 1. Request a nonce from the auth server
|
|
47
|
-
* 2. Build an EIP-4361 SIWE message
|
|
48
|
-
* 3. Sign it with the provided signer
|
|
49
|
-
* 4. Exchange the signature for a scoped JWT token
|
|
50
|
-
*
|
|
51
|
-
* The returned token is cached in memory and will be auto-refreshed
|
|
52
|
-
* by {@link getValidToken}.
|
|
53
|
-
*/
|
|
50
|
+
/** Run the current SIWE, scoped-token creation, and token-exchange flow. */
|
|
54
51
|
async authenticate(signer, options = {}) {
|
|
52
|
+
const scopes = options.scopes ?? [];
|
|
55
53
|
const address = await signer.getAddress();
|
|
56
54
|
const { nonce } = await this.requestNonce();
|
|
57
|
-
|
|
55
|
+
// Webauth uses SIWE only to establish the wallet session. Including PAT
|
|
56
|
+
// scopes here does not round-trip through its signature reconstruction;
|
|
57
|
+
// scope entitlement is enforced by the session-authorized token request.
|
|
58
|
+
const message = generateSiweMessage(address, [], nonce, this.apiBaseUrl);
|
|
58
59
|
const signature = await signer.signMessage(message);
|
|
59
|
-
const
|
|
60
|
-
const
|
|
60
|
+
const cookie = await this.verifySignature(message, signature);
|
|
61
|
+
const scopedToken = await this.createScopedToken(cookie, scopes);
|
|
62
|
+
let exchange;
|
|
63
|
+
try {
|
|
64
|
+
exchange = await this.exchangeScopedToken(scopedToken.token);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
await this.cleanupScopedToken(cookie, scopedToken.id);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
const token = toAuthToken(exchange, scopedToken);
|
|
61
71
|
this.cachedToken = token;
|
|
72
|
+
this.scopedTokenId = scopedToken.id;
|
|
73
|
+
this.sessionCookies = cookie;
|
|
62
74
|
return token;
|
|
63
75
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Return a valid access token, refreshing automatically if the cached
|
|
66
|
-
* token is expired or about to expire.
|
|
67
|
-
*
|
|
68
|
-
* @throws If no token has been obtained yet via {@link authenticate}.
|
|
69
|
-
*/
|
|
76
|
+
/** Return a valid JWT, exchanging the stored scoped token when needed. */
|
|
70
77
|
async getValidToken() {
|
|
71
78
|
if (!this.cachedToken) {
|
|
72
79
|
throw new Error("No auth token available. Call authenticate() with a signer first.");
|
|
73
80
|
}
|
|
74
81
|
if (this.cachedToken.expiresAt.getTime() - Date.now() < REFRESH_BUFFER_MS) {
|
|
75
|
-
const
|
|
76
|
-
this.cachedToken = toAuthToken(
|
|
82
|
+
const exchange = await this.exchangeScopedToken(this.cachedToken.refreshToken);
|
|
83
|
+
this.cachedToken = toAuthToken(exchange, {
|
|
84
|
+
id: this.scopedTokenId ?? "",
|
|
85
|
+
token: this.cachedToken.refreshToken,
|
|
86
|
+
scopes: this.cachedToken.scopes,
|
|
87
|
+
});
|
|
77
88
|
}
|
|
78
89
|
return this.cachedToken;
|
|
79
90
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Revoke an access token.
|
|
82
|
-
*/
|
|
91
|
+
/** Revoke the scoped token backing the current JWT. */
|
|
83
92
|
async revoke(accessToken) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (!response.ok) {
|
|
90
|
-
throw new Error(`Auth server error (${response.status}): ${response.statusText}`);
|
|
91
|
-
}
|
|
92
|
-
if (this.cachedToken?.accessToken === accessToken) {
|
|
93
|
-
this.cachedToken = undefined;
|
|
93
|
+
if (!this.cachedToken ||
|
|
94
|
+
!this.scopedTokenId ||
|
|
95
|
+
!this.sessionCookies ||
|
|
96
|
+
this.cachedToken.accessToken !== accessToken) {
|
|
97
|
+
throw new Error("The access token is not managed by this OpenSeaAuth instance");
|
|
94
98
|
}
|
|
99
|
+
const refreshedCookies = await this.refreshSession(this.sessionCookies);
|
|
100
|
+
// The refresh token rotates, so retain the replacement if DELETE needs a
|
|
101
|
+
// retry after a transient failure.
|
|
102
|
+
this.sessionCookies = refreshedCookies;
|
|
103
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/tokens/${this.scopedTokenId}`, { method: "DELETE", headers: { Cookie: refreshedCookies } });
|
|
104
|
+
await requireOk(response, "Scoped token revocation");
|
|
105
|
+
this.cachedToken = undefined;
|
|
106
|
+
this.scopedTokenId = undefined;
|
|
107
|
+
this.sessionCookies = undefined;
|
|
95
108
|
}
|
|
96
|
-
/** Request a
|
|
109
|
+
/** Request a single-use SIWE nonce. */
|
|
97
110
|
async requestNonce() {
|
|
98
|
-
const response = await fetch(`${this.
|
|
99
|
-
method: "
|
|
111
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/siwe/nonce`, {
|
|
112
|
+
method: "POST",
|
|
100
113
|
headers: { Accept: "application/json" },
|
|
101
114
|
});
|
|
102
|
-
|
|
103
|
-
throw new Error(`Auth server error (${response.status}): ${response.statusText}`);
|
|
104
|
-
}
|
|
115
|
+
await requireOk(response, "Nonce request");
|
|
105
116
|
return response.json();
|
|
106
117
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const response = await fetch(`${this.authBaseUrl}/api/token`, {
|
|
118
|
+
async verifySignature(message, signature) {
|
|
119
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/siwe/verify`, {
|
|
110
120
|
method: "POST",
|
|
111
121
|
headers: { "Content-Type": "application/json" },
|
|
112
|
-
body: JSON.stringify({
|
|
122
|
+
body: JSON.stringify({
|
|
123
|
+
message: (0, siwx_1.parseSiwxMessage)(message),
|
|
124
|
+
signature,
|
|
125
|
+
chainArch: "EVM",
|
|
126
|
+
}),
|
|
113
127
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
128
|
+
await requireOk(response, "SIWE verification");
|
|
129
|
+
return extractSessionCookies(response.headers);
|
|
130
|
+
}
|
|
131
|
+
async createScopedToken(cookie, scopes) {
|
|
132
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/tokens`, {
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers: { "Content-Type": "application/json", Cookie: cookie },
|
|
135
|
+
body: JSON.stringify({
|
|
136
|
+
label: `opensea-sdk-${Date.now()}`,
|
|
137
|
+
scopes,
|
|
138
|
+
expiresInDays: 1,
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
await requireOk(response, "Scoped token creation");
|
|
117
142
|
return response.json();
|
|
118
143
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const response = await fetch(`${this.authBaseUrl}/api/refresh`, {
|
|
144
|
+
async exchangeScopedToken(scopedToken) {
|
|
145
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/tokens/exchange`, {
|
|
122
146
|
method: "POST",
|
|
123
147
|
headers: { "Content-Type": "application/json" },
|
|
124
|
-
body: JSON.stringify({
|
|
148
|
+
body: JSON.stringify({
|
|
149
|
+
subjectToken: scopedToken,
|
|
150
|
+
subjectTokenType: "ACCESS_TOKEN",
|
|
151
|
+
}),
|
|
125
152
|
});
|
|
126
|
-
|
|
127
|
-
throw new Error(`Auth server error (${response.status}): ${response.statusText}`);
|
|
128
|
-
}
|
|
153
|
+
await requireOk(response, "Token exchange");
|
|
129
154
|
return response.json();
|
|
130
155
|
}
|
|
156
|
+
async cleanupScopedToken(cookie, id) {
|
|
157
|
+
for (let attempt = 0; attempt < SCOPED_TOKEN_CLEANUP_ATTEMPTS; attempt++) {
|
|
158
|
+
try {
|
|
159
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/tokens/${id}`, { method: "DELETE", headers: { Cookie: cookie } });
|
|
160
|
+
if (response.ok || response.status === 404)
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// Retry once, then preserve the original exchange failure.
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async refreshSession(cookie) {
|
|
169
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/session/refresh`, { method: "POST", headers: { Cookie: cookie } });
|
|
170
|
+
await requireOk(response, "Session refresh");
|
|
171
|
+
return extractSessionCookies(response.headers);
|
|
172
|
+
}
|
|
131
173
|
}
|
|
132
174
|
exports.OpenSeaAuth = OpenSeaAuth;
|
|
133
|
-
/** Validity window (in ms) for a generated SIWE message. */
|
|
134
|
-
const SIWE_MESSAGE_VALIDITY_MS = 10 * 60_000;
|
|
135
175
|
/**
|
|
136
|
-
* Build
|
|
137
|
-
*
|
|
138
|
-
*
|
|
176
|
+
* Build the SIWE message accepted by the current OpenSea wallet-auth API.
|
|
177
|
+
* The scopes argument remains for compatibility; webauth authorizes PAT scopes
|
|
178
|
+
* on token creation and does not accept them in this session proof.
|
|
139
179
|
*/
|
|
140
|
-
function generateSiweMessage(address,
|
|
141
|
-
|
|
142
|
-
const domain = url.host;
|
|
143
|
-
const uri = `${authBaseUrl}/api/token`;
|
|
144
|
-
const now = Date.now();
|
|
145
|
-
const issuedAt = new Date(now).toISOString();
|
|
146
|
-
const expirationTime = new Date(now + SIWE_MESSAGE_VALIDITY_MS).toISOString();
|
|
147
|
-
const resources = scopes.length > 0
|
|
148
|
-
? `\nResources:${scopes.map(s => `\n- ${s}`).join("")}`
|
|
149
|
-
: "";
|
|
150
|
-
return [
|
|
151
|
-
`${domain} wants you to sign in with your Ethereum account:`,
|
|
180
|
+
function generateSiweMessage(address, _scopes, nonce, _apiBaseUrl, chainId = 1) {
|
|
181
|
+
return (0, siwx_1.createSiwxMessage)({
|
|
152
182
|
address,
|
|
153
|
-
"",
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
183
|
+
chainArch: "EVM",
|
|
184
|
+
chainId,
|
|
185
|
+
nonce,
|
|
186
|
+
statement: SIWE_STATEMENT,
|
|
187
|
+
issuedAt: new Date(),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
async function requireOk(response, operation) {
|
|
191
|
+
if (response.ok)
|
|
192
|
+
return;
|
|
193
|
+
const body = await response.text().catch(() => "");
|
|
194
|
+
throw new Error(`${operation} failed (${response.status}): ${body}`);
|
|
195
|
+
}
|
|
196
|
+
function splitSetCookieHeader(header) {
|
|
197
|
+
const segments = header.split(", ");
|
|
198
|
+
const cookies = [];
|
|
199
|
+
let current = "";
|
|
200
|
+
for (const segment of segments) {
|
|
201
|
+
const trimmed = segment.trim();
|
|
202
|
+
const equals = trimmed.indexOf("=");
|
|
203
|
+
const name = equals > 0 ? trimmed.slice(0, equals).trim() : "";
|
|
204
|
+
if (SESSION_COOKIE_NAMES.includes(name) && current) {
|
|
205
|
+
cookies.push(current);
|
|
206
|
+
current = trimmed;
|
|
207
|
+
}
|
|
208
|
+
else if (SESSION_COOKIE_NAMES.includes(name)) {
|
|
209
|
+
current = trimmed;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
current = current ? `${current}, ${trimmed}` : trimmed;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (current)
|
|
216
|
+
cookies.push(current);
|
|
217
|
+
return cookies;
|
|
218
|
+
}
|
|
219
|
+
function extractSessionCookies(headers) {
|
|
220
|
+
const getSetCookie = headers
|
|
221
|
+
.getSetCookie;
|
|
222
|
+
const raw = getSetCookie?.call(headers) ??
|
|
223
|
+
splitSetCookieHeader(headers.get("set-cookie") ?? "");
|
|
224
|
+
const cookies = new Map();
|
|
225
|
+
for (const cookie of raw) {
|
|
226
|
+
const pair = cookie.split(";")[0];
|
|
227
|
+
const equals = pair.indexOf("=");
|
|
228
|
+
if (equals < 0)
|
|
229
|
+
continue;
|
|
230
|
+
const name = pair.slice(0, equals).trim();
|
|
231
|
+
if (SESSION_COOKIE_NAMES.includes(name)) {
|
|
232
|
+
cookies.set(name, pair.slice(equals + 1).trim());
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (!cookies.has("access_token") || !cookies.has("refresh_token")) {
|
|
236
|
+
throw new Error("SIWE verification did not create a session");
|
|
237
|
+
}
|
|
238
|
+
return [...cookies].map(([name, value]) => `${name}=${value}`).join("; ");
|
|
164
239
|
}
|
|
165
|
-
|
|
166
|
-
function toAuthToken(response) {
|
|
240
|
+
function toAuthToken(exchange, scopedToken) {
|
|
167
241
|
return {
|
|
168
|
-
accessToken:
|
|
169
|
-
refreshToken:
|
|
170
|
-
expiresAt: new Date(Date.now() +
|
|
171
|
-
scopes:
|
|
242
|
+
accessToken: exchange.accessToken,
|
|
243
|
+
refreshToken: scopedToken.token,
|
|
244
|
+
expiresAt: new Date(Date.now() + (exchange.expiresIn ?? DEFAULT_TOKEN_TTL_SECONDS) * 1000),
|
|
245
|
+
scopes: exchange.tokenScopes ?? scopedToken.scopes,
|
|
172
246
|
};
|
|
173
247
|
}
|
|
174
248
|
//# sourceMappingURL=index.js.map
|
package/lib/auth/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":";;;AAoQA,kDAeC;AAnRD,iCAA4D;AAiB5D,+BAMe;AALb,8GAAA,sBAAsB,OAAA;AACtB,yGAAA,iBAAiB,OAAA;AACjB,0GAAA,kBAAkB,OAAA;AAClB,wGAAA,gBAAgB,OAAA;AAChB,wGAAA,gBAAgB,OAAA;AAWlB,MAAM,oBAAoB,GAAG,wBAAwB,CAAA;AACrD,MAAM,yBAAyB,GAAG,IAAI,CAAA;AACtC,MAAM,6BAA6B,GAAG,CAAC,CAAA;AACvC,MAAM,iBAAiB,GAAG,MAAM,CAAA;AAChC,MAAM,cAAc,GAClB,oIAAoI,CAAA;AACtI,MAAM,oBAAoB,GAAG,CAAC,cAAc,EAAE,eAAe,CAAC,CAAA;AAc9D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,WA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EAAE,QAAQ,CAAC,WAAW,IAAI,WAAW,CAAC,MAAM;KACnD,CAAA;AACH,CAAC"}
|
package/lib/auth/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
export interface AuthToken {
|
|
5
5
|
/** JWT access token */
|
|
6
6
|
accessToken: string;
|
|
7
|
-
/**
|
|
7
|
+
/** Scoped token (PAT) used to exchange for a new access token */
|
|
8
8
|
refreshToken: string;
|
|
9
9
|
/** When the access token expires */
|
|
10
10
|
expiresAt: Date;
|
|
@@ -22,7 +22,9 @@ export interface AuthenticateOptions {
|
|
|
22
22
|
* Configuration for the {@link OpenSeaAuth} class.
|
|
23
23
|
*/
|
|
24
24
|
export interface OpenSeaAuthConfig {
|
|
25
|
-
/**
|
|
25
|
+
/** OpenSea API base URL. Defaults to `https://api.opensea.io`. */
|
|
26
|
+
apiBaseUrl?: string;
|
|
27
|
+
/** @deprecated Use `apiBaseUrl`. */
|
|
26
28
|
authBaseUrl?: string;
|
|
27
29
|
}
|
|
28
30
|
/**
|
|
@@ -34,7 +36,8 @@ export interface AuthSigner {
|
|
|
34
36
|
signMessage(message: string): Promise<string>;
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
37
|
-
*
|
|
39
|
+
* Legacy auth response shape retained for source compatibility.
|
|
40
|
+
* @deprecated The public wallet-auth API now returns scoped-token and exchange responses.
|
|
38
41
|
*/
|
|
39
42
|
export interface AuthTokenResponse {
|
|
40
43
|
access_token: string;
|
|
@@ -43,7 +46,7 @@ export interface AuthTokenResponse {
|
|
|
43
46
|
scopes: string[];
|
|
44
47
|
}
|
|
45
48
|
/**
|
|
46
|
-
*
|
|
49
|
+
* Response from the SIWE nonce endpoint.
|
|
47
50
|
*/
|
|
48
51
|
export interface AuthNonceResponse {
|
|
49
52
|
nonce: string;
|
package/lib/scopes.d.ts
CHANGED
|
@@ -11,7 +11,11 @@ import type { AuthScope } from "@opensea/api-types";
|
|
|
11
11
|
export declare const OPENSEA_SCOPES: {
|
|
12
12
|
readonly READ_ELIGIBILITY: "read:eligibility";
|
|
13
13
|
readonly READ_FAVORITES: "read:favorites";
|
|
14
|
+
readonly READ_SOCIAL: "read:social";
|
|
15
|
+
readonly READ_TOOLS: "read:tools";
|
|
14
16
|
readonly WRITE_FAVORITES: "write:favorites";
|
|
17
|
+
readonly WRITE_SOCIAL: "write:social";
|
|
18
|
+
readonly WRITE_TOOLS: "write:tools";
|
|
15
19
|
readonly WRITE_ORDERS: "write:orders";
|
|
16
20
|
readonly WRITE_DROPS: "write:drops";
|
|
17
21
|
readonly WRITE_COLLECTIONS: "write:collections";
|
package/lib/scopes.js
CHANGED
|
@@ -13,7 +13,11 @@ exports.ALL_SCOPES = exports.OPENSEA_SCOPES = void 0;
|
|
|
13
13
|
exports.OPENSEA_SCOPES = {
|
|
14
14
|
READ_ELIGIBILITY: "read:eligibility",
|
|
15
15
|
READ_FAVORITES: "read:favorites",
|
|
16
|
+
READ_SOCIAL: "read:social",
|
|
17
|
+
READ_TOOLS: "read:tools",
|
|
16
18
|
WRITE_FAVORITES: "write:favorites",
|
|
19
|
+
WRITE_SOCIAL: "write:social",
|
|
20
|
+
WRITE_TOOLS: "write:tools",
|
|
17
21
|
WRITE_ORDERS: "write:orders",
|
|
18
22
|
WRITE_DROPS: "write:drops",
|
|
19
23
|
WRITE_COLLECTIONS: "write:collections",
|
package/lib/scopes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scopes.js","sourceRoot":"","sources":["../src/scopes.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;GAQG;AACU,QAAA,cAAc,GAAG;IAC5B,gBAAgB,EAAE,kBAAkB;IACpC,cAAc,EAAE,gBAAgB;IAChC,eAAe,EAAE,iBAAiB;IAClC,YAAY,EAAE,cAAc;IAC5B,WAAW,EAAE,aAAa;IAC1B,iBAAiB,EAAE,mBAAmB;IACtC,aAAa,EAAE,eAAe;IAC9B,aAAa,EAAE,eAAe;CACc,CAAA;AAc9C,MAAM,2BAA2B,GAAgC,IAAI,CAAA;AAErE;;;GAGG;AACU,QAAA,UAAU,GAA4B,MAAM,CAAC,MAAM,CAAC,sBAAc,CAAC,CAAA"}
|
|
1
|
+
{"version":3,"file":"scopes.js","sourceRoot":"","sources":["../src/scopes.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;GAQG;AACU,QAAA,cAAc,GAAG;IAC5B,gBAAgB,EAAE,kBAAkB;IACpC,cAAc,EAAE,gBAAgB;IAChC,WAAW,EAAE,aAAa;IAC1B,UAAU,EAAE,YAAY;IACxB,eAAe,EAAE,iBAAiB;IAClC,YAAY,EAAE,cAAc;IAC5B,WAAW,EAAE,aAAa;IAC1B,YAAY,EAAE,cAAc;IAC5B,WAAW,EAAE,aAAa;IAC1B,iBAAiB,EAAE,mBAAmB;IACtC,aAAa,EAAE,eAAe;IAC9B,aAAa,EAAE,eAAe;CACc,CAAA;AAc9C,MAAM,2BAA2B,GAAgC,IAAI,CAAA;AAErE;;;GAGG;AACU,QAAA,UAAU,GAA4B,MAAM,CAAC,MAAM,CAAC,sBAAc,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opensea/sdk",
|
|
3
|
-
"version": "11.4.
|
|
3
|
+
"version": "11.4.9",
|
|
4
4
|
"description": "TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs, tokens, and our marketplace data",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "OpenSea Developers",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"types": "lib/index.d.ts",
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@noble/hashes": "^2.0.1",
|
|
48
|
-
"@opensea/api-types": "^0.8.
|
|
48
|
+
"@opensea/api-types": "^0.8.2",
|
|
49
49
|
"@opensea/seaport-js": "^4.1.1",
|
|
50
50
|
"ethers": "^6.16.0"
|
|
51
51
|
},
|
package/src/auth/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createSiwxMessage, parseSiwxMessage } from "./siwx"
|
|
1
2
|
import type {
|
|
2
3
|
AuthenticateOptions,
|
|
3
4
|
AuthNonceResponse,
|
|
@@ -30,16 +31,33 @@ export type {
|
|
|
30
31
|
OpenSeaAuthConfig,
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
const DEFAULT_API_BASE_URL = "https://api.opensea.io"
|
|
35
|
+
const DEFAULT_TOKEN_TTL_SECONDS = 3600
|
|
36
|
+
const SCOPED_TOKEN_CLEANUP_ATTEMPTS = 2
|
|
36
37
|
const REFRESH_BUFFER_MS = 60_000
|
|
38
|
+
const SIWE_STATEMENT =
|
|
39
|
+
"Click to sign in and accept the OpenSea Terms of Service (https://opensea.io/tos) and Privacy Policy (https://opensea.io/privacy)."
|
|
40
|
+
const SESSION_COOKIE_NAMES = ["access_token", "refresh_token"]
|
|
41
|
+
|
|
42
|
+
interface ScopedTokenCreatedResponse {
|
|
43
|
+
id: string
|
|
44
|
+
token: string
|
|
45
|
+
scopes: string[]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface ScopedTokenExchangeResponse {
|
|
49
|
+
accessToken: string
|
|
50
|
+
expiresIn?: number
|
|
51
|
+
tokenScopes?: string[]
|
|
52
|
+
}
|
|
37
53
|
|
|
38
54
|
/**
|
|
39
55
|
* SIWE-based authentication helper for OpenSea.
|
|
40
56
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
57
|
+
* Establishes a browser-style wallet session, creates a one-day scoped token,
|
|
58
|
+
* and exchanges that token for the short-lived JWT used by REST and MCP. Token
|
|
59
|
+
* management remains bound to the wallet session, while automatic JWT refresh
|
|
60
|
+
* uses the scoped token.
|
|
43
61
|
*
|
|
44
62
|
* @example
|
|
45
63
|
* ```ts
|
|
@@ -49,62 +67,58 @@ const REFRESH_BUFFER_MS = 60_000
|
|
|
49
67
|
* const token = await auth.authenticate(signer, {
|
|
50
68
|
* scopes: ["read:eligibility", "write:orders"],
|
|
51
69
|
* })
|
|
52
|
-
*
|
|
53
|
-
* // Auto-refresh before expiry
|
|
54
70
|
* const freshToken = await auth.getValidToken()
|
|
55
|
-
*
|
|
56
|
-
* // Revoke
|
|
57
|
-
* await auth.revoke(token.accessToken)
|
|
71
|
+
* await auth.revoke(freshToken.accessToken)
|
|
58
72
|
* ```
|
|
59
73
|
*
|
|
60
74
|
* @category Authentication
|
|
61
75
|
*/
|
|
62
76
|
export class OpenSeaAuth {
|
|
63
|
-
private readonly
|
|
77
|
+
private readonly apiBaseUrl: string
|
|
64
78
|
private cachedToken: AuthToken | undefined
|
|
79
|
+
private scopedTokenId: string | undefined
|
|
80
|
+
private sessionCookies: string | undefined
|
|
65
81
|
|
|
66
82
|
constructor(config: OpenSeaAuthConfig = {}) {
|
|
67
|
-
this.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
this.apiBaseUrl = (
|
|
84
|
+
config.apiBaseUrl ??
|
|
85
|
+
config.authBaseUrl ??
|
|
86
|
+
DEFAULT_API_BASE_URL
|
|
87
|
+
).replace(/\/$/, "")
|
|
71
88
|
}
|
|
72
89
|
|
|
73
|
-
/**
|
|
74
|
-
* Run the full SIWE authentication flow:
|
|
75
|
-
* 1. Request a nonce from the auth server
|
|
76
|
-
* 2. Build an EIP-4361 SIWE message
|
|
77
|
-
* 3. Sign it with the provided signer
|
|
78
|
-
* 4. Exchange the signature for a scoped JWT token
|
|
79
|
-
*
|
|
80
|
-
* The returned token is cached in memory and will be auto-refreshed
|
|
81
|
-
* by {@link getValidToken}.
|
|
82
|
-
*/
|
|
90
|
+
/** Run the current SIWE, scoped-token creation, and token-exchange flow. */
|
|
83
91
|
async authenticate(
|
|
84
92
|
signer: AuthSigner,
|
|
85
93
|
options: AuthenticateOptions = {},
|
|
86
94
|
): Promise<AuthToken> {
|
|
95
|
+
const scopes = options.scopes ?? []
|
|
87
96
|
const address = await signer.getAddress()
|
|
88
97
|
const { nonce } = await this.requestNonce()
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.authBaseUrl,
|
|
94
|
-
)
|
|
98
|
+
// Webauth uses SIWE only to establish the wallet session. Including PAT
|
|
99
|
+
// scopes here does not round-trip through its signature reconstruction;
|
|
100
|
+
// scope entitlement is enforced by the session-authorized token request.
|
|
101
|
+
const message = generateSiweMessage(address, [], nonce, this.apiBaseUrl)
|
|
95
102
|
const signature = await signer.signMessage(message)
|
|
96
|
-
const
|
|
97
|
-
const
|
|
103
|
+
const cookie = await this.verifySignature(message, signature)
|
|
104
|
+
const scopedToken = await this.createScopedToken(cookie, scopes)
|
|
105
|
+
|
|
106
|
+
let exchange: ScopedTokenExchangeResponse
|
|
107
|
+
try {
|
|
108
|
+
exchange = await this.exchangeScopedToken(scopedToken.token)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
await this.cleanupScopedToken(cookie, scopedToken.id)
|
|
111
|
+
throw error
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const token = toAuthToken(exchange, scopedToken)
|
|
98
115
|
this.cachedToken = token
|
|
116
|
+
this.scopedTokenId = scopedToken.id
|
|
117
|
+
this.sessionCookies = cookie
|
|
99
118
|
return token
|
|
100
119
|
}
|
|
101
120
|
|
|
102
|
-
/**
|
|
103
|
-
* Return a valid access token, refreshing automatically if the cached
|
|
104
|
-
* token is expired or about to expire.
|
|
105
|
-
*
|
|
106
|
-
* @throws If no token has been obtained yet via {@link authenticate}.
|
|
107
|
-
*/
|
|
121
|
+
/** Return a valid JWT, exchanging the stored scoped token when needed. */
|
|
108
122
|
async getValidToken(): Promise<AuthToken> {
|
|
109
123
|
if (!this.cachedToken) {
|
|
110
124
|
throw new Error(
|
|
@@ -113,132 +127,213 @@ export class OpenSeaAuth {
|
|
|
113
127
|
}
|
|
114
128
|
|
|
115
129
|
if (this.cachedToken.expiresAt.getTime() - Date.now() < REFRESH_BUFFER_MS) {
|
|
116
|
-
const
|
|
130
|
+
const exchange = await this.exchangeScopedToken(
|
|
117
131
|
this.cachedToken.refreshToken,
|
|
118
132
|
)
|
|
119
|
-
this.cachedToken = toAuthToken(
|
|
133
|
+
this.cachedToken = toAuthToken(exchange, {
|
|
134
|
+
id: this.scopedTokenId ?? "",
|
|
135
|
+
token: this.cachedToken.refreshToken,
|
|
136
|
+
scopes: this.cachedToken.scopes,
|
|
137
|
+
})
|
|
120
138
|
}
|
|
121
139
|
|
|
122
140
|
return this.cachedToken
|
|
123
141
|
}
|
|
124
142
|
|
|
125
|
-
/**
|
|
126
|
-
* Revoke an access token.
|
|
127
|
-
*/
|
|
143
|
+
/** Revoke the scoped token backing the current JWT. */
|
|
128
144
|
async revoke(accessToken: string): Promise<void> {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
145
|
+
if (
|
|
146
|
+
!this.cachedToken ||
|
|
147
|
+
!this.scopedTokenId ||
|
|
148
|
+
!this.sessionCookies ||
|
|
149
|
+
this.cachedToken.accessToken !== accessToken
|
|
150
|
+
) {
|
|
135
151
|
throw new Error(
|
|
136
|
-
|
|
152
|
+
"The access token is not managed by this OpenSeaAuth instance",
|
|
137
153
|
)
|
|
138
154
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
155
|
+
|
|
156
|
+
const refreshedCookies = await this.refreshSession(this.sessionCookies)
|
|
157
|
+
// The refresh token rotates, so retain the replacement if DELETE needs a
|
|
158
|
+
// retry after a transient failure.
|
|
159
|
+
this.sessionCookies = refreshedCookies
|
|
160
|
+
const response = await fetch(
|
|
161
|
+
`${this.apiBaseUrl}/api/v2/auth/tokens/${this.scopedTokenId}`,
|
|
162
|
+
{ method: "DELETE", headers: { Cookie: refreshedCookies } },
|
|
163
|
+
)
|
|
164
|
+
await requireOk(response, "Scoped token revocation")
|
|
165
|
+
this.cachedToken = undefined
|
|
166
|
+
this.scopedTokenId = undefined
|
|
167
|
+
this.sessionCookies = undefined
|
|
142
168
|
}
|
|
143
169
|
|
|
144
|
-
/** Request a
|
|
170
|
+
/** Request a single-use SIWE nonce. */
|
|
145
171
|
async requestNonce(): Promise<AuthNonceResponse> {
|
|
146
|
-
const response = await fetch(`${this.
|
|
147
|
-
method: "
|
|
172
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/siwe/nonce`, {
|
|
173
|
+
method: "POST",
|
|
148
174
|
headers: { Accept: "application/json" },
|
|
149
175
|
})
|
|
150
|
-
|
|
151
|
-
throw new Error(
|
|
152
|
-
`Auth server error (${response.status}): ${response.statusText}`,
|
|
153
|
-
)
|
|
154
|
-
}
|
|
176
|
+
await requireOk(response, "Nonce request")
|
|
155
177
|
return response.json() as Promise<AuthNonceResponse>
|
|
156
178
|
}
|
|
157
179
|
|
|
158
|
-
|
|
159
|
-
private async exchangeSignature(
|
|
180
|
+
private async verifySignature(
|
|
160
181
|
message: string,
|
|
161
182
|
signature: string,
|
|
162
|
-
): Promise<
|
|
163
|
-
const response = await fetch(`${this.
|
|
183
|
+
): Promise<string> {
|
|
184
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/siwe/verify`, {
|
|
164
185
|
method: "POST",
|
|
165
186
|
headers: { "Content-Type": "application/json" },
|
|
166
|
-
body: JSON.stringify({
|
|
187
|
+
body: JSON.stringify({
|
|
188
|
+
message: parseSiwxMessage(message),
|
|
189
|
+
signature,
|
|
190
|
+
chainArch: "EVM",
|
|
191
|
+
}),
|
|
167
192
|
})
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
`Auth server error (${response.status}): ${response.statusText}`,
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
return response.json() as Promise<AuthTokenResponse>
|
|
193
|
+
await requireOk(response, "SIWE verification")
|
|
194
|
+
return extractSessionCookies(response.headers)
|
|
174
195
|
}
|
|
175
196
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
): Promise<
|
|
180
|
-
const response = await fetch(`${this.
|
|
197
|
+
private async createScopedToken(
|
|
198
|
+
cookie: string,
|
|
199
|
+
scopes: string[],
|
|
200
|
+
): Promise<ScopedTokenCreatedResponse> {
|
|
201
|
+
const response = await fetch(`${this.apiBaseUrl}/api/v2/auth/tokens`, {
|
|
181
202
|
method: "POST",
|
|
182
|
-
headers: { "Content-Type": "application/json" },
|
|
183
|
-
body: JSON.stringify({
|
|
203
|
+
headers: { "Content-Type": "application/json", Cookie: cookie },
|
|
204
|
+
body: JSON.stringify({
|
|
205
|
+
label: `opensea-sdk-${Date.now()}`,
|
|
206
|
+
scopes,
|
|
207
|
+
expiresInDays: 1,
|
|
208
|
+
}),
|
|
184
209
|
})
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
210
|
+
await requireOk(response, "Scoped token creation")
|
|
211
|
+
return response.json() as Promise<ScopedTokenCreatedResponse>
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private async exchangeScopedToken(
|
|
215
|
+
scopedToken: string,
|
|
216
|
+
): Promise<ScopedTokenExchangeResponse> {
|
|
217
|
+
const response = await fetch(
|
|
218
|
+
`${this.apiBaseUrl}/api/v2/auth/tokens/exchange`,
|
|
219
|
+
{
|
|
220
|
+
method: "POST",
|
|
221
|
+
headers: { "Content-Type": "application/json" },
|
|
222
|
+
body: JSON.stringify({
|
|
223
|
+
subjectToken: scopedToken,
|
|
224
|
+
subjectTokenType: "ACCESS_TOKEN",
|
|
225
|
+
}),
|
|
226
|
+
},
|
|
227
|
+
)
|
|
228
|
+
await requireOk(response, "Token exchange")
|
|
229
|
+
return response.json() as Promise<ScopedTokenExchangeResponse>
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private async cleanupScopedToken(cookie: string, id: string): Promise<void> {
|
|
233
|
+
for (let attempt = 0; attempt < SCOPED_TOKEN_CLEANUP_ATTEMPTS; attempt++) {
|
|
234
|
+
try {
|
|
235
|
+
const response = await fetch(
|
|
236
|
+
`${this.apiBaseUrl}/api/v2/auth/tokens/${id}`,
|
|
237
|
+
{ method: "DELETE", headers: { Cookie: cookie } },
|
|
238
|
+
)
|
|
239
|
+
if (response.ok || response.status === 404) return
|
|
240
|
+
} catch {
|
|
241
|
+
// Retry once, then preserve the original exchange failure.
|
|
242
|
+
}
|
|
189
243
|
}
|
|
190
|
-
return response.json() as Promise<AuthTokenResponse>
|
|
191
244
|
}
|
|
192
|
-
}
|
|
193
245
|
|
|
194
|
-
|
|
195
|
-
const
|
|
246
|
+
private async refreshSession(cookie: string): Promise<string> {
|
|
247
|
+
const response = await fetch(
|
|
248
|
+
`${this.apiBaseUrl}/api/v2/auth/session/refresh`,
|
|
249
|
+
{ method: "POST", headers: { Cookie: cookie } },
|
|
250
|
+
)
|
|
251
|
+
await requireOk(response, "Session refresh")
|
|
252
|
+
return extractSessionCookies(response.headers)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
196
255
|
|
|
197
256
|
/**
|
|
198
|
-
* Build
|
|
199
|
-
*
|
|
200
|
-
*
|
|
257
|
+
* Build the SIWE message accepted by the current OpenSea wallet-auth API.
|
|
258
|
+
* The scopes argument remains for compatibility; webauth authorizes PAT scopes
|
|
259
|
+
* on token creation and does not accept them in this session proof.
|
|
201
260
|
*/
|
|
202
261
|
export function generateSiweMessage(
|
|
203
262
|
address: string,
|
|
204
|
-
|
|
263
|
+
_scopes: string[],
|
|
205
264
|
nonce: string,
|
|
206
|
-
|
|
265
|
+
_apiBaseUrl: string,
|
|
207
266
|
chainId = 1,
|
|
208
267
|
): string {
|
|
209
|
-
|
|
210
|
-
const domain = url.host
|
|
211
|
-
const uri = `${authBaseUrl}/api/token`
|
|
212
|
-
const now = Date.now()
|
|
213
|
-
const issuedAt = new Date(now).toISOString()
|
|
214
|
-
const expirationTime = new Date(now + SIWE_MESSAGE_VALIDITY_MS).toISOString()
|
|
215
|
-
const resources =
|
|
216
|
-
scopes.length > 0
|
|
217
|
-
? `\nResources:${scopes.map(s => `\n- ${s}`).join("")}`
|
|
218
|
-
: ""
|
|
219
|
-
|
|
220
|
-
return [
|
|
221
|
-
`${domain} wants you to sign in with your Ethereum account:`,
|
|
268
|
+
return createSiwxMessage({
|
|
222
269
|
address,
|
|
223
|
-
"",
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
270
|
+
chainArch: "EVM",
|
|
271
|
+
chainId,
|
|
272
|
+
nonce,
|
|
273
|
+
statement: SIWE_STATEMENT,
|
|
274
|
+
issuedAt: new Date(),
|
|
275
|
+
})
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function requireOk(response: Response, operation: string): Promise<void> {
|
|
279
|
+
if (response.ok) return
|
|
280
|
+
const body = await response.text().catch(() => "")
|
|
281
|
+
throw new Error(`${operation} failed (${response.status}): ${body}`)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function splitSetCookieHeader(header: string): string[] {
|
|
285
|
+
const segments = header.split(", ")
|
|
286
|
+
const cookies: string[] = []
|
|
287
|
+
let current = ""
|
|
288
|
+
for (const segment of segments) {
|
|
289
|
+
const trimmed = segment.trim()
|
|
290
|
+
const equals = trimmed.indexOf("=")
|
|
291
|
+
const name = equals > 0 ? trimmed.slice(0, equals).trim() : ""
|
|
292
|
+
if (SESSION_COOKIE_NAMES.includes(name) && current) {
|
|
293
|
+
cookies.push(current)
|
|
294
|
+
current = trimmed
|
|
295
|
+
} else if (SESSION_COOKIE_NAMES.includes(name)) {
|
|
296
|
+
current = trimmed
|
|
297
|
+
} else {
|
|
298
|
+
current = current ? `${current}, ${trimmed}` : trimmed
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (current) cookies.push(current)
|
|
302
|
+
return cookies
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function extractSessionCookies(headers: Headers): string {
|
|
306
|
+
const getSetCookie = (headers as Headers & { getSetCookie?: () => string[] })
|
|
307
|
+
.getSetCookie
|
|
308
|
+
const raw =
|
|
309
|
+
getSetCookie?.call(headers) ??
|
|
310
|
+
splitSetCookieHeader(headers.get("set-cookie") ?? "")
|
|
311
|
+
const cookies = new Map<string, string>()
|
|
312
|
+
for (const cookie of raw) {
|
|
313
|
+
const pair = cookie.split(";")[0]
|
|
314
|
+
const equals = pair.indexOf("=")
|
|
315
|
+
if (equals < 0) continue
|
|
316
|
+
const name = pair.slice(0, equals).trim()
|
|
317
|
+
if (SESSION_COOKIE_NAMES.includes(name)) {
|
|
318
|
+
cookies.set(name, pair.slice(equals + 1).trim())
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (!cookies.has("access_token") || !cookies.has("refresh_token")) {
|
|
322
|
+
throw new Error("SIWE verification did not create a session")
|
|
323
|
+
}
|
|
324
|
+
return [...cookies].map(([name, value]) => `${name}=${value}`).join("; ")
|
|
234
325
|
}
|
|
235
326
|
|
|
236
|
-
|
|
237
|
-
|
|
327
|
+
function toAuthToken(
|
|
328
|
+
exchange: ScopedTokenExchangeResponse,
|
|
329
|
+
scopedToken: ScopedTokenCreatedResponse,
|
|
330
|
+
): AuthToken {
|
|
238
331
|
return {
|
|
239
|
-
accessToken:
|
|
240
|
-
refreshToken:
|
|
241
|
-
expiresAt: new Date(
|
|
242
|
-
|
|
332
|
+
accessToken: exchange.accessToken,
|
|
333
|
+
refreshToken: scopedToken.token,
|
|
334
|
+
expiresAt: new Date(
|
|
335
|
+
Date.now() + (exchange.expiresIn ?? DEFAULT_TOKEN_TTL_SECONDS) * 1000,
|
|
336
|
+
),
|
|
337
|
+
scopes: exchange.tokenScopes ?? scopedToken.scopes,
|
|
243
338
|
}
|
|
244
339
|
}
|
package/src/auth/types.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
export interface AuthToken {
|
|
5
5
|
/** JWT access token */
|
|
6
6
|
accessToken: string
|
|
7
|
-
/**
|
|
7
|
+
/** Scoped token (PAT) used to exchange for a new access token */
|
|
8
8
|
refreshToken: string
|
|
9
9
|
/** When the access token expires */
|
|
10
10
|
expiresAt: Date
|
|
@@ -24,7 +24,9 @@ export interface AuthenticateOptions {
|
|
|
24
24
|
* Configuration for the {@link OpenSeaAuth} class.
|
|
25
25
|
*/
|
|
26
26
|
export interface OpenSeaAuthConfig {
|
|
27
|
-
/**
|
|
27
|
+
/** OpenSea API base URL. Defaults to `https://api.opensea.io`. */
|
|
28
|
+
apiBaseUrl?: string
|
|
29
|
+
/** @deprecated Use `apiBaseUrl`. */
|
|
28
30
|
authBaseUrl?: string
|
|
29
31
|
}
|
|
30
32
|
|
|
@@ -38,7 +40,8 @@ export interface AuthSigner {
|
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
/**
|
|
41
|
-
*
|
|
43
|
+
* Legacy auth response shape retained for source compatibility.
|
|
44
|
+
* @deprecated The public wallet-auth API now returns scoped-token and exchange responses.
|
|
42
45
|
*/
|
|
43
46
|
export interface AuthTokenResponse {
|
|
44
47
|
access_token: string
|
|
@@ -48,7 +51,7 @@ export interface AuthTokenResponse {
|
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
/**
|
|
51
|
-
*
|
|
54
|
+
* Response from the SIWE nonce endpoint.
|
|
52
55
|
*/
|
|
53
56
|
export interface AuthNonceResponse {
|
|
54
57
|
nonce: string
|
package/src/scopes.ts
CHANGED
|
@@ -12,7 +12,11 @@ import type { AuthScope } from "@opensea/api-types"
|
|
|
12
12
|
export const OPENSEA_SCOPES = {
|
|
13
13
|
READ_ELIGIBILITY: "read:eligibility",
|
|
14
14
|
READ_FAVORITES: "read:favorites",
|
|
15
|
+
READ_SOCIAL: "read:social",
|
|
16
|
+
READ_TOOLS: "read:tools",
|
|
15
17
|
WRITE_FAVORITES: "write:favorites",
|
|
18
|
+
WRITE_SOCIAL: "write:social",
|
|
19
|
+
WRITE_TOOLS: "write:tools",
|
|
16
20
|
WRITE_ORDERS: "write:orders",
|
|
17
21
|
WRITE_DROPS: "write:drops",
|
|
18
22
|
WRITE_COLLECTIONS: "write:collections",
|