@metamask-previews/shield-controller 6.0.2-preview-6eff555ee → 7.0.0-preview-0a30e47

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.
Files changed (97) hide show
  1. package/CHANGELOG.md +19 -1
  2. package/dist/{ShieldController-method-action-types.d.mts → ShieldController-method-action-types.d.ts} +2 -2
  3. package/dist/ShieldController-method-action-types.d.ts.map +1 -0
  4. package/dist/{ShieldController-method-action-types.mjs → ShieldController-method-action-types.js} +1 -1
  5. package/dist/ShieldController-method-action-types.js.map +1 -0
  6. package/dist/{ShieldController.d.cts → ShieldController.d.ts} +10 -10
  7. package/dist/ShieldController.d.ts.map +1 -0
  8. package/dist/ShieldController.js +290 -0
  9. package/dist/ShieldController.js.map +1 -0
  10. package/dist/{constants.d.cts → constants.d.ts} +1 -1
  11. package/dist/constants.d.ts.map +1 -0
  12. package/dist/{constants.mjs → constants.js} +1 -1
  13. package/dist/constants.js.map +1 -0
  14. package/dist/{index.d.cts → index.d.ts} +9 -9
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/logger.d.ts +4 -0
  19. package/dist/logger.d.ts.map +1 -0
  20. package/dist/logger.js +5 -0
  21. package/dist/logger.js.map +1 -0
  22. package/dist/{polling-with-policy.d.cts → polling-with-policy.d.ts} +2 -2
  23. package/dist/polling-with-policy.d.ts.map +1 -0
  24. package/dist/polling-with-policy.js +62 -0
  25. package/dist/polling-with-policy.js.map +1 -0
  26. package/dist/{shield-api-service-method-action-types.d.mts → shield-api-service-method-action-types.d.ts} +2 -2
  27. package/dist/shield-api-service-method-action-types.d.ts.map +1 -0
  28. package/dist/{shield-api-service-method-action-types.mjs → shield-api-service-method-action-types.js} +1 -1
  29. package/dist/shield-api-service-method-action-types.js.map +1 -0
  30. package/dist/{shield-api-service.d.cts → shield-api-service.d.ts} +14 -14
  31. package/dist/shield-api-service.d.ts.map +1 -0
  32. package/dist/{shield-api-service.mjs → shield-api-service.js} +94 -105
  33. package/dist/shield-api-service.js.map +1 -0
  34. package/dist/{types.d.cts → types.d.ts} +4 -4
  35. package/dist/types.d.ts.map +1 -0
  36. package/dist/{types.mjs → types.js} +1 -1
  37. package/dist/types.js.map +1 -0
  38. package/package.json +21 -25
  39. package/dist/ShieldController-method-action-types.cjs +0 -7
  40. package/dist/ShieldController-method-action-types.cjs.map +0 -1
  41. package/dist/ShieldController-method-action-types.d.cts +0 -51
  42. package/dist/ShieldController-method-action-types.d.cts.map +0 -1
  43. package/dist/ShieldController-method-action-types.d.mts.map +0 -1
  44. package/dist/ShieldController-method-action-types.mjs.map +0 -1
  45. package/dist/ShieldController.cjs +0 -294
  46. package/dist/ShieldController.cjs.map +0 -1
  47. package/dist/ShieldController.d.cts.map +0 -1
  48. package/dist/ShieldController.d.mts +0 -106
  49. package/dist/ShieldController.d.mts.map +0 -1
  50. package/dist/ShieldController.mjs +0 -290
  51. package/dist/ShieldController.mjs.map +0 -1
  52. package/dist/constants.cjs +0 -51
  53. package/dist/constants.cjs.map +0 -1
  54. package/dist/constants.d.cts.map +0 -1
  55. package/dist/constants.d.mts +0 -35
  56. package/dist/constants.d.mts.map +0 -1
  57. package/dist/constants.mjs.map +0 -1
  58. package/dist/index.cjs +0 -16
  59. package/dist/index.cjs.map +0 -1
  60. package/dist/index.d.cts.map +0 -1
  61. package/dist/index.d.mts +0 -9
  62. package/dist/index.d.mts.map +0 -1
  63. package/dist/index.mjs +0 -4
  64. package/dist/index.mjs.map +0 -1
  65. package/dist/logger.cjs +0 -8
  66. package/dist/logger.cjs.map +0 -1
  67. package/dist/logger.d.cts +0 -5
  68. package/dist/logger.d.cts.map +0 -1
  69. package/dist/logger.d.mts +0 -5
  70. package/dist/logger.d.mts.map +0 -1
  71. package/dist/logger.mjs +0 -5
  72. package/dist/logger.mjs.map +0 -1
  73. package/dist/polling-with-policy.cjs +0 -77
  74. package/dist/polling-with-policy.cjs.map +0 -1
  75. package/dist/polling-with-policy.d.cts.map +0 -1
  76. package/dist/polling-with-policy.d.mts +0 -9
  77. package/dist/polling-with-policy.d.mts.map +0 -1
  78. package/dist/polling-with-policy.mjs +0 -73
  79. package/dist/polling-with-policy.mjs.map +0 -1
  80. package/dist/shield-api-service-method-action-types.cjs +0 -7
  81. package/dist/shield-api-service-method-action-types.cjs.map +0 -1
  82. package/dist/shield-api-service-method-action-types.d.cts +0 -48
  83. package/dist/shield-api-service-method-action-types.d.cts.map +0 -1
  84. package/dist/shield-api-service-method-action-types.d.mts.map +0 -1
  85. package/dist/shield-api-service-method-action-types.mjs.map +0 -1
  86. package/dist/shield-api-service.cjs +0 -371
  87. package/dist/shield-api-service.cjs.map +0 -1
  88. package/dist/shield-api-service.d.cts.map +0 -1
  89. package/dist/shield-api-service.d.mts +0 -164
  90. package/dist/shield-api-service.d.mts.map +0 -1
  91. package/dist/shield-api-service.mjs.map +0 -1
  92. package/dist/types.cjs +0 -5
  93. package/dist/types.cjs.map +0 -1
  94. package/dist/types.d.cts.map +0 -1
  95. package/dist/types.d.mts +0 -34
  96. package/dist/types.d.mts.map +0 -1
  97. package/dist/types.mjs.map +0 -1
