@portal-hq/utils 4.9.0 → 5.1.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 +1 -0
- package/lib/commonjs/credentials/index.js +262 -0
- package/lib/commonjs/errors/http-error.js +27 -0
- package/lib/commonjs/errors/index.js +13 -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/errors/portal-policy-error.js +83 -0
- package/lib/commonjs/events/index.js +29 -2
- package/lib/commonjs/index.js +22 -2
- package/lib/commonjs/requesters/http.js +33 -16
- package/lib/commonjs/version/version.json +1 -1
- package/lib/esm/config/index.js +1 -0
- package/lib/esm/credentials/index.js +251 -0
- package/lib/esm/errors/http-error.js +27 -0
- package/lib/esm/errors/index.js +3 -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/errors/portal-policy-error.js +77 -0
- package/lib/esm/events/index.js +29 -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/config/index.ts +1 -0
- package/src/credentials/index.ts +309 -0
- package/src/definitions/index.ts +35 -8
- package/src/errors/http-error.ts +40 -1
- package/src/errors/index.ts +17 -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/errors/portal-policy-error.ts +97 -0
- package/src/events/index.ts +30 -2
- package/src/index.ts +26 -0
- package/src/requesters/http.ts +35 -21
- package/src/version/version.json +1 -1
- package/types.d.ts +60 -36
|
@@ -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
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type PortalError, PortalMpcError } from './portal-error'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Every Policy Engine rejection the gateway emits carries an ID under this
|
|
5
|
+
* prefix. Classification is by prefix, not by a known-ID list, so a policy kind
|
|
6
|
+
* added to the gateway later is still recognised without an SDK release.
|
|
7
|
+
*/
|
|
8
|
+
export const POLICY_ERROR_ID_PREFIX = 'POLICY_'
|
|
9
|
+
|
|
10
|
+
/** The policy IDs the gateway emits today. The set is open — see the prefix. */
|
|
11
|
+
export const PolicyErrorId = {
|
|
12
|
+
AmountLimitExceeded: 'POLICY_AMOUNT_LIMIT_EXCEEDED',
|
|
13
|
+
} as const
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether an error reports a Policy Engine rejection.
|
|
17
|
+
*
|
|
18
|
+
* Accepts a raw error envelope or a thrown {@link PortalMpcError}. Matched on
|
|
19
|
+
* the ID, never the companion `code` — the binary maps policy IDs onto a
|
|
20
|
+
* generic legacy code — and never on the message text.
|
|
21
|
+
*/
|
|
22
|
+
export const isPolicyViolation = (error: unknown): boolean => {
|
|
23
|
+
if (typeof error !== 'object' || error === null) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { id } = error as { id?: unknown }
|
|
28
|
+
|
|
29
|
+
return typeof id === 'string' && id.startsWith(POLICY_ERROR_ID_PREFIX)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
34
|
+
* when a consumer's tree resolves two `@portal-hq/utils` — a real policy
|
|
35
|
+
* rejection would then silently degrade to an ordinary MPC failure.
|
|
36
|
+
*
|
|
37
|
+
* This key and the members {@link PortalPolicyError.is} implies are a
|
|
38
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
39
|
+
*/
|
|
40
|
+
const BRAND: unique symbol = Symbol.for('@portal-hq/utils/PortalPolicyError')
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A transaction the Policy Engine refused to sign. Extends
|
|
44
|
+
* {@link PortalMpcError} so callers already branching on that type, or reading
|
|
45
|
+
* its `id`, keep working unchanged. `id` always carries the `POLICY_*`
|
|
46
|
+
* identifier here, even though the inherited type leaves it optional.
|
|
47
|
+
*
|
|
48
|
+
* The rejection is terminal: the same request will be refused again, so it is
|
|
49
|
+
* not retried and it never invalidates the credential — the session is fine,
|
|
50
|
+
* the transaction is not.
|
|
51
|
+
*/
|
|
52
|
+
export class PortalPolicyError extends PortalMpcError {
|
|
53
|
+
/** The underlying failure, when the rejection arrived wrapped in one. */
|
|
54
|
+
public readonly cause?: unknown
|
|
55
|
+
|
|
56
|
+
/** @internal Cross-copy brand. Read it through {@link PortalPolicyError.is}. */
|
|
57
|
+
public readonly [BRAND] = true
|
|
58
|
+
|
|
59
|
+
public constructor(error: PortalError, cause?: unknown) {
|
|
60
|
+
super(error)
|
|
61
|
+
|
|
62
|
+
Object.setPrototypeOf(this, PortalPolicyError.prototype)
|
|
63
|
+
|
|
64
|
+
this.cause = cause
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
69
|
+
* this class is intentional.
|
|
70
|
+
*/
|
|
71
|
+
public static is(error: unknown): error is PortalPolicyError {
|
|
72
|
+
return (
|
|
73
|
+
typeof error === 'object' &&
|
|
74
|
+
error !== null &&
|
|
75
|
+
(error as { [BRAND]?: unknown })[BRAND] === true
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Builds the error for an MPC envelope, promoting a Policy Engine rejection to
|
|
82
|
+
* {@link PortalPolicyError}. Every other envelope stays a {@link PortalMpcError},
|
|
83
|
+
* so only the policy path changes shape.
|
|
84
|
+
*
|
|
85
|
+
* @internal Exported only so the provider packages can share it across the
|
|
86
|
+
* package boundary. Not part of the public API — catch {@link PortalPolicyError}
|
|
87
|
+
* or call {@link isPolicyViolation} instead.
|
|
88
|
+
*/
|
|
89
|
+
export const mpcErrorFrom = (
|
|
90
|
+
error: PortalError,
|
|
91
|
+
cause?: unknown,
|
|
92
|
+
): PortalMpcError =>
|
|
93
|
+
isPolicyViolation(error)
|
|
94
|
+
? new PortalPolicyError(error, cause)
|
|
95
|
+
: new PortalMpcError(error)
|
|
96
|
+
|
|
97
|
+
export default PortalPolicyError
|
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',
|
|
@@ -35,6 +33,18 @@ enum Events {
|
|
|
35
33
|
YieldXyzGetHistoricalActions = 'YieldXyz Get Historical Actions',
|
|
36
34
|
YieldXyzGetTransaction = 'YieldXyz Get Transaction',
|
|
37
35
|
YieldXyzTrack = 'YieldXyz Track',
|
|
36
|
+
YieldXyzBorrowGetIntegrations = 'YieldXyz Borrow Get Integrations',
|
|
37
|
+
YieldXyzBorrowGetIntegration = 'YieldXyz Borrow Get Integration',
|
|
38
|
+
YieldXyzBorrowGetMarkets = 'YieldXyz Borrow Get Markets',
|
|
39
|
+
YieldXyzBorrowGetMarket = 'YieldXyz Borrow Get Market',
|
|
40
|
+
YieldXyzBorrowGetPositions = 'YieldXyz Borrow Get Positions',
|
|
41
|
+
YieldXyzBorrowGetLiquidations = 'YieldXyz Borrow Get Liquidations',
|
|
42
|
+
YieldXyzBorrowCreateAction = 'YieldXyz Borrow Create Action',
|
|
43
|
+
YieldXyzBorrowGetActions = 'YieldXyz Borrow Get Actions',
|
|
44
|
+
YieldXyzBorrowGetAction = 'YieldXyz Borrow Get Action',
|
|
45
|
+
YieldXyzBorrowStepAction = 'YieldXyz Borrow Step Action',
|
|
46
|
+
YieldXyzBorrowSubmitTransaction = 'YieldXyz Borrow Submit Transaction',
|
|
47
|
+
YieldXyzBorrowGetHealth = 'YieldXyz Borrow Get Health',
|
|
38
48
|
LifiGetRoutes = 'Lifi Get Routes',
|
|
39
49
|
LifiGetQuote = 'Lifi Get Quote',
|
|
40
50
|
LifiGetStatus = 'Lifi Get Status',
|
|
@@ -97,6 +107,24 @@ enum Events {
|
|
|
97
107
|
MeldGetBuyLimits = 'Meld Get Buy Limits',
|
|
98
108
|
MeldGetSellLimits = 'Meld Get Sell Limits',
|
|
99
109
|
MeldGetKycLimits = 'Meld Get Kyc Limits',
|
|
110
|
+
|
|
111
|
+
YieldXyzPerpsGetProviders = 'YieldXyz Perps Get Providers',
|
|
112
|
+
YieldXyzPerpsGetProvider = 'YieldXyz Perps Get Provider',
|
|
113
|
+
YieldXyzPerpsGetMarkets = 'YieldXyz Perps Get Markets',
|
|
114
|
+
YieldXyzPerpsGetMarket = 'YieldXyz Perps Get Market',
|
|
115
|
+
YieldXyzPerpsGetMarketCandles = 'YieldXyz Perps Get Market Candles',
|
|
116
|
+
YieldXyzPerpsGetPositions = 'YieldXyz Perps Get Positions',
|
|
117
|
+
YieldXyzPerpsGetOrders = 'YieldXyz Perps Get Orders',
|
|
118
|
+
YieldXyzPerpsGetBalances = 'YieldXyz Perps Get Balances',
|
|
119
|
+
YieldXyzPerpsCreateAction = 'YieldXyz Perps Create Action',
|
|
120
|
+
YieldXyzPerpsGetActions = 'YieldXyz Perps Get Actions',
|
|
121
|
+
YieldXyzPerpsGetAction = 'YieldXyz Perps Get Action',
|
|
122
|
+
YieldXyzPerpsSubmitTransaction = 'YieldXyz Perps Submit Transaction',
|
|
123
|
+
YieldXyzPerpsGetActivity = 'YieldXyz Perps Get Activity',
|
|
124
|
+
YieldXyzPerpsGetEvents = 'YieldXyz Perps Get Events',
|
|
125
|
+
YieldXyzPerpsGetEvent = 'YieldXyz Perps Get Event',
|
|
126
|
+
YieldXyzPerpsGetHealth = 'YieldXyz Perps Get Health',
|
|
127
|
+
YieldXyzPerpsGetProvidersHealth = 'YieldXyz Perps Get Providers Health',
|
|
100
128
|
}
|
|
101
129
|
|
|
102
130
|
export default Events
|
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,23 @@ 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
|
+
PortalPolicyError,
|
|
75
|
+
POLICY_ERROR_ID_PREFIX,
|
|
76
|
+
PolicyErrorId,
|
|
77
|
+
isMpcAuthFailure,
|
|
78
|
+
isPolicyViolation,
|
|
79
|
+
mpcErrorFrom,
|
|
54
80
|
ProviderRpcError,
|
|
55
81
|
RpcErrorCodes,
|
|
56
82
|
} from './errors'
|
package/src/requesters/http.ts
CHANGED
|
@@ -3,14 +3,43 @@ import {
|
|
|
3
3
|
type HttpRequestOptions,
|
|
4
4
|
type HttpRequesterOptions,
|
|
5
5
|
} from '../../types'
|
|
6
|
+
import { HttpError } from '../errors'
|
|
6
7
|
import { HttpRequest } from '../index'
|
|
7
8
|
import { TRACE_ID_HEADER } from '../tracing'
|
|
8
9
|
|
|
9
10
|
class HttpRequester {
|
|
10
11
|
public baseUrl: string
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
private readonly onUnauthorized?: () => Promise<void>
|
|
14
|
+
|
|
15
|
+
constructor({ baseUrl, onUnauthorized }: HttpRequesterOptions) {
|
|
13
16
|
this.baseUrl = baseUrl
|
|
17
|
+
this.onUnauthorized = onUnauthorized
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sends a request, notifying `onUnauthorized` on 401. Observational only:
|
|
22
|
+
* the hook's own failure is swallowed and the original `HttpError` is always
|
|
23
|
+
* what the caller sees.
|
|
24
|
+
*/
|
|
25
|
+
private async send<T>(requestOptions: HttpRequestOptions): Promise<T> {
|
|
26
|
+
try {
|
|
27
|
+
return (await new HttpRequest(requestOptions).send()) as T
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (
|
|
30
|
+
this.onUnauthorized &&
|
|
31
|
+
error instanceof HttpError &&
|
|
32
|
+
error.status === 401
|
|
33
|
+
) {
|
|
34
|
+
try {
|
|
35
|
+
await this.onUnauthorized()
|
|
36
|
+
} catch {
|
|
37
|
+
// Never let credential bookkeeping replace the original failure.
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
throw error
|
|
42
|
+
}
|
|
14
43
|
}
|
|
15
44
|
|
|
16
45
|
public buildHeaders(
|
|
@@ -35,10 +64,7 @@ class HttpRequester {
|
|
|
35
64
|
options?.traceId,
|
|
36
65
|
)
|
|
37
66
|
|
|
38
|
-
|
|
39
|
-
const response = (await request.send()) as T
|
|
40
|
-
|
|
41
|
-
return response
|
|
67
|
+
return this.send<T>(requestOptions)
|
|
42
68
|
}
|
|
43
69
|
|
|
44
70
|
public async get<T>(path: string, options?: HttpOptions): Promise<T> {
|
|
@@ -52,10 +78,7 @@ class HttpRequester {
|
|
|
52
78
|
options?.traceId,
|
|
53
79
|
)
|
|
54
80
|
|
|
55
|
-
|
|
56
|
-
const response = (await request.send()) as T
|
|
57
|
-
|
|
58
|
-
return response
|
|
81
|
+
return this.send<T>(requestOptions)
|
|
59
82
|
}
|
|
60
83
|
|
|
61
84
|
public async post<T>(path: string, options?: HttpOptions): Promise<T> {
|
|
@@ -73,10 +96,7 @@ class HttpRequester {
|
|
|
73
96
|
requestOptions.body = options.body
|
|
74
97
|
}
|
|
75
98
|
|
|
76
|
-
|
|
77
|
-
const response = (await request.send()) as T
|
|
78
|
-
|
|
79
|
-
return response
|
|
99
|
+
return this.send<T>(requestOptions)
|
|
80
100
|
}
|
|
81
101
|
|
|
82
102
|
public async put<T>(path: string, options: HttpOptions): Promise<T> {
|
|
@@ -94,10 +114,7 @@ class HttpRequester {
|
|
|
94
114
|
requestOptions.body = options.body
|
|
95
115
|
}
|
|
96
116
|
|
|
97
|
-
|
|
98
|
-
const response = (await request.send()) as T
|
|
99
|
-
|
|
100
|
-
return response
|
|
117
|
+
return this.send<T>(requestOptions)
|
|
101
118
|
}
|
|
102
119
|
|
|
103
120
|
public async patch<T>(path: string, options: HttpOptions): Promise<T> {
|
|
@@ -115,10 +132,7 @@ class HttpRequester {
|
|
|
115
132
|
requestOptions.body = options.body
|
|
116
133
|
}
|
|
117
134
|
|
|
118
|
-
|
|
119
|
-
const response = (await request.send()) as T
|
|
120
|
-
|
|
121
|
-
return response
|
|
135
|
+
return this.send<T>(requestOptions)
|
|
122
136
|
}
|
|
123
137
|
}
|
|
124
138
|
|
package/src/version/version.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "version": "
|
|
1
|
+
{ "version": "5.1.0" }
|
package/types.d.ts
CHANGED
|
@@ -59,11 +59,25 @@ interface ClientResponseMetadata {
|
|
|
59
59
|
interface ClientResponseMetadataNamespaces {
|
|
60
60
|
eip155?: ClientResponseNamespaceMetadataItem
|
|
61
61
|
solana?: ClientResponseNamespaceMetadataItem
|
|
62
|
+
bip122?: ClientResponseNamespaceMetadataItem
|
|
63
|
+
stellar?: ClientResponseNamespaceMetadataItem
|
|
64
|
+
tron?: ClientResponseNamespaceMetadataItem
|
|
65
|
+
xrpl?: ClientResponseNamespaceMetadataItem
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
interface ClientResponseNamespaceMetadataItem {
|
|
69
|
+
/**
|
|
70
|
+
* For `bip122` this is intentionally an empty string: Bitcoin has no single
|
|
71
|
+
* canonical address, so the usable addresses are sent under `bitcoin` instead.
|
|
72
|
+
*/
|
|
65
73
|
address: string
|
|
66
74
|
curve: PortalCurve
|
|
75
|
+
bitcoin?: {
|
|
76
|
+
p2wpkh?: {
|
|
77
|
+
mainnet?: string
|
|
78
|
+
testnet?: string
|
|
79
|
+
}
|
|
80
|
+
}
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
interface ClientResponseSharePair {
|
|
@@ -132,9 +146,25 @@ export interface Shares {
|
|
|
132
146
|
secp256k1: GenerateResponse
|
|
133
147
|
}
|
|
134
148
|
|
|
149
|
+
export interface Bip122Addresses {
|
|
150
|
+
p2wpkh?: {
|
|
151
|
+
mainnet?: string
|
|
152
|
+
testnet?: string
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
135
156
|
export interface AddressesByNamespace {
|
|
136
157
|
eip155: string
|
|
137
158
|
solana?: string
|
|
159
|
+
stellar?: string
|
|
160
|
+
tron?: string
|
|
161
|
+
xrpl?: string
|
|
162
|
+
/**
|
|
163
|
+
* Bitcoin has no single canonical address: the encoding is per-network and
|
|
164
|
+
* per-script-type, so addresses are keyed by script type and network rather
|
|
165
|
+
* than being a single string like the other namespaces.
|
|
166
|
+
*/
|
|
167
|
+
bip122?: Bip122Addresses
|
|
138
168
|
}
|
|
139
169
|
|
|
140
170
|
export interface WalletsByCurve {
|
|
@@ -191,6 +221,17 @@ export interface Presignature {
|
|
|
191
221
|
data: string
|
|
192
222
|
}
|
|
193
223
|
|
|
224
|
+
/** Any value a JSON document can hold. */
|
|
225
|
+
export type JsonValue =
|
|
226
|
+
| string
|
|
227
|
+
| number
|
|
228
|
+
| boolean
|
|
229
|
+
| null
|
|
230
|
+
| JsonValue[]
|
|
231
|
+
| { [key: string]: JsonValue }
|
|
232
|
+
|
|
233
|
+
export type JsonObject = { [key: string]: JsonValue }
|
|
234
|
+
|
|
194
235
|
export interface HttpErrorOptions {
|
|
195
236
|
message: string
|
|
196
237
|
status: number
|
|
@@ -205,6 +246,12 @@ export interface HttpOptions {
|
|
|
205
246
|
|
|
206
247
|
export interface HttpRequesterOptions extends RequesterOptions {
|
|
207
248
|
baseUrl: string
|
|
249
|
+
/**
|
|
250
|
+
* Invoked when a request to this host returns 401. Opt-in, because this class
|
|
251
|
+
* also talks to third-party hosts (Google Drive, the gateway RPC) where a 401
|
|
252
|
+
* says nothing about the Portal credential. Cannot change the outcome.
|
|
253
|
+
*/
|
|
254
|
+
onUnauthorized?: () => Promise<void>
|
|
208
255
|
}
|
|
209
256
|
|
|
210
257
|
export interface HttpRequestOptions {
|
|
@@ -239,31 +286,6 @@ export interface OutgoingRequest {
|
|
|
239
286
|
data: OutgoingRequestData
|
|
240
287
|
}
|
|
241
288
|
|
|
242
|
-
export interface QuoteArgs {
|
|
243
|
-
affiliateAddress?: string
|
|
244
|
-
buyAmount?: number
|
|
245
|
-
buyToken: string
|
|
246
|
-
buyTokenPercentageFee?: number
|
|
247
|
-
enableSlippageProtection?: boolean
|
|
248
|
-
excludedSources?: string
|
|
249
|
-
feeRecipient?: string
|
|
250
|
-
gasPrice?: number
|
|
251
|
-
includedSources?: string
|
|
252
|
-
intentOnFilling?: boolean
|
|
253
|
-
priceImpactProtectionPercentage?: number
|
|
254
|
-
sellAmount?: number
|
|
255
|
-
sellToken: string
|
|
256
|
-
skipValidation?: boolean
|
|
257
|
-
slippagePercentage?: number
|
|
258
|
-
takerAddress?: string
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export interface QuoteResponse {
|
|
262
|
-
allowanceTarget: string
|
|
263
|
-
cost: string
|
|
264
|
-
transaction: Eip1559 | LegacyTx
|
|
265
|
-
}
|
|
266
|
-
|
|
267
289
|
export interface RequesterOptions {}
|
|
268
290
|
|
|
269
291
|
export interface RpcErrorDefinition {
|
|
@@ -588,16 +610,17 @@ export interface BlockaidValidationFeature {
|
|
|
588
610
|
type: string
|
|
589
611
|
featureId: string
|
|
590
612
|
description: string
|
|
591
|
-
address?: string
|
|
613
|
+
address?: string | null
|
|
592
614
|
}
|
|
593
615
|
|
|
594
616
|
export interface BlockaidValidation {
|
|
595
|
-
classification?: string
|
|
596
|
-
description?: string
|
|
617
|
+
classification?: string | null
|
|
618
|
+
description?: string | null
|
|
597
619
|
features: BlockaidValidationFeature[]
|
|
598
|
-
reason?: string
|
|
599
|
-
resultType:
|
|
620
|
+
reason?: string | null
|
|
621
|
+
resultType: 'Benign' | 'Warning' | 'Malicious' | 'Error'
|
|
600
622
|
status: string
|
|
623
|
+
error?: string | null
|
|
601
624
|
}
|
|
602
625
|
|
|
603
626
|
export interface BlockaidAssetDiff {
|
|
@@ -608,15 +631,16 @@ export interface BlockaidAssetDiff {
|
|
|
608
631
|
|
|
609
632
|
export interface BlockaidSimulation {
|
|
610
633
|
accountAddress?: string
|
|
611
|
-
accountSummary
|
|
612
|
-
addressDetails
|
|
613
|
-
assetsDiffs
|
|
634
|
+
accountSummary?: Record<string, any>
|
|
635
|
+
addressDetails?: Record<string, any>
|
|
636
|
+
assetsDiffs?: Record<string, BlockaidAssetDiff[]>
|
|
614
637
|
block?: number
|
|
615
638
|
chain?: string
|
|
616
|
-
exposures
|
|
639
|
+
exposures?: Record<string, any>
|
|
617
640
|
status: string
|
|
618
|
-
totalUsdDiff
|
|
619
|
-
totalUsdExposure
|
|
641
|
+
totalUsdDiff?: Record<string, any>
|
|
642
|
+
totalUsdExposure?: Record<string, any>
|
|
643
|
+
error?: string
|
|
620
644
|
}
|
|
621
645
|
|
|
622
646
|
export interface BlockaidValidateTrxRes {
|