@opensea/sdk 12.5.0 → 12.6.0
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 +53 -0
- package/lib/api/api.d.ts +16 -1
- package/lib/api/api.js +21 -11
- package/lib/api/api.js.map +1 -1
- package/lib/auth/index.d.ts +2 -0
- package/lib/auth/index.js +13 -10
- package/lib/auth/index.js.map +1 -1
- package/lib/auth/oauth-types.d.ts +10 -0
- package/lib/auth/oauth.d.ts +16 -0
- package/lib/auth/oauth.js +61 -22
- package/lib/auth/oauth.js.map +1 -1
- package/lib/auth/siwx.d.ts +17 -1
- package/lib/auth/siwx.js +10 -4
- package/lib/auth/siwx.js.map +1 -1
- package/lib/auth/types.d.ts +11 -0
- package/lib/sdk/orders.d.ts +27 -3
- package/lib/sdk/orders.js +142 -176
- package/lib/sdk/orders.js.map +1 -1
- package/lib/types.d.ts +8 -3
- package/lib/types.js.map +1 -1
- package/lib/utils/case.d.ts +22 -0
- package/lib/utils/fetchTransport.d.ts +29 -0
- package/lib/utils/fetchTransport.js +26 -0
- package/lib/utils/fetchTransport.js.map +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +7 -0
- package/lib/utils/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/api.ts +32 -17
- package/src/auth/index.ts +45 -26
- package/src/auth/oauth-types.ts +11 -0
- package/src/auth/oauth.ts +70 -26
- package/src/auth/siwx.ts +30 -10
- package/src/auth/types.ts +12 -0
- package/src/sdk/orders.ts +196 -226
- package/src/types.ts +9 -3
- package/src/utils/case.ts +22 -0
- package/src/utils/fetchTransport.ts +36 -0
- package/src/utils/index.ts +7 -0
package/lib/utils/index.d.ts
CHANGED
package/lib/utils/index.js
CHANGED
|
@@ -14,6 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// `./case` carries the SDK's response-casing contract: the API speaks
|
|
18
|
+
// snake_case, the SDK hands back camelCase. Consumers need `Camelize` by name
|
|
19
|
+
// to write down the shape of anything they get back whose camelized view has
|
|
20
|
+
// no dedicated alias in this package. Without it the only spelled-out type for
|
|
21
|
+
// such a response is the raw `@opensea/api-types` one, which is the wrong
|
|
22
|
+
// shape at runtime and, for some schemas, still type-checks.
|
|
23
|
+
__exportStar(require("./case"), exports);
|
|
17
24
|
__exportStar(require("./dateHelper"), exports);
|
|
18
25
|
__exportStar(require("./utils"), exports);
|
|
19
26
|
//# sourceMappingURL=index.js.map
|
package/lib/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,0CAAuB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sEAAsE;AACtE,8EAA8E;AAC9E,6EAA6E;AAC7E,+EAA+E;AAC/E,0EAA0E;AAC1E,6DAA6D;AAC7D,yCAAsB;AACtB,+CAA4B;AAC5B,0CAAuB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opensea/sdk",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.6.0",
|
|
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",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"types": "lib/index.d.ts",
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@noble/hashes": "^2.0.1",
|
|
50
|
-
"@opensea/api-types": "^0.
|
|
50
|
+
"@opensea/api-types": "^0.11.0",
|
|
51
51
|
"@opensea/seaport-js": "^4.2.0",
|
|
52
52
|
"ethers": "^6.16.0"
|
|
53
53
|
},
|
package/src/api/api.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
camelizeKeysDeep,
|
|
18
18
|
snakeizeKeysDeep,
|
|
19
19
|
} from "../utils/case"
|
|
20
|
+
import { type FetchImpl, fetchWith } from "../utils/fetchTransport"
|
|
20
21
|
import { executeWithRateLimit } from "../utils/rateLimit"
|
|
21
22
|
import { AccountsAPI } from "./accounts"
|
|
22
23
|
import { getInstantApiKeyPath } from "./apiPaths"
|
|
@@ -155,6 +156,15 @@ import { WalletAuthAPI } from "./walletAuth"
|
|
|
155
156
|
|
|
156
157
|
/**
|
|
157
158
|
* The API class for the OpenSea SDK.
|
|
159
|
+
*
|
|
160
|
+
* Every method returns the camelCase view of the response. The API sends
|
|
161
|
+
* snake_case, and the `@opensea/api-types` type of the same name describes
|
|
162
|
+
* that wire shape, so annotating a return value here with one of those types
|
|
163
|
+
* leaves every renamed field `undefined` at runtime, while single-word keys
|
|
164
|
+
* survive the rewrite and still read correctly. Use the camelized type this
|
|
165
|
+
* package exports, or `Camelize<WireType>`. {@link RequestOptions.camelizeResponse}
|
|
166
|
+
* turns the rewrite off where the response keys are data rather than field names.
|
|
167
|
+
*
|
|
158
168
|
* @category Main Classes
|
|
159
169
|
*/
|
|
160
170
|
export class OpenSeaAPI {
|
|
@@ -174,7 +184,7 @@ export class OpenSeaAPI {
|
|
|
174
184
|
private apiKey: string | undefined
|
|
175
185
|
private authToken: string | undefined
|
|
176
186
|
/** Transport for every instance request. See {@link OpenSeaAPIConfig.fetch}. */
|
|
177
|
-
private readonly fetchImpl:
|
|
187
|
+
private readonly fetchImpl: FetchImpl | undefined
|
|
178
188
|
private chain: Chain
|
|
179
189
|
|
|
180
190
|
/**
|
|
@@ -209,10 +219,9 @@ export class OpenSeaAPI {
|
|
|
209
219
|
* @param logger Optional function for logging debug strings before and after requests are made. Defaults to no logging
|
|
210
220
|
*/
|
|
211
221
|
constructor(config: OpenSeaAPIConfig, logger?: (arg: string) => void) {
|
|
212
|
-
//
|
|
213
|
-
// globalThis.fetch
|
|
214
|
-
this.fetchImpl =
|
|
215
|
-
config.fetch ?? ((url, init) => globalThis.fetch(url, init))
|
|
222
|
+
// Stored as given, including undefined: fetchWith falls back to globalThis.fetch at call
|
|
223
|
+
// time, so a caller who swaps globalThis.fetch after construction still takes effect.
|
|
224
|
+
this.fetchImpl = config.fetch
|
|
216
225
|
this.apiKey = config.apiKey
|
|
217
226
|
this.authToken = config.authToken
|
|
218
227
|
this.chain = config.chain ?? Chain.Mainnet
|
|
@@ -1718,11 +1727,9 @@ export class OpenSeaAPI {
|
|
|
1718
1727
|
}
|
|
1719
1728
|
|
|
1720
1729
|
try {
|
|
1721
|
-
//
|
|
1722
|
-
//
|
|
1723
|
-
|
|
1724
|
-
// ignores the thisArg, so this does not clobber a deliberately bound transport.
|
|
1725
|
-
const response = await this.fetchImpl.call(globalThis, url, {
|
|
1730
|
+
// fetchWith, not a plain call: it supplies the global receiver that unbound native fetch
|
|
1731
|
+
// needs, and falls back to globalThis.fetch when no transport was configured.
|
|
1732
|
+
const response = await fetchWith(this.fetchImpl, url, {
|
|
1726
1733
|
method,
|
|
1727
1734
|
headers: mergedHeaders,
|
|
1728
1735
|
body: body != null ? JSON.stringify(body) : undefined,
|
|
@@ -1796,20 +1803,28 @@ export class OpenSeaAPI {
|
|
|
1796
1803
|
* ```
|
|
1797
1804
|
*
|
|
1798
1805
|
* @param apiBaseUrl Optional base URL override (defaults to mainnet).
|
|
1806
|
+
* @param options Optional `fetch` transport. This is a static method with no instance to read
|
|
1807
|
+
* {@link OpenSeaAPIConfig.fetch} from, so a consumer that routes every request
|
|
1808
|
+
* through its own transport passes it here. Defaults to the global `fetch`.
|
|
1799
1809
|
* @returns The {@link RequestInstantApiKeyResponse} containing the new key,
|
|
1800
1810
|
* with response keys camelized to match SDK conventions.
|
|
1801
1811
|
*/
|
|
1802
1812
|
public static async requestInstantApiKey(
|
|
1803
1813
|
apiBaseUrl: string = API_BASE_MAINNET,
|
|
1814
|
+
options: { fetch?: FetchImpl } = {},
|
|
1804
1815
|
): Promise<RequestInstantApiKeyResponse> {
|
|
1805
|
-
const response = await
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1816
|
+
const response = await fetchWith(
|
|
1817
|
+
options.fetch,
|
|
1818
|
+
`${apiBaseUrl}${getInstantApiKeyPath()}`,
|
|
1819
|
+
{
|
|
1820
|
+
method: "POST",
|
|
1821
|
+
headers: {
|
|
1822
|
+
"Content-Type": "application/json",
|
|
1823
|
+
"x-app-id": "opensea-js",
|
|
1824
|
+
},
|
|
1825
|
+
body: "{}",
|
|
1810
1826
|
},
|
|
1811
|
-
|
|
1812
|
-
})
|
|
1827
|
+
)
|
|
1813
1828
|
if (!response.ok) {
|
|
1814
1829
|
throw OpenSeaAPI._createApiError(
|
|
1815
1830
|
response,
|
package/src/auth/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type FetchImpl, fetchWith } from "../utils/fetchTransport"
|
|
1
2
|
import { createSiwxMessage, parseSiwxMessage } from "./siwx"
|
|
2
3
|
import type {
|
|
3
4
|
AuthenticateOptions,
|
|
@@ -75,6 +76,8 @@ interface ScopedTokenExchangeResponse {
|
|
|
75
76
|
*/
|
|
76
77
|
export class OpenSeaAuth {
|
|
77
78
|
private readonly apiBaseUrl: string
|
|
79
|
+
/** Transport for every request this instance makes. See {@link OpenSeaAuthConfig.fetch}. */
|
|
80
|
+
private readonly fetchImpl: FetchImpl | undefined
|
|
78
81
|
private cachedToken: AuthToken | undefined
|
|
79
82
|
private scopedTokenId: string | undefined
|
|
80
83
|
private sessionCookies: string | undefined
|
|
@@ -85,6 +88,9 @@ export class OpenSeaAuth {
|
|
|
85
88
|
config.authBaseUrl ??
|
|
86
89
|
DEFAULT_API_BASE_URL
|
|
87
90
|
).replace(/\/$/, "")
|
|
91
|
+
// Stored as given, including undefined: fetchWith falls back to globalThis.fetch at call
|
|
92
|
+
// time, so a caller who swaps globalThis.fetch after construction still takes effect.
|
|
93
|
+
this.fetchImpl = config.fetch
|
|
88
94
|
}
|
|
89
95
|
|
|
90
96
|
/** Run the current SIWE, scoped-token creation, and token-exchange flow. */
|
|
@@ -157,7 +163,8 @@ export class OpenSeaAuth {
|
|
|
157
163
|
// The refresh token rotates, so retain the replacement if DELETE needs a
|
|
158
164
|
// retry after a transient failure.
|
|
159
165
|
this.sessionCookies = refreshedCookies
|
|
160
|
-
const response = await
|
|
166
|
+
const response = await fetchWith(
|
|
167
|
+
this.fetchImpl,
|
|
161
168
|
`${this.apiBaseUrl}/api/v2/auth/tokens/${this.scopedTokenId}`,
|
|
162
169
|
{ method: "DELETE", headers: { Cookie: refreshedCookies } },
|
|
163
170
|
)
|
|
@@ -169,10 +176,11 @@ export class OpenSeaAuth {
|
|
|
169
176
|
|
|
170
177
|
/** Request a single-use SIWE nonce. */
|
|
171
178
|
async requestNonce(): Promise<AuthNonceResponse> {
|
|
172
|
-
const response = await
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
179
|
+
const response = await fetchWith(
|
|
180
|
+
this.fetchImpl,
|
|
181
|
+
`${this.apiBaseUrl}/api/v2/auth/siwe/nonce`,
|
|
182
|
+
{ method: "POST", headers: { Accept: "application/json" } },
|
|
183
|
+
)
|
|
176
184
|
await requireOk(response, "Nonce request")
|
|
177
185
|
return response.json() as Promise<AuthNonceResponse>
|
|
178
186
|
}
|
|
@@ -181,15 +189,19 @@ export class OpenSeaAuth {
|
|
|
181
189
|
message: string,
|
|
182
190
|
signature: string,
|
|
183
191
|
): Promise<string> {
|
|
184
|
-
const response = await
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
192
|
+
const response = await fetchWith(
|
|
193
|
+
this.fetchImpl,
|
|
194
|
+
`${this.apiBaseUrl}/api/v2/auth/siwe/verify`,
|
|
195
|
+
{
|
|
196
|
+
method: "POST",
|
|
197
|
+
headers: { "Content-Type": "application/json" },
|
|
198
|
+
body: JSON.stringify({
|
|
199
|
+
message: parseSiwxMessage(message),
|
|
200
|
+
signature,
|
|
201
|
+
chainArch: "EVM",
|
|
202
|
+
}),
|
|
203
|
+
},
|
|
204
|
+
)
|
|
193
205
|
await requireOk(response, "SIWE verification")
|
|
194
206
|
return extractSessionCookies(response.headers)
|
|
195
207
|
}
|
|
@@ -198,15 +210,19 @@ export class OpenSeaAuth {
|
|
|
198
210
|
cookie: string,
|
|
199
211
|
scopes: string[],
|
|
200
212
|
): Promise<ScopedTokenCreatedResponse> {
|
|
201
|
-
const response = await
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
213
|
+
const response = await fetchWith(
|
|
214
|
+
this.fetchImpl,
|
|
215
|
+
`${this.apiBaseUrl}/api/v2/auth/tokens`,
|
|
216
|
+
{
|
|
217
|
+
method: "POST",
|
|
218
|
+
headers: { "Content-Type": "application/json", Cookie: cookie },
|
|
219
|
+
body: JSON.stringify({
|
|
220
|
+
label: `opensea-sdk-${Date.now()}`,
|
|
221
|
+
scopes,
|
|
222
|
+
expiresInDays: 1,
|
|
223
|
+
}),
|
|
224
|
+
},
|
|
225
|
+
)
|
|
210
226
|
await requireOk(response, "Scoped token creation")
|
|
211
227
|
return response.json() as Promise<ScopedTokenCreatedResponse>
|
|
212
228
|
}
|
|
@@ -214,7 +230,8 @@ export class OpenSeaAuth {
|
|
|
214
230
|
private async exchangeScopedToken(
|
|
215
231
|
scopedToken: string,
|
|
216
232
|
): Promise<ScopedTokenExchangeResponse> {
|
|
217
|
-
const response = await
|
|
233
|
+
const response = await fetchWith(
|
|
234
|
+
this.fetchImpl,
|
|
218
235
|
`${this.apiBaseUrl}/api/v2/auth/tokens/exchange`,
|
|
219
236
|
{
|
|
220
237
|
method: "POST",
|
|
@@ -232,7 +249,8 @@ export class OpenSeaAuth {
|
|
|
232
249
|
private async cleanupScopedToken(cookie: string, id: string): Promise<void> {
|
|
233
250
|
for (let attempt = 0; attempt < SCOPED_TOKEN_CLEANUP_ATTEMPTS; attempt++) {
|
|
234
251
|
try {
|
|
235
|
-
const response = await
|
|
252
|
+
const response = await fetchWith(
|
|
253
|
+
this.fetchImpl,
|
|
236
254
|
`${this.apiBaseUrl}/api/v2/auth/tokens/${id}`,
|
|
237
255
|
{ method: "DELETE", headers: { Cookie: cookie } },
|
|
238
256
|
)
|
|
@@ -244,7 +262,8 @@ export class OpenSeaAuth {
|
|
|
244
262
|
}
|
|
245
263
|
|
|
246
264
|
private async refreshSession(cookie: string): Promise<string> {
|
|
247
|
-
const response = await
|
|
265
|
+
const response = await fetchWith(
|
|
266
|
+
this.fetchImpl,
|
|
248
267
|
`${this.apiBaseUrl}/api/v2/auth/session/refresh`,
|
|
249
268
|
{ method: "POST", headers: { Cookie: cookie } },
|
|
250
269
|
)
|
package/src/auth/oauth-types.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { FetchImpl } from "../utils/fetchTransport"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Configuration for the {@link OpenSeaOAuth} helper.
|
|
3
5
|
*/
|
|
@@ -15,6 +17,15 @@ export interface OpenSeaOAuthConfig {
|
|
|
15
17
|
* connection stalling `opensea login` (or an SDK consumer) indefinitely.
|
|
16
18
|
*/
|
|
17
19
|
timeoutMs?: number
|
|
20
|
+
/**
|
|
21
|
+
* Transport used for every request this instance makes to the authorization server,
|
|
22
|
+
* defaulting to the global `fetch`. Same seam as {@link OpenSeaAPIConfig.fetch}.
|
|
23
|
+
*
|
|
24
|
+
* It sees the PKCE code verifier, the device code and the returned token set, so anything it
|
|
25
|
+
* logs or caches is a credential. The per-request timeout is applied before the transport
|
|
26
|
+
* runs: it receives an init whose `signal` is already the timeout's.
|
|
27
|
+
*/
|
|
28
|
+
fetch?: FetchImpl
|
|
18
29
|
}
|
|
19
30
|
|
|
20
31
|
/**
|
package/src/auth/oauth.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AUTH_SCOPES } from "@opensea/api-types"
|
|
2
|
+
import { type FetchImpl, fetchWith } from "../utils/fetchTransport"
|
|
2
3
|
import type {
|
|
3
4
|
DeviceAuthorizationResponse,
|
|
4
5
|
OAuthDiscoveryDocument,
|
|
@@ -55,6 +56,8 @@ export class OpenSeaOAuth {
|
|
|
55
56
|
private readonly issuer: string
|
|
56
57
|
private readonly clientId: string
|
|
57
58
|
private readonly timeoutMs: number
|
|
59
|
+
/** Transport for every request this instance makes. See {@link OpenSeaOAuthConfig.fetch}. */
|
|
60
|
+
private readonly fetchImpl: FetchImpl | undefined
|
|
58
61
|
private discovery: OAuthDiscoveryDocument | undefined
|
|
59
62
|
|
|
60
63
|
constructor(config: OpenSeaOAuthConfig) {
|
|
@@ -66,6 +69,9 @@ export class OpenSeaOAuth {
|
|
|
66
69
|
this.issuer = issuer
|
|
67
70
|
this.clientId = config.clientId
|
|
68
71
|
this.timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS
|
|
72
|
+
// Stored as given, including undefined: fetchWith falls back to globalThis.fetch at call
|
|
73
|
+
// time, so a caller who swaps globalThis.fetch after construction still takes effect.
|
|
74
|
+
this.fetchImpl = config.fetch
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
/**
|
|
@@ -284,7 +290,10 @@ export class OpenSeaOAuth {
|
|
|
284
290
|
const controller = new AbortController()
|
|
285
291
|
const timer = setTimeout(() => controller.abort(), this.timeoutMs)
|
|
286
292
|
try {
|
|
287
|
-
return await
|
|
293
|
+
return await fetchWith(this.fetchImpl, input, {
|
|
294
|
+
...init,
|
|
295
|
+
signal: controller.signal,
|
|
296
|
+
})
|
|
288
297
|
} catch (error) {
|
|
289
298
|
// Detect our own timeout via the controller rather than the error type:
|
|
290
299
|
// an aborted fetch rejects with a `DOMException` in browsers (not always
|
|
@@ -372,18 +381,16 @@ function canonicalOpenSeaScopes(scopes: string[]): string[] {
|
|
|
372
381
|
* authorization decision.
|
|
373
382
|
*/
|
|
374
383
|
export function extractOpenSeaScopes(accessToken: string): string[] {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
)
|
|
384
|
-
|
|
385
|
-
} catch {
|
|
386
|
-
// Opaque access tokens cannot provide a scope fallback.
|
|
384
|
+
// An opaque token has no claims, and a malformed one cannot be read; either way
|
|
385
|
+
// there is no scope fallback here.
|
|
386
|
+
const claim = tryDecodeJwtPayload(accessToken)?.opensea_scopes
|
|
387
|
+
if (typeof claim === "string") {
|
|
388
|
+
return canonicalOpenSeaScopes(claim.split(/\s+/).filter(Boolean))
|
|
389
|
+
}
|
|
390
|
+
if (Array.isArray(claim)) {
|
|
391
|
+
return canonicalOpenSeaScopes(
|
|
392
|
+
claim.filter((scope): scope is string => typeof scope === "string"),
|
|
393
|
+
)
|
|
387
394
|
}
|
|
388
395
|
return []
|
|
389
396
|
}
|
|
@@ -463,27 +470,52 @@ export function extractWalletAddress(
|
|
|
463
470
|
* decision.
|
|
464
471
|
*/
|
|
465
472
|
export function extractLinkedWallets(accessToken: string): string[] {
|
|
473
|
+
// An opaque token has no claims and a malformed one cannot be read, so `[]`
|
|
474
|
+
// here means "nothing readable" as well as "no linked wallets". A caller that
|
|
475
|
+
// must not silently under-report a portfolio should check
|
|
476
|
+
// `tryDecodeJwtPayload(token) === null` to tell those apart before treating an
|
|
477
|
+
// empty result as the whole picture. The malformed case is the dangerous one:
|
|
478
|
+
// an opaque token is a deliberate choice, a corrupted one looks identical to
|
|
479
|
+
// an account that simply has one wallet.
|
|
480
|
+
const claim = tryDecodeJwtPayload(accessToken)?.linked_wallets
|
|
481
|
+
if (!Array.isArray(claim)) return []
|
|
482
|
+
const wallets = claim.filter(
|
|
483
|
+
(wallet): wallet is string =>
|
|
484
|
+
typeof wallet === "string" && wallet.trim().length > 0,
|
|
485
|
+
)
|
|
486
|
+
// Exact dedupe, not case-folded: EVM addresses differ only by checksum
|
|
487
|
+
// casing, but Solana base58 addresses are case-sensitive, so lowercasing
|
|
488
|
+
// to compare would risk collapsing two distinct Solana wallets into one.
|
|
489
|
+
return [...new Set(wallets)]
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Decode a JWT payload without verifying the signature, or return `null` when
|
|
494
|
+
* the token is not a readable JWT.
|
|
495
|
+
*
|
|
496
|
+
* OpenSea issues both JWT and opaque access tokens, so "not a JWT" is an
|
|
497
|
+
* expected input here rather than an error. Prefer this over wrapping
|
|
498
|
+
* {@link decodeJwtPayload} in a try/catch, and use it to tell "no claim to
|
|
499
|
+
* read" apart from "claim read, and it was empty" — {@link extractLinkedWallets}
|
|
500
|
+
* and {@link extractOpenSeaScopes} both return `[]` for either case.
|
|
501
|
+
*/
|
|
502
|
+
export function tryDecodeJwtPayload(
|
|
503
|
+
token: string,
|
|
504
|
+
): Record<string, unknown> | null {
|
|
466
505
|
try {
|
|
467
|
-
|
|
468
|
-
if (!Array.isArray(claim)) return []
|
|
469
|
-
const wallets = claim.filter(
|
|
470
|
-
(wallet): wallet is string =>
|
|
471
|
-
typeof wallet === "string" && wallet.trim().length > 0,
|
|
472
|
-
)
|
|
473
|
-
// Exact dedupe, not case-folded: EVM addresses differ only by checksum
|
|
474
|
-
// casing, but Solana base58 addresses are case-sensitive, so lowercasing
|
|
475
|
-
// to compare would risk collapsing two distinct Solana wallets into one.
|
|
476
|
-
return [...new Set(wallets)]
|
|
506
|
+
return decodeJwtPayload(token)
|
|
477
507
|
} catch {
|
|
478
|
-
|
|
508
|
+
return null
|
|
479
509
|
}
|
|
480
|
-
return []
|
|
481
510
|
}
|
|
482
511
|
|
|
483
512
|
/**
|
|
484
513
|
* Decode a JWT payload without verifying the signature. Intended for reading
|
|
485
514
|
* claims out of a token the authorization server just issued to us over TLS —
|
|
486
515
|
* NOT for validating inbound tokens.
|
|
516
|
+
*
|
|
517
|
+
* Throws when `token` is not a JWT. {@link tryDecodeJwtPayload} is the
|
|
518
|
+
* non-throwing form, and is what the claim extractors in this module use.
|
|
487
519
|
*/
|
|
488
520
|
export function decodeJwtPayload(token: string): Record<string, unknown> {
|
|
489
521
|
const parts = token.split(".")
|
|
@@ -503,7 +535,19 @@ export function decodeJwtPayload(token: string): Record<string, unknown> {
|
|
|
503
535
|
Uint8Array.from(atob(padded), c => c.charCodeAt(0)),
|
|
504
536
|
)
|
|
505
537
|
: Buffer.from(padded, "base64").toString("utf-8")
|
|
506
|
-
|
|
538
|
+
const payload: unknown = JSON.parse(json)
|
|
539
|
+
// `JSON.parse` happily returns a number, string, boolean, array or null, and
|
|
540
|
+
// asserting any of those to Record<string, unknown> would be a lie that only
|
|
541
|
+
// shows up as `undefined` at the claim read. Rejecting them here is what lets
|
|
542
|
+
// tryDecodeJwtPayload returning null be the complete "unreadable" signal.
|
|
543
|
+
if (
|
|
544
|
+
typeof payload !== "object" ||
|
|
545
|
+
payload === null ||
|
|
546
|
+
Array.isArray(payload)
|
|
547
|
+
) {
|
|
548
|
+
throw new Error("JWT payload is not an object")
|
|
549
|
+
}
|
|
550
|
+
return payload as Record<string, unknown>
|
|
507
551
|
}
|
|
508
552
|
|
|
509
553
|
function randomUrlSafeString(byteLength: number): string {
|
package/src/auth/siwx.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
OperationResponse,
|
|
4
4
|
} from "@opensea/api-types"
|
|
5
5
|
import { getAddress } from "ethers"
|
|
6
|
+
import { type FetchImpl, fetchWith } from "../utils/fetchTransport"
|
|
6
7
|
import type { AuthSigner } from "./types"
|
|
7
8
|
|
|
8
9
|
const DEFAULT_API_BASE_URL = "https://api.opensea.io"
|
|
@@ -55,6 +56,14 @@ export interface LinkWalletWithSiwxOptions
|
|
|
55
56
|
authToken: string
|
|
56
57
|
chainArch: ChainArch
|
|
57
58
|
chainId: number
|
|
59
|
+
/**
|
|
60
|
+
* Transport used for the nonce request and the link request, defaulting to the global `fetch`.
|
|
61
|
+
* Same seam as {@link OpenSeaAPIConfig.fetch}.
|
|
62
|
+
*
|
|
63
|
+
* It sees the auth token, the API key and the wallet signature, so anything it logs or caches
|
|
64
|
+
* is sensitive.
|
|
65
|
+
*/
|
|
66
|
+
fetch?: FetchImpl
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
const DOMAIN_REGEX =
|
|
@@ -253,11 +262,18 @@ export function parseSiwxMessage(message: string): ParseSiwxMessageResult {
|
|
|
253
262
|
|
|
254
263
|
/**
|
|
255
264
|
* Request a single-use nonce from the OpenSea API.
|
|
265
|
+
*
|
|
266
|
+
* @param apiBaseUrl Base URL to request the nonce from. Defaults to mainnet.
|
|
267
|
+
* @param options Optional `fetch` transport, defaulting to the global `fetch`. Same seam as
|
|
268
|
+
* {@link OpenSeaAPIConfig.fetch}, for a caller routing every request through
|
|
269
|
+
* its own transport.
|
|
256
270
|
*/
|
|
257
271
|
export async function requestSiwxNonce(
|
|
258
272
|
apiBaseUrl = DEFAULT_API_BASE_URL,
|
|
273
|
+
options: { fetch?: FetchImpl } = {},
|
|
259
274
|
): Promise<string> {
|
|
260
|
-
const response = await
|
|
275
|
+
const response = await fetchWith(
|
|
276
|
+
options.fetch,
|
|
261
277
|
`${stripTrailingSlash(apiBaseUrl)}/api/v2/auth/siwe/nonce`,
|
|
262
278
|
{
|
|
263
279
|
method: "POST",
|
|
@@ -290,7 +306,7 @@ export async function linkWalletWithSiwx(
|
|
|
290
306
|
const apiBaseUrl = stripTrailingSlash(
|
|
291
307
|
options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
|
|
292
308
|
)
|
|
293
|
-
const nonce = await requestSiwxNonce(apiBaseUrl)
|
|
309
|
+
const nonce = await requestSiwxNonce(apiBaseUrl, { fetch: options.fetch })
|
|
294
310
|
const address = await signer.getAddress()
|
|
295
311
|
const message = createSiwxMessage({
|
|
296
312
|
address,
|
|
@@ -315,15 +331,19 @@ export async function linkWalletWithSiwx(
|
|
|
315
331
|
chainArch: options.chainArch,
|
|
316
332
|
}
|
|
317
333
|
|
|
318
|
-
const response = await
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
334
|
+
const response = await fetchWith(
|
|
335
|
+
options.fetch,
|
|
336
|
+
`${apiBaseUrl}/api/v2/accounts/wallets/siwx`,
|
|
337
|
+
{
|
|
338
|
+
method: "POST",
|
|
339
|
+
headers: {
|
|
340
|
+
Authorization: `Bearer ${options.authToken}`,
|
|
341
|
+
"Content-Type": "application/json",
|
|
342
|
+
...(options.apiKey ? { "X-API-KEY": options.apiKey } : {}),
|
|
343
|
+
},
|
|
344
|
+
body: JSON.stringify(payload),
|
|
324
345
|
},
|
|
325
|
-
|
|
326
|
-
})
|
|
346
|
+
)
|
|
327
347
|
if (!response.ok) {
|
|
328
348
|
const body = await response.text().catch(() => "")
|
|
329
349
|
throw new Error(
|
package/src/auth/types.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { FetchImpl } from "../utils/fetchTransport"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* A scoped JWT token returned by the auth server.
|
|
3
5
|
*/
|
|
@@ -28,6 +30,16 @@ export interface OpenSeaAuthConfig {
|
|
|
28
30
|
apiBaseUrl?: string
|
|
29
31
|
/** @deprecated Use `apiBaseUrl`. */
|
|
30
32
|
authBaseUrl?: string
|
|
33
|
+
/**
|
|
34
|
+
* Transport used for every request this instance makes, defaulting to the global `fetch`.
|
|
35
|
+
* Same seam as {@link OpenSeaAPIConfig.fetch}, so one wrapper can cover the auth flow and the
|
|
36
|
+
* API client.
|
|
37
|
+
*
|
|
38
|
+
* It sees the session cookie, the scoped token and the exchanged JWT, so anything it logs or
|
|
39
|
+
* caches is a credential. Nothing here is a cacheable representation: every call either
|
|
40
|
+
* mints, exchanges or revokes a token.
|
|
41
|
+
*/
|
|
42
|
+
fetch?: FetchImpl
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
/**
|