@portal-hq/utils 5.0.0 → 5.2.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 +4 -2
- package/lib/commonjs/errors/http-error.js +27 -0
- package/lib/commonjs/errors/index.js +12 -1
- package/lib/commonjs/errors/portal-idempotency-error.js +74 -0
- package/lib/commonjs/errors/portal-policy-error.js +83 -0
- package/lib/commonjs/events/index.js +29 -0
- package/lib/commonjs/idempotency/index.js +74 -0
- package/lib/commonjs/index.js +14 -1
- package/lib/commonjs/version/version.json +1 -1
- package/lib/esm/config/index.js +1 -0
- package/lib/esm/credentials/index.js +5 -3
- package/lib/esm/errors/http-error.js +27 -0
- package/lib/esm/errors/index.js +2 -0
- package/lib/esm/errors/portal-idempotency-error.js +69 -0
- package/lib/esm/errors/portal-policy-error.js +77 -0
- package/lib/esm/events/index.js +29 -0
- package/lib/esm/idempotency/index.js +66 -0
- package/lib/esm/index.js +2 -1
- 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 +5 -3
- package/src/definitions/index.ts +48 -0
- package/src/errors/http-error.ts +40 -1
- package/src/errors/index.ts +14 -0
- package/src/errors/portal-idempotency-error.ts +91 -0
- package/src/errors/portal-policy-error.ts +97 -0
- package/src/events/index.ts +30 -0
- package/src/idempotency/index.ts +97 -0
- package/src/index.ts +16 -0
- package/src/version/version.json +1 -1
- package/types.d.ts +27 -11
|
@@ -20,6 +20,7 @@ exports.SUPPORTED_CHAINS = [
|
|
|
20
20
|
{ namespace: 'eip155', reference: '84532' },
|
|
21
21
|
{ namespace: 'eip155', reference: '143' },
|
|
22
22
|
{ namespace: 'eip155', reference: '10143' },
|
|
23
|
+
{ namespace: 'eip155', reference: '42161' },
|
|
23
24
|
{ namespace: 'solana', reference: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' },
|
|
24
25
|
{ namespace: 'solana', reference: 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1' },
|
|
25
26
|
];
|
|
@@ -226,7 +226,9 @@ exports.reportUnauthorized = reportUnauthorized;
|
|
|
226
226
|
* envelope rather than a status code any requester here can see.
|
|
227
227
|
*
|
|
228
228
|
* The binary's error is rethrown unchanged either way — the invalidation is
|
|
229
|
-
* bookkeeping, and callers keep branching on `PortalMpcError` and its `id`.
|
|
229
|
+
* bookkeeping, and callers keep branching on `PortalMpcError` and its `id`. A
|
|
230
|
+
* Policy Engine rejection is promoted to `PortalPolicyError`, a subclass, so
|
|
231
|
+
* that stays true.
|
|
230
232
|
*/
|
|
231
233
|
function failFromMpcError(credentials, error) {
|
|
232
234
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -238,7 +240,7 @@ function failFromMpcError(credentials, error) {
|
|
|
238
240
|
logger_1.sdkLogger.error('[Portal] Failed to invalidate a credential the MPC binary rejected', bookkeeping);
|
|
239
241
|
}
|
|
240
242
|
}
|
|
241
|
-
throw
|
|
243
|
+
throw (0, errors_1.mpcErrorFrom)(error);
|
|
242
244
|
});
|
|
243
245
|
}
|
|
244
246
|
exports.failFromMpcError = failFromMpcError;
|
|
@@ -6,6 +6,33 @@ class HttpError extends Error {
|
|
|
6
6
|
// Set the prototype explicitly.
|
|
7
7
|
Object.setPrototypeOf(this, HttpError.prototype);
|
|
8
8
|
this.status = status;
|
|
9
|
+
const body = parseJsonBody(message);
|
|
10
|
+
if (!isJsonObject(body)) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (typeof body.error === 'string') {
|
|
14
|
+
this.errorMessage = body.error;
|
|
15
|
+
}
|
|
16
|
+
if (typeof body.id === 'string') {
|
|
17
|
+
this.id = body.id;
|
|
18
|
+
}
|
|
19
|
+
if (isJsonObject(body.details)) {
|
|
20
|
+
this.details = body.details;
|
|
21
|
+
}
|
|
9
22
|
}
|
|
10
23
|
}
|
|
24
|
+
function parseJsonBody(responseText) {
|
|
25
|
+
if (responseText.trim() === '') {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(responseText);
|
|
30
|
+
}
|
|
31
|
+
catch (_a) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function isJsonObject(value) {
|
|
36
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
37
|
+
}
|
|
11
38
|
exports.default = HttpError;
|
|
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.RpcErrorCodes = exports.ProviderRpcError = exports.PortalCredentialErrorReason = exports.PortalCredentialError = exports.PortalAuthErrorReason = exports.PortalAuthError = exports.NoAddressError = exports.MpcSigningError = exports.MissingOptionError = exports.InvalidGatewayConfigError = exports.InvalidChainIdError = exports.InvalidApiKeyError = exports.HttpError = exports.CloudStorageReadError = void 0;
|
|
20
|
+
exports.RpcErrorCodes = exports.ProviderRpcError = exports.mpcErrorFrom = exports.isPolicyViolation = exports.PolicyErrorId = exports.POLICY_ERROR_ID_PREFIX = exports.PortalPolicyError = exports.isIdempotencyRejection = exports.isIdempotencyKeyReused = exports.PortalIdempotencyErrorId = exports.PortalIdempotencyError = exports.PortalCredentialErrorReason = exports.PortalCredentialError = exports.PortalAuthErrorReason = exports.PortalAuthError = exports.NoAddressError = exports.MpcSigningError = exports.MissingOptionError = exports.InvalidGatewayConfigError = exports.InvalidChainIdError = exports.InvalidApiKeyError = exports.HttpError = exports.CloudStorageReadError = void 0;
|
|
21
21
|
var cloud_storage_read_error_1 = require("./cloud-storage-read-error");
|
|
22
22
|
Object.defineProperty(exports, "CloudStorageReadError", { enumerable: true, get: function () { return __importDefault(cloud_storage_read_error_1).default; } });
|
|
23
23
|
var http_error_1 = require("./http-error");
|
|
@@ -41,6 +41,17 @@ var portal_credential_error_1 = require("./portal-credential-error");
|
|
|
41
41
|
Object.defineProperty(exports, "PortalCredentialError", { enumerable: true, get: function () { return __importDefault(portal_credential_error_1).default; } });
|
|
42
42
|
Object.defineProperty(exports, "PortalCredentialErrorReason", { enumerable: true, get: function () { return portal_credential_error_1.PortalCredentialErrorReason; } });
|
|
43
43
|
__exportStar(require("./portal-error"), exports);
|
|
44
|
+
var portal_idempotency_error_1 = require("./portal-idempotency-error");
|
|
45
|
+
Object.defineProperty(exports, "PortalIdempotencyError", { enumerable: true, get: function () { return __importDefault(portal_idempotency_error_1).default; } });
|
|
46
|
+
Object.defineProperty(exports, "PortalIdempotencyErrorId", { enumerable: true, get: function () { return portal_idempotency_error_1.PortalIdempotencyErrorId; } });
|
|
47
|
+
Object.defineProperty(exports, "isIdempotencyKeyReused", { enumerable: true, get: function () { return portal_idempotency_error_1.isIdempotencyKeyReused; } });
|
|
48
|
+
Object.defineProperty(exports, "isIdempotencyRejection", { enumerable: true, get: function () { return portal_idempotency_error_1.isIdempotencyRejection; } });
|
|
49
|
+
var portal_policy_error_1 = require("./portal-policy-error");
|
|
50
|
+
Object.defineProperty(exports, "PortalPolicyError", { enumerable: true, get: function () { return __importDefault(portal_policy_error_1).default; } });
|
|
51
|
+
Object.defineProperty(exports, "POLICY_ERROR_ID_PREFIX", { enumerable: true, get: function () { return portal_policy_error_1.POLICY_ERROR_ID_PREFIX; } });
|
|
52
|
+
Object.defineProperty(exports, "PolicyErrorId", { enumerable: true, get: function () { return portal_policy_error_1.PolicyErrorId; } });
|
|
53
|
+
Object.defineProperty(exports, "isPolicyViolation", { enumerable: true, get: function () { return portal_policy_error_1.isPolicyViolation; } });
|
|
54
|
+
Object.defineProperty(exports, "mpcErrorFrom", { enumerable: true, get: function () { return portal_policy_error_1.mpcErrorFrom; } });
|
|
44
55
|
var provider_rpc_error_1 = require("./provider-rpc-error");
|
|
45
56
|
Object.defineProperty(exports, "ProviderRpcError", { enumerable: true, get: function () { return __importDefault(provider_rpc_error_1).default; } });
|
|
46
57
|
Object.defineProperty(exports, "RpcErrorCodes", { enumerable: true, get: function () { return provider_rpc_error_1.RpcErrorCodes; } });
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isIdempotencyKeyReused = exports.isIdempotencyRejection = exports.PortalIdempotencyErrorId = void 0;
|
|
5
|
+
/**
|
|
6
|
+
* The IDs Portal returns when it refuses a request because of its idempotency
|
|
7
|
+
* key. None of them carries the original transaction hash, and several mean
|
|
8
|
+
* Portal cannot confirm whether the transaction reached the chain: look it up
|
|
9
|
+
* before sending the operation again under a new key.
|
|
10
|
+
*/
|
|
11
|
+
exports.PortalIdempotencyErrorId = {
|
|
12
|
+
RequestInProgress: 'IDEMPOTENT_REQUEST_IN_PROGRESS',
|
|
13
|
+
RequestAlreadyCompleted: 'IDEMPOTENT_REQUEST_ALREADY_COMPLETED',
|
|
14
|
+
RequestPreviouslyFailed: 'IDEMPOTENT_REQUEST_PREVIOUSLY_FAILED',
|
|
15
|
+
RequestUnexpectedState: 'IDEMPOTENT_REQUEST_UNEXPECTED_STATE',
|
|
16
|
+
KeyReused: 'IDEMPOTENCY_KEY_REUSED',
|
|
17
|
+
TxMissing: 'IDEMPOTENT_TX_MISSING',
|
|
18
|
+
};
|
|
19
|
+
const IDEMPOTENCY_ERROR_IDS = new Set(Object.values(exports.PortalIdempotencyErrorId));
|
|
20
|
+
const errorIdOf = (error) => typeof error === 'object' && error !== null
|
|
21
|
+
? error.id
|
|
22
|
+
: undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Whether Portal refused the request because of its idempotency key: the
|
|
25
|
+
* error's `id` is one of {@link PortalIdempotencyErrorId}. Accepts a raw error
|
|
26
|
+
* envelope or a thrown error, and matches the ID exactly, never the message or
|
|
27
|
+
* `code`.
|
|
28
|
+
*/
|
|
29
|
+
const isIdempotencyRejection = (error) => {
|
|
30
|
+
const id = errorIdOf(error);
|
|
31
|
+
return typeof id === 'string' && IDEMPOTENCY_ERROR_IDS.has(id);
|
|
32
|
+
};
|
|
33
|
+
exports.isIdempotencyRejection = isIdempotencyRejection;
|
|
34
|
+
/**
|
|
35
|
+
* Whether the idempotency key was already used for a request with a different
|
|
36
|
+
* payload ({@link PortalIdempotencyErrorId.KeyReused}).
|
|
37
|
+
*/
|
|
38
|
+
const isIdempotencyKeyReused = (error) => errorIdOf(error) === exports.PortalIdempotencyErrorId.KeyReused;
|
|
39
|
+
exports.isIdempotencyKeyReused = isIdempotencyKeyReused;
|
|
40
|
+
/**
|
|
41
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
42
|
+
* when a consumer's tree resolves two `@portal-hq/utils`.
|
|
43
|
+
*
|
|
44
|
+
* This key and the members {@link PortalIdempotencyError.is} implies are a
|
|
45
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
46
|
+
*/
|
|
47
|
+
const BRAND = Symbol.for('@portal-hq/utils/PortalIdempotencyError');
|
|
48
|
+
/**
|
|
49
|
+
* Thrown by the SDK, before anything is signed or sent, when an idempotency key
|
|
50
|
+
* cannot be used: `invalidKey` for a key that breaks Portal's rules, and
|
|
51
|
+
* `unsupportedTarget` for a key on a broadcast Portal cannot deduplicate. The
|
|
52
|
+
* message names the problem and never contains the key.
|
|
53
|
+
*/
|
|
54
|
+
class PortalIdempotencyError extends Error {
|
|
55
|
+
constructor(reason, message) {
|
|
56
|
+
super(message);
|
|
57
|
+
/** @internal Cross-copy brand. Read it through {@link PortalIdempotencyError.is}. */
|
|
58
|
+
this[_a] = true;
|
|
59
|
+
Object.setPrototypeOf(this, PortalIdempotencyError.prototype);
|
|
60
|
+
this.name = 'PortalIdempotencyError';
|
|
61
|
+
this.reason = reason;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
65
|
+
* this class is intentional.
|
|
66
|
+
*/
|
|
67
|
+
static is(error) {
|
|
68
|
+
return (typeof error === 'object' &&
|
|
69
|
+
error !== null &&
|
|
70
|
+
error[BRAND] === true);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
_a = BRAND;
|
|
74
|
+
exports.default = PortalIdempotencyError;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.mpcErrorFrom = exports.PortalPolicyError = exports.isPolicyViolation = exports.PolicyErrorId = exports.POLICY_ERROR_ID_PREFIX = void 0;
|
|
5
|
+
const portal_error_1 = require("./portal-error");
|
|
6
|
+
/**
|
|
7
|
+
* Every Policy Engine rejection the gateway emits carries an ID under this
|
|
8
|
+
* prefix. Classification is by prefix, not by a known-ID list, so a policy kind
|
|
9
|
+
* added to the gateway later is still recognised without an SDK release.
|
|
10
|
+
*/
|
|
11
|
+
exports.POLICY_ERROR_ID_PREFIX = 'POLICY_';
|
|
12
|
+
/** The policy IDs the gateway emits today. The set is open — see the prefix. */
|
|
13
|
+
exports.PolicyErrorId = {
|
|
14
|
+
AmountLimitExceeded: 'POLICY_AMOUNT_LIMIT_EXCEEDED',
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Whether an error reports a Policy Engine rejection.
|
|
18
|
+
*
|
|
19
|
+
* Accepts a raw error envelope or a thrown {@link PortalMpcError}. Matched on
|
|
20
|
+
* the ID, never the companion `code` — the binary maps policy IDs onto a
|
|
21
|
+
* generic legacy code — and never on the message text.
|
|
22
|
+
*/
|
|
23
|
+
const isPolicyViolation = (error) => {
|
|
24
|
+
if (typeof error !== 'object' || error === null) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const { id } = error;
|
|
28
|
+
return typeof id === 'string' && id.startsWith(exports.POLICY_ERROR_ID_PREFIX);
|
|
29
|
+
};
|
|
30
|
+
exports.isPolicyViolation = isPolicyViolation;
|
|
31
|
+
/**
|
|
32
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
33
|
+
* when a consumer's tree resolves two `@portal-hq/utils` — a real policy
|
|
34
|
+
* rejection would then silently degrade to an ordinary MPC failure.
|
|
35
|
+
*
|
|
36
|
+
* This key and the members {@link PortalPolicyError.is} implies are a
|
|
37
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
38
|
+
*/
|
|
39
|
+
const BRAND = Symbol.for('@portal-hq/utils/PortalPolicyError');
|
|
40
|
+
/**
|
|
41
|
+
* A transaction the Policy Engine refused to sign. Extends
|
|
42
|
+
* {@link PortalMpcError} so callers already branching on that type, or reading
|
|
43
|
+
* its `id`, keep working unchanged. `id` always carries the `POLICY_*`
|
|
44
|
+
* identifier here, even though the inherited type leaves it optional.
|
|
45
|
+
*
|
|
46
|
+
* The rejection is terminal: the same request will be refused again, so it is
|
|
47
|
+
* not retried and it never invalidates the credential — the session is fine,
|
|
48
|
+
* the transaction is not.
|
|
49
|
+
*/
|
|
50
|
+
class PortalPolicyError extends portal_error_1.PortalMpcError {
|
|
51
|
+
constructor(error, cause) {
|
|
52
|
+
super(error);
|
|
53
|
+
/** @internal Cross-copy brand. Read it through {@link PortalPolicyError.is}. */
|
|
54
|
+
this[_a] = true;
|
|
55
|
+
Object.setPrototypeOf(this, PortalPolicyError.prototype);
|
|
56
|
+
this.cause = cause;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
60
|
+
* this class is intentional.
|
|
61
|
+
*/
|
|
62
|
+
static is(error) {
|
|
63
|
+
return (typeof error === 'object' &&
|
|
64
|
+
error !== null &&
|
|
65
|
+
error[BRAND] === true);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.PortalPolicyError = PortalPolicyError;
|
|
69
|
+
_a = BRAND;
|
|
70
|
+
/**
|
|
71
|
+
* Builds the error for an MPC envelope, promoting a Policy Engine rejection to
|
|
72
|
+
* {@link PortalPolicyError}. Every other envelope stays a {@link PortalMpcError},
|
|
73
|
+
* so only the policy path changes shape.
|
|
74
|
+
*
|
|
75
|
+
* @internal Exported only so the provider packages can share it across the
|
|
76
|
+
* package boundary. Not part of the public API — catch {@link PortalPolicyError}
|
|
77
|
+
* or call {@link isPolicyViolation} instead.
|
|
78
|
+
*/
|
|
79
|
+
const mpcErrorFrom = (error, cause) => (0, exports.isPolicyViolation)(error)
|
|
80
|
+
? new PortalPolicyError(error, cause)
|
|
81
|
+
: new portal_error_1.PortalMpcError(error);
|
|
82
|
+
exports.mpcErrorFrom = mpcErrorFrom;
|
|
83
|
+
exports.default = PortalPolicyError;
|
|
@@ -35,6 +35,18 @@ var Events;
|
|
|
35
35
|
Events["YieldXyzGetHistoricalActions"] = "YieldXyz Get Historical Actions";
|
|
36
36
|
Events["YieldXyzGetTransaction"] = "YieldXyz Get Transaction";
|
|
37
37
|
Events["YieldXyzTrack"] = "YieldXyz Track";
|
|
38
|
+
Events["YieldXyzBorrowGetIntegrations"] = "YieldXyz Borrow Get Integrations";
|
|
39
|
+
Events["YieldXyzBorrowGetIntegration"] = "YieldXyz Borrow Get Integration";
|
|
40
|
+
Events["YieldXyzBorrowGetMarkets"] = "YieldXyz Borrow Get Markets";
|
|
41
|
+
Events["YieldXyzBorrowGetMarket"] = "YieldXyz Borrow Get Market";
|
|
42
|
+
Events["YieldXyzBorrowGetPositions"] = "YieldXyz Borrow Get Positions";
|
|
43
|
+
Events["YieldXyzBorrowGetLiquidations"] = "YieldXyz Borrow Get Liquidations";
|
|
44
|
+
Events["YieldXyzBorrowCreateAction"] = "YieldXyz Borrow Create Action";
|
|
45
|
+
Events["YieldXyzBorrowGetActions"] = "YieldXyz Borrow Get Actions";
|
|
46
|
+
Events["YieldXyzBorrowGetAction"] = "YieldXyz Borrow Get Action";
|
|
47
|
+
Events["YieldXyzBorrowStepAction"] = "YieldXyz Borrow Step Action";
|
|
48
|
+
Events["YieldXyzBorrowSubmitTransaction"] = "YieldXyz Borrow Submit Transaction";
|
|
49
|
+
Events["YieldXyzBorrowGetHealth"] = "YieldXyz Borrow Get Health";
|
|
38
50
|
Events["LifiGetRoutes"] = "Lifi Get Routes";
|
|
39
51
|
Events["LifiGetQuote"] = "Lifi Get Quote";
|
|
40
52
|
Events["LifiGetStatus"] = "Lifi Get Status";
|
|
@@ -89,5 +101,22 @@ var Events;
|
|
|
89
101
|
Events["MeldGetBuyLimits"] = "Meld Get Buy Limits";
|
|
90
102
|
Events["MeldGetSellLimits"] = "Meld Get Sell Limits";
|
|
91
103
|
Events["MeldGetKycLimits"] = "Meld Get Kyc Limits";
|
|
104
|
+
Events["YieldXyzPerpsGetProviders"] = "YieldXyz Perps Get Providers";
|
|
105
|
+
Events["YieldXyzPerpsGetProvider"] = "YieldXyz Perps Get Provider";
|
|
106
|
+
Events["YieldXyzPerpsGetMarkets"] = "YieldXyz Perps Get Markets";
|
|
107
|
+
Events["YieldXyzPerpsGetMarket"] = "YieldXyz Perps Get Market";
|
|
108
|
+
Events["YieldXyzPerpsGetMarketCandles"] = "YieldXyz Perps Get Market Candles";
|
|
109
|
+
Events["YieldXyzPerpsGetPositions"] = "YieldXyz Perps Get Positions";
|
|
110
|
+
Events["YieldXyzPerpsGetOrders"] = "YieldXyz Perps Get Orders";
|
|
111
|
+
Events["YieldXyzPerpsGetBalances"] = "YieldXyz Perps Get Balances";
|
|
112
|
+
Events["YieldXyzPerpsCreateAction"] = "YieldXyz Perps Create Action";
|
|
113
|
+
Events["YieldXyzPerpsGetActions"] = "YieldXyz Perps Get Actions";
|
|
114
|
+
Events["YieldXyzPerpsGetAction"] = "YieldXyz Perps Get Action";
|
|
115
|
+
Events["YieldXyzPerpsSubmitTransaction"] = "YieldXyz Perps Submit Transaction";
|
|
116
|
+
Events["YieldXyzPerpsGetActivity"] = "YieldXyz Perps Get Activity";
|
|
117
|
+
Events["YieldXyzPerpsGetEvents"] = "YieldXyz Perps Get Events";
|
|
118
|
+
Events["YieldXyzPerpsGetEvent"] = "YieldXyz Perps Get Event";
|
|
119
|
+
Events["YieldXyzPerpsGetHealth"] = "YieldXyz Perps Get Health";
|
|
120
|
+
Events["YieldXyzPerpsGetProvidersHealth"] = "YieldXyz Perps Get Providers Health";
|
|
92
121
|
})(Events || (Events = {}));
|
|
93
122
|
exports.default = Events;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateIdempotencyKey = exports.generateIdempotencyKey = exports.PORTAL_IDEMPOTENCY_KEY_HEADER = void 0;
|
|
7
|
+
const portal_idempotency_error_1 = __importDefault(require("../errors/portal-idempotency-error"));
|
|
8
|
+
const tracing_1 = require("../tracing");
|
|
9
|
+
/**
|
|
10
|
+
* The HTTP header the Enclave MPC API reads a transaction broadcast's
|
|
11
|
+
* idempotency key from.
|
|
12
|
+
*/
|
|
13
|
+
exports.PORTAL_IDEMPOTENCY_KEY_HEADER = 'Idempotency-Key';
|
|
14
|
+
const IDEMPOTENCY_KEY_MAX_LENGTH = 255;
|
|
15
|
+
/** RFC 3986 unreserved characters, the only ones Portal accepts in a key. */
|
|
16
|
+
const IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9._~-]+$/;
|
|
17
|
+
const uuidV4FromRandomValues = (getRandomValues) => {
|
|
18
|
+
const bytes = getRandomValues(new Uint8Array(16));
|
|
19
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
20
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
21
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
22
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Generates an idempotency key: a lowercase UUID v4. Generate one per logical
|
|
26
|
+
* operation, store it with the pending operation, and reuse it only to retry
|
|
27
|
+
* that identical request.
|
|
28
|
+
*
|
|
29
|
+
* The key comes from `crypto.randomUUID` or `crypto.getRandomValues` when the
|
|
30
|
+
* runtime provides one. Hermes provides neither unless the app installs a
|
|
31
|
+
* polyfill such as `react-native-get-random-values`; without one the key is
|
|
32
|
+
* built from `Math.random`, which keeps keys unique in practice but is not a
|
|
33
|
+
* cryptographic source.
|
|
34
|
+
*/
|
|
35
|
+
const generateIdempotencyKey = () => {
|
|
36
|
+
const runtimeCrypto = typeof crypto === 'undefined' ? undefined : crypto;
|
|
37
|
+
if (typeof (runtimeCrypto === null || runtimeCrypto === void 0 ? void 0 : runtimeCrypto.randomUUID) === 'function') {
|
|
38
|
+
return runtimeCrypto.randomUUID().toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
if (typeof (runtimeCrypto === null || runtimeCrypto === void 0 ? void 0 : runtimeCrypto.getRandomValues) === 'function') {
|
|
41
|
+
return uuidV4FromRandomValues(runtimeCrypto.getRandomValues.bind(runtimeCrypto));
|
|
42
|
+
}
|
|
43
|
+
// With neither API available, generateTraceId builds the key from
|
|
44
|
+
// Math.random.
|
|
45
|
+
return (0, tracing_1.generateTraceId)().toLowerCase();
|
|
46
|
+
};
|
|
47
|
+
exports.generateIdempotencyKey = generateIdempotencyKey;
|
|
48
|
+
/**
|
|
49
|
+
* Trims `key` the way Portal does (`String.prototype.trim`) and checks it
|
|
50
|
+
* against Portal's rules: 1–255 characters of `A-Z a-z 0-9 . _ ~ -`.
|
|
51
|
+
*
|
|
52
|
+
* @internal Exported only so the SDK packages can share it across the package
|
|
53
|
+
* boundary. Not part of the public API.
|
|
54
|
+
*
|
|
55
|
+
* @returns The trimmed key, which is the value the SDK sends.
|
|
56
|
+
* @throws {PortalIdempotencyError} `invalidKey`, naming the rule that failed.
|
|
57
|
+
*/
|
|
58
|
+
const validateIdempotencyKey = (key) => {
|
|
59
|
+
if (typeof key !== 'string') {
|
|
60
|
+
throw new portal_idempotency_error_1.default('invalidKey', 'idempotencyKey must be a string');
|
|
61
|
+
}
|
|
62
|
+
const trimmed = key.trim();
|
|
63
|
+
if (trimmed.length === 0) {
|
|
64
|
+
throw new portal_idempotency_error_1.default('invalidKey', 'idempotencyKey must not be empty or whitespace-only');
|
|
65
|
+
}
|
|
66
|
+
if (trimmed.length > IDEMPOTENCY_KEY_MAX_LENGTH) {
|
|
67
|
+
throw new portal_idempotency_error_1.default('invalidKey', `idempotencyKey must be at most ${IDEMPOTENCY_KEY_MAX_LENGTH} characters`);
|
|
68
|
+
}
|
|
69
|
+
if (!IDEMPOTENCY_KEY_PATTERN.test(trimmed)) {
|
|
70
|
+
throw new portal_idempotency_error_1.default('invalidKey', "idempotencyKey may only contain the characters A-Z, a-z, 0-9, '.', '_', '~' and '-'");
|
|
71
|
+
}
|
|
72
|
+
return trimmed;
|
|
73
|
+
};
|
|
74
|
+
exports.validateIdempotencyKey = validateIdempotencyKey;
|
package/lib/commonjs/index.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PortalMpcError = exports.PortalErrorCodeTypes = exports.PortalErrorCodes = exports.MPC_AUTH_FAILED_ERROR_ID = exports.PortalCredentialErrorReason = exports.PortalCredentialError = exports.PortalAuthErrorReason = exports.PortalAuthError = exports.NoAddressError = exports.MpcSigningError = exports.MissingOptionError = exports.InvalidGatewayConfigError = exports.InvalidChainIdError = exports.InvalidApiKeyError = exports.HttpError = exports.CloudStorageReadError = exports.Storage = exports.PortalRequestMethod = exports.PasswordStorage = exports.MpcStatuses = exports.KeychainAdapter = exports.convert = exports.convertCryptoToLocal = exports.convertLocalToCrypto = exports.staticApiKeyOf = exports.resolveCredentialToken = exports.resolveCredentials = exports.reportUnauthorized = exports.onCredentialsInvalidated = exports.invalidateCredentials = exports.failFromMpcError = exports.StaticCredentials = exports.DEFAULT_HOSTS = exports.buildDefaultGatewayConfig = exports.NAMESPACE_TO_CURVE = exports.ALL_CHAIN_IDS = exports.BITCOIN_CHAIN_IDS = exports.TRON_CHAIN_IDS = exports.SOLANA_CHAIN_IDS = exports.EVM_CHAIN_IDS = exports.DEFAULT_CHAIN_ID_CAIP2 = exports.DEFAULT_CHAIN_ID_NUMERIC = exports.CAIP2_CHAIN_ID_REGEX = exports.FRIENDLY_CHAIN_NAME_TO_CAIP2 = exports.SUPPORTED_BITCOIN_P2WPKH_CHAINS = exports.CHAIN_NAMESPACES = exports.getChainNameForId = exports.getChainIdForName = exports.chainNameToId = exports.chainIdToName = void 0;
|
|
7
|
-
exports.sdkLogger = exports.UUID_V4_REGEX = exports.TRACE_ID_HEADER = exports.createExecutionContext = exports.generateTraceId = exports.EvaluateTransactionOperationType = exports.HttpRequester = exports.PortalRequests = exports.HttpRequest = exports.parseRequest = exports.numberToHexString = exports.getClientPlatformVersion = exports.Events = exports.RpcErrorCodes = exports.ProviderRpcError = exports.isMpcAuthFailure = void 0;
|
|
7
|
+
exports.sdkLogger = exports.UUID_V4_REGEX = exports.TRACE_ID_HEADER = exports.createExecutionContext = exports.generateTraceId = exports.EvaluateTransactionOperationType = exports.HttpRequester = exports.PortalRequests = exports.HttpRequest = exports.parseRequest = exports.numberToHexString = exports.getClientPlatformVersion = exports.Events = exports.validateIdempotencyKey = exports.PORTAL_IDEMPOTENCY_KEY_HEADER = exports.generateIdempotencyKey = exports.RpcErrorCodes = exports.ProviderRpcError = exports.mpcErrorFrom = exports.isPolicyViolation = exports.isMpcAuthFailure = exports.PolicyErrorId = exports.POLICY_ERROR_ID_PREFIX = exports.PortalPolicyError = exports.isIdempotencyRejection = exports.isIdempotencyKeyReused = exports.PortalIdempotencyErrorId = exports.PortalIdempotencyError = void 0;
|
|
8
8
|
var chains_1 = require("./chains");
|
|
9
9
|
Object.defineProperty(exports, "chainIdToName", { enumerable: true, get: function () { return chains_1.chainIdToName; } });
|
|
10
10
|
Object.defineProperty(exports, "chainNameToId", { enumerable: true, get: function () { return chains_1.chainNameToId; } });
|
|
@@ -61,9 +61,22 @@ Object.defineProperty(exports, "MPC_AUTH_FAILED_ERROR_ID", { enumerable: true, g
|
|
|
61
61
|
Object.defineProperty(exports, "PortalErrorCodes", { enumerable: true, get: function () { return errors_1.PortalErrorCodes; } });
|
|
62
62
|
Object.defineProperty(exports, "PortalErrorCodeTypes", { enumerable: true, get: function () { return errors_1.PortalErrorCodeTypes; } });
|
|
63
63
|
Object.defineProperty(exports, "PortalMpcError", { enumerable: true, get: function () { return errors_1.PortalMpcError; } });
|
|
64
|
+
Object.defineProperty(exports, "PortalIdempotencyError", { enumerable: true, get: function () { return errors_1.PortalIdempotencyError; } });
|
|
65
|
+
Object.defineProperty(exports, "PortalIdempotencyErrorId", { enumerable: true, get: function () { return errors_1.PortalIdempotencyErrorId; } });
|
|
66
|
+
Object.defineProperty(exports, "isIdempotencyKeyReused", { enumerable: true, get: function () { return errors_1.isIdempotencyKeyReused; } });
|
|
67
|
+
Object.defineProperty(exports, "isIdempotencyRejection", { enumerable: true, get: function () { return errors_1.isIdempotencyRejection; } });
|
|
68
|
+
Object.defineProperty(exports, "PortalPolicyError", { enumerable: true, get: function () { return errors_1.PortalPolicyError; } });
|
|
69
|
+
Object.defineProperty(exports, "POLICY_ERROR_ID_PREFIX", { enumerable: true, get: function () { return errors_1.POLICY_ERROR_ID_PREFIX; } });
|
|
70
|
+
Object.defineProperty(exports, "PolicyErrorId", { enumerable: true, get: function () { return errors_1.PolicyErrorId; } });
|
|
64
71
|
Object.defineProperty(exports, "isMpcAuthFailure", { enumerable: true, get: function () { return errors_1.isMpcAuthFailure; } });
|
|
72
|
+
Object.defineProperty(exports, "isPolicyViolation", { enumerable: true, get: function () { return errors_1.isPolicyViolation; } });
|
|
73
|
+
Object.defineProperty(exports, "mpcErrorFrom", { enumerable: true, get: function () { return errors_1.mpcErrorFrom; } });
|
|
65
74
|
Object.defineProperty(exports, "ProviderRpcError", { enumerable: true, get: function () { return errors_1.ProviderRpcError; } });
|
|
66
75
|
Object.defineProperty(exports, "RpcErrorCodes", { enumerable: true, get: function () { return errors_1.RpcErrorCodes; } });
|
|
76
|
+
var idempotency_1 = require("./idempotency");
|
|
77
|
+
Object.defineProperty(exports, "generateIdempotencyKey", { enumerable: true, get: function () { return idempotency_1.generateIdempotencyKey; } });
|
|
78
|
+
Object.defineProperty(exports, "PORTAL_IDEMPOTENCY_KEY_HEADER", { enumerable: true, get: function () { return idempotency_1.PORTAL_IDEMPOTENCY_KEY_HEADER; } });
|
|
79
|
+
Object.defineProperty(exports, "validateIdempotencyKey", { enumerable: true, get: function () { return idempotency_1.validateIdempotencyKey; } });
|
|
67
80
|
var events_1 = require("./events");
|
|
68
81
|
Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return __importDefault(events_1).default; } });
|
|
69
82
|
var version_1 = require("./version");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "version": "5.
|
|
1
|
+
{ "version": "5.2.0" }
|
package/lib/esm/config/index.js
CHANGED
|
@@ -17,6 +17,7 @@ export const SUPPORTED_CHAINS = [
|
|
|
17
17
|
{ namespace: 'eip155', reference: '84532' },
|
|
18
18
|
{ namespace: 'eip155', reference: '143' },
|
|
19
19
|
{ namespace: 'eip155', reference: '10143' },
|
|
20
|
+
{ namespace: 'eip155', reference: '42161' },
|
|
20
21
|
{ namespace: 'solana', reference: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' },
|
|
21
22
|
{ namespace: 'solana', reference: 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1' },
|
|
22
23
|
];
|
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
var _a;
|
|
11
|
-
import { InvalidApiKeyError, PortalCredentialError, PortalCredentialErrorReason,
|
|
11
|
+
import { InvalidApiKeyError, PortalCredentialError, PortalCredentialErrorReason, isMpcAuthFailure, mpcErrorFrom, } from '../errors';
|
|
12
12
|
import { sdkLogger } from '../logger';
|
|
13
13
|
/**
|
|
14
14
|
* Cross-copy brand. The key and the branded shape (a string `value`) are a
|
|
@@ -217,7 +217,9 @@ export function reportUnauthorized(credentials) {
|
|
|
217
217
|
* envelope rather than a status code any requester here can see.
|
|
218
218
|
*
|
|
219
219
|
* The binary's error is rethrown unchanged either way — the invalidation is
|
|
220
|
-
* bookkeeping, and callers keep branching on `PortalMpcError` and its `id`.
|
|
220
|
+
* bookkeeping, and callers keep branching on `PortalMpcError` and its `id`. A
|
|
221
|
+
* Policy Engine rejection is promoted to `PortalPolicyError`, a subclass, so
|
|
222
|
+
* that stays true.
|
|
221
223
|
*/
|
|
222
224
|
export function failFromMpcError(credentials, error) {
|
|
223
225
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -229,7 +231,7 @@ export function failFromMpcError(credentials, error) {
|
|
|
229
231
|
sdkLogger.error('[Portal] Failed to invalidate a credential the MPC binary rejected', bookkeeping);
|
|
230
232
|
}
|
|
231
233
|
}
|
|
232
|
-
throw
|
|
234
|
+
throw mpcErrorFrom(error);
|
|
233
235
|
});
|
|
234
236
|
}
|
|
235
237
|
/**
|
|
@@ -4,6 +4,33 @@ class HttpError extends Error {
|
|
|
4
4
|
// Set the prototype explicitly.
|
|
5
5
|
Object.setPrototypeOf(this, HttpError.prototype);
|
|
6
6
|
this.status = status;
|
|
7
|
+
const body = parseJsonBody(message);
|
|
8
|
+
if (!isJsonObject(body)) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (typeof body.error === 'string') {
|
|
12
|
+
this.errorMessage = body.error;
|
|
13
|
+
}
|
|
14
|
+
if (typeof body.id === 'string') {
|
|
15
|
+
this.id = body.id;
|
|
16
|
+
}
|
|
17
|
+
if (isJsonObject(body.details)) {
|
|
18
|
+
this.details = body.details;
|
|
19
|
+
}
|
|
7
20
|
}
|
|
8
21
|
}
|
|
22
|
+
function parseJsonBody(responseText) {
|
|
23
|
+
if (responseText.trim() === '') {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(responseText);
|
|
28
|
+
}
|
|
29
|
+
catch (_a) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function isJsonObject(value) {
|
|
34
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
35
|
+
}
|
|
9
36
|
export default HttpError;
|
package/lib/esm/errors/index.js
CHANGED
|
@@ -9,4 +9,6 @@ export { default as NoAddressError } from './no-address-error';
|
|
|
9
9
|
export { default as PortalAuthError, PortalAuthErrorReason, } from './portal-auth-error';
|
|
10
10
|
export { default as PortalCredentialError, PortalCredentialErrorReason, } from './portal-credential-error';
|
|
11
11
|
export * from './portal-error';
|
|
12
|
+
export { default as PortalIdempotencyError, PortalIdempotencyErrorId, isIdempotencyKeyReused, isIdempotencyRejection, } from './portal-idempotency-error';
|
|
13
|
+
export { default as PortalPolicyError, POLICY_ERROR_ID_PREFIX, PolicyErrorId, isPolicyViolation, mpcErrorFrom, } from './portal-policy-error';
|
|
12
14
|
export { default as ProviderRpcError, RpcErrorCodes, } from './provider-rpc-error';
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
/**
|
|
3
|
+
* The IDs Portal returns when it refuses a request because of its idempotency
|
|
4
|
+
* key. None of them carries the original transaction hash, and several mean
|
|
5
|
+
* Portal cannot confirm whether the transaction reached the chain: look it up
|
|
6
|
+
* before sending the operation again under a new key.
|
|
7
|
+
*/
|
|
8
|
+
export const PortalIdempotencyErrorId = {
|
|
9
|
+
RequestInProgress: 'IDEMPOTENT_REQUEST_IN_PROGRESS',
|
|
10
|
+
RequestAlreadyCompleted: 'IDEMPOTENT_REQUEST_ALREADY_COMPLETED',
|
|
11
|
+
RequestPreviouslyFailed: 'IDEMPOTENT_REQUEST_PREVIOUSLY_FAILED',
|
|
12
|
+
RequestUnexpectedState: 'IDEMPOTENT_REQUEST_UNEXPECTED_STATE',
|
|
13
|
+
KeyReused: 'IDEMPOTENCY_KEY_REUSED',
|
|
14
|
+
TxMissing: 'IDEMPOTENT_TX_MISSING',
|
|
15
|
+
};
|
|
16
|
+
const IDEMPOTENCY_ERROR_IDS = new Set(Object.values(PortalIdempotencyErrorId));
|
|
17
|
+
const errorIdOf = (error) => typeof error === 'object' && error !== null
|
|
18
|
+
? error.id
|
|
19
|
+
: undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Whether Portal refused the request because of its idempotency key: the
|
|
22
|
+
* error's `id` is one of {@link PortalIdempotencyErrorId}. Accepts a raw error
|
|
23
|
+
* envelope or a thrown error, and matches the ID exactly, never the message or
|
|
24
|
+
* `code`.
|
|
25
|
+
*/
|
|
26
|
+
export const isIdempotencyRejection = (error) => {
|
|
27
|
+
const id = errorIdOf(error);
|
|
28
|
+
return typeof id === 'string' && IDEMPOTENCY_ERROR_IDS.has(id);
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Whether the idempotency key was already used for a request with a different
|
|
32
|
+
* payload ({@link PortalIdempotencyErrorId.KeyReused}).
|
|
33
|
+
*/
|
|
34
|
+
export const isIdempotencyKeyReused = (error) => errorIdOf(error) === PortalIdempotencyErrorId.KeyReused;
|
|
35
|
+
/**
|
|
36
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
37
|
+
* when a consumer's tree resolves two `@portal-hq/utils`.
|
|
38
|
+
*
|
|
39
|
+
* This key and the members {@link PortalIdempotencyError.is} implies are a
|
|
40
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
41
|
+
*/
|
|
42
|
+
const BRAND = Symbol.for('@portal-hq/utils/PortalIdempotencyError');
|
|
43
|
+
/**
|
|
44
|
+
* Thrown by the SDK, before anything is signed or sent, when an idempotency key
|
|
45
|
+
* cannot be used: `invalidKey` for a key that breaks Portal's rules, and
|
|
46
|
+
* `unsupportedTarget` for a key on a broadcast Portal cannot deduplicate. The
|
|
47
|
+
* message names the problem and never contains the key.
|
|
48
|
+
*/
|
|
49
|
+
class PortalIdempotencyError extends Error {
|
|
50
|
+
constructor(reason, message) {
|
|
51
|
+
super(message);
|
|
52
|
+
/** @internal Cross-copy brand. Read it through {@link PortalIdempotencyError.is}. */
|
|
53
|
+
this[_a] = true;
|
|
54
|
+
Object.setPrototypeOf(this, PortalIdempotencyError.prototype);
|
|
55
|
+
this.name = 'PortalIdempotencyError';
|
|
56
|
+
this.reason = reason;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
60
|
+
* this class is intentional.
|
|
61
|
+
*/
|
|
62
|
+
static is(error) {
|
|
63
|
+
return (typeof error === 'object' &&
|
|
64
|
+
error !== null &&
|
|
65
|
+
error[BRAND] === true);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
_a = BRAND;
|
|
69
|
+
export default PortalIdempotencyError;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { PortalMpcError } from './portal-error';
|
|
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
|
+
/** The policy IDs the gateway emits today. The set is open — see the prefix. */
|
|
10
|
+
export const PolicyErrorId = {
|
|
11
|
+
AmountLimitExceeded: 'POLICY_AMOUNT_LIMIT_EXCEEDED',
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Whether an error reports a Policy Engine rejection.
|
|
15
|
+
*
|
|
16
|
+
* Accepts a raw error envelope or a thrown {@link PortalMpcError}. Matched on
|
|
17
|
+
* the ID, never the companion `code` — the binary maps policy IDs onto a
|
|
18
|
+
* generic legacy code — and never on the message text.
|
|
19
|
+
*/
|
|
20
|
+
export const isPolicyViolation = (error) => {
|
|
21
|
+
if (typeof error !== 'object' || error === null) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
const { id } = error;
|
|
25
|
+
return typeof id === 'string' && id.startsWith(POLICY_ERROR_ID_PREFIX);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cross-copy brand. `instanceof` compares prototypes, so it returns `false`
|
|
29
|
+
* when a consumer's tree resolves two `@portal-hq/utils` — a real policy
|
|
30
|
+
* rejection would then silently degrade to an ordinary MPC failure.
|
|
31
|
+
*
|
|
32
|
+
* This key and the members {@link PortalPolicyError.is} implies are a
|
|
33
|
+
* compatibility contract: changing either breaks recognition between copies.
|
|
34
|
+
*/
|
|
35
|
+
const BRAND = Symbol.for('@portal-hq/utils/PortalPolicyError');
|
|
36
|
+
/**
|
|
37
|
+
* A transaction the Policy Engine refused to sign. Extends
|
|
38
|
+
* {@link PortalMpcError} so callers already branching on that type, or reading
|
|
39
|
+
* its `id`, keep working unchanged. `id` always carries the `POLICY_*`
|
|
40
|
+
* identifier here, even though the inherited type leaves it optional.
|
|
41
|
+
*
|
|
42
|
+
* The rejection is terminal: the same request will be refused again, so it is
|
|
43
|
+
* not retried and it never invalidates the credential — the session is fine,
|
|
44
|
+
* the transaction is not.
|
|
45
|
+
*/
|
|
46
|
+
export class PortalPolicyError extends PortalMpcError {
|
|
47
|
+
constructor(error, cause) {
|
|
48
|
+
super(error);
|
|
49
|
+
/** @internal Cross-copy brand. Read it through {@link PortalPolicyError.is}. */
|
|
50
|
+
this[_a] = true;
|
|
51
|
+
Object.setPrototypeOf(this, PortalPolicyError.prototype);
|
|
52
|
+
this.cause = cause;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Brand check — see BRAND. Narrowing a branded error from another copy to
|
|
56
|
+
* this class is intentional.
|
|
57
|
+
*/
|
|
58
|
+
static is(error) {
|
|
59
|
+
return (typeof error === 'object' &&
|
|
60
|
+
error !== null &&
|
|
61
|
+
error[BRAND] === true);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
_a = BRAND;
|
|
65
|
+
/**
|
|
66
|
+
* Builds the error for an MPC envelope, promoting a Policy Engine rejection to
|
|
67
|
+
* {@link PortalPolicyError}. Every other envelope stays a {@link PortalMpcError},
|
|
68
|
+
* so only the policy path changes shape.
|
|
69
|
+
*
|
|
70
|
+
* @internal Exported only so the provider packages can share it across the
|
|
71
|
+
* package boundary. Not part of the public API — catch {@link PortalPolicyError}
|
|
72
|
+
* or call {@link isPolicyViolation} instead.
|
|
73
|
+
*/
|
|
74
|
+
export const mpcErrorFrom = (error, cause) => isPolicyViolation(error)
|
|
75
|
+
? new PortalPolicyError(error, cause)
|
|
76
|
+
: new PortalMpcError(error);
|
|
77
|
+
export default PortalPolicyError;
|