@portal-hq/utils 4.8.0 → 5.0.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/commonjs/credentials/index.js +260 -0
- package/lib/commonjs/errors/http-error.js +1 -0
- package/lib/commonjs/errors/index.js +7 -1
- package/lib/commonjs/errors/portal-auth-error.js +63 -0
- package/lib/commonjs/errors/portal-credential-error.js +58 -0
- package/lib/commonjs/errors/portal-error.js +13 -1
- package/lib/commonjs/events/index.js +2 -2
- package/lib/commonjs/index.js +17 -2
- package/lib/commonjs/requesters/http.js +33 -16
- package/lib/commonjs/version/version.json +1 -1
- package/lib/esm/credentials/index.js +249 -0
- package/lib/esm/errors/http-error.js +1 -0
- package/lib/esm/errors/index.js +2 -0
- package/lib/esm/errors/portal-auth-error.js +60 -0
- package/lib/esm/errors/portal-credential-error.js +55 -0
- package/lib/esm/errors/portal-error.js +11 -0
- package/lib/esm/events/index.js +2 -2
- package/lib/esm/index.js +2 -1
- package/lib/esm/requesters/http.js +33 -16
- package/lib/esm/version/version.json +1 -1
- package/package.json +2 -2
- package/src/credentials/index.ts +307 -0
- package/src/definitions/index.ts +40 -8
- package/src/errors/http-error.ts +5 -0
- package/src/errors/index.ts +10 -0
- package/src/errors/portal-auth-error.ts +86 -0
- package/src/errors/portal-credential-error.ts +80 -0
- package/src/errors/portal-error.ts +14 -0
- package/src/events/index.ts +3 -2
- package/src/index.ts +21 -0
- package/src/requesters/http.ts +35 -21
- package/src/version/version.json +1 -1
- package/types.d.ts +34 -25
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { type PortalCredentials } from '../definitions'
|
|
2
|
+
import {
|
|
3
|
+
InvalidApiKeyError,
|
|
4
|
+
PortalCredentialError,
|
|
5
|
+
PortalCredentialErrorReason,
|
|
6
|
+
type PortalError,
|
|
7
|
+
PortalMpcError,
|
|
8
|
+
isMpcAuthFailure,
|
|
9
|
+
} from '../errors'
|
|
10
|
+
import { sdkLogger } from '../logger'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Cross-copy brand. The key and the branded shape (a string `value`) are a
|
|
14
|
+
* compatibility contract across versions — see `PortalCredentialError`.
|
|
15
|
+
*/
|
|
16
|
+
const BRAND: unique symbol = Symbol.for('@portal-hq/utils/StaticCredentials')
|
|
17
|
+
|
|
18
|
+
export class StaticCredentials implements PortalCredentials {
|
|
19
|
+
/**
|
|
20
|
+
* Public only so {@link staticApiKeyOf} can bridge subsystems that read a
|
|
21
|
+
* synchronous `apiKey`. Prefer `getToken()`.
|
|
22
|
+
*/
|
|
23
|
+
public readonly value: string
|
|
24
|
+
|
|
25
|
+
/** @internal Cross-copy brand. Read it through {@link StaticCredentials.is}. */
|
|
26
|
+
public readonly [BRAND] = true
|
|
27
|
+
|
|
28
|
+
constructor(apiKey: string) {
|
|
29
|
+
this.value = apiKey
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Brand check, not `instanceof` — an instance from another copy of this
|
|
34
|
+
* package must still match. Reports the brand only: callers reading a member
|
|
35
|
+
* must re-check it, as {@link staticApiKeyOf} does.
|
|
36
|
+
*/
|
|
37
|
+
public static is(credentials: unknown): credentials is StaticCredentials {
|
|
38
|
+
return (
|
|
39
|
+
typeof credentials === 'object' &&
|
|
40
|
+
credentials !== null &&
|
|
41
|
+
(credentials as { [BRAND]?: unknown })[BRAND] === true
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getToken(): Promise<string> {
|
|
46
|
+
return Promise.resolve(this.value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** No-op: a Client API Key is custodian-owned and holds no local state to drop. */
|
|
50
|
+
public invalidate(): Promise<void> {
|
|
51
|
+
return Promise.resolve()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface CredentialSource {
|
|
56
|
+
/** A Client API Key. Mutually exclusive with `credentials`. */
|
|
57
|
+
apiKey?: string
|
|
58
|
+
/** Anything that can supply a credential — e.g. a `PortalAuth` session. */
|
|
59
|
+
credentials?: PortalCredentials
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Resolves the single credential source a component was constructed with.
|
|
64
|
+
*
|
|
65
|
+
* Throws `InvalidApiKeyError` when neither source is given — callers depend on
|
|
66
|
+
* that type.
|
|
67
|
+
*/
|
|
68
|
+
export function resolveCredentials({
|
|
69
|
+
apiKey,
|
|
70
|
+
credentials,
|
|
71
|
+
}: CredentialSource): PortalCredentials {
|
|
72
|
+
if (credentials && apiKey) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
'[Portal] Provide either `apiKey` or `credentials`, not both.',
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (credentials) {
|
|
79
|
+
return credentials
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!apiKey) {
|
|
83
|
+
throw new InvalidApiKeyError()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return new StaticCredentials(apiKey)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The SDK's credential boundary: normalizes every failure to
|
|
91
|
+
* `PortalCredentialError`, since a host-supplied `PortalCredentials` can throw
|
|
92
|
+
* anything. A `PortalCredentialError` from the provider passes through
|
|
93
|
+
* untouched so its `reason` is never downgraded.
|
|
94
|
+
*/
|
|
95
|
+
export async function resolveCredentialToken(
|
|
96
|
+
credentials: PortalCredentials,
|
|
97
|
+
): Promise<string> {
|
|
98
|
+
let token: string
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
token = await credentials.getToken()
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (PortalCredentialError.is(error)) {
|
|
104
|
+
throw error
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
throw new PortalCredentialError({
|
|
108
|
+
reason: PortalCredentialErrorReason.ProviderFailure,
|
|
109
|
+
cause: error,
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!token) {
|
|
114
|
+
throw new PortalCredentialError({
|
|
115
|
+
reason: PortalCredentialErrorReason.Unavailable,
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return token
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Keyed by the credential, not by subsystem, so several requesters 401-ing at
|
|
124
|
+
* once — all of which arrive here through `reportUnauthorized` — share one
|
|
125
|
+
* invalidation instead of each issuing a keychain delete.
|
|
126
|
+
*/
|
|
127
|
+
const invalidations = new WeakMap<PortalCredentials, Promise<void>>()
|
|
128
|
+
|
|
129
|
+
export function invalidateCredentials(
|
|
130
|
+
credentials: PortalCredentials,
|
|
131
|
+
): Promise<void> {
|
|
132
|
+
const inFlight = invalidations.get(credentials)
|
|
133
|
+
|
|
134
|
+
if (inFlight) {
|
|
135
|
+
return inFlight
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// A host-supplied `invalidate()` may throw synchronously or return something
|
|
139
|
+
// that is not a native Promise. Starting from `Promise.resolve()` turns both
|
|
140
|
+
// into a rejection here rather than a throw that escapes before the guard
|
|
141
|
+
// below is registered, which would silently disable deduplication.
|
|
142
|
+
const invalidation = Promise.resolve()
|
|
143
|
+
.then(() => credentials.invalidate())
|
|
144
|
+
.finally(() => {
|
|
145
|
+
invalidations.delete(credentials)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
invalidations.set(credentials, invalidation)
|
|
149
|
+
|
|
150
|
+
return invalidation
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Host listeners, keyed by the credential rather than by the `Portal` holding
|
|
155
|
+
* it, so they are reachable from the 401 call sites — which see a credential
|
|
156
|
+
* and nothing else — and so a credential that is dropped takes its listeners
|
|
157
|
+
* with it.
|
|
158
|
+
*
|
|
159
|
+
* One entry is one subscription, never one callback: two subscribers may hand
|
|
160
|
+
* in the same function, and an entry keyed on the function itself would
|
|
161
|
+
* collapse them into one that either unsubscribe removes — silently
|
|
162
|
+
* deregistering a subscriber that never asked to be.
|
|
163
|
+
*/
|
|
164
|
+
const invalidationListeners = new WeakMap<PortalCredentials, Set<() => void>>()
|
|
165
|
+
|
|
166
|
+
/** Credentials already announced to the host, so each is reported once. */
|
|
167
|
+
const reported = new WeakSet<PortalCredentials>()
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Subscribes to the backend invalidating `credentials`. Returns an
|
|
171
|
+
* unsubscribe; see `Portal.onSessionInvalidated` for the host-facing contract.
|
|
172
|
+
*
|
|
173
|
+
* A credential can already have been reported before the host gets to
|
|
174
|
+
* subscribe — validity is discovered by making a request, so a host that
|
|
175
|
+
* awaits one before mounting its subscriber would otherwise never hear that
|
|
176
|
+
* the session is over, and nothing else exposes that state.
|
|
177
|
+
*/
|
|
178
|
+
export function onCredentialsInvalidated(
|
|
179
|
+
credentials: PortalCredentials,
|
|
180
|
+
listener: () => void,
|
|
181
|
+
): () => void {
|
|
182
|
+
const listeners = invalidationListeners.get(credentials) ?? new Set()
|
|
183
|
+
// This subscription's identity in `listeners`, distinct from every other
|
|
184
|
+
// subscription of the same `listener` — see the note on that map.
|
|
185
|
+
const subscription = () => listener()
|
|
186
|
+
|
|
187
|
+
listeners.add(subscription)
|
|
188
|
+
invalidationListeners.set(credentials, listeners)
|
|
189
|
+
|
|
190
|
+
// `notifyInvalidated` will not run again for this credential, so catch this
|
|
191
|
+
// subscription up itself. Deferred so the caller holds its unsubscribe
|
|
192
|
+
// before the callback can run, and re-checked then so a subscription
|
|
193
|
+
// dropped straight away stays silent — which is also what keeps a React
|
|
194
|
+
// effect that tears down and remounts within one commit to a single call.
|
|
195
|
+
// A Client API Key never reaches here: it is never recorded as reported.
|
|
196
|
+
if (reported.has(credentials)) {
|
|
197
|
+
void Promise.resolve().then(() => {
|
|
198
|
+
if (!listeners.has(subscription)) {
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
subscription()
|
|
204
|
+
} catch {
|
|
205
|
+
// As in `notifyInvalidated`: a host callback failing is not this
|
|
206
|
+
// module's problem to propagate.
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return () => {
|
|
212
|
+
listeners.delete(subscription)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* At most once per credential, and never for a Client API Key: that credential
|
|
218
|
+
* is custodian-owned, its `invalidate()` is a no-op, and there is no local
|
|
219
|
+
* session for a 401 to have ended.
|
|
220
|
+
*/
|
|
221
|
+
function notifyInvalidated(credentials: PortalCredentials): void {
|
|
222
|
+
if (StaticCredentials.is(credentials) || reported.has(credentials)) {
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
reported.add(credentials)
|
|
227
|
+
|
|
228
|
+
// Snapshot: a listener is free to unsubscribe itself, or subscribe another,
|
|
229
|
+
// from inside the callback.
|
|
230
|
+
for (const listener of Array.from(
|
|
231
|
+
invalidationListeners.get(credentials) ?? [],
|
|
232
|
+
)) {
|
|
233
|
+
try {
|
|
234
|
+
listener()
|
|
235
|
+
} catch {
|
|
236
|
+
// A host callback failing must not take the other listeners, the
|
|
237
|
+
// invalidation, or this bookkeeping down with it.
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The 401 path every Portal-authenticated requester routes through: invalidate
|
|
244
|
+
* the credential, then tell the host its session ended.
|
|
245
|
+
*
|
|
246
|
+
* Kept separate from {@link invalidateCredentials}, which stays the plain
|
|
247
|
+
* operation a host-initiated sign-out uses — only a backend rejection is news
|
|
248
|
+
* to the host.
|
|
249
|
+
*
|
|
250
|
+
* Notifies even when the invalidation rejects: the token is dropped in memory
|
|
251
|
+
* first, so the session is over either way. The rejection still propagates,
|
|
252
|
+
* and every call site already treats it as bookkeeping that must not replace
|
|
253
|
+
* the original failure.
|
|
254
|
+
*/
|
|
255
|
+
export async function reportUnauthorized(
|
|
256
|
+
credentials: PortalCredentials,
|
|
257
|
+
): Promise<void> {
|
|
258
|
+
try {
|
|
259
|
+
await invalidateCredentials(credentials)
|
|
260
|
+
} finally {
|
|
261
|
+
notifyInvalidated(credentials)
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The MPC equivalent of the 401 path above: the binary issues its own
|
|
267
|
+
* authenticated requests, so a rejected credential arrives as an `AUTH_FAILED`
|
|
268
|
+
* envelope rather than a status code any requester here can see.
|
|
269
|
+
*
|
|
270
|
+
* The binary's error is rethrown unchanged either way — the invalidation is
|
|
271
|
+
* bookkeeping, and callers keep branching on `PortalMpcError` and its `id`.
|
|
272
|
+
*/
|
|
273
|
+
export async function failFromMpcError(
|
|
274
|
+
credentials: PortalCredentials,
|
|
275
|
+
error: PortalError,
|
|
276
|
+
): Promise<never> {
|
|
277
|
+
if (isMpcAuthFailure(error)) {
|
|
278
|
+
try {
|
|
279
|
+
await reportUnauthorized(credentials)
|
|
280
|
+
} catch (bookkeeping) {
|
|
281
|
+
sdkLogger.error(
|
|
282
|
+
'[Portal] Failed to invalidate a credential the MPC binary rejected',
|
|
283
|
+
bookkeeping,
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
throw new PortalMpcError(error)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The raw Client API Key behind a credential, or `''` when it is not a static
|
|
293
|
+
* key — never a wrong or stale token. Temporary bridge for subsystems that
|
|
294
|
+
* need a synchronous `apiKey`.
|
|
295
|
+
*/
|
|
296
|
+
export function staticApiKeyOf(credentials: PortalCredentials): string {
|
|
297
|
+
if (!StaticCredentials.is(credentials)) {
|
|
298
|
+
return ''
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// The brand identifies the shape, it does not prove it: an instance from
|
|
302
|
+
// another copy of this package is only structurally compatible, so re-check
|
|
303
|
+
// the type.
|
|
304
|
+
const { value } = credentials
|
|
305
|
+
|
|
306
|
+
return typeof value === 'string' ? value : ''
|
|
307
|
+
}
|
package/src/definitions/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
BuildSolanaTransactionResponse,
|
|
10
10
|
FundParams,
|
|
11
11
|
FundResponse,
|
|
12
|
+
GenerateApiResponse,
|
|
12
13
|
NFTAsset,
|
|
13
14
|
YieldXyzEnterRequest,
|
|
14
15
|
YieldXyzEnterYieldResponse,
|
|
@@ -47,8 +48,6 @@ import type {
|
|
|
47
48
|
PortalKeychainMetadata,
|
|
48
49
|
Presignature,
|
|
49
50
|
ProgressCallback,
|
|
50
|
-
QuoteArgs,
|
|
51
|
-
QuoteResponse,
|
|
52
51
|
Shares,
|
|
53
52
|
SigningSharePairMetadata,
|
|
54
53
|
SimulateTransactionParam,
|
|
@@ -219,8 +218,36 @@ export interface RawSignOptions {
|
|
|
219
218
|
|
|
220
219
|
export type SigningRequestParams = Eip1559 | LegacyTx
|
|
221
220
|
|
|
221
|
+
/**
|
|
222
|
+
* Supplies the credential `Portal` authenticates its API calls with.
|
|
223
|
+
*
|
|
224
|
+
* This is the entire contract between `Portal` and whatever owns the
|
|
225
|
+
* credential: there is no refresh, no polling, and no observers. A `PortalAuth`
|
|
226
|
+
* session implements it, and so can a host app that manages credentials itself.
|
|
227
|
+
*/
|
|
228
|
+
export interface PortalCredentials {
|
|
229
|
+
/**
|
|
230
|
+
* Resolves the credential to send. Called per request; returning the same
|
|
231
|
+
* value repeatedly is the normal case.
|
|
232
|
+
*/
|
|
233
|
+
getToken(): Promise<string>
|
|
234
|
+
/**
|
|
235
|
+
* Called when the backend rejects the credential with a 401, so a dead token
|
|
236
|
+
* is neither handed out again nor restored on the next launch.
|
|
237
|
+
*/
|
|
238
|
+
invalidate(): Promise<void>
|
|
239
|
+
}
|
|
240
|
+
|
|
222
241
|
export interface IPortalApi {
|
|
242
|
+
/**
|
|
243
|
+
* @deprecated Read the credential through `getToken()` instead.
|
|
244
|
+
*
|
|
245
|
+
* Only populated when the owning `Portal` was constructed with `apiKey`; it
|
|
246
|
+
* is an empty string when it was constructed with `credentials`.
|
|
247
|
+
*/
|
|
223
248
|
apiKey: string
|
|
249
|
+
credentials: PortalCredentials
|
|
250
|
+
getToken(): Promise<string>
|
|
224
251
|
apiUrl: string
|
|
225
252
|
requests: HttpRequester
|
|
226
253
|
buildTransaction(
|
|
@@ -231,6 +258,10 @@ export interface IPortalApi {
|
|
|
231
258
|
traceId?: string,
|
|
232
259
|
): Promise<BuildEip155TransactionResponse | BuildSolanaTransactionResponse>
|
|
233
260
|
fund(chainId: string, params: FundParams): Promise<FundResponse>
|
|
261
|
+
generatePreGeneratedShares(
|
|
262
|
+
metadataStr: string,
|
|
263
|
+
traceId?: string,
|
|
264
|
+
): Promise<GenerateApiResponse>
|
|
234
265
|
getBackupSharesMetadata(chainId?: string): Promise<BackupSharePairMetadata[]>
|
|
235
266
|
getBalances(chainId?: string, traceId?: string): Promise<Balance[]>
|
|
236
267
|
getClient(traceId?: string): Promise<ClientResponse>
|
|
@@ -239,15 +270,9 @@ export interface IPortalApi {
|
|
|
239
270
|
getNFTs(chainId?: string, traceId?: string): Promise<NFT[]>
|
|
240
271
|
getNftAssets(chainId: string, traceId?: string): Promise<NFTAsset[]>
|
|
241
272
|
getNetworks(): Promise<Network[]>
|
|
242
|
-
getQuote(
|
|
243
|
-
apiKey: string,
|
|
244
|
-
args: QuoteArgs,
|
|
245
|
-
chainId?: string,
|
|
246
|
-
): Promise<QuoteResponse>
|
|
247
273
|
getSigningSharesMetadata(
|
|
248
274
|
chainId?: string,
|
|
249
275
|
): Promise<SigningSharePairMetadata[]>
|
|
250
|
-
getSources(apiKey: string, chainId?: string): Promise<Record<string, string>>
|
|
251
276
|
getTransactions(
|
|
252
277
|
chainId?: string,
|
|
253
278
|
limit?: number,
|
|
@@ -365,7 +390,14 @@ export interface IPortalConnect {
|
|
|
365
390
|
}
|
|
366
391
|
|
|
367
392
|
export interface IPortalProvider {
|
|
393
|
+
/**
|
|
394
|
+
* @deprecated Read the credential through `credentials.getToken()` instead.
|
|
395
|
+
*
|
|
396
|
+
* Empty when the provider was constructed with `credentials` rather than an
|
|
397
|
+
* `apiKey`.
|
|
398
|
+
*/
|
|
368
399
|
apiKey: string
|
|
400
|
+
credentials: PortalCredentials
|
|
369
401
|
autoApprove: boolean
|
|
370
402
|
chainId?: string
|
|
371
403
|
log: Console
|
package/src/errors/http-error.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { type HttpErrorOptions } from '../../types'
|
|
2
2
|
|
|
3
3
|
class HttpError extends Error {
|
|
4
|
+
/** The HTTP status code that produced this error, so callers can distinguish retryable (5xx) from terminal (4xx) failures. */
|
|
5
|
+
public status: number
|
|
6
|
+
|
|
4
7
|
constructor({ message, status, statusText }: HttpErrorOptions) {
|
|
5
8
|
super(`${status} - ${statusText}: ${message}`)
|
|
6
9
|
|
|
7
10
|
// Set the prototype explicitly.
|
|
8
11
|
Object.setPrototypeOf(this, HttpError.prototype)
|
|
12
|
+
|
|
13
|
+
this.status = status
|
|
9
14
|
}
|
|
10
15
|
}
|
|
11
16
|
|
package/src/errors/index.ts
CHANGED
|
@@ -6,6 +6,16 @@ export { default as InvalidGatewayConfigError } from './invalid-gateway-config-e
|
|
|
6
6
|
export { default as MissingOptionError } from './missing-option-error'
|
|
7
7
|
export { default as MpcSigningError } from './mpc-signing-error'
|
|
8
8
|
export { default as NoAddressError } from './no-address-error'
|
|
9
|
+
export {
|
|
10
|
+
default as PortalAuthError,
|
|
11
|
+
PortalAuthErrorReason,
|
|
12
|
+
type PortalAuthErrorOptions,
|
|
13
|
+
} from './portal-auth-error'
|
|
14
|
+
export {
|
|
15
|
+
default as PortalCredentialError,
|
|
16
|
+
PortalCredentialErrorReason,
|
|
17
|
+
type PortalCredentialErrorOptions,
|
|
18
|
+
} from './portal-credential-error'
|
|
9
19
|
export * from './portal-error'
|
|
10
20
|
export {
|
|
11
21
|
default as ProviderRpcError,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why a Client Auth login could not be completed.
|
|
3
|
+
*
|
|
4
|
+
* The backend answers every unusable grant with the same undifferentiated 401
|
|
5
|
+
* — replayed, expired, forged, wrong auth environment, and disabled or invalid
|
|
6
|
+
* auth method are deliberately indistinguishable — so these reasons describe
|
|
7
|
+
* what the SDK actually knows and the action the host should take, not what
|
|
8
|
+
* went wrong server-side.
|
|
9
|
+
*/
|
|
10
|
+
export enum PortalAuthErrorReason {
|
|
11
|
+
/**
|
|
12
|
+
* The backend refused to exchange the grant. Whatever the cause, the grant
|
|
13
|
+
* is spent and cannot be retried: start a new sign-in.
|
|
14
|
+
*/
|
|
15
|
+
GrantRejected = 'GRANT_REJECTED',
|
|
16
|
+
/**
|
|
17
|
+
* The authentication provider sent the user back with an error instead of a
|
|
18
|
+
* grant. The user never completed the provider's flow.
|
|
19
|
+
*/
|
|
20
|
+
ProviderRejected = 'PROVIDER_REJECTED',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const authErrors: Record<PortalAuthErrorReason, string> = {
|
|
24
|
+
[PortalAuthErrorReason.GrantRejected]:
|
|
25
|
+
'The authentication grant was rejected. It may already have been used, expired, or been issued for a different auth environment. Start a new sign-in.',
|
|
26
|
+
[PortalAuthErrorReason.ProviderRejected]:
|
|
27
|
+
'The authentication provider rejected the sign-in.',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface PortalAuthErrorOptions {
|
|
31
|
+
reason: PortalAuthErrorReason
|
|
32
|
+
/**
|
|
33
|
+
* Appended to the reason's message. Reaches this class from an inbound
|
|
34
|
+
* redirect URL, so callers bound it before passing it here.
|
|
35
|
+
*/
|
|
36
|
+
detail?: string
|
|
37
|
+
/**
|
|
38
|
+
* The underlying failure, preserved for debugging. Deliberately kept off the
|
|
39
|
+
* message so transport internals are not surfaced to callers.
|
|
40
|
+
*/
|
|
41
|
+
cause?: unknown
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
46
|
+
* when a consumer's tree resolves two `@portal-hq/utils`.
|
|
47
|
+
*
|
|
48
|
+
* This key and the members {@link PortalAuthError.is} implies are a
|
|
49
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
50
|
+
*/
|
|
51
|
+
const BRAND: unique symbol = Symbol.for('@portal-hq/utils/PortalAuthError')
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The single error every Client Auth completion failure is normalized to;
|
|
55
|
+
* branch on `reason`.
|
|
56
|
+
*/
|
|
57
|
+
class PortalAuthError extends Error {
|
|
58
|
+
public readonly reason: PortalAuthErrorReason
|
|
59
|
+
public readonly cause?: unknown
|
|
60
|
+
|
|
61
|
+
/** @internal Cross-copy brand. Read it through {@link PortalAuthError.is}. */
|
|
62
|
+
public readonly [BRAND] = true
|
|
63
|
+
|
|
64
|
+
constructor({ reason, detail, cause }: PortalAuthErrorOptions) {
|
|
65
|
+
super(`[PortalAuth] ${authErrors[reason]}${detail ? ` (${detail})` : ''}`)
|
|
66
|
+
|
|
67
|
+
Object.setPrototypeOf(this, PortalAuthError.prototype)
|
|
68
|
+
|
|
69
|
+
this.reason = reason
|
|
70
|
+
this.cause = cause
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
75
|
+
* this class is intentional.
|
|
76
|
+
*/
|
|
77
|
+
public static is(error: unknown): error is PortalAuthError {
|
|
78
|
+
return (
|
|
79
|
+
typeof error === 'object' &&
|
|
80
|
+
error !== null &&
|
|
81
|
+
(error as { [BRAND]?: unknown })[BRAND] === true
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export default PortalAuthError
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** Why a credential could not be turned into a usable token. */
|
|
2
|
+
export enum PortalCredentialErrorReason {
|
|
3
|
+
/** Resolution succeeded but produced nothing usable. */
|
|
4
|
+
Unavailable = 'CREDENTIAL_UNAVAILABLE',
|
|
5
|
+
/** The credential provider itself failed — a storage read, a custom
|
|
6
|
+
* `PortalCredentials` implementation throwing, and so on. */
|
|
7
|
+
ProviderFailure = 'CREDENTIAL_PROVIDER_FAILURE',
|
|
8
|
+
/** The credential is known to be dead: the session was invalidated. */
|
|
9
|
+
SessionInvalidated = 'SESSION_INVALIDATED',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const credentialErrors: Record<PortalCredentialErrorReason, string> = {
|
|
13
|
+
[PortalCredentialErrorReason.Unavailable]:
|
|
14
|
+
'No credential was available. Provide an apiKey or credentials when constructing Portal.',
|
|
15
|
+
[PortalCredentialErrorReason.ProviderFailure]:
|
|
16
|
+
'The credential provider failed to supply a credential.',
|
|
17
|
+
[PortalCredentialErrorReason.SessionInvalidated]:
|
|
18
|
+
'The session is no longer valid. Authenticate again to obtain a new one.',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PortalCredentialErrorOptions {
|
|
22
|
+
reason: PortalCredentialErrorReason
|
|
23
|
+
/**
|
|
24
|
+
* The underlying failure, preserved for debugging. Deliberately kept off the
|
|
25
|
+
* message so provider internals are not surfaced to callers.
|
|
26
|
+
*/
|
|
27
|
+
cause?: unknown
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
32
|
+
* when a consumer's tree resolves two `@portal-hq/utils` — a real
|
|
33
|
+
* `SessionInvalidated` then silently degrades to a provider failure.
|
|
34
|
+
*
|
|
35
|
+
* This key and the members {@link PortalCredentialError.is} implies are a
|
|
36
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
37
|
+
*/
|
|
38
|
+
const BRAND: unique symbol = Symbol.for(
|
|
39
|
+
'@portal-hq/utils/PortalCredentialError',
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The single error every credential resolution failure is normalized to; branch
|
|
44
|
+
* on `reason`.
|
|
45
|
+
*/
|
|
46
|
+
class PortalCredentialError extends Error {
|
|
47
|
+
public readonly reason: PortalCredentialErrorReason
|
|
48
|
+
public readonly cause?: unknown
|
|
49
|
+
|
|
50
|
+
/** @internal Cross-copy brand. Read it through {@link PortalCredentialError.is}. */
|
|
51
|
+
public readonly [BRAND] = true
|
|
52
|
+
|
|
53
|
+
constructor({ reason, cause }: PortalCredentialErrorOptions) {
|
|
54
|
+
super(`[Portal] ${credentialErrors[reason]}`)
|
|
55
|
+
|
|
56
|
+
Object.setPrototypeOf(this, PortalCredentialError.prototype)
|
|
57
|
+
|
|
58
|
+
this.reason = reason
|
|
59
|
+
this.cause = cause
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
64
|
+
* this class is intentional.
|
|
65
|
+
*/
|
|
66
|
+
public static is(error: unknown): error is PortalCredentialError {
|
|
67
|
+
return (
|
|
68
|
+
typeof error === 'object' &&
|
|
69
|
+
error !== null &&
|
|
70
|
+
(error as { [BRAND]?: unknown })[BRAND] === true
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** True when re-authentication is the only way forward. */
|
|
75
|
+
public get requiresReauthentication(): boolean {
|
|
76
|
+
return this.reason === PortalCredentialErrorReason.SessionInvalidated
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export default PortalCredentialError
|
|
@@ -91,3 +91,17 @@ export class PortalMpcError extends Error {
|
|
|
91
91
|
this.id = error.id
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The MPC binary issues its own authenticated requests, so the SDK never sees
|
|
97
|
+
* the HTTP 401 behind a rejected credential — this ID is the only form it
|
|
98
|
+
* reaches us in. Matched on the ID, never the companion `code`, which exists
|
|
99
|
+
* only for clients predating these IDs.
|
|
100
|
+
*/
|
|
101
|
+
export const MPC_AUTH_FAILED_ERROR_ID = 'AUTH_FAILED'
|
|
102
|
+
|
|
103
|
+
/** Accepts a raw error envelope or a thrown {@link PortalMpcError}. */
|
|
104
|
+
export const isMpcAuthFailure = (error: unknown): boolean =>
|
|
105
|
+
typeof error === 'object' &&
|
|
106
|
+
error !== null &&
|
|
107
|
+
(error as { id?: unknown }).id === MPC_AUTH_FAILED_ERROR_ID
|
package/src/events/index.ts
CHANGED
|
@@ -13,9 +13,7 @@ enum Events {
|
|
|
13
13
|
GetEnabledDapps = 'Get Enabled Dapps',
|
|
14
14
|
GetNFTs = 'Get NFTs',
|
|
15
15
|
GetNetworks = 'Get Networks',
|
|
16
|
-
GetQuote = 'Get Quote',
|
|
17
16
|
GetSigningShareMetadata = 'Get Signing Share Metadata',
|
|
18
|
-
GetSources = 'Get Sources',
|
|
19
17
|
GetTransactions = 'Get Transactions',
|
|
20
18
|
GetTransactionHistory = 'Get Transaction History',
|
|
21
19
|
PrepareClientEject = 'Prepare Client Eject',
|
|
@@ -68,6 +66,9 @@ enum Events {
|
|
|
68
66
|
EvmAccountType7702BuildUpgradeTx = 'Evm Account Type 7702 BuildUpgradeTx',
|
|
69
67
|
EvmAccountType7702Upgrade = 'Evm Account Type 7702 Upgrade',
|
|
70
68
|
|
|
69
|
+
UserOperationsBuildBatched = 'UserOperations BuildBatched',
|
|
70
|
+
UserOperationsBroadcastBatched = 'UserOperations BroadcastBatched',
|
|
71
|
+
|
|
71
72
|
NoahInitiateKyc = 'Noah Initiate KYC',
|
|
72
73
|
NoahInitiatePayin = 'Noah Initiate Payin',
|
|
73
74
|
NoahSimulatePayin = 'Noah Simulate Payin',
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,18 @@ export {
|
|
|
21
21
|
|
|
22
22
|
export { buildDefaultGatewayConfig, DEFAULT_HOSTS } from './config'
|
|
23
23
|
|
|
24
|
+
export {
|
|
25
|
+
StaticCredentials,
|
|
26
|
+
failFromMpcError,
|
|
27
|
+
invalidateCredentials,
|
|
28
|
+
onCredentialsInvalidated,
|
|
29
|
+
reportUnauthorized,
|
|
30
|
+
resolveCredentials,
|
|
31
|
+
resolveCredentialToken,
|
|
32
|
+
staticApiKeyOf,
|
|
33
|
+
type CredentialSource,
|
|
34
|
+
} from './credentials'
|
|
35
|
+
|
|
24
36
|
export { convertLocalToCrypto, convertCryptoToLocal, convert } from './currency'
|
|
25
37
|
|
|
26
38
|
export {
|
|
@@ -30,6 +42,7 @@ export {
|
|
|
30
42
|
KeychainAdapter,
|
|
31
43
|
MpcStatuses,
|
|
32
44
|
PasswordStorage,
|
|
45
|
+
type PortalCredentials,
|
|
33
46
|
PortalRequestMethod,
|
|
34
47
|
type RawSignOptions,
|
|
35
48
|
type RequestArguments,
|
|
@@ -47,10 +60,18 @@ export {
|
|
|
47
60
|
MissingOptionError,
|
|
48
61
|
MpcSigningError,
|
|
49
62
|
NoAddressError,
|
|
63
|
+
PortalAuthError,
|
|
64
|
+
PortalAuthErrorReason,
|
|
65
|
+
type PortalAuthErrorOptions,
|
|
66
|
+
PortalCredentialError,
|
|
67
|
+
PortalCredentialErrorReason,
|
|
68
|
+
type PortalCredentialErrorOptions,
|
|
69
|
+
MPC_AUTH_FAILED_ERROR_ID,
|
|
50
70
|
PortalError,
|
|
51
71
|
PortalErrorCodes,
|
|
52
72
|
PortalErrorCodeTypes,
|
|
53
73
|
PortalMpcError,
|
|
74
|
+
isMpcAuthFailure,
|
|
54
75
|
ProviderRpcError,
|
|
55
76
|
RpcErrorCodes,
|
|
56
77
|
} from './errors'
|