@metamask-previews/compliance-controller 2.1.0-preview-b13d916 → 2.1.0-preview-4f4c98e

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 (83) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/dist/{ComplianceController-method-action-types.d.mts → ComplianceController-method-action-types.d.ts} +2 -2
  3. package/dist/ComplianceController-method-action-types.d.ts.map +1 -0
  4. package/dist/{ComplianceController-method-action-types.mjs → ComplianceController-method-action-types.js} +1 -1
  5. package/dist/ComplianceController-method-action-types.js.map +1 -0
  6. package/dist/{ComplianceController.d.cts → ComplianceController.d.ts} +7 -7
  7. package/dist/ComplianceController.d.ts.map +1 -0
  8. package/dist/{ComplianceController.mjs → ComplianceController.js} +3 -3
  9. package/dist/ComplianceController.js.map +1 -0
  10. package/dist/{ComplianceService-method-action-types.d.cts → ComplianceService-method-action-types.d.ts} +2 -2
  11. package/dist/ComplianceService-method-action-types.d.ts.map +1 -0
  12. package/dist/{ComplianceService-method-action-types.mjs → ComplianceService-method-action-types.js} +1 -1
  13. package/dist/ComplianceService-method-action-types.js.map +1 -0
  14. package/dist/{ComplianceService.d.mts → ComplianceService.d.ts} +6 -6
  15. package/dist/ComplianceService.d.ts.map +1 -0
  16. package/dist/{ComplianceService.mjs → ComplianceService.js} +5 -5
  17. package/dist/ComplianceService.js.map +1 -0
  18. package/dist/{index.d.cts → index.d.ts} +9 -9
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +4 -0
  21. package/dist/{index.cjs.map → index.js.map} +1 -1
  22. package/dist/{selectors.d.mts → selectors.d.ts} +4 -4
  23. package/dist/selectors.d.ts.map +1 -0
  24. package/dist/{selectors.mjs → selectors.js} +3 -3
  25. package/dist/selectors.js.map +1 -0
  26. package/dist/{types.d.cts → types.d.ts} +1 -1
  27. package/dist/types.d.ts.map +1 -0
  28. package/dist/types.js +2 -0
  29. package/dist/types.js.map +1 -0
  30. package/dist/{utils.d.cts → utils.d.ts} +2 -2
  31. package/dist/utils.d.ts.map +1 -0
  32. package/dist/{utils.mjs → utils.js} +2 -2
  33. package/dist/utils.js.map +1 -0
  34. package/package.json +12 -16
  35. package/dist/ComplianceController-method-action-types.cjs +0 -7
  36. package/dist/ComplianceController-method-action-types.cjs.map +0 -1
  37. package/dist/ComplianceController-method-action-types.d.cts +0 -44
  38. package/dist/ComplianceController-method-action-types.d.cts.map +0 -1
  39. package/dist/ComplianceController-method-action-types.d.mts.map +0 -1
  40. package/dist/ComplianceController-method-action-types.mjs.map +0 -1
  41. package/dist/ComplianceController.cjs +0 -158
  42. package/dist/ComplianceController.cjs.map +0 -1
  43. package/dist/ComplianceController.d.cts.map +0 -1
  44. package/dist/ComplianceController.d.mts +0 -112
  45. package/dist/ComplianceController.d.mts.map +0 -1
  46. package/dist/ComplianceController.mjs.map +0 -1
  47. package/dist/ComplianceService-method-action-types.cjs +0 -7
  48. package/dist/ComplianceService-method-action-types.cjs.map +0 -1
  49. package/dist/ComplianceService-method-action-types.d.cts.map +0 -1
  50. package/dist/ComplianceService-method-action-types.d.mts +0 -30
  51. package/dist/ComplianceService-method-action-types.d.mts.map +0 -1
  52. package/dist/ComplianceService-method-action-types.mjs.map +0 -1
  53. package/dist/ComplianceService.cjs +0 -239
  54. package/dist/ComplianceService.cjs.map +0 -1
  55. package/dist/ComplianceService.d.cts +0 -184
  56. package/dist/ComplianceService.d.cts.map +0 -1
  57. package/dist/ComplianceService.d.mts.map +0 -1
  58. package/dist/ComplianceService.mjs.map +0 -1
  59. package/dist/index.cjs +0 -12
  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/selectors.cjs +0 -27
  66. package/dist/selectors.cjs.map +0 -1
  67. package/dist/selectors.d.cts +0 -20
  68. package/dist/selectors.d.cts.map +0 -1
  69. package/dist/selectors.d.mts.map +0 -1
  70. package/dist/selectors.mjs.map +0 -1
  71. package/dist/types.cjs +0 -3
  72. package/dist/types.cjs.map +0 -1
  73. package/dist/types.d.cts.map +0 -1
  74. package/dist/types.d.mts +0 -18
  75. package/dist/types.d.mts.map +0 -1
  76. package/dist/types.mjs +0 -2
  77. package/dist/types.mjs.map +0 -1
  78. package/dist/utils.cjs +0 -15
  79. package/dist/utils.cjs.map +0 -1
  80. package/dist/utils.d.cts.map +0 -1
  81. package/dist/utils.d.mts +0 -3
  82. package/dist/utils.d.mts.map +0 -1
  83. package/dist/utils.mjs.map +0 -1