@@ -1,77 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
- if (kind === "m") throw new TypeError("Private method is not writable");
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
- };
13
- var _PollingWithCockatielPolicy_instances, _PollingWithCockatielPolicy_policy, _PollingWithCockatielPolicy_requestEntry, _PollingWithCockatielPolicy_addNewRequestEntry, _PollingWithCockatielPolicy_cleanup, _PollingWithCockatielPolicy_shouldRetry;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.PollingWithCockatielPolicy = void 0;
16
- const controller_utils_1 = require("@metamask/controller-utils");
17
- const cockatiel_1 = require("cockatiel");
18
- class PollingWithCockatielPolicy {
19
- constructor(policyOptions = {}) {
20
- _PollingWithCockatielPolicy_instances.add(this);
21
- _PollingWithCockatielPolicy_policy.set(this, void 0);
22
- _PollingWithCockatielPolicy_requestEntry.set(this, new Map());
23
- const shouldRetryFunc = __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_shouldRetry).bind(this);
24
- const retryFilterPolicy = (0, cockatiel_1.handleWhen)(shouldRetryFunc);
25
- __classPrivateFieldSet(this, _PollingWithCockatielPolicy_policy, (0, controller_utils_1.createServicePolicy)({
26
- ...policyOptions,
27
- retryFilterPolicy,
28
- }), "f");
29
- }
30
- async start(requestId, requestFn) {
31
- const abortController = __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_addNewRequestEntry).call(this, requestId);
32
- try {
33
- const result = await __classPrivateFieldGet(this, _PollingWithCockatielPolicy_policy, "f").execute(async ({ signal }) => {
34
- return requestFn(signal);
35
- }, abortController.signal);
36
- return result;
37
- }
38
- catch (error) {
39
- if (abortController.signal.aborted) {
40
- throw new Error('Request cancelled');
41
- }
42
- throw error;
43
- }
44
- finally {
45
- // Only cleanup if this abort controller is still active. If a new request with the same
46
- // requestId started while this one was running, it would have replaced with a new abort controller.
47
- // We must not delete the new request's controller when this older request finishes.
48
- if (abortController === __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").get(requestId)) {
49
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_cleanup).call(this, requestId);
50
- }
51
- }
52
- }
53
- abortPendingRequest(requestId) {
54
- const abortController = __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").get(requestId);
55
- abortController?.abort();
56
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_cleanup).call(this, requestId);
57
- }
58
- }
59
- exports.PollingWithCockatielPolicy = PollingWithCockatielPolicy;
60
- _PollingWithCockatielPolicy_policy = new WeakMap(), _PollingWithCockatielPolicy_requestEntry = new WeakMap(), _PollingWithCockatielPolicy_instances = new WeakSet(), _PollingWithCockatielPolicy_addNewRequestEntry = function _PollingWithCockatielPolicy_addNewRequestEntry(requestId) {
61
- // abort the previous request if it exists
62
- this.abortPendingRequest(requestId);
63
- // create a new abort controller for the new request
64
- const abortController = new AbortController();
65
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").set(requestId, abortController);
66
- return abortController;
67
- }, _PollingWithCockatielPolicy_cleanup = function _PollingWithCockatielPolicy_cleanup(requestId) {
68
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").delete(requestId);
69
- }, _PollingWithCockatielPolicy_shouldRetry = function _PollingWithCockatielPolicy_shouldRetry(error) {
70
- if (error instanceof controller_utils_1.HttpError) {
71
- // Note: we don't retry on 5xx errors, only on 4xx errors.
72
- // but we won't retry on 400 coz it means that the request body is invalid.
73
- return error.httpStatus > 400 && error.httpStatus < 500;
74
- }
75
- return false;
76
- };
77
- //# sourceMappingURL=polling-with-policy.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"polling-with-policy.cjs","sourceRoot":"","sources":["../src/polling-with-policy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iEAA4E;AAK5E,yCAAuC;AAMvC,MAAa,0BAA0B;IAKrC,YAAY,gBAA4C,EAAE;;QAJjD,qDAAuB;QAEvB,mDAAgB,IAAI,GAAG,EAA2B,EAAC;QAG1D,MAAM,eAAe,GAAG,uBAAA,IAAI,sFAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrD,MAAM,iBAAiB,GAAG,IAAA,sBAAU,EAAC,eAAe,CAAC,CAAC;QACtD,uBAAA,IAAI,sCAAW,IAAA,sCAAmB,EAAC;YACjC,GAAG,aAAa;YAChB,iBAAiB;SAClB,CAAC,MAAA,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAK,CACT,SAAiB,EACjB,SAAgC;QAEhC,MAAM,eAAe,GAAG,uBAAA,IAAI,6FAAoB,MAAxB,IAAI,EAAqB,SAAS,CAAC,CAAC;QAE5D,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,0CAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;gBAC7D,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;YAC3B,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;YAC3B,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,eAAe,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnC,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;YACvC,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;gBAAS,CAAC;YACT,wFAAwF;YACxF,oGAAoG;YACpG,oFAAoF;YACpF,IAAI,eAAe,KAAK,uBAAA,IAAI,gDAAc,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC1D,uBAAA,IAAI,kFAAS,MAAb,IAAI,EAAU,SAAS,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAED,mBAAmB,CAAC,SAAiB;QACnC,MAAM,eAAe,GAAG,uBAAA,IAAI,gDAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC1D,eAAe,EAAE,KAAK,EAAE,CAAC;QACzB,uBAAA,IAAI,kFAAS,MAAb,IAAI,EAAU,SAAS,CAAC,CAAC;IAC3B,CAAC;CAwBF;AApED,gEAoEC;8QAtBqB,SAAiB;IACnC,0CAA0C;IAC1C,IAAI,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAEpC,oDAAoD;IACpD,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;IAC9C,uBAAA,IAAI,gDAAc,CAAC,GAAG,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;IACnD,OAAO,eAAe,CAAC;AACzB,CAAC,qFAEQ,SAAiB;IACxB,uBAAA,IAAI,gDAAc,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AACvC,CAAC,6FAEY,KAAY;IACvB,IAAI,KAAK,YAAY,4BAAS,EAAE,CAAC;QAC/B,0DAA0D;QAC1D,2EAA2E;QAC3E,OAAO,KAAK,CAAC,UAAU,GAAG,GAAG,IAAI,KAAK,CAAC,UAAU,GAAG,GAAG,CAAC;IAC1D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport { handleWhen } from 'cockatiel';\n\nexport type RequestFn<ReturnType> = (\n signal: AbortSignal,\n) => Promise<ReturnType>;\n\nexport class PollingWithCockatielPolicy {\n readonly #policy: ServicePolicy;\n\n readonly #requestEntry = new Map<string, AbortController>();\n\n constructor(policyOptions: CreateServicePolicyOptions = {}) {\n const shouldRetryFunc = this.#shouldRetry.bind(this);\n const retryFilterPolicy = handleWhen(shouldRetryFunc);\n this.#policy = createServicePolicy({\n ...policyOptions,\n retryFilterPolicy,\n });\n }\n\n async start<ReturnType>(\n requestId: string,\n requestFn: RequestFn<ReturnType>,\n ): Promise<ReturnType> {\n const abortController = this.#addNewRequestEntry(requestId);\n\n try {\n const result = await this.#policy.execute(async ({ signal }) => {\n return requestFn(signal);\n }, abortController.signal);\n return result;\n } catch (error) {\n if (abortController.signal.aborted) {\n throw new Error('Request cancelled');\n }\n throw error;\n } finally {\n // Only cleanup if this abort controller is still active. If a new request with the same\n // requestId started while this one was running, it would have replaced with a new abort controller.\n // We must not delete the new request's controller when this older request finishes.\n if (abortController === this.#requestEntry.get(requestId)) {\n this.#cleanup(requestId);\n }\n }\n }\n\n abortPendingRequest(requestId: string): void {\n const abortController = this.#requestEntry.get(requestId);\n abortController?.abort();\n this.#cleanup(requestId);\n }\n\n #addNewRequestEntry(requestId: string): AbortController {\n // abort the previous request if it exists\n this.abortPendingRequest(requestId);\n\n // create a new abort controller for the new request\n const abortController = new AbortController();\n this.#requestEntry.set(requestId, abortController);\n return abortController;\n }\n\n #cleanup(requestId: string): void {\n this.#requestEntry.delete(requestId);\n }\n\n #shouldRetry(error: Error): boolean {\n if (error instanceof HttpError) {\n // Note: we don't retry on 5xx errors, only on 4xx errors.\n // but we won't retry on 400 coz it means that the request body is invalid.\n return error.httpStatus > 400 && error.httpStatus < 500;\n }\n return false;\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"polling-with-policy.d.cts","sourceRoot":"","sources":["../src/polling-with-policy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0BAA0B,EAE3B,mCAAmC;AAGpC,MAAM,MAAM,SAAS,CAAC,UAAU,IAAI,CAClC,MAAM,EAAE,WAAW,KAChB,OAAO,CAAC,UAAU,CAAC,CAAC;AAEzB,qBAAa,0BAA0B;;gBAKzB,aAAa,GAAE,0BAA+B;IASpD,KAAK,CAAC,UAAU,EACpB,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,SAAS,CAAC,UAAU,CAAC,GAC/B,OAAO,CAAC,UAAU,CAAC;IAuBtB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;CA4B7C"}
@@ -1,9 +0,0 @@
1
- import type { CreateServicePolicyOptions } from "@metamask/controller-utils";
2
- export type RequestFn<ReturnType> = (signal: AbortSignal) => Promise<ReturnType>;
3
- export declare class PollingWithCockatielPolicy {
4
- #private;
5
- constructor(policyOptions?: CreateServicePolicyOptions);
6
- start<ReturnType>(requestId: string, requestFn: RequestFn<ReturnType>): Promise<ReturnType>;
7
- abortPendingRequest(requestId: string): void;
8
- }
9
- //# sourceMappingURL=polling-with-policy.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"polling-with-policy.d.mts","sourceRoot":"","sources":["../src/polling-with-policy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0BAA0B,EAE3B,mCAAmC;AAGpC,MAAM,MAAM,SAAS,CAAC,UAAU,IAAI,CAClC,MAAM,EAAE,WAAW,KAChB,OAAO,CAAC,UAAU,CAAC,CAAC;AAEzB,qBAAa,0BAA0B;;gBAKzB,aAAa,GAAE,0BAA+B;IASpD,KAAK,CAAC,UAAU,EACpB,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,SAAS,CAAC,UAAU,CAAC,GAC/B,OAAO,CAAC,UAAU,CAAC;IAuBtB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;CA4B7C"}
@@ -1,73 +0,0 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _PollingWithCockatielPolicy_instances, _PollingWithCockatielPolicy_policy, _PollingWithCockatielPolicy_requestEntry, _PollingWithCockatielPolicy_addNewRequestEntry, _PollingWithCockatielPolicy_cleanup, _PollingWithCockatielPolicy_shouldRetry;
13
- import { createServicePolicy, HttpError } from "@metamask/controller-utils";
14
- import { handleWhen } from "cockatiel";
15
- export class PollingWithCockatielPolicy {
16
- constructor(policyOptions = {}) {
17
- _PollingWithCockatielPolicy_instances.add(this);
18
- _PollingWithCockatielPolicy_policy.set(this, void 0);
19
- _PollingWithCockatielPolicy_requestEntry.set(this, new Map());
20
- const shouldRetryFunc = __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_shouldRetry).bind(this);
21
- const retryFilterPolicy = handleWhen(shouldRetryFunc);
22
- __classPrivateFieldSet(this, _PollingWithCockatielPolicy_policy, createServicePolicy({
23
- ...policyOptions,
24
- retryFilterPolicy,
25
- }), "f");
26
- }
27
- async start(requestId, requestFn) {
28
- const abortController = __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_addNewRequestEntry).call(this, requestId);
29
- try {
30
- const result = await __classPrivateFieldGet(this, _PollingWithCockatielPolicy_policy, "f").execute(async ({ signal }) => {
31
- return requestFn(signal);
32
- }, abortController.signal);
33
- return result;
34
- }
35
- catch (error) {
36
- if (abortController.signal.aborted) {
37
- throw new Error('Request cancelled');
38
- }
39
- throw error;
40
- }
41
- finally {
42
- // Only cleanup if this abort controller is still active. If a new request with the same
43
- // requestId started while this one was running, it would have replaced with a new abort controller.
44
- // We must not delete the new request's controller when this older request finishes.
45
- if (abortController === __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").get(requestId)) {
46
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_cleanup).call(this, requestId);
47
- }
48
- }
49
- }
50
- abortPendingRequest(requestId) {
51
- const abortController = __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").get(requestId);
52
- abortController?.abort();
53
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_instances, "m", _PollingWithCockatielPolicy_cleanup).call(this, requestId);
54
- }
55
- }
56
- _PollingWithCockatielPolicy_policy = new WeakMap(), _PollingWithCockatielPolicy_requestEntry = new WeakMap(), _PollingWithCockatielPolicy_instances = new WeakSet(), _PollingWithCockatielPolicy_addNewRequestEntry = function _PollingWithCockatielPolicy_addNewRequestEntry(requestId) {
57
- // abort the previous request if it exists
58
- this.abortPendingRequest(requestId);
59
- // create a new abort controller for the new request
60
- const abortController = new AbortController();
61
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").set(requestId, abortController);
62
- return abortController;
63
- }, _PollingWithCockatielPolicy_cleanup = function _PollingWithCockatielPolicy_cleanup(requestId) {
64
- __classPrivateFieldGet(this, _PollingWithCockatielPolicy_requestEntry, "f").delete(requestId);
65
- }, _PollingWithCockatielPolicy_shouldRetry = function _PollingWithCockatielPolicy_shouldRetry(error) {
66
- if (error instanceof HttpError) {
67
- // Note: we don't retry on 5xx errors, only on 4xx errors.
68
- // but we won't retry on 400 coz it means that the request body is invalid.
69
- return error.httpStatus > 400 && error.httpStatus < 500;
70
- }
71
- return false;
72
- };
73
- //# sourceMappingURL=polling-with-policy.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"polling-with-policy.mjs","sourceRoot":"","sources":["../src/polling-with-policy.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,mCAAmC;AAK5E,OAAO,EAAE,UAAU,EAAE,kBAAkB;AAMvC,MAAM,OAAO,0BAA0B;IAKrC,YAAY,gBAA4C,EAAE;;QAJjD,qDAAuB;QAEvB,mDAAgB,IAAI,GAAG,EAA2B,EAAC;QAG1D,MAAM,eAAe,GAAG,uBAAA,IAAI,sFAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrD,MAAM,iBAAiB,GAAG,UAAU,CAAC,eAAe,CAAC,CAAC;QACtD,uBAAA,IAAI,sCAAW,mBAAmB,CAAC;YACjC,GAAG,aAAa;YAChB,iBAAiB;SAClB,CAAC,MAAA,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAK,CACT,SAAiB,EACjB,SAAgC;QAEhC,MAAM,eAAe,GAAG,uBAAA,IAAI,6FAAoB,MAAxB,IAAI,EAAqB,SAAS,CAAC,CAAC;QAE5D,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,0CAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;gBAC7D,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;YAC3B,CAAC,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;YAC3B,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,eAAe,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnC,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;YACvC,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;gBAAS,CAAC;YACT,wFAAwF;YACxF,oGAAoG;YACpG,oFAAoF;YACpF,IAAI,eAAe,KAAK,uBAAA,IAAI,gDAAc,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC1D,uBAAA,IAAI,kFAAS,MAAb,IAAI,EAAU,SAAS,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAED,mBAAmB,CAAC,SAAiB;QACnC,MAAM,eAAe,GAAG,uBAAA,IAAI,gDAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC1D,eAAe,EAAE,KAAK,EAAE,CAAC;QACzB,uBAAA,IAAI,kFAAS,MAAb,IAAI,EAAU,SAAS,CAAC,CAAC;IAC3B,CAAC;CAwBF;8QAtBqB,SAAiB;IACnC,0CAA0C;IAC1C,IAAI,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAEpC,oDAAoD;IACpD,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;IAC9C,uBAAA,IAAI,gDAAc,CAAC,GAAG,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;IACnD,OAAO,eAAe,CAAC;AACzB,CAAC,qFAEQ,SAAiB;IACxB,uBAAA,IAAI,gDAAc,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AACvC,CAAC,6FAEY,KAAY;IACvB,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;QAC/B,0DAA0D;QAC1D,2EAA2E;QAC3E,OAAO,KAAK,CAAC,UAAU,GAAG,GAAG,IAAI,KAAK,CAAC,UAAU,GAAG,GAAG,CAAC;IAC1D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport { handleWhen } from 'cockatiel';\n\nexport type RequestFn<ReturnType> = (\n signal: AbortSignal,\n) => Promise<ReturnType>;\n\nexport class PollingWithCockatielPolicy {\n readonly #policy: ServicePolicy;\n\n readonly #requestEntry = new Map<string, AbortController>();\n\n constructor(policyOptions: CreateServicePolicyOptions = {}) {\n const shouldRetryFunc = this.#shouldRetry.bind(this);\n const retryFilterPolicy = handleWhen(shouldRetryFunc);\n this.#policy = createServicePolicy({\n ...policyOptions,\n retryFilterPolicy,\n });\n }\n\n async start<ReturnType>(\n requestId: string,\n requestFn: RequestFn<ReturnType>,\n ): Promise<ReturnType> {\n const abortController = this.#addNewRequestEntry(requestId);\n\n try {\n const result = await this.#policy.execute(async ({ signal }) => {\n return requestFn(signal);\n }, abortController.signal);\n return result;\n } catch (error) {\n if (abortController.signal.aborted) {\n throw new Error('Request cancelled');\n }\n throw error;\n } finally {\n // Only cleanup if this abort controller is still active. If a new request with the same\n // requestId started while this one was running, it would have replaced with a new abort controller.\n // We must not delete the new request's controller when this older request finishes.\n if (abortController === this.#requestEntry.get(requestId)) {\n this.#cleanup(requestId);\n }\n }\n }\n\n abortPendingRequest(requestId: string): void {\n const abortController = this.#requestEntry.get(requestId);\n abortController?.abort();\n this.#cleanup(requestId);\n }\n\n #addNewRequestEntry(requestId: string): AbortController {\n // abort the previous request if it exists\n this.abortPendingRequest(requestId);\n\n // create a new abort controller for the new request\n const abortController = new AbortController();\n this.#requestEntry.set(requestId, abortController);\n return abortController;\n }\n\n #cleanup(requestId: string): void {\n this.#requestEntry.delete(requestId);\n }\n\n #shouldRetry(error: Error): boolean {\n if (error instanceof HttpError) {\n // Note: we don't retry on 5xx errors, only on 4xx errors.\n // but we won't retry on 400 coz it means that the request body is invalid.\n return error.httpStatus > 400 && error.httpStatus < 500;\n }\n return false;\n }\n}\n"]}
@@ -1,7 +0,0 @@
1
- "use strict";
2
- /**
3
- * This file is auto generated.
4
- * Do not edit manually.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- //# sourceMappingURL=shield-api-service-method-action-types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shield-api-service-method-action-types.cjs","sourceRoot":"","sources":["../src/shield-api-service-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ShieldApiService } from './shield-api-service.js';\n\n/**\n * Checks the coverage of a transaction.\n *\n * @param req - The coverage check request.\n * @returns The coverage result.\n */\nexport type ShieldApiServiceCheckCoverageAction = {\n type: `ShieldApiService:checkCoverage`;\n handler: ShieldApiService['checkCoverage'];\n};\n\n/**\n * Checks the coverage of a signature request.\n *\n * @param req - The signature coverage check request.\n * @returns The coverage result.\n */\nexport type ShieldApiServiceCheckSignatureCoverageAction = {\n type: `ShieldApiService:checkSignatureCoverage`;\n handler: ShieldApiService['checkSignatureCoverage'];\n};\n\n/**\n * Logs a signed signature request.\n *\n * @param req - The log signature request.\n */\nexport type ShieldApiServiceLogSignatureAction = {\n type: `ShieldApiService:logSignature`;\n handler: ShieldApiService['logSignature'];\n};\n\n/**\n * Logs a submitted transaction.\n *\n * @param req - The log transaction request.\n */\nexport type ShieldApiServiceLogTransactionAction = {\n type: `ShieldApiService:logTransaction`;\n handler: ShieldApiService['logTransaction'];\n};\n\n/**\n * Union of all ShieldApiService action types.\n */\nexport type ShieldApiServiceMethodActions =\n | ShieldApiServiceCheckCoverageAction\n | ShieldApiServiceCheckSignatureCoverageAction\n | ShieldApiServiceLogSignatureAction\n | ShieldApiServiceLogTransactionAction;\n"]}
@@ -1,48 +0,0 @@
1
- /**
2
- * This file is auto generated.
3
- * Do not edit manually.
4
- */
5
- import type { ShieldApiService } from "./shield-api-service.cjs";
6
- /**
7
- * Checks the coverage of a transaction.
8
- *
9
- * @param req - The coverage check request.
10
- * @returns The coverage result.
11
- */
12
- export type ShieldApiServiceCheckCoverageAction = {
13
- type: `ShieldApiService:checkCoverage`;
14
- handler: ShieldApiService['checkCoverage'];
15
- };
16
- /**
17
- * Checks the coverage of a signature request.
18
- *
19
- * @param req - The signature coverage check request.
20
- * @returns The coverage result.
21
- */
22
- export type ShieldApiServiceCheckSignatureCoverageAction = {
23
- type: `ShieldApiService:checkSignatureCoverage`;
24
- handler: ShieldApiService['checkSignatureCoverage'];
25
- };
26
- /**
27
- * Logs a signed signature request.
28
- *
29
- * @param req - The log signature request.
30
- */
31
- export type ShieldApiServiceLogSignatureAction = {
32
- type: `ShieldApiService:logSignature`;
33
- handler: ShieldApiService['logSignature'];
34
- };
35
- /**
36
- * Logs a submitted transaction.
37
- *
38
- * @param req - The log transaction request.
39
- */
40
- export type ShieldApiServiceLogTransactionAction = {
41
- type: `ShieldApiService:logTransaction`;
42
- handler: ShieldApiService['logTransaction'];
43
- };
44
- /**
45
- * Union of all ShieldApiService action types.
46
- */
47
- export type ShieldApiServiceMethodActions = ShieldApiServiceCheckCoverageAction | ShieldApiServiceCheckSignatureCoverageAction | ShieldApiServiceLogSignatureAction | ShieldApiServiceLogTransactionAction;
48
- //# sourceMappingURL=shield-api-service-method-action-types.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shield-api-service-method-action-types.d.cts","sourceRoot":"","sources":["../src/shield-api-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,iCAAgC;AAEhE;;;;;GAKG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,gCAAgC,CAAC;IACvC,OAAO,EAAE,gBAAgB,CAAC,eAAe,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,yCAAyC,CAAC;IAChD,OAAO,EAAE,gBAAgB,CAAC,wBAAwB,CAAC,CAAC;CACrD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;CAC3C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,oCAAoC,GAAG;IACjD,IAAI,EAAE,iCAAiC,CAAC;IACxC,OAAO,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;CAC7C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GACrC,mCAAmC,GACnC,4CAA4C,GAC5C,kCAAkC,GAClC,oCAAoC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"shield-api-service-method-action-types.d.mts","sourceRoot":"","sources":["../src/shield-api-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,iCAAgC;AAEhE;;;;;GAKG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,gCAAgC,CAAC;IACvC,OAAO,EAAE,gBAAgB,CAAC,eAAe,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,yCAAyC,CAAC;IAChD,OAAO,EAAE,gBAAgB,CAAC,wBAAwB,CAAC,CAAC;CACrD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;CAC3C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,oCAAoC,GAAG;IACjD,IAAI,EAAE,iCAAiC,CAAC;IACxC,OAAO,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;CAC7C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GACrC,mCAAmC,GACnC,4CAA4C,GAC5C,kCAAkC,GAClC,oCAAoC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"shield-api-service-method-action-types.mjs","sourceRoot":"","sources":["../src/shield-api-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ShieldApiService } from './shield-api-service.js';\n\n/**\n * Checks the coverage of a transaction.\n *\n * @param req - The coverage check request.\n * @returns The coverage result.\n */\nexport type ShieldApiServiceCheckCoverageAction = {\n type: `ShieldApiService:checkCoverage`;\n handler: ShieldApiService['checkCoverage'];\n};\n\n/**\n * Checks the coverage of a signature request.\n *\n * @param req - The signature coverage check request.\n * @returns The coverage result.\n */\nexport type ShieldApiServiceCheckSignatureCoverageAction = {\n type: `ShieldApiService:checkSignatureCoverage`;\n handler: ShieldApiService['checkSignatureCoverage'];\n};\n\n/**\n * Logs a signed signature request.\n *\n * @param req - The log signature request.\n */\nexport type ShieldApiServiceLogSignatureAction = {\n type: `ShieldApiService:logSignature`;\n handler: ShieldApiService['logSignature'];\n};\n\n/**\n * Logs a submitted transaction.\n *\n * @param req - The log transaction request.\n */\nexport type ShieldApiServiceLogTransactionAction = {\n type: `ShieldApiService:logTransaction`;\n handler: ShieldApiService['logTransaction'];\n};\n\n/**\n * Union of all ShieldApiService action types.\n */\nexport type ShieldApiServiceMethodActions =\n | ShieldApiServiceCheckCoverageAction\n | ShieldApiServiceCheckSignatureCoverageAction\n | ShieldApiServiceLogSignatureAction\n | ShieldApiServiceLogTransactionAction;\n"]}
@@ -1,371 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _ShieldApiService_instances, _ShieldApiService_baseUrl, _ShieldApiService_fetch, _ShieldApiService_pollingPolicy, _ShieldApiService_captureException, _ShieldApiService_initCoverageCheck, _ShieldApiService_getCoverageResult, _ShieldApiService_authHeaders;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.parseSignatureRequestMethod = exports.makeInitCoverageCheckBody = exports.ShieldApiService = exports.serviceName = void 0;
16
- const base_data_service_1 = require("@metamask/base-data-service");
17
- const controller_utils_1 = require("@metamask/controller-utils");
18
- const signature_controller_1 = require("@metamask/signature-controller");
19
- const constants_js_1 = require("./constants.cjs");
20
- const polling_with_policy_js_1 = require("./polling-with-policy.cjs");
21
- /**
22
- * The name of the {@link ShieldApiService}, used to namespace the service's
23
- * actions and events.
24
- */
25
- exports.serviceName = 'ShieldApiService';
26
- const MESSENGER_EXPOSED_METHODS = [
27
- 'checkCoverage',
28
- 'checkSignatureCoverage',
29
- 'logSignature',
30
- 'logTransaction',
31
- ];
32
- const DEFAULT_POLICY_OPTIONS = {
33
- maxRetries: 0,
34
- };
35
- /**
36
- * This service is responsible for communicating with the Shield API.
37
- *
38
- * All requests are authenticated via JWT Bearer tokens obtained from the
39
- * `AuthenticationController:getBearerToken` messenger action.
40
- */
41
- class ShieldApiService extends base_data_service_1.BaseDataService {
42
- /**
43
- * Constructs a new ShieldApiService.
44
- *
45
- * @param args - The constructor arguments.
46
- * @param args.messenger - The messenger suited for this service.
47
- * @param args.env - The environment used to resolve the Shield API URL.
48
- * @param args.fetch - The fetch implementation. Defaults to `globalThis.fetch`.
49
- * @param args.captureException - Optional error reporter.
50
- * @param args.getCoverageResultTimeout - Timeout for coverage result polling.
51
- * @param args.getCoverageResultPollInterval - Poll interval for coverage results.
52
- * @param args.queryClientConfig - Configuration for the underlying TanStack
53
- * Query client.
54
- * @param args.policyOptions - Options to pass to `createServicePolicy`.
55
- */
56
- constructor({ messenger, env, fetch: fetchFn = globalThis.fetch, captureException: captureExceptionFn, getCoverageResultTimeout = 5000, getCoverageResultPollInterval = 1000, queryClientConfig = {}, policyOptions = {}, }) {
57
- super({
58
- name: exports.serviceName,
59
- messenger,
60
- queryClientConfig,
61
- policyOptions: { ...DEFAULT_POLICY_OPTIONS, ...policyOptions },
62
- });
63
- _ShieldApiService_instances.add(this);
64
- _ShieldApiService_baseUrl.set(this, void 0);
65
- _ShieldApiService_fetch.set(this, void 0);
66
- _ShieldApiService_pollingPolicy.set(this, void 0);
67
- _ShieldApiService_captureException.set(this, void 0);
68
- __classPrivateFieldSet(this, _ShieldApiService_baseUrl, (0, constants_js_1.getShieldApiBaseUrl)(env), "f");
69
- __classPrivateFieldSet(this, _ShieldApiService_fetch, fetchFn, "f");
70
- const { backoff, maxRetries } = computePollingIntervalAndRetryCount(getCoverageResultTimeout, getCoverageResultPollInterval);
71
- __classPrivateFieldSet(this, _ShieldApiService_pollingPolicy, new polling_with_policy_js_1.PollingWithCockatielPolicy({
72
- backoff,
73
- maxRetries,
74
- }), "f");
75
- __classPrivateFieldSet(this, _ShieldApiService_captureException, (error) => {
76
- try {
77
- captureExceptionFn?.(error);
78
- }
79
- catch {
80
- // ignore error thrown when calling captureException
81
- }
82
- }, "f");
83
- this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
84
- }
85
- /**
86
- * Checks the coverage of a transaction.
87
- *
88
- * @param req - The coverage check request.
89
- * @returns The coverage result.
90
- */
91
- async checkCoverage(req) {
92
- let { coverageId } = req;
93
- if (!coverageId) {
94
- const reqBody = makeInitCoverageCheckBody(req.txMeta);
95
- ({ coverageId } = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_initCoverageCheck).call(this, 'v1/transaction/coverage/init', reqBody, req.txMeta.id));
96
- }
97
- const txCoverageResultUrl = `${__classPrivateFieldGet(this, _ShieldApiService_baseUrl, "f")}/v1/transaction/coverage/result`;
98
- const coverageResult = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_getCoverageResult).call(this, req.txMeta.id, coverageId, txCoverageResultUrl);
99
- return {
100
- coverageId,
101
- message: coverageResult.message,
102
- reasonCode: coverageResult.reasonCode,
103
- status: coverageResult.status,
104
- metrics: coverageResult.metrics,
105
- };
106
- }
107
- /**
108
- * Checks the coverage of a signature request.
109
- *
110
- * @param req - The signature coverage check request.
111
- * @returns The coverage result.
112
- */
113
- async checkSignatureCoverage(req) {
114
- let { coverageId } = req;
115
- if (!coverageId) {
116
- const reqBody = makeInitSignatureCoverageCheckBody(req.signatureRequest);
117
- ({ coverageId } = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_initCoverageCheck).call(this, 'v1/signature/coverage/init', reqBody, req.signatureRequest.id));
118
- }
119
- const signatureCoverageResultUrl = `${__classPrivateFieldGet(this, _ShieldApiService_baseUrl, "f")}/v1/signature/coverage/result`;
120
- const coverageResult = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_getCoverageResult).call(this, req.signatureRequest.id, coverageId, signatureCoverageResultUrl);
121
- return {
122
- coverageId,
123
- message: coverageResult.message,
124
- reasonCode: coverageResult.reasonCode,
125
- status: coverageResult.status,
126
- metrics: coverageResult.metrics,
127
- };
128
- }
129
- /**
130
- * Logs a signed signature request.
131
- *
132
- * @param req - The log signature request.
133
- */
134
- async logSignature(req) {
135
- try {
136
- const initBody = makeInitSignatureCoverageCheckBody(req.signatureRequest);
137
- const body = {
138
- signature: req.signature,
139
- status: req.status,
140
- ...initBody,
141
- };
142
- __classPrivateFieldGet(this, _ShieldApiService_pollingPolicy, "f").abortPendingRequest(req.signatureRequest.id);
143
- const headers = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_authHeaders).call(this);
144
- await this.fetchQuery({
145
- queryKey: [
146
- `${this.name}:logSignature`,
147
- req.signatureRequest.id,
148
- req.status,
149
- ],
150
- staleTime: 0,
151
- gcTime: 0,
152
- queryFn: async () => {
153
- const res = await __classPrivateFieldGet(this, _ShieldApiService_fetch, "f").call(this, `${__classPrivateFieldGet(this, _ShieldApiService_baseUrl, "f")}/v1/signature/coverage/log`, {
154
- method: 'POST',
155
- headers,
156
- body: JSON.stringify(body),
157
- });
158
- if (res.status !== 200) {
159
- throw new controller_utils_1.HttpError(res.status, `Failed to log signature: ${res.status}`);
160
- }
161
- return null;
162
- },
163
- });
164
- }
165
- catch (error) {
166
- const sentryError = createSentryError('Failed to log signature', error);
167
- __classPrivateFieldGet(this, _ShieldApiService_captureException, "f")?.call(this, sentryError);
168
- throw error;
169
- }
170
- }
171
- /**
172
- * Logs a submitted transaction.
173
- *
174
- * @param req - The log transaction request.
175
- */
176
- async logTransaction(req) {
177
- try {
178
- const initBody = makeInitCoverageCheckBody(req.txMeta);
179
- const body = {
180
- transactionHash: req.transactionHash,
181
- rawTransactionHex: req.rawTransactionHex,
182
- status: req.status,
183
- ...initBody,
184
- };
185
- __classPrivateFieldGet(this, _ShieldApiService_pollingPolicy, "f").abortPendingRequest(req.txMeta.id);
186
- const headers = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_authHeaders).call(this);
187
- await this.fetchQuery({
188
- queryKey: [
189
- `${this.name}:logTransaction`,
190
- req.txMeta.id,
191
- req.transactionHash,
192
- req.status,
193
- ],
194
- staleTime: 0,
195
- gcTime: 0,
196
- queryFn: async () => {
197
- const res = await __classPrivateFieldGet(this, _ShieldApiService_fetch, "f").call(this, `${__classPrivateFieldGet(this, _ShieldApiService_baseUrl, "f")}/v1/transaction/coverage/log`, {
198
- method: 'POST',
199
- headers,
200
- body: JSON.stringify(body),
201
- });
202
- if (res.status !== 200) {
203
- throw new controller_utils_1.HttpError(res.status, `Failed to log transaction: ${res.status}`);
204
- }
205
- return null;
206
- },
207
- });
208
- }
209
- catch (error) {
210
- const sentryError = createSentryError('Failed to log transaction', error);
211
- __classPrivateFieldGet(this, _ShieldApiService_captureException, "f")?.call(this, sentryError);
212
- throw error;
213
- }
214
- }
215
- }
216
- exports.ShieldApiService = ShieldApiService;
217
- _ShieldApiService_baseUrl = new WeakMap(), _ShieldApiService_fetch = new WeakMap(), _ShieldApiService_pollingPolicy = new WeakMap(), _ShieldApiService_captureException = new WeakMap(), _ShieldApiService_instances = new WeakSet(), _ShieldApiService_initCoverageCheck = async function _ShieldApiService_initCoverageCheck(path, reqBody, requestId) {
218
- try {
219
- const headers = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_authHeaders).call(this);
220
- return await this.fetchQuery({
221
- queryKey: [`${this.name}:initCoverageCheck`, path, requestId],
222
- staleTime: 0,
223
- gcTime: 0,
224
- queryFn: async () => {
225
- const res = await __classPrivateFieldGet(this, _ShieldApiService_fetch, "f").call(this, `${__classPrivateFieldGet(this, _ShieldApiService_baseUrl, "f")}/${path}`, {
226
- method: 'POST',
227
- headers,
228
- body: JSON.stringify(reqBody),
229
- });
230
- if (res.status !== 200) {
231
- throw new controller_utils_1.HttpError(res.status, `Failed to init coverage check: ${res.status}`);
232
- }
233
- return (await res.json());
234
- },
235
- });
236
- }
237
- catch (error) {
238
- const sentryError = createSentryError('Failed to init coverage check', error);
239
- __classPrivateFieldGet(this, _ShieldApiService_captureException, "f")?.call(this, sentryError);
240
- throw error;
241
- }
242
- }, _ShieldApiService_getCoverageResult = async function _ShieldApiService_getCoverageResult(requestId, coverageId, coverageResultUrl) {
243
- const reqBody = {
244
- coverageId,
245
- };
246
- const headers = await __classPrivateFieldGet(this, _ShieldApiService_instances, "m", _ShieldApiService_authHeaders).call(this);
247
- const startTime = Date.now();
248
- const getCoverageResultFn = async (signal) => {
249
- const res = await __classPrivateFieldGet(this, _ShieldApiService_fetch, "f").call(this, coverageResultUrl, {
250
- method: 'POST',
251
- headers,
252
- body: JSON.stringify(reqBody),
253
- signal,
254
- });
255
- if (res.status === 200) {
256
- return (await res.json());
257
- }
258
- let errorMessage = 'Timeout waiting for coverage result';
259
- try {
260
- const errorJson = (await res.json());
261
- errorMessage = `Failed to get coverage result: ${errorJson.message ?? errorJson.status}`;
262
- }
263
- catch {
264
- errorMessage = `Failed to get coverage result: ${res.status}`;
265
- }
266
- throw new controller_utils_1.HttpError(res.status, errorMessage);
267
- };
268
- const result = await __classPrivateFieldGet(this, _ShieldApiService_pollingPolicy, "f").start(requestId, getCoverageResultFn);
269
- const totalLatency = Date.now() - startTime;
270
- return {
271
- ...result,
272
- metrics: { latency: totalLatency },
273
- };
274
- }, _ShieldApiService_authHeaders = async function _ShieldApiService_authHeaders() {
275
- const token = await this.messenger.call('AuthenticationController:getBearerToken');
276
- return {
277
- 'Content-Type': 'application/json',
278
- Authorization: `Bearer ${token}`,
279
- };
280
- };
281
- /**
282
- * Make the body for the init coverage check request.
283
- *
284
- * @param txMeta - The transaction metadata.
285
- * @returns The body for the init coverage check request.
286
- */
287
- function makeInitCoverageCheckBody(txMeta) {
288
- return {
289
- txParams: [
290
- {
291
- authorizationList: txMeta.txParams.authorizationList,
292
- from: txMeta.txParams.from,
293
- to: txMeta.txParams.to,
294
- value: txMeta.txParams.value,
295
- data: txMeta.txParams.data,
296
- nonce: txMeta.txParams.nonce,
297
- },
298
- ],
299
- chainId: txMeta.chainId,
300
- origin: txMeta.origin,
301
- };
302
- }
303
- exports.makeInitCoverageCheckBody = makeInitCoverageCheckBody;
304
- /**
305
- * Make the body for the init signature coverage check request.
306
- *
307
- * @param signatureRequest - The signature request.
308
- * @returns The body for the init signature coverage check request.
309
- */
310
- function makeInitSignatureCoverageCheckBody(signatureRequest) {
311
- const method = parseSignatureRequestMethod(signatureRequest);
312
- return {
313
- chainId: signatureRequest.chainId,
314
- data: signatureRequest.messageParams.data,
315
- from: signatureRequest.messageParams.from,
316
- method,
317
- origin: signatureRequest.messageParams.origin,
318
- };
319
- }
320
- /**
321
- * Parse the JSON-RPC method from the signature request.
322
- *
323
- * @param signatureRequest - The signature request.
324
- * @returns The JSON-RPC method.
325
- */
326
- function parseSignatureRequestMethod(signatureRequest) {
327
- if (signatureRequest.type === signature_controller_1.SignatureRequestType.TypedSign) {
328
- switch (signatureRequest.version) {
329
- case constants_js_1.SignTypedDataVersion.V3:
330
- return signature_controller_1.EthMethod.SignTypedDataV3;
331
- case constants_js_1.SignTypedDataVersion.V4:
332
- return signature_controller_1.EthMethod.SignTypedDataV4;
333
- case constants_js_1.SignTypedDataVersion.V1:
334
- default:
335
- return signature_controller_1.SignatureRequestType.TypedSign;
336
- }
337
- }
338
- return signatureRequest.type;
339
- }
340
- exports.parseSignatureRequestMethod = parseSignatureRequestMethod;
341
- /**
342
- * Compute the polling interval and retry count for the Cockatiel policy based on the timeout and poll interval given.
343
- *
344
- * @param timeout - The timeout in milliseconds.
345
- * @param pollInterval - The poll interval in milliseconds.
346
- * @returns The polling interval and retry count.
347
- */
348
- function computePollingIntervalAndRetryCount(timeout, pollInterval) {
349
- const backoff = new controller_utils_1.ConstantBackoff(pollInterval);
350
- const computedMaxRetries = Math.floor(timeout / pollInterval) + 1;
351
- const maxRetries = isNaN(computedMaxRetries) || !isFinite(computedMaxRetries)
352
- ? controller_utils_1.DEFAULT_MAX_RETRIES
353
- : computedMaxRetries;
354
- return {
355
- backoff,
356
- maxRetries,
357
- };
358
- }
359
- /**
360
- * Create an error instance with a readable message, a cause and a context for Sentry.
361
- *
362
- * @param errorMessage - The error message.
363
- * @param cause - The cause of the error.
364
- * @returns A Sentry error.
365
- */
366
- function createSentryError(errorMessage, cause) {
367
- const error = new Error(errorMessage);
368
- error.cause = cause;
369
- return error;
370
- }
371
- //# sourceMappingURL=shield-api-service.cjs.map