@opensea/sdk 11.1.1 → 11.3.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/lib/api/accounts.d.ts +13 -1
- package/lib/api/accounts.js +18 -0
- package/lib/api/accounts.js.map +1 -1
- package/lib/api/api.d.ts +14 -1
- package/lib/api/api.js +22 -0
- package/lib/api/api.js.map +1 -1
- package/lib/api/apiPaths.d.ts +3 -0
- package/lib/api/apiPaths.js +13 -1
- package/lib/api/apiPaths.js.map +1 -1
- package/lib/api/types.d.ts +31 -1
- package/lib/auth/index.d.ts +67 -0
- package/lib/auth/index.js +174 -0
- package/lib/auth/index.js.map +1 -0
- package/lib/auth/oauth-types.d.ts +69 -0
- package/lib/auth/oauth-types.js +3 -0
- package/lib/auth/oauth-types.js.map +1 -0
- package/lib/auth/oauth.d.ts +109 -0
- package/lib/auth/oauth.js +342 -0
- package/lib/auth/oauth.js.map +1 -0
- package/lib/auth/siwx.d.ts +63 -0
- package/lib/auth/siwx.js +276 -0
- package/lib/auth/siwx.js.map +1 -0
- package/lib/auth/types.d.ts +50 -0
- package/lib/auth/types.js +3 -0
- package/lib/auth/types.js.map +1 -0
- package/lib/index.d.ts +7 -1
- package/lib/index.js +9 -1
- package/lib/index.js.map +1 -1
- package/lib/scopes.d.ts +29 -0
- package/lib/scopes.js +27 -0
- package/lib/scopes.js.map +1 -0
- package/lib/sdk/base.d.ts +1 -0
- package/lib/sdk/base.js +6 -3
- package/lib/sdk/base.js.map +1 -1
- package/lib/sdk/cancellation.js +33 -23
- package/lib/sdk/cancellation.js.map +1 -1
- package/lib/types.d.ts +5 -1
- package/lib/types.js +1 -0
- package/lib/types.js.map +1 -1
- package/lib/utils/chain.d.ts +1 -1
- package/lib/utils/chain.js +30 -5
- package/lib/utils/chain.js.map +1 -1
- package/lib/utils/chainIds.generated.js +1 -0
- package/lib/utils/chainIds.generated.js.map +1 -1
- package/lib/utils/units.js +9 -5
- package/lib/utils/units.js.map +1 -1
- package/lib/viem.d.ts +1 -0
- package/lib/viem.js +1 -0
- package/lib/viem.js.map +1 -1
- package/package.json +7 -7
- package/src/api/accounts.ts +41 -0
- package/src/api/api.ts +36 -0
- package/src/api/apiPaths.ts +12 -0
- package/src/api/types.ts +36 -0
- package/src/auth/index.ts +244 -0
- package/src/auth/oauth-types.ts +73 -0
- package/src/auth/oauth.ts +428 -0
- package/src/auth/siwx.ts +400 -0
- package/src/auth/types.ts +55 -0
- package/src/index.ts +10 -0
- package/src/scopes.ts +39 -0
- package/src/sdk/base.ts +6 -4
- package/src/sdk/cancellation.ts +39 -25
- package/src/types.ts +4 -0
- package/src/utils/chain.ts +37 -5
- package/src/utils/chainIds.generated.ts +1 -0
- package/src/utils/units.ts +9 -5
- package/src/viem.ts +1 -0
package/src/auth/siwx.ts
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
OperationRequestBody,
|
|
3
|
+
OperationResponse,
|
|
4
|
+
} from "@opensea/api-types"
|
|
5
|
+
import { getAddress } from "ethers"
|
|
6
|
+
import type { AuthSigner } from "./types"
|
|
7
|
+
|
|
8
|
+
const DEFAULT_AUTH_BASE_URL = "https://auth.opensea.io"
|
|
9
|
+
const DEFAULT_API_BASE_URL = "https://api.opensea.io"
|
|
10
|
+
const DEFAULT_STATEMENT =
|
|
11
|
+
"Click to sign in and accept the OpenSea Terms of Service (https://opensea.io/tos) and Privacy Policy (https://opensea.io/privacy)."
|
|
12
|
+
|
|
13
|
+
export type ChainArch = "EVM" | "SVM" | "BITCOIN"
|
|
14
|
+
export type AccountType = "Ethereum" | "Solana" | "Bitcoin"
|
|
15
|
+
|
|
16
|
+
export interface CreateSiwxMessageOptions {
|
|
17
|
+
address: string
|
|
18
|
+
chainId: number
|
|
19
|
+
nonce: string
|
|
20
|
+
domain?: string
|
|
21
|
+
uri?: string
|
|
22
|
+
version?: "1"
|
|
23
|
+
statement?: string
|
|
24
|
+
issuedAt?: Date
|
|
25
|
+
expirationTime?: Date
|
|
26
|
+
notBefore?: Date
|
|
27
|
+
requestId?: string
|
|
28
|
+
resources?: string[]
|
|
29
|
+
scheme?: string
|
|
30
|
+
chainArch?: ChainArch
|
|
31
|
+
accountType?: AccountType
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ParseSiwxMessageResult {
|
|
35
|
+
domain: string
|
|
36
|
+
address: string
|
|
37
|
+
statement: string
|
|
38
|
+
uri: string
|
|
39
|
+
version: string
|
|
40
|
+
chainId: string
|
|
41
|
+
nonce: string
|
|
42
|
+
issuedAt: string
|
|
43
|
+
expirationTime?: string
|
|
44
|
+
notBefore?: string
|
|
45
|
+
requestId?: string
|
|
46
|
+
resources?: string[]
|
|
47
|
+
accountType?: AccountType
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface LinkWalletWithSiwxOptions
|
|
51
|
+
extends Omit<CreateSiwxMessageOptions, "address" | "chainId" | "nonce"> {
|
|
52
|
+
authBaseUrl?: string
|
|
53
|
+
apiBaseUrl?: string
|
|
54
|
+
authToken: string
|
|
55
|
+
chainArch: ChainArch
|
|
56
|
+
chainId: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const DOMAIN_REGEX =
|
|
60
|
+
/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$/
|
|
61
|
+
const IP_REGEX =
|
|
62
|
+
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/
|
|
63
|
+
const LOCALHOST_REGEX = /^localhost(:[0-9]{1,5})?$/
|
|
64
|
+
const NONCE_REGEX = /^[a-zA-Z0-9]{8,}$/
|
|
65
|
+
const SCHEME_REGEX = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/
|
|
66
|
+
const FIELD_PREFIXES = {
|
|
67
|
+
uri: "URI: ",
|
|
68
|
+
version: "Version: ",
|
|
69
|
+
chainId: "Chain ID: ",
|
|
70
|
+
nonce: "Nonce: ",
|
|
71
|
+
issuedAt: "Issued At: ",
|
|
72
|
+
expirationTime: "Expiration Time: ",
|
|
73
|
+
notBefore: "Not Before: ",
|
|
74
|
+
requestId: "Request ID: ",
|
|
75
|
+
} as const
|
|
76
|
+
|
|
77
|
+
type FieldKey = keyof typeof FIELD_PREFIXES
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Translate a chain architecture to the SIWX account type prefix.
|
|
81
|
+
*/
|
|
82
|
+
export function chainArchToAccountType(
|
|
83
|
+
chainArch: ChainArch,
|
|
84
|
+
): AccountType | undefined {
|
|
85
|
+
switch (chainArch) {
|
|
86
|
+
case "EVM":
|
|
87
|
+
return "Ethereum"
|
|
88
|
+
case "SVM":
|
|
89
|
+
return "Solana"
|
|
90
|
+
case "BITCOIN":
|
|
91
|
+
return "Bitcoin"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Build an EIP-4361/SIWX message for OpenSea wallet linking.
|
|
97
|
+
*/
|
|
98
|
+
export function createSiwxMessage(options: CreateSiwxMessageOptions): string {
|
|
99
|
+
const accountType =
|
|
100
|
+
options.accountType ??
|
|
101
|
+
(options.chainArch ? chainArchToAccountType(options.chainArch) : undefined)
|
|
102
|
+
const {
|
|
103
|
+
address,
|
|
104
|
+
chainId,
|
|
105
|
+
domain = "opensea.io",
|
|
106
|
+
expirationTime,
|
|
107
|
+
issuedAt = new Date(),
|
|
108
|
+
nonce,
|
|
109
|
+
notBefore,
|
|
110
|
+
requestId,
|
|
111
|
+
resources,
|
|
112
|
+
scheme,
|
|
113
|
+
uri = "https://opensea.io",
|
|
114
|
+
version = "1",
|
|
115
|
+
} = options
|
|
116
|
+
|
|
117
|
+
validateFields({
|
|
118
|
+
chainId,
|
|
119
|
+
domain,
|
|
120
|
+
nonce,
|
|
121
|
+
scheme,
|
|
122
|
+
statement: options.statement,
|
|
123
|
+
uri,
|
|
124
|
+
version,
|
|
125
|
+
resources,
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const formattedAddress =
|
|
129
|
+
accountType === "Ethereum" ? getAddress(address) : address
|
|
130
|
+
const origin = scheme ? `${scheme}://${domain}` : domain
|
|
131
|
+
const statement = options.statement ? `${options.statement}\n` : ""
|
|
132
|
+
const accountTypePrefix = accountType ? `${accountType} ` : ""
|
|
133
|
+
const prefix = `${origin} wants you to sign in with your ${accountTypePrefix}account:\n${formattedAddress}\n\n${statement}`
|
|
134
|
+
|
|
135
|
+
let suffix = `URI: ${uri}\nVersion: ${version}\nChain ID: ${chainId}\nNonce: ${nonce}\nIssued At: ${issuedAt.toISOString()}`
|
|
136
|
+
if (expirationTime) {
|
|
137
|
+
suffix += `\nExpiration Time: ${expirationTime.toISOString()}`
|
|
138
|
+
}
|
|
139
|
+
if (notBefore) {
|
|
140
|
+
suffix += `\nNot Before: ${notBefore.toISOString()}`
|
|
141
|
+
}
|
|
142
|
+
if (requestId) {
|
|
143
|
+
suffix += `\nRequest ID: ${requestId}`
|
|
144
|
+
}
|
|
145
|
+
if (resources && resources.length > 0) {
|
|
146
|
+
suffix += "\nResources:"
|
|
147
|
+
for (const resource of resources) {
|
|
148
|
+
suffix += `\n- ${resource}`
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return `${prefix}\n${suffix}`
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Parse a SIWX message into the JSON body expected by the wallet-link API.
|
|
157
|
+
*/
|
|
158
|
+
export function parseSiwxMessage(message: string): ParseSiwxMessageResult {
|
|
159
|
+
const lines = message.split("\n")
|
|
160
|
+
const domainMatch =
|
|
161
|
+
/^(?<domain>[^ ]+) wants you to sign in with your (?:(?<accountType>Ethereum|Solana|Bitcoin) )?account:$/.exec(
|
|
162
|
+
lines[0] ?? "",
|
|
163
|
+
)
|
|
164
|
+
const addressMatch = /^(?<address>.+)$/.exec(lines[1] ?? "")
|
|
165
|
+
if (!domainMatch || !addressMatch) {
|
|
166
|
+
throw new Error("Invalid SIWX message")
|
|
167
|
+
}
|
|
168
|
+
if (domainMatch.groups == null || addressMatch.groups == null) {
|
|
169
|
+
throw new Error("Invalid SIWX message")
|
|
170
|
+
}
|
|
171
|
+
const { domain, accountType } = domainMatch.groups
|
|
172
|
+
const { address } = addressMatch.groups
|
|
173
|
+
|
|
174
|
+
const fields: Record<FieldKey, string> = {
|
|
175
|
+
uri: "",
|
|
176
|
+
version: "",
|
|
177
|
+
chainId: "",
|
|
178
|
+
nonce: "",
|
|
179
|
+
issuedAt: "",
|
|
180
|
+
expirationTime: "",
|
|
181
|
+
notBefore: "",
|
|
182
|
+
requestId: "",
|
|
183
|
+
}
|
|
184
|
+
const resources: string[] = []
|
|
185
|
+
|
|
186
|
+
let firstFieldLineIndex = lines.length
|
|
187
|
+
for (let i = 2; i < lines.length; i += 1) {
|
|
188
|
+
const line = lines[i]
|
|
189
|
+
for (const key of Object.keys(FIELD_PREFIXES) as FieldKey[]) {
|
|
190
|
+
const prefix = FIELD_PREFIXES[key]
|
|
191
|
+
if (fields[key] === "" && line.startsWith(prefix)) {
|
|
192
|
+
fields[key] = line.slice(prefix.length)
|
|
193
|
+
firstFieldLineIndex = Math.min(firstFieldLineIndex, i)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (line === "Resources:") {
|
|
197
|
+
firstFieldLineIndex = Math.min(firstFieldLineIndex, i)
|
|
198
|
+
for (let j = i + 1; j < lines.length; j += 1) {
|
|
199
|
+
const resourceLine = lines[j]
|
|
200
|
+
if (resourceLine.startsWith("- ")) {
|
|
201
|
+
resources.push(resourceLine.slice(2))
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
break
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const statementLines = lines.slice(2, firstFieldLineIndex)
|
|
209
|
+
while (statementLines.length > 0 && statementLines[0] === "") {
|
|
210
|
+
statementLines.shift()
|
|
211
|
+
}
|
|
212
|
+
while (statementLines.length > 0 && statementLines.at(-1) === "") {
|
|
213
|
+
statementLines.pop()
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
domain,
|
|
218
|
+
address,
|
|
219
|
+
statement: statementLines.join("\n"),
|
|
220
|
+
uri: fields.uri,
|
|
221
|
+
version: fields.version,
|
|
222
|
+
chainId: fields.chainId,
|
|
223
|
+
nonce: fields.nonce,
|
|
224
|
+
issuedAt: fields.issuedAt,
|
|
225
|
+
...(fields.expirationTime
|
|
226
|
+
? {
|
|
227
|
+
expirationTime: fields.expirationTime,
|
|
228
|
+
}
|
|
229
|
+
: {}),
|
|
230
|
+
...(fields.notBefore
|
|
231
|
+
? {
|
|
232
|
+
notBefore: fields.notBefore,
|
|
233
|
+
}
|
|
234
|
+
: {}),
|
|
235
|
+
...(fields.requestId
|
|
236
|
+
? {
|
|
237
|
+
requestId: fields.requestId,
|
|
238
|
+
}
|
|
239
|
+
: {}),
|
|
240
|
+
...(resources.length > 0
|
|
241
|
+
? {
|
|
242
|
+
resources,
|
|
243
|
+
}
|
|
244
|
+
: {}),
|
|
245
|
+
...(accountType
|
|
246
|
+
? {
|
|
247
|
+
accountType: accountType as AccountType,
|
|
248
|
+
}
|
|
249
|
+
: {}),
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Request a single-use nonce from the auth service.
|
|
255
|
+
*/
|
|
256
|
+
export async function requestSiwxNonce(
|
|
257
|
+
authBaseUrl = DEFAULT_AUTH_BASE_URL,
|
|
258
|
+
): Promise<string> {
|
|
259
|
+
const response = await fetch(`${stripTrailingSlash(authBaseUrl)}/api/nonce`, {
|
|
260
|
+
method: "GET",
|
|
261
|
+
headers: { Accept: "application/json" },
|
|
262
|
+
})
|
|
263
|
+
if (!response.ok) {
|
|
264
|
+
throw new Error(
|
|
265
|
+
`Auth server error (${response.status}): ${response.statusText}`,
|
|
266
|
+
)
|
|
267
|
+
}
|
|
268
|
+
const data = (await response.json()) as { nonce: string }
|
|
269
|
+
return data.nonce
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Fetch, sign, and submit a wallet-link request.
|
|
274
|
+
*/
|
|
275
|
+
export async function linkWalletWithSiwx(
|
|
276
|
+
signer: AuthSigner,
|
|
277
|
+
options: LinkWalletWithSiwxOptions,
|
|
278
|
+
): Promise<OperationResponse<"link_wallet_with_siwx">> {
|
|
279
|
+
if (
|
|
280
|
+
typeof options.authToken !== "string" ||
|
|
281
|
+
options.authToken.trim() === ""
|
|
282
|
+
) {
|
|
283
|
+
throw new Error("authToken is required to link a wallet")
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const authBaseUrl = stripTrailingSlash(
|
|
287
|
+
options.authBaseUrl ?? DEFAULT_AUTH_BASE_URL,
|
|
288
|
+
)
|
|
289
|
+
const apiBaseUrl = stripTrailingSlash(
|
|
290
|
+
options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
|
|
291
|
+
)
|
|
292
|
+
const nonce = await requestSiwxNonce(authBaseUrl)
|
|
293
|
+
const address = await signer.getAddress()
|
|
294
|
+
const message = createSiwxMessage({
|
|
295
|
+
address,
|
|
296
|
+
chainArch: options.chainArch,
|
|
297
|
+
chainId: options.chainId,
|
|
298
|
+
nonce,
|
|
299
|
+
domain: options.domain,
|
|
300
|
+
uri: options.uri,
|
|
301
|
+
version: options.version,
|
|
302
|
+
statement: options.statement ?? DEFAULT_STATEMENT,
|
|
303
|
+
issuedAt: options.issuedAt,
|
|
304
|
+
expirationTime: options.expirationTime,
|
|
305
|
+
notBefore: options.notBefore,
|
|
306
|
+
requestId: options.requestId,
|
|
307
|
+
resources: options.resources,
|
|
308
|
+
scheme: options.scheme,
|
|
309
|
+
})
|
|
310
|
+
const signature = await signer.signMessage(message)
|
|
311
|
+
const payload: OperationRequestBody<"link_wallet_with_siwx"> = {
|
|
312
|
+
message: parseSiwxMessage(message),
|
|
313
|
+
signature,
|
|
314
|
+
chainArch: options.chainArch,
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const response = await fetch(`${apiBaseUrl}/api/v2/accounts/wallets/siwx`, {
|
|
318
|
+
method: "POST",
|
|
319
|
+
headers: {
|
|
320
|
+
Authorization: `Bearer ${options.authToken}`,
|
|
321
|
+
"Content-Type": "application/json",
|
|
322
|
+
},
|
|
323
|
+
body: JSON.stringify(payload),
|
|
324
|
+
})
|
|
325
|
+
if (!response.ok) {
|
|
326
|
+
const body = await response.text().catch(() => "")
|
|
327
|
+
throw new Error(
|
|
328
|
+
`Wallet link failed (${response.status}): ${body || response.statusText}`,
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
return response.json() as Promise<OperationResponse<"link_wallet_with_siwx">>
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function validateFields({
|
|
335
|
+
chainId,
|
|
336
|
+
domain,
|
|
337
|
+
nonce,
|
|
338
|
+
scheme,
|
|
339
|
+
statement,
|
|
340
|
+
uri,
|
|
341
|
+
version,
|
|
342
|
+
resources,
|
|
343
|
+
}: {
|
|
344
|
+
chainId: number
|
|
345
|
+
domain: string
|
|
346
|
+
nonce: string
|
|
347
|
+
scheme?: string
|
|
348
|
+
statement?: string
|
|
349
|
+
uri: string
|
|
350
|
+
version: string
|
|
351
|
+
resources?: string[]
|
|
352
|
+
}) {
|
|
353
|
+
if (chainId !== Math.floor(chainId)) {
|
|
354
|
+
throw new Error("Chain ID must be a whole number")
|
|
355
|
+
}
|
|
356
|
+
if (
|
|
357
|
+
!(
|
|
358
|
+
DOMAIN_REGEX.test(domain) ||
|
|
359
|
+
IP_REGEX.test(domain) ||
|
|
360
|
+
LOCALHOST_REGEX.test(domain)
|
|
361
|
+
)
|
|
362
|
+
) {
|
|
363
|
+
throw new Error(`Invalid domain: ${domain}`)
|
|
364
|
+
}
|
|
365
|
+
if (!NONCE_REGEX.test(nonce)) {
|
|
366
|
+
throw new Error(`Invalid nonce: ${nonce}`)
|
|
367
|
+
}
|
|
368
|
+
if (scheme && !SCHEME_REGEX.test(scheme)) {
|
|
369
|
+
throw new Error(`Invalid scheme: ${scheme}`)
|
|
370
|
+
}
|
|
371
|
+
if (!isUri(uri)) {
|
|
372
|
+
throw new Error(`Invalid uri: ${uri}`)
|
|
373
|
+
}
|
|
374
|
+
if (version !== "1") {
|
|
375
|
+
throw new Error(`Invalid version: ${version}`)
|
|
376
|
+
}
|
|
377
|
+
if (statement?.includes("\n")) {
|
|
378
|
+
throw new Error("Statement must not include newlines")
|
|
379
|
+
}
|
|
380
|
+
if (resources) {
|
|
381
|
+
for (const resource of resources) {
|
|
382
|
+
if (!isUri(resource)) {
|
|
383
|
+
throw new Error(`Invalid resource URI: ${resource}`)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function isUri(value: string): boolean {
|
|
390
|
+
try {
|
|
391
|
+
new URL(value)
|
|
392
|
+
return true
|
|
393
|
+
} catch {
|
|
394
|
+
return false
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function stripTrailingSlash(value: string): string {
|
|
399
|
+
return value.replace(/\/$/, "")
|
|
400
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A scoped JWT token returned by the auth server.
|
|
3
|
+
*/
|
|
4
|
+
export interface AuthToken {
|
|
5
|
+
/** JWT access token */
|
|
6
|
+
accessToken: string
|
|
7
|
+
/** Refresh token for obtaining a new access token */
|
|
8
|
+
refreshToken: string
|
|
9
|
+
/** When the access token expires */
|
|
10
|
+
expiresAt: Date
|
|
11
|
+
/** Scopes granted to this token */
|
|
12
|
+
scopes: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for the {@link OpenSeaAuth.authenticate} method.
|
|
17
|
+
*/
|
|
18
|
+
export interface AuthenticateOptions {
|
|
19
|
+
/** Scopes to request (e.g. `["read:eligibility", "write:orders"]`) */
|
|
20
|
+
scopes?: string[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Configuration for the {@link OpenSeaAuth} class.
|
|
25
|
+
*/
|
|
26
|
+
export interface OpenSeaAuthConfig {
|
|
27
|
+
/** Auth server base URL. Defaults to `https://auth.opensea.io`. */
|
|
28
|
+
authBaseUrl?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Signer interface compatible with both ethers and viem signers.
|
|
33
|
+
* Requires `getAddress()` and `signMessage(message)`.
|
|
34
|
+
*/
|
|
35
|
+
export interface AuthSigner {
|
|
36
|
+
getAddress(): Promise<string>
|
|
37
|
+
signMessage(message: string): Promise<string>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Raw token response from the auth server `/api/token` and `/api/refresh` endpoints.
|
|
42
|
+
*/
|
|
43
|
+
export interface AuthTokenResponse {
|
|
44
|
+
access_token: string
|
|
45
|
+
refresh_token: string
|
|
46
|
+
expires_in: number
|
|
47
|
+
scopes: string[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Nonce response from the auth server `/api/nonce` endpoint.
|
|
52
|
+
*/
|
|
53
|
+
export interface AuthNonceResponse {
|
|
54
|
+
nonce: string
|
|
55
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { OpenSeaAPI } from "./api/api"
|
|
2
|
+
import { OpenSeaAuth } from "./auth"
|
|
3
|
+
import { OpenSeaOAuth } from "./auth/oauth"
|
|
2
4
|
import { OpenSeaSDK } from "./sdk"
|
|
3
5
|
|
|
4
6
|
export * from "./api/types"
|
|
7
|
+
export * from "./auth"
|
|
8
|
+
export * from "./auth/oauth"
|
|
9
|
+
export * from "./auth/oauth-types"
|
|
5
10
|
export * from "./constants"
|
|
6
11
|
export * from "./orders/types"
|
|
7
12
|
export type {
|
|
@@ -11,6 +16,7 @@ export type {
|
|
|
11
16
|
OpenSeaWallet,
|
|
12
17
|
TransactionResponse,
|
|
13
18
|
} from "./provider/types"
|
|
19
|
+
export * from "./scopes"
|
|
14
20
|
export * from "./types"
|
|
15
21
|
export * from "./utils"
|
|
16
22
|
/**
|
|
@@ -29,6 +35,10 @@ export {
|
|
|
29
35
|
// API client (for direct use or accessing static helpers like
|
|
30
36
|
// `OpenSeaAPI.requestInstantApiKey()`)
|
|
31
37
|
OpenSeaAPI,
|
|
38
|
+
// Auth helper for SIWE-based wallet authentication
|
|
39
|
+
OpenSeaAuth,
|
|
40
|
+
// OAuth 2.1 (code + PKCE / device) helper for keyless login
|
|
41
|
+
OpenSeaOAuth,
|
|
32
42
|
// Main SDK export
|
|
33
43
|
OpenSeaSDK,
|
|
34
44
|
}
|
package/src/scopes.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { AuthScope } from "@opensea/api-types"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OAuth-style scopes recognized by the OpenSea API.
|
|
5
|
+
*
|
|
6
|
+
* The set of valid scopes is defined by the `AuthScope` schema in the
|
|
7
|
+
* OpenAPI spec (via `@opensea/api-types`). The compile-time assertions
|
|
8
|
+
* below fail the build if this runtime constant drifts from the spec
|
|
9
|
+
* in either direction.
|
|
10
|
+
* @category Auth
|
|
11
|
+
*/
|
|
12
|
+
export const OPENSEA_SCOPES = {
|
|
13
|
+
READ_ELIGIBILITY: "read:eligibility",
|
|
14
|
+
READ_FAVORITES: "read:favorites",
|
|
15
|
+
READ_REWARDS: "read:rewards",
|
|
16
|
+
WRITE_ORDERS: "write:orders",
|
|
17
|
+
WRITE_DROPS: "write:drops",
|
|
18
|
+
WRITE_WALLETS: "write:wallets",
|
|
19
|
+
} as const satisfies Record<string, AuthScope>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Union of all valid OpenSea API scope strings, derived from the
|
|
23
|
+
* OpenAPI spec's `AuthScope` schema.
|
|
24
|
+
* @category Auth
|
|
25
|
+
*/
|
|
26
|
+
export type OpenSeaScope = AuthScope
|
|
27
|
+
|
|
28
|
+
type _AssertAllAuthScopesCovered =
|
|
29
|
+
AuthScope extends (typeof OPENSEA_SCOPES)[keyof typeof OPENSEA_SCOPES]
|
|
30
|
+
? true
|
|
31
|
+
: never
|
|
32
|
+
|
|
33
|
+
const _assertAllAuthScopesCovered: _AssertAllAuthScopesCovered = true
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* All OpenSea API scopes as a readonly list.
|
|
37
|
+
* @category Auth
|
|
38
|
+
*/
|
|
39
|
+
export const ALL_SCOPES: readonly OpenSeaScope[] = Object.values(OPENSEA_SCOPES)
|
package/src/sdk/base.ts
CHANGED
|
@@ -585,15 +585,17 @@ export class BaseOpenSeaSDK {
|
|
|
585
585
|
}
|
|
586
586
|
}
|
|
587
587
|
|
|
588
|
-
|
|
589
|
-
const basePrice = amountWei
|
|
590
|
-
|
|
591
|
-
if (amount == null || amountWei < 0) {
|
|
588
|
+
if (amount == null) {
|
|
592
589
|
throw new Error("Starting price must be a number >= 0")
|
|
593
590
|
}
|
|
594
591
|
if (isEther && orderSide === OrderSide.OFFER) {
|
|
595
592
|
throw new Error("Offers must use wrapped ETH or an ERC-20 token.")
|
|
596
593
|
}
|
|
594
|
+
const amountWei = parseUnits(amount.toString(), decimals)
|
|
595
|
+
const basePrice = amountWei
|
|
596
|
+
if (amountWei < 0) {
|
|
597
|
+
throw new Error("Starting price must be a number >= 0")
|
|
598
|
+
}
|
|
597
599
|
return { basePrice }
|
|
598
600
|
}
|
|
599
601
|
|
package/src/sdk/cancellation.ts
CHANGED
|
@@ -167,39 +167,35 @@ export class CancellationManager {
|
|
|
167
167
|
throw new Error("At least one order hash must be provided")
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
-
|
|
170
|
+
let orderComponents: OrderComponents[]
|
|
171
|
+
const protocolAddresses = new Set<string>()
|
|
172
|
+
let firstOrderV2: OrderV2 | undefined
|
|
173
|
+
let firstFetchedOrder: Offer | Listing | undefined
|
|
171
174
|
|
|
172
|
-
|
|
173
|
-
await this.context.requireAccountIsAvailable(accountAddress)
|
|
175
|
+
requireValidProtocol(protocolAddress)
|
|
174
176
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
+
const addProtocolAddress = (value: string) => {
|
|
178
|
+
requireValidProtocol(value)
|
|
179
|
+
protocolAddresses.add(value)
|
|
180
|
+
}
|
|
177
181
|
|
|
178
182
|
if (orders) {
|
|
179
183
|
// Extract OrderComponents from either OrderV2 objects or use OrderComponents directly
|
|
180
|
-
let firstOrderV2: OrderV2 | undefined
|
|
181
184
|
orderComponents = orders.map(order => {
|
|
182
185
|
if ("protocolData" in order) {
|
|
183
186
|
// It's an OrderV2 object
|
|
184
187
|
const orderV2 = order as OrderV2
|
|
185
|
-
|
|
186
|
-
effectiveProtocolAddress = orderV2.protocolAddress
|
|
188
|
+
addProtocolAddress(orderV2.protocolAddress)
|
|
187
189
|
if (!firstOrderV2) {
|
|
188
190
|
firstOrderV2 = orderV2
|
|
189
191
|
}
|
|
190
192
|
return orderV2.protocolData.parameters
|
|
191
193
|
} else {
|
|
192
194
|
// It's already OrderComponents
|
|
195
|
+
addProtocolAddress(protocolAddress)
|
|
193
196
|
return order as OrderComponents
|
|
194
197
|
}
|
|
195
198
|
})
|
|
196
|
-
// Dispatch event for the first OrderV2 if available
|
|
197
|
-
if (firstOrderV2) {
|
|
198
|
-
this.context.dispatch(EventType.CancelOrder, {
|
|
199
|
-
orderV2: firstOrderV2,
|
|
200
|
-
accountAddress,
|
|
201
|
-
})
|
|
202
|
-
}
|
|
203
199
|
} else if (orderHashes) {
|
|
204
200
|
// Fetch orders from the API using order hashes
|
|
205
201
|
const fetchedOrders: (Offer | Listing)[] = []
|
|
@@ -219,23 +215,41 @@ export class CancellationManager {
|
|
|
219
215
|
`Order ${fetched.orderHash} is missing protocolAddress or protocolData — cannot cancel.`,
|
|
220
216
|
)
|
|
221
217
|
}
|
|
222
|
-
|
|
223
|
-
effectiveProtocolAddress = fetched.protocolAddress
|
|
218
|
+
addProtocolAddress(fetched.protocolAddress)
|
|
224
219
|
return fetched.protocolData.parameters
|
|
225
220
|
})
|
|
226
|
-
|
|
227
|
-
// Dispatch event for the first fetched order
|
|
228
|
-
if (fetchedOrders.length > 0) {
|
|
229
|
-
this.context.dispatch(EventType.CancelOrder, {
|
|
230
|
-
order: fetchedOrders[0],
|
|
231
|
-
accountAddress,
|
|
232
|
-
})
|
|
233
|
-
}
|
|
221
|
+
firstFetchedOrder = fetchedOrders[0]
|
|
234
222
|
} else {
|
|
235
223
|
// Should never reach here due to earlier validation
|
|
236
224
|
throw new Error("Invalid input")
|
|
237
225
|
}
|
|
238
226
|
|
|
227
|
+
if (protocolAddresses.size > 1) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
"All orders in a cancelOrders batch must share the same protocolAddress. Cancel each protocol separately.",
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const effectiveProtocolAddress = protocolAddresses.values().next().value
|
|
234
|
+
if (effectiveProtocolAddress == null) {
|
|
235
|
+
throw new Error("Invalid input")
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Check account availability after parameter validation
|
|
239
|
+
await this.context.requireAccountIsAvailable(accountAddress)
|
|
240
|
+
|
|
241
|
+
if (firstOrderV2) {
|
|
242
|
+
this.context.dispatch(EventType.CancelOrder, {
|
|
243
|
+
orderV2: firstOrderV2,
|
|
244
|
+
accountAddress,
|
|
245
|
+
})
|
|
246
|
+
} else if (firstFetchedOrder) {
|
|
247
|
+
this.context.dispatch(EventType.CancelOrder, {
|
|
248
|
+
order: firstFetchedOrder,
|
|
249
|
+
accountAddress,
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
|
|
239
253
|
// Transact and get the transaction hash
|
|
240
254
|
const transactionHash = await this.cancelSeaportOrders({
|
|
241
255
|
orders: orderComponents,
|
package/src/types.ts
CHANGED
|
@@ -121,11 +121,14 @@ export interface EventData {
|
|
|
121
121
|
* @param chain `Chain` to use. Defaults to Ethereum Mainnet (`Chain.Mainnet`)
|
|
122
122
|
* @param apiKey API key to use
|
|
123
123
|
* @param apiBaseUrl Optional base URL to use for the API
|
|
124
|
+
* @param authToken Scoped JWT token for wallet-authenticated endpoints
|
|
124
125
|
*/
|
|
125
126
|
export interface OpenSeaAPIConfig {
|
|
126
127
|
chain?: Chain
|
|
127
128
|
apiKey?: string
|
|
128
129
|
apiBaseUrl?: string
|
|
130
|
+
/** Scoped JWT token for wallet-authenticated endpoints. */
|
|
131
|
+
authToken?: string
|
|
129
132
|
}
|
|
130
133
|
|
|
131
134
|
/**
|
|
@@ -164,6 +167,7 @@ export enum Chain {
|
|
|
164
167
|
Hyperliquid = "hyperliquid",
|
|
165
168
|
AnimeChain = "animechain",
|
|
166
169
|
Ink = "ink",
|
|
170
|
+
Robinhood = "robinhood",
|
|
167
171
|
}
|
|
168
172
|
|
|
169
173
|
// Compile-time check: every ChainIdentifier from the API spec must be assignable to Chain.
|