@portal-hq/utils 5.1.0 → 6.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/config/index.js +0 -1
- package/lib/commonjs/definitions/index.js +7 -1
- package/lib/commonjs/errors/index.js +9 -1
- package/lib/commonjs/errors/portal-auth-error.js +10 -2
- package/lib/commonjs/errors/portal-idempotency-error.js +74 -0
- package/lib/commonjs/errors/portal-passkey-prf-error.js +69 -0
- package/lib/commonjs/events/index.js +34 -0
- package/lib/commonjs/http-request/index.js +17 -2
- package/lib/commonjs/idempotency/index.js +74 -0
- package/lib/commonjs/index.js +12 -2
- package/lib/commonjs/requesters/http.js +9 -7
- package/lib/commonjs/version/version.json +1 -1
- package/lib/esm/config/index.js +0 -1
- package/lib/esm/definitions/index.js +7 -1
- package/lib/esm/errors/index.js +2 -0
- package/lib/esm/errors/portal-auth-error.js +10 -2
- package/lib/esm/errors/portal-idempotency-error.js +69 -0
- package/lib/esm/errors/portal-passkey-prf-error.js +66 -0
- package/lib/esm/events/index.js +34 -0
- package/lib/esm/http-request/index.js +17 -2
- package/lib/esm/idempotency/index.js +66 -0
- package/lib/esm/index.js +2 -1
- package/lib/esm/requesters/http.js +9 -7
- package/lib/esm/version/version.json +1 -1
- package/package.json +4 -4
- package/src/config/index.ts +0 -1
- package/src/definitions/index.ts +116 -20
- package/src/errors/index.ts +12 -0
- package/src/errors/portal-auth-error.ts +11 -2
- package/src/errors/portal-idempotency-error.ts +91 -0
- package/src/errors/portal-passkey-prf-error.ts +97 -0
- package/src/events/index.ts +35 -0
- package/src/http-request/index.ts +25 -2
- package/src/idempotency/index.ts +97 -0
- package/src/index.ts +18 -1
- package/src/requesters/http.ts +13 -7
- package/src/version/version.json +1 -1
- package/types.d.ts +40 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The IDs Portal returns when it refuses a request because of its idempotency
|
|
3
|
+
* key. None of them carries the original transaction hash, and several mean
|
|
4
|
+
* Portal cannot confirm whether the transaction reached the chain: look it up
|
|
5
|
+
* before sending the operation again under a new key.
|
|
6
|
+
*/
|
|
7
|
+
export const PortalIdempotencyErrorId = {
|
|
8
|
+
RequestInProgress: 'IDEMPOTENT_REQUEST_IN_PROGRESS',
|
|
9
|
+
RequestAlreadyCompleted: 'IDEMPOTENT_REQUEST_ALREADY_COMPLETED',
|
|
10
|
+
RequestPreviouslyFailed: 'IDEMPOTENT_REQUEST_PREVIOUSLY_FAILED',
|
|
11
|
+
RequestUnexpectedState: 'IDEMPOTENT_REQUEST_UNEXPECTED_STATE',
|
|
12
|
+
KeyReused: 'IDEMPOTENCY_KEY_REUSED',
|
|
13
|
+
TxMissing: 'IDEMPOTENT_TX_MISSING',
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
const IDEMPOTENCY_ERROR_IDS: ReadonlySet<string> = new Set(
|
|
17
|
+
Object.values(PortalIdempotencyErrorId),
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
const errorIdOf = (error: unknown): unknown =>
|
|
21
|
+
typeof error === 'object' && error !== null
|
|
22
|
+
? (error as { id?: unknown }).id
|
|
23
|
+
: undefined
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether Portal refused the request because of its idempotency key: the
|
|
27
|
+
* error's `id` is one of {@link PortalIdempotencyErrorId}. Accepts a raw error
|
|
28
|
+
* envelope or a thrown error, and matches the ID exactly, never the message or
|
|
29
|
+
* `code`.
|
|
30
|
+
*/
|
|
31
|
+
export const isIdempotencyRejection = (error: unknown): boolean => {
|
|
32
|
+
const id = errorIdOf(error)
|
|
33
|
+
|
|
34
|
+
return typeof id === 'string' && IDEMPOTENCY_ERROR_IDS.has(id)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether the idempotency key was already used for a request with a different
|
|
39
|
+
* payload ({@link PortalIdempotencyErrorId.KeyReused}).
|
|
40
|
+
*/
|
|
41
|
+
export const isIdempotencyKeyReused = (error: unknown): boolean =>
|
|
42
|
+
errorIdOf(error) === PortalIdempotencyErrorId.KeyReused
|
|
43
|
+
|
|
44
|
+
export type PortalIdempotencyErrorReason = 'invalidKey' | 'unsupportedTarget'
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
48
|
+
* when a consumer's tree resolves two `@portal-hq/utils`.
|
|
49
|
+
*
|
|
50
|
+
* This key and the members {@link PortalIdempotencyError.is} implies are a
|
|
51
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
52
|
+
*/
|
|
53
|
+
const BRAND: unique symbol = Symbol.for(
|
|
54
|
+
'@portal-hq/utils/PortalIdempotencyError',
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Thrown by the SDK, before anything is signed or sent, when an idempotency key
|
|
59
|
+
* cannot be used: `invalidKey` for a key that breaks Portal's rules, and
|
|
60
|
+
* `unsupportedTarget` for a key on a broadcast Portal cannot deduplicate. The
|
|
61
|
+
* message names the problem and never contains the key.
|
|
62
|
+
*/
|
|
63
|
+
class PortalIdempotencyError extends Error {
|
|
64
|
+
public readonly reason: PortalIdempotencyErrorReason
|
|
65
|
+
|
|
66
|
+
/** @internal Cross-copy brand. Read it through {@link PortalIdempotencyError.is}. */
|
|
67
|
+
public readonly [BRAND] = true
|
|
68
|
+
|
|
69
|
+
constructor(reason: PortalIdempotencyErrorReason, message: string) {
|
|
70
|
+
super(message)
|
|
71
|
+
|
|
72
|
+
Object.setPrototypeOf(this, PortalIdempotencyError.prototype)
|
|
73
|
+
|
|
74
|
+
this.name = 'PortalIdempotencyError'
|
|
75
|
+
this.reason = reason
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
80
|
+
* this class is intentional.
|
|
81
|
+
*/
|
|
82
|
+
public static is(error: unknown): error is PortalIdempotencyError {
|
|
83
|
+
return (
|
|
84
|
+
typeof error === 'object' &&
|
|
85
|
+
error !== null &&
|
|
86
|
+
(error as { [BRAND]?: unknown })[BRAND] === true
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default PortalIdempotencyError
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** Why a passkey PRF backup could not be enrolled, read or decrypted. */
|
|
2
|
+
export enum PortalPasskeyPrfErrorReason {
|
|
3
|
+
/** The passkey ceremony failed before a secret was derived, for a reason other than the two below. */
|
|
4
|
+
CeremonyFailed = 'PRF_CEREMONY_FAILED',
|
|
5
|
+
/** The user dismissed the passkey prompt. */
|
|
6
|
+
UserCancelled = 'PRF_USER_CANCELLED',
|
|
7
|
+
/** The platform reported that it has no passkey to offer for this request. */
|
|
8
|
+
PasskeyUnavailable = 'PRF_PASSKEY_UNAVAILABLE',
|
|
9
|
+
/** No passkey has been enrolled for this client, so there is nothing to recover with. */
|
|
10
|
+
CredentialNotFound = 'PRF_CREDENTIAL_NOT_FOUND',
|
|
11
|
+
/** The backup could not be decrypted with the secret the passkey produced. */
|
|
12
|
+
DecryptFailed = 'PRF_DECRYPT_FAILED',
|
|
13
|
+
/** The device or passkey provider cannot evaluate the WebAuthn PRF extension. */
|
|
14
|
+
NotSupported = 'PRF_NOT_SUPPORTED',
|
|
15
|
+
/** The passkey was enrolled under a different relying party than the one configured. */
|
|
16
|
+
RpIdMismatch = 'PRF_RP_ID_MISMATCH',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const passkeyPrfErrors: Record<PortalPasskeyPrfErrorReason, string> = {
|
|
20
|
+
[PortalPasskeyPrfErrorReason.CeremonyFailed]:
|
|
21
|
+
'The passkey request could not be completed.',
|
|
22
|
+
[PortalPasskeyPrfErrorReason.UserCancelled]:
|
|
23
|
+
'The passkey request was cancelled.',
|
|
24
|
+
[PortalPasskeyPrfErrorReason.PasskeyUnavailable]:
|
|
25
|
+
'The enrolled passkey is not available on this device.',
|
|
26
|
+
[PortalPasskeyPrfErrorReason.CredentialNotFound]:
|
|
27
|
+
'No passkey has been enrolled for this backup method.',
|
|
28
|
+
[PortalPasskeyPrfErrorReason.DecryptFailed]:
|
|
29
|
+
'The backup could not be decrypted with the secret from this passkey (wrong passkey or wrong ciphertext).',
|
|
30
|
+
[PortalPasskeyPrfErrorReason.NotSupported]:
|
|
31
|
+
'This device or passkey provider does not support the WebAuthn PRF extension.',
|
|
32
|
+
[PortalPasskeyPrfErrorReason.RpIdMismatch]:
|
|
33
|
+
'The passkey was enrolled under a different relying party.',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PortalPasskeyPrfErrorOptions {
|
|
37
|
+
reason: PortalPasskeyPrfErrorReason
|
|
38
|
+
/**
|
|
39
|
+
* The underlying failure, preserved for debugging. Deliberately kept off the
|
|
40
|
+
* message so platform internals are not surfaced to callers.
|
|
41
|
+
*/
|
|
42
|
+
cause?: unknown
|
|
43
|
+
/** Extra detail appended to the message, for example the two relying party IDs. */
|
|
44
|
+
context?: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
49
|
+
* when a consumer's tree resolves two `@portal-hq/utils` — a dismissed prompt
|
|
50
|
+
* would then be reported as an unexpected MPC failure.
|
|
51
|
+
*
|
|
52
|
+
* This key and the members {@link PortalPasskeyPrfError.is} implies are a
|
|
53
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
54
|
+
*/
|
|
55
|
+
const BRAND: unique symbol = Symbol.for(
|
|
56
|
+
'@portal-hq/utils/PortalPasskeyPrfError',
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The single error every passkey PRF backup failure is normalized to; branch
|
|
61
|
+
* on `reason`.
|
|
62
|
+
*/
|
|
63
|
+
class PortalPasskeyPrfError extends Error {
|
|
64
|
+
public readonly reason: PortalPasskeyPrfErrorReason
|
|
65
|
+
public readonly cause?: unknown
|
|
66
|
+
|
|
67
|
+
/** @internal Cross-copy brand. Read it through {@link PortalPasskeyPrfError.is}. */
|
|
68
|
+
public readonly [BRAND] = true
|
|
69
|
+
|
|
70
|
+
constructor({ reason, cause, context }: PortalPasskeyPrfErrorOptions) {
|
|
71
|
+
super(`[Portal] ${passkeyPrfErrors[reason]}${context ? ` ${context}` : ''}`)
|
|
72
|
+
|
|
73
|
+
Object.setPrototypeOf(this, PortalPasskeyPrfError.prototype)
|
|
74
|
+
|
|
75
|
+
this.reason = reason
|
|
76
|
+
this.cause = cause
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
81
|
+
* this class is intentional.
|
|
82
|
+
*/
|
|
83
|
+
public static is(error: unknown): error is PortalPasskeyPrfError {
|
|
84
|
+
return (
|
|
85
|
+
typeof error === 'object' &&
|
|
86
|
+
error !== null &&
|
|
87
|
+
(error as { [BRAND]?: unknown })[BRAND] === true
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** True when the user chose not to continue, rather than something failing. */
|
|
92
|
+
public get isUserCancelled(): boolean {
|
|
93
|
+
return this.reason === PortalPasskeyPrfErrorReason.UserCancelled
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export default PortalPasskeyPrfError
|
package/src/events/index.ts
CHANGED
|
@@ -125,6 +125,41 @@ enum Events {
|
|
|
125
125
|
YieldXyzPerpsGetEvent = 'YieldXyz Perps Get Event',
|
|
126
126
|
YieldXyzPerpsGetHealth = 'YieldXyz Perps Get Health',
|
|
127
127
|
YieldXyzPerpsGetProvidersHealth = 'YieldXyz Perps Get Providers Health',
|
|
128
|
+
|
|
129
|
+
AggCreateLinkChallenge = 'Agg Create Link Challenge',
|
|
130
|
+
AggCreateLink = 'Agg Create Link',
|
|
131
|
+
AggGetLink = 'Agg Get Link',
|
|
132
|
+
AggDeleteLink = 'Agg Delete Link',
|
|
133
|
+
AggListEvents = 'Agg List Events',
|
|
134
|
+
AggGetEvent = 'Agg Get Event',
|
|
135
|
+
AggGetEventBootstrap = 'Agg Get Event Bootstrap',
|
|
136
|
+
AggListMarkets = 'Agg List Markets',
|
|
137
|
+
AggSearch = 'Agg Search',
|
|
138
|
+
AggListCategories = 'Agg List Categories',
|
|
139
|
+
AggGetVenueGeoPolicy = 'Agg Get Venue Geo Policy',
|
|
140
|
+
AggGetAppConfig = 'Agg Get App Config',
|
|
141
|
+
AggGetMidpoints = 'Agg Get Midpoints',
|
|
142
|
+
AggGetOrderbooks = 'Agg Get Orderbooks',
|
|
143
|
+
AggGetOutcomeOrderbook = 'Agg Get Outcome Orderbook',
|
|
144
|
+
AggGetChartBars = 'Agg Get Chart Bars',
|
|
145
|
+
AggGetChartHistoryManifest = 'Agg Get Chart History Manifest',
|
|
146
|
+
AggGetUser = 'Agg Get User',
|
|
147
|
+
AggGetDepositAddresses = 'Agg Get Deposit Addresses',
|
|
148
|
+
AggGetBalances = 'Agg Get Balances',
|
|
149
|
+
AggListPositions = 'Agg List Positions',
|
|
150
|
+
AggListOrders = 'Agg List Orders',
|
|
151
|
+
AggListActivity = 'Agg List Activity',
|
|
152
|
+
AggCreateQuote = 'Agg Create Quote',
|
|
153
|
+
AggCreateOrder = 'Agg Create Order',
|
|
154
|
+
AggGetOrder = 'Agg Get Order',
|
|
155
|
+
AggCreateLimitOrder = 'Agg Create Limit Order',
|
|
156
|
+
AggCreateDirectOrder = 'Agg Create Direct Order',
|
|
157
|
+
AggCancelOrder = 'Agg Cancel Order',
|
|
158
|
+
AggRedeemPositions = 'Agg Redeem Positions',
|
|
159
|
+
AggQuoteWithdrawable = 'Agg Quote Withdrawable',
|
|
160
|
+
AggPreviewWithdrawal = 'Agg Preview Withdrawal',
|
|
161
|
+
AggCreateWithdrawal = 'Agg Create Withdrawal',
|
|
162
|
+
AggGetWithdrawal = 'Agg Get Withdrawal',
|
|
128
163
|
}
|
|
129
164
|
|
|
130
165
|
export default Events
|
|
@@ -8,13 +8,21 @@ class HttpRequest {
|
|
|
8
8
|
public body: Record<string, any>
|
|
9
9
|
public headers: Record<string, any>
|
|
10
10
|
public method: ValidHttpRequestMethods
|
|
11
|
+
public rejectTransportFailures: boolean
|
|
11
12
|
public request: XMLHttpRequest
|
|
12
13
|
public url: string
|
|
13
14
|
|
|
14
|
-
constructor({
|
|
15
|
+
constructor({
|
|
16
|
+
body = {},
|
|
17
|
+
headers = {},
|
|
18
|
+
method,
|
|
19
|
+
rejectTransportFailures = false,
|
|
20
|
+
url,
|
|
21
|
+
}: HttpRequestOptions) {
|
|
15
22
|
this.body = body
|
|
16
23
|
this.headers = headers
|
|
17
24
|
this.method = method
|
|
25
|
+
this.rejectTransportFailures = rejectTransportFailures
|
|
18
26
|
this.url = url
|
|
19
27
|
|
|
20
28
|
this.request = new XMLHttpRequest()
|
|
@@ -34,11 +42,18 @@ class HttpRequest {
|
|
|
34
42
|
this.request.send()
|
|
35
43
|
}
|
|
36
44
|
|
|
45
|
+
if (this.rejectTransportFailures) {
|
|
46
|
+
this.request.onerror = () => reject(this.buildTransportError())
|
|
47
|
+
this.request.ontimeout = () => reject(this.buildTransportError())
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
// Adds an event listener for when the request state changes
|
|
38
51
|
this.request.onreadystatechange = () => {
|
|
39
52
|
if (this.request.readyState === 4) {
|
|
40
53
|
// readyState 4 means the request has completed
|
|
41
|
-
if (this.request.status
|
|
54
|
+
if (this.rejectTransportFailures && this.request.status === 0) {
|
|
55
|
+
reject(this.buildTransportError())
|
|
56
|
+
} else if (this.request.status >= 300) {
|
|
42
57
|
// If we didn't get an 'OK' status, error out
|
|
43
58
|
reject(
|
|
44
59
|
new HttpError({
|
|
@@ -56,6 +71,14 @@ class HttpRequest {
|
|
|
56
71
|
})
|
|
57
72
|
}
|
|
58
73
|
|
|
74
|
+
private buildTransportError(): HttpError {
|
|
75
|
+
return new HttpError({
|
|
76
|
+
message: `Network request to ${this.url} failed`,
|
|
77
|
+
status: 0,
|
|
78
|
+
statusText: 'Network Error',
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
59
82
|
private buildResponse<T>(): T {
|
|
60
83
|
// If the response is empty, just return an empty object
|
|
61
84
|
if (this.request.status === 204) {
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import PortalIdempotencyError from '../errors/portal-idempotency-error'
|
|
2
|
+
import { generateTraceId } from '../tracing'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The HTTP header the Enclave MPC API reads a transaction broadcast's
|
|
6
|
+
* idempotency key from.
|
|
7
|
+
*/
|
|
8
|
+
export const PORTAL_IDEMPOTENCY_KEY_HEADER = 'Idempotency-Key'
|
|
9
|
+
|
|
10
|
+
const IDEMPOTENCY_KEY_MAX_LENGTH = 255
|
|
11
|
+
|
|
12
|
+
/** RFC 3986 unreserved characters, the only ones Portal accepts in a key. */
|
|
13
|
+
const IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9._~-]+$/
|
|
14
|
+
|
|
15
|
+
const uuidV4FromRandomValues = (getRandomValues: Crypto['getRandomValues']) => {
|
|
16
|
+
const bytes = getRandomValues(new Uint8Array(16))
|
|
17
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40
|
|
18
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80
|
|
19
|
+
const hex = Array.from(bytes, (byte) =>
|
|
20
|
+
byte.toString(16).padStart(2, '0'),
|
|
21
|
+
).join('')
|
|
22
|
+
|
|
23
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Generates an idempotency key: a lowercase UUID v4. Generate one per logical
|
|
28
|
+
* operation, store it with the pending operation, and reuse it only to retry
|
|
29
|
+
* that identical request.
|
|
30
|
+
*
|
|
31
|
+
* The key comes from `crypto.randomUUID` or `crypto.getRandomValues` when the
|
|
32
|
+
* runtime provides one. Hermes provides neither unless the app installs a
|
|
33
|
+
* polyfill such as `react-native-get-random-values`; without one the key is
|
|
34
|
+
* built from `Math.random`, which keeps keys unique in practice but is not a
|
|
35
|
+
* cryptographic source.
|
|
36
|
+
*/
|
|
37
|
+
export const generateIdempotencyKey = (): string => {
|
|
38
|
+
const runtimeCrypto = typeof crypto === 'undefined' ? undefined : crypto
|
|
39
|
+
|
|
40
|
+
if (typeof runtimeCrypto?.randomUUID === 'function') {
|
|
41
|
+
return runtimeCrypto.randomUUID().toLowerCase()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof runtimeCrypto?.getRandomValues === 'function') {
|
|
45
|
+
return uuidV4FromRandomValues(
|
|
46
|
+
runtimeCrypto.getRandomValues.bind(runtimeCrypto),
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// With neither API available, generateTraceId builds the key from
|
|
51
|
+
// Math.random.
|
|
52
|
+
return generateTraceId().toLowerCase()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Trims `key` the way Portal does (`String.prototype.trim`) and checks it
|
|
57
|
+
* against Portal's rules: 1–255 characters of `A-Z a-z 0-9 . _ ~ -`.
|
|
58
|
+
*
|
|
59
|
+
* @internal Exported only so the SDK packages can share it across the package
|
|
60
|
+
* boundary. Not part of the public API.
|
|
61
|
+
*
|
|
62
|
+
* @returns The trimmed key, which is the value the SDK sends.
|
|
63
|
+
* @throws {PortalIdempotencyError} `invalidKey`, naming the rule that failed.
|
|
64
|
+
*/
|
|
65
|
+
export const validateIdempotencyKey = (key: unknown): string => {
|
|
66
|
+
if (typeof key !== 'string') {
|
|
67
|
+
throw new PortalIdempotencyError(
|
|
68
|
+
'invalidKey',
|
|
69
|
+
'idempotencyKey must be a string',
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const trimmed = key.trim()
|
|
74
|
+
|
|
75
|
+
if (trimmed.length === 0) {
|
|
76
|
+
throw new PortalIdempotencyError(
|
|
77
|
+
'invalidKey',
|
|
78
|
+
'idempotencyKey must not be empty or whitespace-only',
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (trimmed.length > IDEMPOTENCY_KEY_MAX_LENGTH) {
|
|
83
|
+
throw new PortalIdempotencyError(
|
|
84
|
+
'invalidKey',
|
|
85
|
+
`idempotencyKey must be at most ${IDEMPOTENCY_KEY_MAX_LENGTH} characters`,
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!IDEMPOTENCY_KEY_PATTERN.test(trimmed)) {
|
|
90
|
+
throw new PortalIdempotencyError(
|
|
91
|
+
'invalidKey',
|
|
92
|
+
"idempotencyKey may only contain the characters A-Z, a-z, 0-9, '.', '_', '~' and '-'",
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return trimmed
|
|
97
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -36,8 +36,11 @@ export {
|
|
|
36
36
|
export { convertLocalToCrypto, convertCryptoToLocal, convert } from './currency'
|
|
37
37
|
|
|
38
38
|
export {
|
|
39
|
+
type BackupKeyMatcher,
|
|
40
|
+
type BackupKeyOptions,
|
|
41
|
+
type DerivedSecretStorage,
|
|
42
|
+
type GetClientOptions,
|
|
39
43
|
type IPortalApi,
|
|
40
|
-
type IPortalConnect,
|
|
41
44
|
type IPortalProvider,
|
|
42
45
|
KeychainAdapter,
|
|
43
46
|
MpcStatuses,
|
|
@@ -60,6 +63,9 @@ export {
|
|
|
60
63
|
MissingOptionError,
|
|
61
64
|
MpcSigningError,
|
|
62
65
|
NoAddressError,
|
|
66
|
+
PortalPasskeyPrfError,
|
|
67
|
+
PortalPasskeyPrfErrorReason,
|
|
68
|
+
type PortalPasskeyPrfErrorOptions,
|
|
63
69
|
PortalAuthError,
|
|
64
70
|
PortalAuthErrorReason,
|
|
65
71
|
type PortalAuthErrorOptions,
|
|
@@ -71,6 +77,11 @@ export {
|
|
|
71
77
|
PortalErrorCodes,
|
|
72
78
|
PortalErrorCodeTypes,
|
|
73
79
|
PortalMpcError,
|
|
80
|
+
PortalIdempotencyError,
|
|
81
|
+
type PortalIdempotencyErrorReason,
|
|
82
|
+
PortalIdempotencyErrorId,
|
|
83
|
+
isIdempotencyKeyReused,
|
|
84
|
+
isIdempotencyRejection,
|
|
74
85
|
PortalPolicyError,
|
|
75
86
|
POLICY_ERROR_ID_PREFIX,
|
|
76
87
|
PolicyErrorId,
|
|
@@ -81,6 +92,12 @@ export {
|
|
|
81
92
|
RpcErrorCodes,
|
|
82
93
|
} from './errors'
|
|
83
94
|
|
|
95
|
+
export {
|
|
96
|
+
generateIdempotencyKey,
|
|
97
|
+
PORTAL_IDEMPOTENCY_KEY_HEADER,
|
|
98
|
+
validateIdempotencyKey,
|
|
99
|
+
} from './idempotency'
|
|
100
|
+
|
|
84
101
|
export { default as Events } from './events'
|
|
85
102
|
|
|
86
103
|
export { getClientPlatformVersion } from './version'
|
package/src/requesters/http.ts
CHANGED
|
@@ -22,9 +22,15 @@ class HttpRequester {
|
|
|
22
22
|
* the hook's own failure is swallowed and the original `HttpError` is always
|
|
23
23
|
* what the caller sees.
|
|
24
24
|
*/
|
|
25
|
-
private async send<T>(
|
|
25
|
+
private async send<T>(
|
|
26
|
+
requestOptions: HttpRequestOptions,
|
|
27
|
+
options?: HttpOptions,
|
|
28
|
+
): Promise<T> {
|
|
26
29
|
try {
|
|
27
|
-
|
|
30
|
+
const sentOptions = options?.rejectTransportFailures
|
|
31
|
+
? { ...requestOptions, rejectTransportFailures: true }
|
|
32
|
+
: requestOptions
|
|
33
|
+
return (await new HttpRequest(sentOptions).send()) as T
|
|
28
34
|
} catch (error) {
|
|
29
35
|
if (
|
|
30
36
|
this.onUnauthorized &&
|
|
@@ -64,7 +70,7 @@ class HttpRequester {
|
|
|
64
70
|
options?.traceId,
|
|
65
71
|
)
|
|
66
72
|
|
|
67
|
-
return this.send<T>(requestOptions)
|
|
73
|
+
return this.send<T>(requestOptions, options)
|
|
68
74
|
}
|
|
69
75
|
|
|
70
76
|
public async get<T>(path: string, options?: HttpOptions): Promise<T> {
|
|
@@ -78,7 +84,7 @@ class HttpRequester {
|
|
|
78
84
|
options?.traceId,
|
|
79
85
|
)
|
|
80
86
|
|
|
81
|
-
return this.send<T>(requestOptions)
|
|
87
|
+
return this.send<T>(requestOptions, options)
|
|
82
88
|
}
|
|
83
89
|
|
|
84
90
|
public async post<T>(path: string, options?: HttpOptions): Promise<T> {
|
|
@@ -96,7 +102,7 @@ class HttpRequester {
|
|
|
96
102
|
requestOptions.body = options.body
|
|
97
103
|
}
|
|
98
104
|
|
|
99
|
-
return this.send<T>(requestOptions)
|
|
105
|
+
return this.send<T>(requestOptions, options)
|
|
100
106
|
}
|
|
101
107
|
|
|
102
108
|
public async put<T>(path: string, options: HttpOptions): Promise<T> {
|
|
@@ -114,7 +120,7 @@ class HttpRequester {
|
|
|
114
120
|
requestOptions.body = options.body
|
|
115
121
|
}
|
|
116
122
|
|
|
117
|
-
return this.send<T>(requestOptions)
|
|
123
|
+
return this.send<T>(requestOptions, options)
|
|
118
124
|
}
|
|
119
125
|
|
|
120
126
|
public async patch<T>(path: string, options: HttpOptions): Promise<T> {
|
|
@@ -132,7 +138,7 @@ class HttpRequester {
|
|
|
132
138
|
requestOptions.body = options.body
|
|
133
139
|
}
|
|
134
140
|
|
|
135
|
-
return this.send<T>(requestOptions)
|
|
141
|
+
return this.send<T>(requestOptions, options)
|
|
136
142
|
}
|
|
137
143
|
}
|
|
138
144
|
|
package/src/version/version.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "version": "
|
|
1
|
+
{ "version": "6.0.0" }
|
package/types.d.ts
CHANGED
|
@@ -241,6 +241,12 @@ export interface HttpErrorOptions {
|
|
|
241
241
|
export interface HttpOptions {
|
|
242
242
|
body?: Record<string, any>
|
|
243
243
|
headers?: Record<string, any>
|
|
244
|
+
/**
|
|
245
|
+
* Rejects network errors, timeouts and status 0 responses instead of
|
|
246
|
+
* resolving them as empty successes. Opt-in so existing callers keep their
|
|
247
|
+
* behavior.
|
|
248
|
+
*/
|
|
249
|
+
rejectTransportFailures?: boolean
|
|
244
250
|
traceId?: string
|
|
245
251
|
}
|
|
246
252
|
|
|
@@ -258,6 +264,7 @@ export interface HttpRequestOptions {
|
|
|
258
264
|
body?: Record<string, any>
|
|
259
265
|
headers?: Record<string, any>
|
|
260
266
|
method: ValidHttpRequestMethods
|
|
267
|
+
rejectTransportFailures?: boolean
|
|
261
268
|
url: string
|
|
262
269
|
}
|
|
263
270
|
|
|
@@ -315,6 +322,7 @@ export interface SigningRequestArguments {
|
|
|
315
322
|
readonly sponsorGas?: boolean
|
|
316
323
|
readonly signatureApprovalMemo?: string
|
|
317
324
|
readonly traceId?: string
|
|
325
|
+
readonly idempotencyKey?: string
|
|
318
326
|
}
|
|
319
327
|
|
|
320
328
|
export interface SigningResponse {
|
|
@@ -720,6 +728,38 @@ export interface OpenSeaMetadata {
|
|
|
720
728
|
|
|
721
729
|
export interface BackupConfigs {
|
|
722
730
|
passwordStorage?: PasswordStorageConfig
|
|
731
|
+
passkeyPrfStorage?: PasskeyPrfStorageConfig
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/** Labels a passkey provider stores with a passkey and shows in its UI. */
|
|
735
|
+
export interface PasskeyPrfUser {
|
|
736
|
+
/** Account name the passkey provider stores alongside the passkey. */
|
|
737
|
+
name: string
|
|
738
|
+
/** Name shown to the user when they are asked to pick a passkey. */
|
|
739
|
+
displayName: string
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** Per-backup options for the passkey PRF backup method. */
|
|
743
|
+
export interface PasskeyPrfStorageConfig {
|
|
744
|
+
/**
|
|
745
|
+
* Labels for the passkey this backup creates. Used only when the client has
|
|
746
|
+
* no enrolled passkey yet: an enrolled passkey is reused, and its labels
|
|
747
|
+
* cannot be changed. Overrides the adapter's `user` option.
|
|
748
|
+
*/
|
|
749
|
+
user?: PasskeyPrfUser
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Identifies the passkey a client enrolled for the passkey PRF backup method.
|
|
754
|
+
* None of it is secret: the share-encryption secret is derived from the
|
|
755
|
+
* passkey on demand and is never stored.
|
|
756
|
+
*/
|
|
757
|
+
export interface PasskeyPrfCredential {
|
|
758
|
+
/** Unpadded base64url WebAuthn credential ID. */
|
|
759
|
+
credentialId: string
|
|
760
|
+
/** Relying party the passkey is scoped to. Fixed once enrolled. */
|
|
761
|
+
rpId: string
|
|
762
|
+
transports?: string[]
|
|
723
763
|
}
|
|
724
764
|
|
|
725
765
|
export interface PasswordStorageConfig {
|