@metamask-previews/snap-networks-utils 3.0.0-preview-fea83f42 → 3.0.0-preview-3fffa79
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/CHANGELOG.md +0 -1
- package/dist/clients/security-alerts-api/SecurityAlertsApiClient.d.mts +2 -11
- package/dist/clients/security-alerts-api/SecurityAlertsApiClient.d.mts.map +1 -1
- package/dist/clients/security-alerts-api/SecurityAlertsApiClient.mjs +5 -27
- package/dist/clients/security-alerts-api/SecurityAlertsApiClient.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -15,7 +15,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
15
15
|
- Add the `FetchPolicy` type, the minimal policy surface `executeWithFetchPolicy` and `fetchWithPolicy` accept ([#478](https://github.com/MetaMask/internal-snaps/pull/478))
|
|
16
16
|
- Add the shared `BaseTransactionScanService` base class for network snap transaction scan services, owning the shared scan dependencies (Security Alerts API client, analytics, logger, error reporting) and the scan analytics orchestration ([#397](https://github.com/MetaMask/internal-snaps/pull/397))
|
|
17
17
|
- Add the shared Security Alerts API HTTP client for `BaseTransactionScanService`.
|
|
18
|
-
- Add an optional `policy` to the shared Security Alerts API client options, retrying transient scan failures (network errors, timeouts, HTTP 408, 425, 429, 500, 502, 503 and 504) with backoff and circuit breaking when given ([#492](https://github.com/MetaMask/internal-snaps/pull/492))
|
|
19
18
|
|
|
20
19
|
## [3.0.0]
|
|
21
20
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { FetchPolicy } from "../../utils/fetchPolicy/executeWithFetchPolicy.mjs";
|
|
2
1
|
/**
|
|
3
2
|
* Headers sent with every Security Alerts API request.
|
|
4
3
|
*/
|
|
@@ -13,12 +12,6 @@ export type SecurityAlertsApiClientOptions = {
|
|
|
13
12
|
scanUrl: string;
|
|
14
13
|
/** The fetch implementation to use. Defaults to `globalThis.fetch`. */
|
|
15
14
|
fetch?: typeof globalThis.fetch;
|
|
16
|
-
/**
|
|
17
|
-
* Optional fetch policy (see `createFetchPolicy`). When given, transient
|
|
18
|
-
* failures are retried with backoff and a circuit breaker. Without one, the
|
|
19
|
-
* request is made directly.
|
|
20
|
-
*/
|
|
21
|
-
policy?: FetchPolicy;
|
|
22
15
|
};
|
|
23
16
|
/**
|
|
24
17
|
* HTTP client for a Security Alerts API scan endpoint.
|
|
@@ -42,10 +35,8 @@ export declare class SecurityAlertsApiClient {
|
|
|
42
35
|
* an invalid URL fails fast at construction.
|
|
43
36
|
* @param options.fetch - Optional fetch implementation; defaults to
|
|
44
37
|
* `globalThis.fetch`.
|
|
45
|
-
* @param options.policy - Optional fetch policy for retries and circuit
|
|
46
|
-
* breaking.
|
|
47
38
|
*/
|
|
48
|
-
constructor({ scanUrl, fetch: fetchFn,
|
|
39
|
+
constructor({ scanUrl, fetch: fetchFn, }: SecurityAlertsApiClientOptions);
|
|
49
40
|
/**
|
|
50
41
|
* Scans a transaction by posting the request body to the scan endpoint.
|
|
51
42
|
*
|
|
@@ -54,6 +45,6 @@ export declare class SecurityAlertsApiClient {
|
|
|
54
45
|
* @throws HttpError if the request fails, the API responds
|
|
55
46
|
* with a non-2xx status, or the response body cannot be parsed as JSON.
|
|
56
47
|
*/
|
|
57
|
-
scanTransaction(body: unknown): Promise<
|
|
48
|
+
scanTransaction(body: unknown): Promise<any>;
|
|
58
49
|
}
|
|
59
50
|
//# sourceMappingURL=SecurityAlertsApiClient.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SecurityAlertsApiClient.d.mts","sourceRoot":"","sources":["../../../src/clients/security-alerts-api/SecurityAlertsApiClient.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SecurityAlertsApiClient.d.mts","sourceRoot":"","sources":["../../../src/clients/security-alerts-api/SecurityAlertsApiClient.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,+BAA+B,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAGlE,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,qBAAa,uBAAuB;;IAKlC;;;;;;;;OAQG;gBACS,EACV,OAAO,EACP,KAAK,EAAE,OAA0B,GAClC,EAAE,8BAA8B;IAOjC;;;;;;;OAOG;IAEG,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC;CAuBnD"}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { assert } from "@metamask/superstruct";
|
|
2
2
|
import { HttpError } from "../../utils/errors/index.mjs";
|
|
3
|
-
import { FetchPolicyErrorName } from "../../utils/fetchPolicy/createFetchPolicy.mjs";
|
|
4
|
-
import { executeWithFetchPolicy, parseRetryAfter } from "../../utils/fetchPolicy/executeWithFetchPolicy.mjs";
|
|
5
3
|
import { UrlStruct } from "../../utils/urlStruct/urlStruct.mjs";
|
|
6
|
-
/**
|
|
7
|
-
* The timeout for a single scan request, in milliseconds.
|
|
8
|
-
*/
|
|
9
|
-
const REQUEST_TIMEOUT_MS = 10_000;
|
|
10
4
|
/**
|
|
11
5
|
* Headers sent with every Security Alerts API request.
|
|
12
6
|
*/
|
|
@@ -29,7 +23,6 @@ export const SECURITY_ALERTS_REQUEST_HEADERS = {
|
|
|
29
23
|
export class SecurityAlertsApiClient {
|
|
30
24
|
#fetch;
|
|
31
25
|
#scanUrl;
|
|
32
|
-
#policy;
|
|
33
26
|
/**
|
|
34
27
|
* Creates a new SecurityAlertsApiClient.
|
|
35
28
|
*
|
|
@@ -38,14 +31,11 @@ export class SecurityAlertsApiClient {
|
|
|
38
31
|
* an invalid URL fails fast at construction.
|
|
39
32
|
* @param options.fetch - Optional fetch implementation; defaults to
|
|
40
33
|
* `globalThis.fetch`.
|
|
41
|
-
* @param options.policy - Optional fetch policy for retries and circuit
|
|
42
|
-
* breaking.
|
|
43
34
|
*/
|
|
44
|
-
constructor({ scanUrl, fetch: fetchFn = globalThis.fetch,
|
|
35
|
+
constructor({ scanUrl, fetch: fetchFn = globalThis.fetch, }) {
|
|
45
36
|
assert(scanUrl, UrlStruct);
|
|
46
37
|
this.#fetch = fetchFn;
|
|
47
38
|
this.#scanUrl = scanUrl;
|
|
48
|
-
this.#policy = policy;
|
|
49
39
|
}
|
|
50
40
|
/**
|
|
51
41
|
* Scans a transaction by posting the request body to the scan endpoint.
|
|
@@ -55,37 +45,25 @@ export class SecurityAlertsApiClient {
|
|
|
55
45
|
* @throws HttpError if the request fails, the API responds
|
|
56
46
|
* with a non-2xx status, or the response body cannot be parsed as JSON.
|
|
57
47
|
*/
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
49
|
async scanTransaction(body) {
|
|
59
|
-
|
|
50
|
+
try {
|
|
60
51
|
const response = await this.#fetch(this.#scanUrl, {
|
|
61
52
|
headers: { ...SECURITY_ALERTS_REQUEST_HEADERS },
|
|
62
53
|
method: 'POST',
|
|
63
54
|
body: JSON.stringify(body),
|
|
64
|
-
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
65
55
|
});
|
|
66
56
|
if (!response.ok) {
|
|
67
|
-
throw
|
|
68
|
-
httpStatus: response.status,
|
|
69
|
-
retryAfterMs: parseRetryAfter(response.headers?.get('Retry-After')),
|
|
70
|
-
});
|
|
57
|
+
throw new HttpError(`HTTP error! status: ${response.status}`);
|
|
71
58
|
}
|
|
72
59
|
return await response.json();
|
|
73
|
-
};
|
|
74
|
-
try {
|
|
75
|
-
return await (this.#policy
|
|
76
|
-
? executeWithFetchPolicy(this.#policy, request)
|
|
77
|
-
: request());
|
|
78
60
|
}
|
|
79
61
|
catch (error) {
|
|
80
62
|
if (error instanceof HttpError) {
|
|
81
63
|
throw error;
|
|
82
64
|
}
|
|
83
65
|
throw new HttpError('Security Alerts API error', {
|
|
84
|
-
cause: error
|
|
85
|
-
error.name === FetchPolicyErrorName.Network &&
|
|
86
|
-
error.cause !== undefined
|
|
87
|
-
? error.cause
|
|
88
|
-
: error,
|
|
66
|
+
cause: error,
|
|
89
67
|
});
|
|
90
68
|
}
|
|
91
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SecurityAlertsApiClient.mjs","sourceRoot":"","sources":["../../../src/clients/security-alerts-api/SecurityAlertsApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAE/C,OAAO,EAAE,SAAS,EAAE,qCAA2B;AAC/C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"SecurityAlertsApiClient.mjs","sourceRoot":"","sources":["../../../src/clients/security-alerts-api/SecurityAlertsApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAE/C,OAAO,EAAE,SAAS,EAAE,qCAA2B;AAC/C,OAAO,EAAE,SAAS,EAAE,4CAAwC;AAE5D;;GAEG;AACH,MAAM,CAAC,MAAM,+BAA+B,GAA2B;IACrE,cAAc,EAAE,kBAAkB;IAClC,MAAM,EAAE,kBAAkB;CAC3B,CAAC;AAcF;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,uBAAuB;IACzB,MAAM,CAA0B;IAEhC,QAAQ,CAAS;IAE1B;;;;;;;;OAQG;IACH,YAAY,EACV,OAAO,EACP,KAAK,EAAE,OAAO,GAAG,UAAU,CAAC,KAAK,GACF;QAC/B,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAE3B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;QACtB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;;;;OAOG;IACH,8DAA8D;IAC9D,KAAK,CAAC,eAAe,CAAC,IAAa;QACjC,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAChD,OAAO,EAAE,EAAE,GAAG,+BAA+B,EAAE;gBAC/C,MAAM,EAAE,MAAM;gBACd,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;aAC3B,CAAC,CAAC;YAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,IAAI,SAAS,CAAC,uBAAuB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;YAChE,CAAC;YAED,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC/B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;gBAC/B,MAAM,KAAK,CAAC;YACd,CAAC;YAED,MAAM,IAAI,SAAS,CAAC,2BAA2B,EAAE;gBAC/C,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF","sourcesContent":["import { assert } from '@metamask/superstruct';\n\nimport { HttpError } from '../../utils/errors';\nimport { UrlStruct } from '../../utils/urlStruct/urlStruct';\n\n/**\n * Headers sent with every Security Alerts API request.\n */\nexport const SECURITY_ALERTS_REQUEST_HEADERS: Record<string, string> = {\n 'Content-Type': 'application/json',\n accept: 'application/json',\n};\n\n/**\n * Options for a `SecurityAlertsApiClient` constructor.\n */\nexport type SecurityAlertsApiClientOptions = {\n /**\n * The full scan endpoint URL, e.g. `https://security-alerts.api.cx.metamask.io/tron/transaction/scan`.\n */\n scanUrl: string;\n /** The fetch implementation to use. Defaults to `globalThis.fetch`. */\n fetch?: typeof globalThis.fetch;\n};\n\n/**\n * HTTP client for a Security Alerts API scan endpoint.\n *\n * Owns the request envelope shared by every chain — POSTing the JSON body,\n * wrapping request failures, non-2xx responses, and malformed JSON bodies as\n * {@link HttpError}, and parsing the JSON response — and nothing\n * else.\n *\n * The request body is a chain-specific Blockaid contract: the caller\n * supplies a typed body (e.g. checked with `satisfies` against a per-snap\n * body type) and is responsible for validating the parsed response payload.\n */\nexport class SecurityAlertsApiClient {\n readonly #fetch: typeof globalThis.fetch;\n\n readonly #scanUrl: string;\n\n /**\n * Creates a new SecurityAlertsApiClient.\n *\n * @param options - The client options.\n * @param options.scanUrl - The full scan endpoint URL. Validated once, so\n * an invalid URL fails fast at construction.\n * @param options.fetch - Optional fetch implementation; defaults to\n * `globalThis.fetch`.\n */\n constructor({\n scanUrl,\n fetch: fetchFn = globalThis.fetch,\n }: SecurityAlertsApiClientOptions) {\n assert(scanUrl, UrlStruct);\n\n this.#fetch = fetchFn;\n this.#scanUrl = scanUrl;\n }\n\n /**\n * Scans a transaction by posting the request body to the scan endpoint.\n *\n * @param body - The chain-specific scan request body.\n * @returns The parsed scan response.\n * @throws HttpError if the request fails, the API responds\n * with a non-2xx status, or the response body cannot be parsed as JSON.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n async scanTransaction(body: unknown): Promise<any> {\n try {\n const response = await this.#fetch(this.#scanUrl, {\n headers: { ...SECURITY_ALERTS_REQUEST_HEADERS },\n method: 'POST',\n body: JSON.stringify(body),\n });\n\n if (!response.ok) {\n throw new HttpError(`HTTP error! status: ${response.status}`);\n }\n\n return await response.json();\n } catch (error) {\n if (error instanceof HttpError) {\n throw error;\n }\n\n throw new HttpError('Security Alerts API error', {\n cause: error,\n });\n }\n }\n}\n"]}
|