@@ -1,158 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ComplianceController = exports.getDefaultComplianceControllerState = exports.controllerName = void 0;
4
- const base_controller_1 = require("@metamask/base-controller");
5
- const utils_js_1 = require("./utils.cjs");
6
- // === GENERAL ===
7
- /**
8
- * The name of the {@link ComplianceController}, used to namespace the
9
- * controller's actions and events and to namespace the controller's state data
10
- * when composed with other controllers.
11
- */
12
- exports.controllerName = 'ComplianceController';
13
- /**
14
- * The metadata for each property in {@link ComplianceControllerState}.
15
- */
16
- const complianceControllerMetadata = {
17
- walletComplianceStatusMap: {
18
- includeInDebugSnapshot: false,
19
- includeInStateLogs: false,
20
- persist: true,
21
- usedInUi: true,
22
- },
23
- lastCheckedAt: {
24
- includeInDebugSnapshot: false,
25
- includeInStateLogs: true,
26
- persist: true,
27
- usedInUi: false,
28
- },
29
- };
30
- /**
31
- * Constructs the default {@link ComplianceController} state. This allows
32
- * consumers to provide a partial state object when initializing the controller
33
- * and also helps in constructing complete state objects for this controller in
34
- * tests.
35
- *
36
- * @returns The default {@link ComplianceController} state.
37
- */
38
- function getDefaultComplianceControllerState() {
39
- return {
40
- walletComplianceStatusMap: {},
41
- lastCheckedAt: null,
42
- };
43
- }
44
- exports.getDefaultComplianceControllerState = getDefaultComplianceControllerState;
45
- // === MESSENGER ===
46
- const MESSENGER_EXPOSED_METHODS = [
47
- 'checkWalletCompliance',
48
- 'checkWalletsCompliance',
49
- 'clearComplianceState',
50
- ];
51
- // === CONTROLLER DEFINITION ===
52
- /**
53
- * `ComplianceController` manages OFAC compliance state for wallet addresses.
54
- * It performs on-demand compliance checks via the API and caches results
55
- * per address in state. Cached results serve as a fallback if the API is
56
- * unavailable for a subsequent check on the same address.
57
- */
58
- class ComplianceController extends base_controller_1.BaseController {
59
- /**
60
- * Constructs a new {@link ComplianceController}.
61
- *
62
- * @param args - The constructor arguments.
63
- * @param args.messenger - The messenger suited for this controller.
64
- * @param args.state - The desired state with which to init this
65
- * controller. Missing properties will be filled in with defaults.
66
- */
67
- constructor({ messenger, state, }) {
68
- super({
69
- messenger,
70
- metadata: complianceControllerMetadata,
71
- name: exports.controllerName,
72
- state: {
73
- ...getDefaultComplianceControllerState(),
74
- ...state,
75
- },
76
- });
77
- this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
78
- }
79
- /**
80
- * Checks compliance status for a single wallet address via the API and
81
- * persists the result to state. If the API call fails and a previously
82
- * cached result exists for the address, the cached result is returned as a
83
- * fallback. If no cached result exists, the error is re-thrown.
84
- *
85
- * @param address - The wallet address to check.
86
- * @returns The compliance status of the wallet.
87
- */
88
- async checkWalletCompliance(address) {
89
- try {
90
- const result = await this.messenger.call('ComplianceService:checkWalletCompliance', address);
91
- const now = new Date().toISOString();
92
- const status = {
93
- address: result.address,
94
- blocked: result.blocked,
95
- checkedAt: now,
96
- };
97
- this.update((draftState) => {
98
- draftState.walletComplianceStatusMap[address] = status;
99
- draftState.lastCheckedAt = now;
100
- });
101
- return status;
102
- }
103
- catch (error) {
104
- const cached = (0, utils_js_1.getWalletComplianceStatus)(this.state.walletComplianceStatusMap, address);
105
- if (cached) {
106
- return cached;
107
- }
108
- throw error;
109
- }
110
- }
111
- /**
112
- * Checks compliance status for multiple wallet addresses via the API and
113
- * persists the results to state. If the API call fails and every requested
114
- * address has a previously cached result, those cached results are returned
115
- * as a fallback. If any address lacks a cached result, the error is
116
- * re-thrown.
117
- *
118
- * @param addresses - The wallet addresses to check.
119
- * @returns The compliance statuses of the wallets.
120
- */
121
- async checkWalletsCompliance(addresses) {
122
- try {
123
- const results = await this.messenger.call('ComplianceService:checkWalletsCompliance', addresses);
124
- const now = new Date().toISOString();
125
- const statuses = results.map((result) => ({
126
- address: result.address,
127
- blocked: result.blocked,
128
- checkedAt: now,
129
- }));
130
- this.update((draftState) => {
131
- for (let idx = 0; idx < statuses.length; idx++) {
132
- const callerAddress = addresses[idx];
133
- draftState.walletComplianceStatusMap[callerAddress] = statuses[idx];
134
- }
135
- draftState.lastCheckedAt = now;
136
- });
137
- return statuses;
138
- }
139
- catch (error) {
140
- const cachedStatuses = addresses.map((address) => (0, utils_js_1.getWalletComplianceStatus)(this.state.walletComplianceStatusMap, address));
141
- if (cachedStatuses.every((status) => Boolean(status))) {
142
- return cachedStatuses;
143
- }
144
- throw error;
145
- }
146
- }
147
- /**
148
- * Clears all compliance data from state.
149
- */
150
- clearComplianceState() {
151
- this.update((draftState) => {
152
- draftState.walletComplianceStatusMap = {};
153
- draftState.lastCheckedAt = null;
154
- });
155
- }
156
- }
157
- exports.ComplianceController = ComplianceController;
158
- //# sourceMappingURL=ComplianceController.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceController.cjs","sourceRoot":"","sources":["../src/ComplianceController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAS3D,0CAAuD;AAEvD,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAqBrD;;GAEG;AACH,MAAM,4BAA4B,GAAG;IACnC,yBAAyB,EAAE;QACzB,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,IAAI;KACf;IACD,aAAa,EAAE;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IA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C/C,IAAA,oCAAyB,EACvB,IAAI,CAAC,KAAK,CAAC,yBAAyB,EACpC,OAAO,CACR,CACF,CAAC;YACF,IACE,cAAc,CAAC,KAAK,CAAC,CAAC,MAAM,EAAoC,EAAE,CAChE,OAAO,CAAC,MAAM,CAAC,CAChB,EACD,CAAC;gBACD,OAAO,cAAc,CAAC;YACxB,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACzB,UAAU,CAAC,yBAAyB,GAAG,EAAE,CAAC;YAC1C,UAAU,CAAC,aAAa,GAAG,IAAI,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA7ID,oDA6IC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { ComplianceControllerMethodActions } from './ComplianceController-method-action-types.js';\nimport type {\n ComplianceServiceCheckWalletComplianceAction,\n ComplianceServiceCheckWalletsComplianceAction,\n} from './ComplianceService-method-action-types.js';\nimport type { WalletComplianceStatus } from './types.js';\nimport { getWalletComplianceStatus } from './utils.js';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link ComplianceController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ComplianceController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link ComplianceController}.\n */\nexport type ComplianceControllerState = {\n /**\n * A map of wallet addresses to their compliance check results, used as a\n * fallback cache when the API is unavailable.\n */\n walletComplianceStatusMap: Record<string, WalletComplianceStatus>;\n\n /**\n * The date/time (in ISO-8601 format) when the last compliance check was\n * performed, or `null` if no checks have been performed yet.\n */\n lastCheckedAt: string | null;\n};\n\n/**\n * The metadata for each property in {@link ComplianceControllerState}.\n */\nconst complianceControllerMetadata = {\n walletComplianceStatusMap: {\n includeInDebugSnapshot: false,\n includeInStateLogs: false,\n persist: true,\n usedInUi: true,\n },\n lastCheckedAt: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ComplianceControllerState>;\n\n/**\n * Constructs the default {@link ComplianceController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ComplianceController} state.\n */\nexport function getDefaultComplianceControllerState(): ComplianceControllerState {\n return {\n walletComplianceStatusMap: {},\n lastCheckedAt: null,\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'checkWalletCompliance',\n 'checkWalletsCompliance',\n 'clearComplianceState',\n] as const;\n\n/**\n * Retrieves the state of the {@link ComplianceController}.\n */\nexport type ComplianceControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ComplianceControllerState\n>;\n\n/**\n * Actions that {@link ComplianceController} exposes to other consumers.\n */\nexport type ComplianceControllerActions =\n | ComplianceControllerGetStateAction\n | ComplianceControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ComplianceController} calls.\n */\ntype AllowedActions =\n | ComplianceServiceCheckWalletComplianceAction\n | ComplianceServiceCheckWalletsComplianceAction;\n\n/**\n * Published when the state of {@link ComplianceController} changes.\n */\nexport type ComplianceControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n ComplianceControllerState\n>;\n\n/**\n * Events that {@link ComplianceController} exposes to other consumers.\n */\nexport type ComplianceControllerEvents = ComplianceControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ComplianceController} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ComplianceController}.\n */\nexport type ComplianceControllerMessenger = Messenger<\n typeof controllerName,\n ComplianceControllerActions | AllowedActions,\n ComplianceControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `ComplianceController` manages OFAC compliance state for wallet addresses.\n * It performs on-demand compliance checks via the API and caches results\n * per address in state. Cached results serve as a fallback if the API is\n * unavailable for a subsequent check on the same address.\n */\nexport class ComplianceController extends BaseController<\n typeof controllerName,\n ComplianceControllerState,\n ComplianceControllerMessenger\n> {\n /**\n * Constructs a new {@link ComplianceController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to init this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: ComplianceControllerMessenger;\n state?: Partial<ComplianceControllerState>;\n }) {\n super({\n messenger,\n metadata: complianceControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultComplianceControllerState(),\n ...state,\n },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Checks compliance status for a single wallet address via the API and\n * persists the result to state. If the API call fails and a previously\n * cached result exists for the address, the cached result is returned as a\n * fallback. If no cached result exists, the error is re-thrown.\n *\n * @param address - The wallet address to check.\n * @returns The compliance status of the wallet.\n */\n async checkWalletCompliance(\n address: string,\n ): Promise<WalletComplianceStatus> {\n try {\n const result = await this.messenger.call(\n 'ComplianceService:checkWalletCompliance',\n address,\n );\n\n const now = new Date().toISOString();\n const status: WalletComplianceStatus = {\n address: result.address,\n blocked: result.blocked,\n checkedAt: now,\n };\n\n this.update((draftState) => {\n draftState.walletComplianceStatusMap[address] = status;\n draftState.lastCheckedAt = now;\n });\n\n return status;\n } catch (error) {\n const cached = getWalletComplianceStatus(\n this.state.walletComplianceStatusMap,\n address,\n );\n if (cached) {\n return cached;\n }\n throw error;\n }\n }\n\n /**\n * Checks compliance status for multiple wallet addresses via the API and\n * persists the results to state. If the API call fails and every requested\n * address has a previously cached result, those cached results are returned\n * as a fallback. If any address lacks a cached result, the error is\n * re-thrown.\n *\n * @param addresses - The wallet addresses to check.\n * @returns The compliance statuses of the wallets.\n */\n async checkWalletsCompliance(\n addresses: string[],\n ): Promise<WalletComplianceStatus[]> {\n try {\n const results = await this.messenger.call(\n 'ComplianceService:checkWalletsCompliance',\n addresses,\n );\n\n const now = new Date().toISOString();\n const statuses: WalletComplianceStatus[] = results.map((result) => ({\n address: result.address,\n blocked: result.blocked,\n checkedAt: now,\n }));\n\n this.update((draftState) => {\n for (let idx = 0; idx < statuses.length; idx++) {\n const callerAddress = addresses[idx];\n draftState.walletComplianceStatusMap[callerAddress] = statuses[idx];\n }\n draftState.lastCheckedAt = now;\n });\n\n return statuses;\n } catch (error) {\n const cachedStatuses = addresses.map((address) =>\n getWalletComplianceStatus(\n this.state.walletComplianceStatusMap,\n address,\n ),\n );\n if (\n cachedStatuses.every((status): status is WalletComplianceStatus =>\n Boolean(status),\n )\n ) {\n return cachedStatuses;\n }\n throw error;\n }\n }\n\n /**\n * Clears all compliance data from state.\n */\n clearComplianceState(): void {\n this.update((draftState) => {\n draftState.walletComplianceStatusMap = {};\n draftState.lastCheckedAt = null;\n });\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceController.d.cts","sourceRoot":"","sources":["../src/ComplianceController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,iCAAiC,EAAE,uDAAsD;AACvG,OAAO,KAAK,EACV,4CAA4C,EAC5C,6CAA6C,EAC9C,oDAAmD;AACpD,OAAO,KAAK,EAAE,sBAAsB,EAAE,oBAAmB;AAKzD;;;;GAIG;AACH,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAIrD;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC;;;OAGG;IACH,yBAAyB,EAAE,MAAM,CAAC,MAAM,EAAE,sBAAsB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@@ -1,112 +0,0 @@
1
- import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
2
- import { BaseController } from "@metamask/base-controller";
3
- import type { Messenger } from "@metamask/messenger";
4
- import type { ComplianceControllerMethodActions } from "./ComplianceController-method-action-types.mjs";
5
- import type { ComplianceServiceCheckWalletComplianceAction, ComplianceServiceCheckWalletsComplianceAction } from "./ComplianceService-method-action-types.mjs";
6
- import type { WalletComplianceStatus } from "./types.mjs";
7
- /**
8
- * The name of the {@link ComplianceController}, used to namespace the
9
- * controller's actions and events and to namespace the controller's state data
10
- * when composed with other controllers.
11
- */
12
- export declare const controllerName = "ComplianceController";
13
- /**
14
- * Describes the shape of the state object for {@link ComplianceController}.
15
- */
16
- export type ComplianceControllerState = {
17
- /**
18
- * A map of wallet addresses to their compliance check results, used as a
19
- * fallback cache when the API is unavailable.
20
- */
21
- walletComplianceStatusMap: Record<string, WalletComplianceStatus>;
22
- /**
23
- * The date/time (in ISO-8601 format) when the last compliance check was
24
- * performed, or `null` if no checks have been performed yet.
25
- */
26
- lastCheckedAt: string | null;
27
- };
28
- /**
29
- * Constructs the default {@link ComplianceController} state. This allows
30
- * consumers to provide a partial state object when initializing the controller
31
- * and also helps in constructing complete state objects for this controller in
32
- * tests.
33
- *
34
- * @returns The default {@link ComplianceController} state.
35
- */
36
- export declare function getDefaultComplianceControllerState(): ComplianceControllerState;
37
- /**
38
- * Retrieves the state of the {@link ComplianceController}.
39
- */
40
- export type ComplianceControllerGetStateAction = ControllerGetStateAction<typeof controllerName, ComplianceControllerState>;
41
- /**
42
- * Actions that {@link ComplianceController} exposes to other consumers.
43
- */
44
- export type ComplianceControllerActions = ComplianceControllerGetStateAction | ComplianceControllerMethodActions;
45
- /**
46
- * Actions from other messengers that {@link ComplianceController} calls.
47
- */
48
- type AllowedActions = ComplianceServiceCheckWalletComplianceAction | ComplianceServiceCheckWalletsComplianceAction;
49
- /**
50
- * Published when the state of {@link ComplianceController} changes.
51
- */
52
- export type ComplianceControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, ComplianceControllerState>;
53
- /**
54
- * Events that {@link ComplianceController} exposes to other consumers.
55
- */
56
- export type ComplianceControllerEvents = ComplianceControllerStateChangeEvent;
57
- /**
58
- * Events from other messengers that {@link ComplianceController} subscribes to.
59
- */
60
- type AllowedEvents = never;
61
- /**
62
- * The messenger restricted to actions and events accessed by
63
- * {@link ComplianceController}.
64
- */
65
- export type ComplianceControllerMessenger = Messenger<typeof controllerName, ComplianceControllerActions | AllowedActions, ComplianceControllerEvents | AllowedEvents>;
66
- /**
67
- * `ComplianceController` manages OFAC compliance state for wallet addresses.
68
- * It performs on-demand compliance checks via the API and caches results
69
- * per address in state. Cached results serve as a fallback if the API is
70
- * unavailable for a subsequent check on the same address.
71
- */
72
- export declare class ComplianceController extends BaseController<typeof controllerName, ComplianceControllerState, ComplianceControllerMessenger> {
73
- /**
74
- * Constructs a new {@link ComplianceController}.
75
- *
76
- * @param args - The constructor arguments.
77
- * @param args.messenger - The messenger suited for this controller.
78
- * @param args.state - The desired state with which to init this
79
- * controller. Missing properties will be filled in with defaults.
80
- */
81
- constructor({ messenger, state, }: {
82
- messenger: ComplianceControllerMessenger;
83
- state?: Partial<ComplianceControllerState>;
84
- });
85
- /**
86
- * Checks compliance status for a single wallet address via the API and
87
- * persists the result to state. If the API call fails and a previously
88
- * cached result exists for the address, the cached result is returned as a
89
- * fallback. If no cached result exists, the error is re-thrown.
90
- *
91
- * @param address - The wallet address to check.
92
- * @returns The compliance status of the wallet.
93
- */
94
- checkWalletCompliance(address: string): Promise<WalletComplianceStatus>;
95
- /**
96
- * Checks compliance status for multiple wallet addresses via the API and
97
- * persists the results to state. If the API call fails and every requested
98
- * address has a previously cached result, those cached results are returned
99
- * as a fallback. If any address lacks a cached result, the error is
100
- * re-thrown.
101
- *
102
- * @param addresses - The wallet addresses to check.
103
- * @returns The compliance statuses of the wallets.
104
- */
105
- checkWalletsCompliance(addresses: string[]): Promise<WalletComplianceStatus[]>;
106
- /**
107
- * Clears all compliance data from state.
108
- */
109
- clearComplianceState(): void;
110
- }
111
- export {};
112
- //# sourceMappingURL=ComplianceController.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceController.d.mts","sourceRoot":"","sources":["../src/ComplianceController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,iCAAiC,EAAE,uDAAsD;AACvG,OAAO,KAAK,EACV,4CAA4C,EAC5C,6CAA6C,EAC9C,oDAAmD;AACpD,OAAO,KAAK,EAAE,sBAAsB,EAAE,oBAAmB;AAKzD;;;;GAIG;AACH,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAIrD;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC;;;OAGG;IACH,yBAAyB,EAAE,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IAElE;;;OAGG;IACH,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,CAAC;AAoBF;;;;;;;GAOG;AACH,wBAAgB,mCAAmC,IAAI,yBAAyB,CAK/E;AAUD;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAAG,wBAAwB,CACvE,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,2BAA2B,GACnC,kCAAkC,GAClC,iCAAiC,CAAC;AAEtC;;GAEG;AACH,KAAK,cAAc,GACf,4CAA4C,GAC5C,6CAA6C,CAAC;AAElD;;GAEG;AACH,MAAM,MAAM,oCAAoC,GAAG,0BAA0B,CAC3E,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,oCAAoC,CAAC;AAE9E;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,6BAA6B,GAAG,SAAS,CACnD,OAAO,cAAc,EACrB,2BAA2B,GAAG,cAAc,EAC5C,0BAA0B,GAAG,aAAa,CAC3C,CAAC;AAIF;;;;;GAKG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CACtD,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAC9B;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,6BAA6B,CAAC;QACzC,KAAK,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;KAC5C;IAiBD;;;;;;;;OAQG;IACG,qBAAqB,CACzB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,sBAAsB,CAAC;IAgClC;;;;;;;;;OASG;IACG,sBAAsB,CAC1B,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,sBAAsB,EAAE,CAAC;IAyCpC;;OAEG;IACH,oBAAoB,IAAI,IAAI;CAM7B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceController.mjs","sourceRoot":"","sources":["../src/ComplianceController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAS3D,OAAO,EAAE,yBAAyB,EAAE,oBAAmB;AAEvD,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAqBrD;;GAEG;AACH,MAAM,4BAA4B,GAAG;IACnC,yBAAyB,EAAE;QACzB,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,IAAI;KACf;IACD,aAAa,EAAE;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,KAAK;KAChB;CACiD,CAAC;AAErD;;;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CACR,CACF,CAAC;YACF,IACE,cAAc,CAAC,KAAK,CAAC,CAAC,MAAM,EAAoC,EAAE,CAChE,OAAO,CAAC,MAAM,CAAC,CAChB,EACD,CAAC;gBACD,OAAO,cAAc,CAAC;YACxB,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACzB,UAAU,CAAC,yBAAyB,GAAG,EAAE,CAAC;YAC1C,UAAU,CAAC,aAAa,GAAG,IAAI,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { ComplianceControllerMethodActions } from './ComplianceController-method-action-types.js';\nimport type {\n ComplianceServiceCheckWalletComplianceAction,\n ComplianceServiceCheckWalletsComplianceAction,\n} from './ComplianceService-method-action-types.js';\nimport type { WalletComplianceStatus } from './types.js';\nimport { getWalletComplianceStatus } from './utils.js';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link ComplianceController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ComplianceController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link ComplianceController}.\n */\nexport type ComplianceControllerState = {\n /**\n * A map of wallet addresses to their compliance check results, used as a\n * fallback cache when the API is unavailable.\n */\n walletComplianceStatusMap: Record<string, WalletComplianceStatus>;\n\n /**\n * The date/time (in ISO-8601 format) when the last compliance check was\n * performed, or `null` if no checks have been performed yet.\n */\n lastCheckedAt: string | null;\n};\n\n/**\n * The metadata for each property in {@link ComplianceControllerState}.\n */\nconst complianceControllerMetadata = {\n walletComplianceStatusMap: {\n includeInDebugSnapshot: false,\n includeInStateLogs: false,\n persist: true,\n usedInUi: true,\n },\n lastCheckedAt: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ComplianceControllerState>;\n\n/**\n * Constructs the default {@link ComplianceController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ComplianceController} state.\n */\nexport function getDefaultComplianceControllerState(): ComplianceControllerState {\n return {\n walletComplianceStatusMap: {},\n lastCheckedAt: null,\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'checkWalletCompliance',\n 'checkWalletsCompliance',\n 'clearComplianceState',\n] as const;\n\n/**\n * Retrieves the state of the {@link ComplianceController}.\n */\nexport type ComplianceControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ComplianceControllerState\n>;\n\n/**\n * Actions that {@link ComplianceController} exposes to other consumers.\n */\nexport type ComplianceControllerActions =\n | ComplianceControllerGetStateAction\n | ComplianceControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ComplianceController} calls.\n */\ntype AllowedActions =\n | ComplianceServiceCheckWalletComplianceAction\n | ComplianceServiceCheckWalletsComplianceAction;\n\n/**\n * Published when the state of {@link ComplianceController} changes.\n */\nexport type ComplianceControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n ComplianceControllerState\n>;\n\n/**\n * Events that {@link ComplianceController} exposes to other consumers.\n */\nexport type ComplianceControllerEvents = ComplianceControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ComplianceController} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ComplianceController}.\n */\nexport type ComplianceControllerMessenger = Messenger<\n typeof controllerName,\n ComplianceControllerActions | AllowedActions,\n ComplianceControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `ComplianceController` manages OFAC compliance state for wallet addresses.\n * It performs on-demand compliance checks via the API and caches results\n * per address in state. Cached results serve as a fallback if the API is\n * unavailable for a subsequent check on the same address.\n */\nexport class ComplianceController extends BaseController<\n typeof controllerName,\n ComplianceControllerState,\n ComplianceControllerMessenger\n> {\n /**\n * Constructs a new {@link ComplianceController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to init this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: ComplianceControllerMessenger;\n state?: Partial<ComplianceControllerState>;\n }) {\n super({\n messenger,\n metadata: complianceControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultComplianceControllerState(),\n ...state,\n },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Checks compliance status for a single wallet address via the API and\n * persists the result to state. If the API call fails and a previously\n * cached result exists for the address, the cached result is returned as a\n * fallback. If no cached result exists, the error is re-thrown.\n *\n * @param address - The wallet address to check.\n * @returns The compliance status of the wallet.\n */\n async checkWalletCompliance(\n address: string,\n ): Promise<WalletComplianceStatus> {\n try {\n const result = await this.messenger.call(\n 'ComplianceService:checkWalletCompliance',\n address,\n );\n\n const now = new Date().toISOString();\n const status: WalletComplianceStatus = {\n address: result.address,\n blocked: result.blocked,\n checkedAt: now,\n };\n\n this.update((draftState) => {\n draftState.walletComplianceStatusMap[address] = status;\n draftState.lastCheckedAt = now;\n });\n\n return status;\n } catch (error) {\n const cached = getWalletComplianceStatus(\n this.state.walletComplianceStatusMap,\n address,\n );\n if (cached) {\n return cached;\n }\n throw error;\n }\n }\n\n /**\n * Checks compliance status for multiple wallet addresses via the API and\n * persists the results to state. If the API call fails and every requested\n * address has a previously cached result, those cached results are returned\n * as a fallback. If any address lacks a cached result, the error is\n * re-thrown.\n *\n * @param addresses - The wallet addresses to check.\n * @returns The compliance statuses of the wallets.\n */\n async checkWalletsCompliance(\n addresses: string[],\n ): Promise<WalletComplianceStatus[]> {\n try {\n const results = await this.messenger.call(\n 'ComplianceService:checkWalletsCompliance',\n addresses,\n );\n\n const now = new Date().toISOString();\n const statuses: WalletComplianceStatus[] = results.map((result) => ({\n address: result.address,\n blocked: result.blocked,\n checkedAt: now,\n }));\n\n this.update((draftState) => {\n for (let idx = 0; idx < statuses.length; idx++) {\n const callerAddress = addresses[idx];\n draftState.walletComplianceStatusMap[callerAddress] = statuses[idx];\n }\n draftState.lastCheckedAt = now;\n });\n\n return statuses;\n } catch (error) {\n const cachedStatuses = addresses.map((address) =>\n getWalletComplianceStatus(\n this.state.walletComplianceStatusMap,\n address,\n ),\n );\n if (\n cachedStatuses.every((status): status is WalletComplianceStatus =>\n Boolean(status),\n )\n ) {\n return cachedStatuses;\n }\n throw error;\n }\n }\n\n /**\n * Clears all compliance data from state.\n */\n clearComplianceState(): void {\n this.update((draftState) => {\n draftState.walletComplianceStatusMap = {};\n draftState.lastCheckedAt = null;\n });\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=ComplianceService-method-action-types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceService-method-action-types.cjs","sourceRoot":"","sources":["../src/ComplianceService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ComplianceService } from './ComplianceService.js';\n\n/**\n * Checks compliance status for a single wallet address.\n *\n * @param address - The wallet address to check.\n * @returns The compliance status of the wallet.\n */\nexport type ComplianceServiceCheckWalletComplianceAction = {\n type: `ComplianceService:checkWalletCompliance`;\n handler: ComplianceService['checkWalletCompliance'];\n};\n\n/**\n * Checks compliance status for multiple wallet addresses in a single request.\n *\n * @param addresses - The wallet addresses to check.\n * @returns The compliance statuses of the wallets.\n */\nexport type ComplianceServiceCheckWalletsComplianceAction = {\n type: `ComplianceService:checkWalletsCompliance`;\n handler: ComplianceService['checkWalletsCompliance'];\n};\n\n/**\n * Union of all ComplianceService action types.\n */\nexport type ComplianceServiceMethodActions =\n | ComplianceServiceCheckWalletComplianceAction\n | ComplianceServiceCheckWalletsComplianceAction;\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceService-method-action-types.d.cts","sourceRoot":"","sources":["../src/ComplianceService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA+B;AAEhE;;;;;GAKG;AACH,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,yCAAyC,CAAC;IAChD,OAAO,EAAE,iBAAiB,CAAC,uBAAuB,CAAC,CAAC;CACrD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,iBAAiB,CAAC,wBAAwB,CAAC,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACtC,4CAA4C,GAC5C,6CAA6C,CAAC"}
@@ -1,30 +0,0 @@
1
- /**
2
- * This file is auto generated.
3
- * Do not edit manually.
4
- */
5
- import type { ComplianceService } from "./ComplianceService.mjs";
6
- /**
7
- * Checks compliance status for a single wallet address.
8
- *
9
- * @param address - The wallet address to check.
10
- * @returns The compliance status of the wallet.
11
- */
12
- export type ComplianceServiceCheckWalletComplianceAction = {
13
- type: `ComplianceService:checkWalletCompliance`;
14
- handler: ComplianceService['checkWalletCompliance'];
15
- };
16
- /**
17
- * Checks compliance status for multiple wallet addresses in a single request.
18
- *
19
- * @param addresses - The wallet addresses to check.
20
- * @returns The compliance statuses of the wallets.
21
- */
22
- export type ComplianceServiceCheckWalletsComplianceAction = {
23
- type: `ComplianceService:checkWalletsCompliance`;
24
- handler: ComplianceService['checkWalletsCompliance'];
25
- };
26
- /**
27
- * Union of all ComplianceService action types.
28
- */
29
- export type ComplianceServiceMethodActions = ComplianceServiceCheckWalletComplianceAction | ComplianceServiceCheckWalletsComplianceAction;
30
- //# sourceMappingURL=ComplianceService-method-action-types.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceService-method-action-types.d.mts","sourceRoot":"","sources":["../src/ComplianceService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA+B;AAEhE;;;;;GAKG;AACH,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,yCAAyC,CAAC;IAChD,OAAO,EAAE,iBAAiB,CAAC,uBAAuB,CAAC,CAAC;CACrD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,iBAAiB,CAAC,wBAAwB,CAAC,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACtC,4CAA4C,GAC5C,6CAA6C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComplianceService-method-action-types.mjs","sourceRoot":"","sources":["../src/ComplianceService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ComplianceService } from './ComplianceService.js';\n\n/**\n * Checks compliance status for a single wallet address.\n *\n * @param address - The wallet address to check.\n * @returns The compliance status of the wallet.\n */\nexport type ComplianceServiceCheckWalletComplianceAction = {\n type: `ComplianceService:checkWalletCompliance`;\n handler: ComplianceService['checkWalletCompliance'];\n};\n\n/**\n * Checks compliance status for multiple wallet addresses in a single request.\n *\n * @param addresses - The wallet addresses to check.\n * @returns The compliance statuses of the wallets.\n */\nexport type ComplianceServiceCheckWalletsComplianceAction = {\n type: `ComplianceService:checkWalletsCompliance`;\n handler: ComplianceService['checkWalletsCompliance'];\n};\n\n/**\n * Union of all ComplianceService action types.\n */\nexport type ComplianceServiceMethodActions =\n | ComplianceServiceCheckWalletComplianceAction\n | ComplianceServiceCheckWalletsComplianceAction;\n"]}
@@ -1,239 +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 _a, _ComplianceService_messenger, _ComplianceService_fetch, _ComplianceService_complianceApiUrl, _ComplianceService_policy;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.ComplianceService = exports.serviceName = void 0;
16
- const controller_utils_1 = require("@metamask/controller-utils");
17
- const superstruct_1 = require("@metamask/superstruct");
18
- // === GENERAL ===
19
- /**
20
- * The name of the {@link ComplianceService}, used to namespace the service's
21
- * actions and events.
22
- */
23
- exports.serviceName = 'ComplianceService';
24
- const COMPLIANCE_API_URLS = {
25
- production: 'https://compliance.api.cx.metamask.io',
26
- development: 'https://compliance.dev-api.cx.metamask.io',
27
- };
28
- // === MESSENGER ===
29
- const MESSENGER_EXPOSED_METHODS = [
30
- 'checkWalletCompliance',
31
- 'checkWalletsCompliance',
32
- ];
33
- // === API RESPONSE SCHEMAS ===
34
- /**
35
- * Schema for the response from `GET /v1/wallet/:address`.
36
- */
37
- const WalletCheckResponseStruct = (0, superstruct_1.object)({
38
- address: (0, superstruct_1.string)(),
39
- blocked: (0, superstruct_1.boolean)(),
40
- });
41
- /**
42
- * Schema for each item in the response from `POST /v1/wallet/batch`.
43
- * Reuses the same shape as a single wallet check.
44
- */
45
- const BatchWalletCheckResponseItemStruct = WalletCheckResponseStruct;
46
- // === SERVICE DEFINITION ===
47
- /**
48
- * `ComplianceService` communicates with the Compliance API to check whether
49
- * wallet addresses are sanctioned under OFAC regulations.
50
- *
51
- * @example
52
- *
53
- * ``` ts
54
- * import { Messenger } from '@metamask/messenger';
55
- * import type {
56
- * ComplianceServiceActions,
57
- * ComplianceServiceEvents,
58
- * } from '@metamask/compliance-controller';
59
- * import { ComplianceService } from '@metamask/compliance-controller';
60
- *
61
- * const rootMessenger = new Messenger<
62
- * 'Root',
63
- * ComplianceServiceActions,
64
- * ComplianceServiceEvents,
65
- * >({ namespace: 'Root' });
66
- * const serviceMessenger = new Messenger<
67
- * 'ComplianceService',
68
- * ComplianceServiceActions,
69
- * ComplianceServiceEvents,
70
- * typeof rootMessenger,
71
- * >({
72
- * namespace: 'ComplianceService',
73
- * parent: rootMessenger,
74
- * });
75
- * new ComplianceService({
76
- * messenger: serviceMessenger,
77
- * fetch,
78
- * apiUrl: 'https://compliance.api.cx.metamask.io',
79
- * });
80
- *
81
- * // Check a single wallet
82
- * const result = await rootMessenger.call(
83
- * 'ComplianceService:checkWalletCompliance',
84
- * '0x1234...',
85
- * );
86
- * // => { address: '0x1234...', blocked: false }
87
- * ```
88
- */
89
- class ComplianceService {
90
- /**
91
- * Constructs a new ComplianceService object.
92
- *
93
- * @param args - The constructor arguments.
94
- * @param args.messenger - The messenger suited for this service.
95
- * @param args.fetch - A function that can be used to make an HTTP request.
96
- * @param args.apiUrl - The explicit Compliance API URL.
97
- * @param args.env - The fallback environment to use for the Compliance API
98
- * when `apiUrl` is not provided.
99
- * @param args.policyOptions - Options to pass to `createServicePolicy`, which
100
- * is used to wrap each request. See {@link CreateServicePolicyOptions}.
101
- */
102
- constructor({ messenger, fetch: fetchFunction, apiUrl, env = 'production', policyOptions = {}, }) {
103
- /**
104
- * The messenger suited for this service.
105
- */
106
- _ComplianceService_messenger.set(this, void 0);
107
- /**
108
- * A function that can be used to make an HTTP request.
109
- */
110
- _ComplianceService_fetch.set(this, void 0);
111
- /**
112
- * The resolved base URL for the Compliance API.
113
- */
114
- _ComplianceService_complianceApiUrl.set(this, void 0);
115
- /**
116
- * The policy that wraps each request.
117
- *
118
- * @see {@link createServicePolicy}
119
- */
120
- _ComplianceService_policy.set(this, void 0);
121
- this.name = exports.serviceName;
122
- __classPrivateFieldSet(this, _ComplianceService_messenger, messenger, "f");
123
- __classPrivateFieldSet(this, _ComplianceService_fetch, fetchFunction, "f");
124
- __classPrivateFieldSet(this, _ComplianceService_complianceApiUrl, getComplianceApiUrl({ apiUrl, env }), "f");
125
- __classPrivateFieldSet(this, _ComplianceService_policy, (0, controller_utils_1.createServicePolicy)(policyOptions), "f");
126
- __classPrivateFieldGet(this, _ComplianceService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
127
- }
128
- /**
129
- * Registers a handler that will be called after a request returns a non-500
130
- * response, causing a retry.
131
- *
132
- * @param listener - The handler to be called.
133
- * @returns An object that can be used to unregister the handler.
134
- * @see {@link createServicePolicy}
135
- */
136
- onRetry(listener) {
137
- return __classPrivateFieldGet(this, _ComplianceService_policy, "f").onRetry(listener);
138
- }
139
- /**
140
- * Registers a handler that will be called after a set number of retry rounds
141
- * prove that requests to the API endpoint consistently return a 5xx response.
142
- *
143
- * @param listener - The handler to be called.
144
- * @returns An object that can be used to unregister the handler.
145
- * @see {@link createServicePolicy}
146
- */
147
- onBreak(listener) {
148
- return __classPrivateFieldGet(this, _ComplianceService_policy, "f").onBreak(listener);
149
- }
150
- /**
151
- * Registers a handler that will be called when the service is degraded due
152
- * to slow responses or repeated failures.
153
- *
154
- * @param listener - The handler to be called.
155
- * @returns An object that can be used to unregister the handler.
156
- * @see {@link createServicePolicy}
157
- */
158
- onDegraded(listener) {
159
- return __classPrivateFieldGet(this, _ComplianceService_policy, "f").onDegraded(listener);
160
- }
161
- /**
162
- * Checks compliance status for a single wallet address.
163
- *
164
- * @param address - The wallet address to check.
165
- * @returns The compliance status of the wallet.
166
- */
167
- async checkWalletCompliance(address) {
168
- const response = await __classPrivateFieldGet(this, _ComplianceService_policy, "f").execute(async () => {
169
- const url = new URL(`v1/wallet/${encodeURIComponent(address)}`, __classPrivateFieldGet(this, _ComplianceService_complianceApiUrl, "f"));
170
- const localResponse = await __classPrivateFieldGet(this, _ComplianceService_fetch, "f").call(this, url);
171
- if (!localResponse.ok) {
172
- throw new controller_utils_1.HttpError(localResponse.status, `Fetching '${url.toString()}' failed with status '${localResponse.status}'`);
173
- }
174
- return localResponse;
175
- });
176
- const jsonResponse = await response.json();
177
- return validateResponse(jsonResponse, WalletCheckResponseStruct, 'compliance wallet check API');
178
- }
179
- /**
180
- * Checks compliance status for multiple wallet addresses in a single request.
181
- *
182
- * @param addresses - The wallet addresses to check.
183
- * @returns The compliance statuses of the wallets.
184
- */
185
- async checkWalletsCompliance(addresses) {
186
- const response = await __classPrivateFieldGet(this, _ComplianceService_policy, "f").execute(async () => {
187
- const url = new URL('v1/wallet/batch', __classPrivateFieldGet(this, _ComplianceService_complianceApiUrl, "f"));
188
- const localResponse = await __classPrivateFieldGet(this, _ComplianceService_fetch, "f").call(this, url, {
189
- method: 'POST',
190
- headers: { 'Content-Type': 'application/json' },
191
- body: JSON.stringify(addresses),
192
- });
193
- if (!localResponse.ok) {
194
- throw new controller_utils_1.HttpError(localResponse.status, `Fetching '${url.toString()}' failed with status '${localResponse.status}'`);
195
- }
196
- return localResponse;
197
- });
198
- const jsonResponse = await response.json();
199
- return validateResponse(jsonResponse, (0, superstruct_1.array)(BatchWalletCheckResponseItemStruct), 'compliance batch check API');
200
- }
201
- }
202
- exports.ComplianceService = ComplianceService;
203
- _a = ComplianceService, _ComplianceService_messenger = new WeakMap(), _ComplianceService_fetch = new WeakMap(), _ComplianceService_complianceApiUrl = new WeakMap(), _ComplianceService_policy = new WeakMap();
204
- function getComplianceApiUrl({ apiUrl, env, }) {
205
- if (apiUrl === undefined) {
206
- return COMPLIANCE_API_URLS[env];
207
- }
208
- let url;
209
- try {
210
- url = new URL(apiUrl);
211
- }
212
- catch {
213
- throw new Error(`Invalid Compliance API URL: ${apiUrl}`);
214
- }
215
- if (url.search || url.hash) {
216
- throw new Error(`Invalid Compliance API URL: ${apiUrl}. Query strings and fragments are not supported.`);
217
- }
218
- if (!url.pathname.endsWith('/')) {
219
- url.pathname = `${url.pathname}/`;
220
- }
221
- return url.href;
222
- }
223
- /**
224
- * Validates an API response against a superstruct schema.
225
- *
226
- * @param data - The raw response data to validate.
227
- * @param struct - The superstruct schema to validate against.
228
- * @param struct.is - The type guard function from the schema.
229
- * @param apiName - A human-readable name for the API, used in error messages.
230
- * @returns The validated data.
231
- * @throws If the data does not match the schema.
232
- */
233
- function validateResponse(data, struct, apiName) {
234
- if (struct.is(data)) {
235
- return data;
236
- }
237
- throw new Error(`Malformed response received from ${apiName}`);
238
- }
239
- //# sourceMappingURL=ComplianceService.cjs.map