@metamask/profile-sync-controller 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/CHANGELOG.md +32 -1
  2. package/dist/controllers/user-storage/UserStorageController.cjs +55 -22
  3. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  4. package/dist/controllers/user-storage/UserStorageController.d.cts +32 -15
  5. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  6. package/dist/controllers/user-storage/UserStorageController.d.mts +32 -15
  7. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  8. package/dist/controllers/user-storage/UserStorageController.mjs +56 -23
  9. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  10. package/dist/controllers/user-storage/__fixtures__/mockMessenger.cjs +171 -0
  11. package/dist/controllers/user-storage/__fixtures__/mockMessenger.cjs.map +1 -0
  12. package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.cts +102 -0
  13. package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.cts.map +1 -0
  14. package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.mts +102 -0
  15. package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.mts.map +1 -0
  16. package/dist/controllers/user-storage/__fixtures__/mockMessenger.mjs +166 -0
  17. package/dist/controllers/user-storage/__fixtures__/mockMessenger.mjs.map +1 -0
  18. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.cjs +20 -2
  19. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.cjs.map +1 -1
  20. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.d.cts +8 -1
  21. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.d.cts.map +1 -1
  22. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.d.mts +8 -1
  23. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.d.mts.map +1 -1
  24. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.mjs +17 -1
  25. package/dist/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.mjs.map +1 -1
  26. package/dist/controllers/user-storage/network-syncing/add-network-utils.cjs +35 -0
  27. package/dist/controllers/user-storage/network-syncing/add-network-utils.cjs.map +1 -0
  28. package/dist/controllers/user-storage/network-syncing/add-network-utils.d.cts +21 -0
  29. package/dist/controllers/user-storage/network-syncing/add-network-utils.d.cts.map +1 -0
  30. package/dist/controllers/user-storage/network-syncing/add-network-utils.d.mts +21 -0
  31. package/dist/controllers/user-storage/network-syncing/add-network-utils.d.mts.map +1 -0
  32. package/dist/controllers/user-storage/network-syncing/add-network-utils.mjs +30 -0
  33. package/dist/controllers/user-storage/network-syncing/add-network-utils.mjs.map +1 -0
  34. package/dist/controllers/user-storage/network-syncing/controller-integration.cjs +157 -29
  35. package/dist/controllers/user-storage/network-syncing/controller-integration.cjs.map +1 -1
  36. package/dist/controllers/user-storage/network-syncing/controller-integration.d.cts +45 -3
  37. package/dist/controllers/user-storage/network-syncing/controller-integration.d.cts.map +1 -1
  38. package/dist/controllers/user-storage/network-syncing/controller-integration.d.mts +45 -3
  39. package/dist/controllers/user-storage/network-syncing/controller-integration.d.mts.map +1 -1
  40. package/dist/controllers/user-storage/network-syncing/controller-integration.mjs +154 -28
  41. package/dist/controllers/user-storage/network-syncing/controller-integration.mjs.map +1 -1
  42. package/dist/controllers/user-storage/network-syncing/services.cjs +9 -4
  43. package/dist/controllers/user-storage/network-syncing/services.cjs.map +1 -1
  44. package/dist/controllers/user-storage/network-syncing/services.d.cts.map +1 -1
  45. package/dist/controllers/user-storage/network-syncing/services.d.mts.map +1 -1
  46. package/dist/controllers/user-storage/network-syncing/services.mjs +10 -5
  47. package/dist/controllers/user-storage/network-syncing/services.mjs.map +1 -1
  48. package/dist/controllers/user-storage/network-syncing/sync-all.cjs +10 -28
  49. package/dist/controllers/user-storage/network-syncing/sync-all.cjs.map +1 -1
  50. package/dist/controllers/user-storage/network-syncing/sync-all.d.cts +5 -8
  51. package/dist/controllers/user-storage/network-syncing/sync-all.d.cts.map +1 -1
  52. package/dist/controllers/user-storage/network-syncing/sync-all.d.mts +5 -8
  53. package/dist/controllers/user-storage/network-syncing/sync-all.d.mts.map +1 -1
  54. package/dist/controllers/user-storage/network-syncing/sync-all.mjs +9 -26
  55. package/dist/controllers/user-storage/network-syncing/sync-all.mjs.map +1 -1
  56. package/dist/controllers/user-storage/network-syncing/sync-mutations.cjs +1 -1
  57. package/dist/controllers/user-storage/network-syncing/sync-mutations.cjs.map +1 -1
  58. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.cts +1 -1
  59. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.cts.map +1 -1
  60. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.mts +1 -1
  61. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.mts.map +1 -1
  62. package/dist/controllers/user-storage/network-syncing/sync-mutations.mjs +1 -1
  63. package/dist/controllers/user-storage/network-syncing/sync-mutations.mjs.map +1 -1
  64. package/dist/controllers/user-storage/network-syncing/types.cjs.map +1 -1
  65. package/dist/controllers/user-storage/network-syncing/types.d.cts +1 -4
  66. package/dist/controllers/user-storage/network-syncing/types.d.cts.map +1 -1
  67. package/dist/controllers/user-storage/network-syncing/types.d.mts +1 -4
  68. package/dist/controllers/user-storage/network-syncing/types.d.mts.map +1 -1
  69. package/dist/controllers/user-storage/network-syncing/types.mjs.map +1 -1
  70. package/dist/controllers/user-storage/network-syncing/update-network-utils.cjs +126 -0
  71. package/dist/controllers/user-storage/network-syncing/update-network-utils.cjs.map +1 -0
  72. package/dist/controllers/user-storage/network-syncing/update-network-utils.d.cts +56 -0
  73. package/dist/controllers/user-storage/network-syncing/update-network-utils.d.cts.map +1 -0
  74. package/dist/controllers/user-storage/network-syncing/update-network-utils.d.mts +56 -0
  75. package/dist/controllers/user-storage/network-syncing/update-network-utils.d.mts.map +1 -0
  76. package/dist/controllers/user-storage/network-syncing/update-network-utils.mjs +119 -0
  77. package/dist/controllers/user-storage/network-syncing/update-network-utils.mjs.map +1 -0
  78. package/dist/controllers/user-storage/services.cjs +2 -0
  79. package/dist/controllers/user-storage/services.cjs.map +1 -1
  80. package/dist/controllers/user-storage/services.d.cts.map +1 -1
  81. package/dist/controllers/user-storage/services.d.mts.map +1 -1
  82. package/dist/controllers/user-storage/services.mjs +2 -0
  83. package/dist/controllers/user-storage/services.mjs.map +1 -1
  84. package/dist/sdk/authentication-jwt-bearer/flow-siwe.mjs +1 -2
  85. package/dist/sdk/authentication-jwt-bearer/flow-siwe.mjs.map +1 -1
  86. package/package.json +11 -6
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMockRemoteNetworkConfiguration = exports.createMockNetworkConfiguration = void 0;
3
+ exports.createMockInfuraRpcEndpoint = exports.createMockCustomRpcEndpoint = exports.createMockRemoteNetworkConfiguration = exports.createMockNetworkConfiguration = void 0;
4
+ const network_controller_1 = require("@metamask/network-controller");
4
5
  const createMockNetworkConfiguration = (override) => {
5
6
  return {
6
7
  chainId: '0x1337',
7
- blockExplorerUrls: [],
8
+ blockExplorerUrls: ['https://etherscan.io'],
8
9
  defaultRpcEndpointIndex: 0,
9
10
  name: 'Mock Network',
10
11
  nativeCurrency: 'MOCK TOKEN',
@@ -22,4 +23,21 @@ const createMockRemoteNetworkConfiguration = (override) => {
22
23
  };
23
24
  };
24
25
  exports.createMockRemoteNetworkConfiguration = createMockRemoteNetworkConfiguration;
26
+ const createMockCustomRpcEndpoint = (override) => {
27
+ return {
28
+ type: network_controller_1.RpcEndpointType.Custom,
29
+ networkClientId: '1111-1111-1111',
30
+ url: `https://FAKE_RPC/`,
31
+ ...override,
32
+ };
33
+ };
34
+ exports.createMockCustomRpcEndpoint = createMockCustomRpcEndpoint;
35
+ const createMockInfuraRpcEndpoint = () => {
36
+ return {
37
+ type: network_controller_1.RpcEndpointType.Infura,
38
+ networkClientId: 'mainnet',
39
+ url: `https://mainnet.infura.io/v3/{infuraProjectId}`,
40
+ };
41
+ };
42
+ exports.createMockInfuraRpcEndpoint = createMockInfuraRpcEndpoint;
25
43
  //# sourceMappingURL=mockNetwork.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"mockNetwork.cjs","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":";;;AAKO,MAAM,8BAA8B,GAAG,CAC5C,QAAwC,EAClB,EAAE;IACxB,OAAO;QACL,OAAO,EAAE,QAAQ;QACjB,iBAAiB,EAAE,EAAE;QACrB,uBAAuB,EAAE,CAAC;QAC1B,IAAI,EAAE,cAAc;QACpB,cAAc,EAAE,YAAY;QAC5B,YAAY,EAAE,EAAE;QAChB,4BAA4B,EAAE,CAAC;QAC/B,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AAbW,QAAA,8BAA8B,kCAazC;AAEK,MAAM,oCAAoC,GAAG,CAClD,QAA8C,EAClB,EAAE;IAC9B,OAAO;QACL,CAAC,EAAE,GAAG;QACN,GAAG,IAAA,sCAA8B,GAAE;QACnC,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,oCAAoC,wCAQ/C","sourcesContent":["import type {\n NetworkConfiguration,\n RemoteNetworkConfiguration,\n} from '../types';\n\nexport const createMockNetworkConfiguration = (\n override?: Partial<NetworkConfiguration>,\n): NetworkConfiguration => {\n return {\n chainId: '0x1337',\n blockExplorerUrls: [],\n defaultRpcEndpointIndex: 0,\n name: 'Mock Network',\n nativeCurrency: 'MOCK TOKEN',\n rpcEndpoints: [],\n defaultBlockExplorerUrlIndex: 0,\n ...override,\n };\n};\n\nexport const createMockRemoteNetworkConfiguration = (\n override?: Partial<RemoteNetworkConfiguration>,\n): RemoteNetworkConfiguration => {\n return {\n v: '1',\n ...createMockNetworkConfiguration(),\n ...override,\n };\n};\n"]}
1
+ {"version":3,"file":"mockNetwork.cjs","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":";;;AACA,qEAA+D;AAMxD,MAAM,8BAA8B,GAAG,CAC5C,QAAwC,EAClB,EAAE;IACxB,OAAO;QACL,OAAO,EAAE,QAAQ;QACjB,iBAAiB,EAAE,CAAC,sBAAsB,CAAC;QAC3C,uBAAuB,EAAE,CAAC;QAC1B,IAAI,EAAE,cAAc;QACpB,cAAc,EAAE,YAAY;QAC5B,YAAY,EAAE,EAAE;QAChB,4BAA4B,EAAE,CAAC;QAC/B,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AAbW,QAAA,8BAA8B,kCAazC;AAEK,MAAM,oCAAoC,GAAG,CAClD,QAA8C,EAClB,EAAE;IAC9B,OAAO;QACL,CAAC,EAAE,GAAG;QACN,GAAG,IAAA,sCAA8B,GAAE;QACnC,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,oCAAoC,wCAQ/C;AAEK,MAAM,2BAA2B,GAAG,CACzC,QAAyE,EAC5D,EAAE;IACf,OAAO;QACL,IAAI,EAAE,oCAAe,CAAC,MAAM;QAC5B,eAAe,EAAE,gBAAgB;QACjC,GAAG,EAAE,mBAAmB;QACxB,GAAG,QAAQ;KACG,CAAC;AACnB,CAAC,CAAC;AATW,QAAA,2BAA2B,+BAStC;AAEK,MAAM,2BAA2B,GAAG,GAAgB,EAAE;IAC3D,OAAO;QACL,IAAI,EAAE,oCAAe,CAAC,MAAM;QAC5B,eAAe,EAAE,SAAS;QAC1B,GAAG,EAAE,gDAAgD;KACtD,CAAC;AACJ,CAAC,CAAC;AANW,QAAA,2BAA2B,+BAMtC","sourcesContent":["import type { NetworkConfiguration } from '@metamask/network-controller';\nimport { RpcEndpointType } from '@metamask/network-controller';\n\nimport type { RemoteNetworkConfiguration } from '../types';\n\nexport type RPCEndpoint = NetworkConfiguration['rpcEndpoints'][number];\n\nexport const createMockNetworkConfiguration = (\n override?: Partial<NetworkConfiguration>,\n): NetworkConfiguration => {\n return {\n chainId: '0x1337',\n blockExplorerUrls: ['https://etherscan.io'],\n defaultRpcEndpointIndex: 0,\n name: 'Mock Network',\n nativeCurrency: 'MOCK TOKEN',\n rpcEndpoints: [],\n defaultBlockExplorerUrlIndex: 0,\n ...override,\n };\n};\n\nexport const createMockRemoteNetworkConfiguration = (\n override?: Partial<RemoteNetworkConfiguration>,\n): RemoteNetworkConfiguration => {\n return {\n v: '1',\n ...createMockNetworkConfiguration(),\n ...override,\n };\n};\n\nexport const createMockCustomRpcEndpoint = (\n override: Partial<Extract<RPCEndpoint, { type: RpcEndpointType.Custom }>>,\n): RPCEndpoint => {\n return {\n type: RpcEndpointType.Custom,\n networkClientId: '1111-1111-1111',\n url: `https://FAKE_RPC/`,\n ...override,\n } as RPCEndpoint;\n};\n\nexport const createMockInfuraRpcEndpoint = (): RPCEndpoint => {\n return {\n type: RpcEndpointType.Infura,\n networkClientId: 'mainnet',\n url: `https://mainnet.infura.io/v3/{infuraProjectId}`,\n };\n};\n"]}
@@ -1,4 +1,11 @@
1
- import type { NetworkConfiguration, RemoteNetworkConfiguration } from "../types.cjs";
1
+ import type { NetworkConfiguration } from "@metamask/network-controller";
2
+ import { RpcEndpointType } from "@metamask/network-controller";
3
+ import type { RemoteNetworkConfiguration } from "../types.cjs";
4
+ export type RPCEndpoint = NetworkConfiguration['rpcEndpoints'][number];
2
5
  export declare const createMockNetworkConfiguration: (override?: Partial<NetworkConfiguration>) => NetworkConfiguration;
3
6
  export declare const createMockRemoteNetworkConfiguration: (override?: Partial<RemoteNetworkConfiguration>) => RemoteNetworkConfiguration;
7
+ export declare const createMockCustomRpcEndpoint: (override: Partial<Extract<RPCEndpoint, {
8
+ type: RpcEndpointType.Custom;
9
+ }>>) => RPCEndpoint;
10
+ export declare const createMockInfuraRpcEndpoint: () => RPCEndpoint;
4
11
  //# sourceMappingURL=mockNetwork.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mockNetwork.d.cts","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,0BAA0B,EAC3B,qBAAiB;AAElB,eAAO,MAAM,8BAA8B,cAC9B,QAAQ,oBAAoB,CAAC,KACvC,oBAWF,CAAC;AAEF,eAAO,MAAM,oCAAoC,cACpC,QAAQ,0BAA0B,CAAC,KAC7C,0BAMF,CAAC"}
1
+ {"version":3,"file":"mockNetwork.d.cts","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qCAAqC;AACzE,OAAO,EAAE,eAAe,EAAE,qCAAqC;AAE/D,OAAO,KAAK,EAAE,0BAA0B,EAAE,qBAAiB;AAE3D,MAAM,MAAM,WAAW,GAAG,oBAAoB,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAEvE,eAAO,MAAM,8BAA8B,cAC9B,QAAQ,oBAAoB,CAAC,KACvC,oBAWF,CAAC;AAEF,eAAO,MAAM,oCAAoC,cACpC,QAAQ,0BAA0B,CAAC,KAC7C,0BAMF,CAAC;AAEF,eAAO,MAAM,2BAA2B,aAC5B,QAAQ,QAAQ,WAAW,EAAE;IAAE,IAAI,EAAE,gBAAgB,MAAM,CAAA;CAAE,CAAC,CAAC,KACxE,WAOF,CAAC;AAEF,eAAO,MAAM,2BAA2B,QAAO,WAM9C,CAAC"}
@@ -1,4 +1,11 @@
1
- import type { NetworkConfiguration, RemoteNetworkConfiguration } from "../types.mjs";
1
+ import type { NetworkConfiguration } from "@metamask/network-controller";
2
+ import { RpcEndpointType } from "@metamask/network-controller";
3
+ import type { RemoteNetworkConfiguration } from "../types.mjs";
4
+ export type RPCEndpoint = NetworkConfiguration['rpcEndpoints'][number];
2
5
  export declare const createMockNetworkConfiguration: (override?: Partial<NetworkConfiguration>) => NetworkConfiguration;
3
6
  export declare const createMockRemoteNetworkConfiguration: (override?: Partial<RemoteNetworkConfiguration>) => RemoteNetworkConfiguration;
7
+ export declare const createMockCustomRpcEndpoint: (override: Partial<Extract<RPCEndpoint, {
8
+ type: RpcEndpointType.Custom;
9
+ }>>) => RPCEndpoint;
10
+ export declare const createMockInfuraRpcEndpoint: () => RPCEndpoint;
4
11
  //# sourceMappingURL=mockNetwork.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mockNetwork.d.mts","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,0BAA0B,EAC3B,qBAAiB;AAElB,eAAO,MAAM,8BAA8B,cAC9B,QAAQ,oBAAoB,CAAC,KACvC,oBAWF,CAAC;AAEF,eAAO,MAAM,oCAAoC,cACpC,QAAQ,0BAA0B,CAAC,KAC7C,0BAMF,CAAC"}
1
+ {"version":3,"file":"mockNetwork.d.mts","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qCAAqC;AACzE,OAAO,EAAE,eAAe,EAAE,qCAAqC;AAE/D,OAAO,KAAK,EAAE,0BAA0B,EAAE,qBAAiB;AAE3D,MAAM,MAAM,WAAW,GAAG,oBAAoB,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAEvE,eAAO,MAAM,8BAA8B,cAC9B,QAAQ,oBAAoB,CAAC,KACvC,oBAWF,CAAC;AAEF,eAAO,MAAM,oCAAoC,cACpC,QAAQ,0BAA0B,CAAC,KAC7C,0BAMF,CAAC;AAEF,eAAO,MAAM,2BAA2B,aAC5B,QAAQ,QAAQ,WAAW,EAAE;IAAE,IAAI,EAAE,gBAAgB,MAAM,CAAA;CAAE,CAAC,CAAC,KACxE,WAOF,CAAC;AAEF,eAAO,MAAM,2BAA2B,QAAO,WAM9C,CAAC"}
@@ -1,7 +1,8 @@
1
+ import { RpcEndpointType } from "@metamask/network-controller";
1
2
  export const createMockNetworkConfiguration = (override) => {
2
3
  return {
3
4
  chainId: '0x1337',
4
- blockExplorerUrls: [],
5
+ blockExplorerUrls: ['https://etherscan.io'],
5
6
  defaultRpcEndpointIndex: 0,
6
7
  name: 'Mock Network',
7
8
  nativeCurrency: 'MOCK TOKEN',
@@ -17,4 +18,19 @@ export const createMockRemoteNetworkConfiguration = (override) => {
17
18
  ...override,
18
19
  };
19
20
  };
21
+ export const createMockCustomRpcEndpoint = (override) => {
22
+ return {
23
+ type: RpcEndpointType.Custom,
24
+ networkClientId: '1111-1111-1111',
25
+ url: `https://FAKE_RPC/`,
26
+ ...override,
27
+ };
28
+ };
29
+ export const createMockInfuraRpcEndpoint = () => {
30
+ return {
31
+ type: RpcEndpointType.Infura,
32
+ networkClientId: 'mainnet',
33
+ url: `https://mainnet.infura.io/v3/{infuraProjectId}`,
34
+ };
35
+ };
20
36
  //# sourceMappingURL=mockNetwork.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"mockNetwork.mjs","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAC5C,QAAwC,EAClB,EAAE;IACxB,OAAO;QACL,OAAO,EAAE,QAAQ;QACjB,iBAAiB,EAAE,EAAE;QACrB,uBAAuB,EAAE,CAAC;QAC1B,IAAI,EAAE,cAAc;QACpB,cAAc,EAAE,YAAY;QAC5B,YAAY,EAAE,EAAE;QAChB,4BAA4B,EAAE,CAAC;QAC/B,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oCAAoC,GAAG,CAClD,QAA8C,EAClB,EAAE;IAC9B,OAAO;QACL,CAAC,EAAE,GAAG;QACN,GAAG,8BAA8B,EAAE;QACnC,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type {\n NetworkConfiguration,\n RemoteNetworkConfiguration,\n} from '../types';\n\nexport const createMockNetworkConfiguration = (\n override?: Partial<NetworkConfiguration>,\n): NetworkConfiguration => {\n return {\n chainId: '0x1337',\n blockExplorerUrls: [],\n defaultRpcEndpointIndex: 0,\n name: 'Mock Network',\n nativeCurrency: 'MOCK TOKEN',\n rpcEndpoints: [],\n defaultBlockExplorerUrlIndex: 0,\n ...override,\n };\n};\n\nexport const createMockRemoteNetworkConfiguration = (\n override?: Partial<RemoteNetworkConfiguration>,\n): RemoteNetworkConfiguration => {\n return {\n v: '1',\n ...createMockNetworkConfiguration(),\n ...override,\n };\n};\n"]}
1
+ {"version":3,"file":"mockNetwork.mjs","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/network-syncing/__fixtures__/mockNetwork.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,qCAAqC;AAM/D,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAC5C,QAAwC,EAClB,EAAE;IACxB,OAAO;QACL,OAAO,EAAE,QAAQ;QACjB,iBAAiB,EAAE,CAAC,sBAAsB,CAAC;QAC3C,uBAAuB,EAAE,CAAC;QAC1B,IAAI,EAAE,cAAc;QACpB,cAAc,EAAE,YAAY;QAC5B,YAAY,EAAE,EAAE;QAChB,4BAA4B,EAAE,CAAC;QAC/B,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oCAAoC,GAAG,CAClD,QAA8C,EAClB,EAAE;IAC9B,OAAO;QACL,CAAC,EAAE,GAAG;QACN,GAAG,8BAA8B,EAAE;QACnC,GAAG,QAAQ;KACZ,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,2BAA2B,GAAG,CACzC,QAAyE,EAC5D,EAAE;IACf,OAAO;QACL,IAAI,EAAE,eAAe,CAAC,MAAM;QAC5B,eAAe,EAAE,gBAAgB;QACjC,GAAG,EAAE,mBAAmB;QACxB,GAAG,QAAQ;KACG,CAAC;AACnB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,2BAA2B,GAAG,GAAgB,EAAE;IAC3D,OAAO;QACL,IAAI,EAAE,eAAe,CAAC,MAAM;QAC5B,eAAe,EAAE,SAAS;QAC1B,GAAG,EAAE,gDAAgD;KACtD,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { NetworkConfiguration } from '@metamask/network-controller';\nimport { RpcEndpointType } from '@metamask/network-controller';\n\nimport type { RemoteNetworkConfiguration } from '../types';\n\nexport type RPCEndpoint = NetworkConfiguration['rpcEndpoints'][number];\n\nexport const createMockNetworkConfiguration = (\n override?: Partial<NetworkConfiguration>,\n): NetworkConfiguration => {\n return {\n chainId: '0x1337',\n blockExplorerUrls: ['https://etherscan.io'],\n defaultRpcEndpointIndex: 0,\n name: 'Mock Network',\n nativeCurrency: 'MOCK TOKEN',\n rpcEndpoints: [],\n defaultBlockExplorerUrlIndex: 0,\n ...override,\n };\n};\n\nexport const createMockRemoteNetworkConfiguration = (\n override?: Partial<RemoteNetworkConfiguration>,\n): RemoteNetworkConfiguration => {\n return {\n v: '1',\n ...createMockNetworkConfiguration(),\n ...override,\n };\n};\n\nexport const createMockCustomRpcEndpoint = (\n override: Partial<Extract<RPCEndpoint, { type: RpcEndpointType.Custom }>>,\n): RPCEndpoint => {\n return {\n type: RpcEndpointType.Custom,\n networkClientId: '1111-1111-1111',\n url: `https://FAKE_RPC/`,\n ...override,\n } as RPCEndpoint;\n};\n\nexport const createMockInfuraRpcEndpoint = (): RPCEndpoint => {\n return {\n type: RpcEndpointType.Infura,\n networkClientId: 'mainnet',\n url: `https://mainnet.infura.io/v3/{infuraProjectId}`,\n };\n};\n"]}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBoundedNetworksToAdd = exports.calculateAvailableSpaceToAdd = exports.MAX_NETWORKS_SIZE = void 0;
4
+ exports.MAX_NETWORKS_SIZE = 50;
5
+ /**
6
+ * Calculates the available space to add new networks
7
+ * exported for testability.
8
+ * @param originalListSize - size of original list
9
+ * @param maxSize - max size
10
+ * @returns a positive number on the available space
11
+ */
12
+ const calculateAvailableSpaceToAdd = (originalListSize, maxSize) => {
13
+ return Math.max(0, maxSize - originalListSize);
14
+ };
15
+ exports.calculateAvailableSpaceToAdd = calculateAvailableSpaceToAdd;
16
+ /**
17
+ * Returns a bounded number of networks to add (set by a max bound)
18
+ * The items will be ordered to give determinism on items to append (not random)
19
+ *
20
+ * @param originalNetworks - The original list of network configurations.
21
+ * @param networksToAdd - The list of network configurations to add.
22
+ * @param maxSize - The maximum allowed size of the list. Defaults to MAX_NETWORKS_SIZE.
23
+ * @returns The networks to add, sorted by chainId.
24
+ */
25
+ const getBoundedNetworksToAdd = (originalNetworks, networksToAdd, maxSize = exports.MAX_NETWORKS_SIZE) => {
26
+ const availableSpace = (0, exports.calculateAvailableSpaceToAdd)(originalNetworks.length, maxSize);
27
+ const numberOfNetworksToAppend = Math.min(availableSpace, networksToAdd.length);
28
+ // Order and slice the networks to append
29
+ // Ordering so we have some determinism on the order of items
30
+ return networksToAdd
31
+ .sort((a, b) => a.chainId.localeCompare(b.chainId))
32
+ .slice(0, numberOfNetworksToAppend);
33
+ };
34
+ exports.getBoundedNetworksToAdd = getBoundedNetworksToAdd;
35
+ //# sourceMappingURL=add-network-utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-network-utils.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/add-network-utils.ts"],"names":[],"mappings":";;;AAEa,QAAA,iBAAiB,GAAG,EAAE,CAAC;AAEpC;;;;;;GAMG;AACI,MAAM,4BAA4B,GAAG,CAC1C,gBAAwB,EACxB,OAAe,EACf,EAAE;IACF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,gBAAgB,CAAC,CAAC;AACjD,CAAC,CAAC;AALW,QAAA,4BAA4B,gCAKvC;AAEF;;;;;;;;GAQG;AACI,MAAM,uBAAuB,GAAG,CACrC,gBAAwC,EACxC,aAAqC,EACrC,OAAO,GAAG,yBAAiB,EAC3B,EAAE;IACF,MAAM,cAAc,GAAG,IAAA,oCAA4B,EACjD,gBAAgB,CAAC,MAAM,EACvB,OAAO,CACR,CAAC;IACF,MAAM,wBAAwB,GAAG,IAAI,CAAC,GAAG,CACvC,cAAc,EACd,aAAa,CAAC,MAAM,CACrB,CAAC;IAEF,yCAAyC;IACzC,6DAA6D;IAC7D,OAAO,aAAa;SACjB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;SAClD,KAAK,CAAC,CAAC,EAAE,wBAAwB,CAAC,CAAC;AACxC,CAAC,CAAC;AAnBW,QAAA,uBAAuB,2BAmBlC","sourcesContent":["import type { NetworkConfiguration } from '@metamask/network-controller';\n\nexport const MAX_NETWORKS_SIZE = 50;\n\n/**\n * Calculates the available space to add new networks\n * exported for testability.\n * @param originalListSize - size of original list\n * @param maxSize - max size\n * @returns a positive number on the available space\n */\nexport const calculateAvailableSpaceToAdd = (\n originalListSize: number,\n maxSize: number,\n) => {\n return Math.max(0, maxSize - originalListSize);\n};\n\n/**\n * Returns a bounded number of networks to add (set by a max bound)\n * The items will be ordered to give determinism on items to append (not random)\n *\n * @param originalNetworks - The original list of network configurations.\n * @param networksToAdd - The list of network configurations to add.\n * @param maxSize - The maximum allowed size of the list. Defaults to MAX_NETWORKS_SIZE.\n * @returns The networks to add, sorted by chainId.\n */\nexport const getBoundedNetworksToAdd = (\n originalNetworks: NetworkConfiguration[],\n networksToAdd: NetworkConfiguration[],\n maxSize = MAX_NETWORKS_SIZE,\n) => {\n const availableSpace = calculateAvailableSpaceToAdd(\n originalNetworks.length,\n maxSize,\n );\n const numberOfNetworksToAppend = Math.min(\n availableSpace,\n networksToAdd.length,\n );\n\n // Order and slice the networks to append\n // Ordering so we have some determinism on the order of items\n return networksToAdd\n .sort((a, b) => a.chainId.localeCompare(b.chainId))\n .slice(0, numberOfNetworksToAppend);\n};\n"]}
@@ -0,0 +1,21 @@
1
+ import type { NetworkConfiguration } from "@metamask/network-controller";
2
+ export declare const MAX_NETWORKS_SIZE = 50;
3
+ /**
4
+ * Calculates the available space to add new networks
5
+ * exported for testability.
6
+ * @param originalListSize - size of original list
7
+ * @param maxSize - max size
8
+ * @returns a positive number on the available space
9
+ */
10
+ export declare const calculateAvailableSpaceToAdd: (originalListSize: number, maxSize: number) => number;
11
+ /**
12
+ * Returns a bounded number of networks to add (set by a max bound)
13
+ * The items will be ordered to give determinism on items to append (not random)
14
+ *
15
+ * @param originalNetworks - The original list of network configurations.
16
+ * @param networksToAdd - The list of network configurations to add.
17
+ * @param maxSize - The maximum allowed size of the list. Defaults to MAX_NETWORKS_SIZE.
18
+ * @returns The networks to add, sorted by chainId.
19
+ */
20
+ export declare const getBoundedNetworksToAdd: (originalNetworks: NetworkConfiguration[], networksToAdd: NetworkConfiguration[], maxSize?: number) => NetworkConfiguration[];
21
+ //# sourceMappingURL=add-network-utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-network-utils.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/add-network-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qCAAqC;AAEzE,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,qBACrB,MAAM,WACf,MAAM,WAGhB,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB,qBAChB,oBAAoB,EAAE,iBACzB,oBAAoB,EAAE,6CAiBtC,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { NetworkConfiguration } from "@metamask/network-controller";
2
+ export declare const MAX_NETWORKS_SIZE = 50;
3
+ /**
4
+ * Calculates the available space to add new networks
5
+ * exported for testability.
6
+ * @param originalListSize - size of original list
7
+ * @param maxSize - max size
8
+ * @returns a positive number on the available space
9
+ */
10
+ export declare const calculateAvailableSpaceToAdd: (originalListSize: number, maxSize: number) => number;
11
+ /**
12
+ * Returns a bounded number of networks to add (set by a max bound)
13
+ * The items will be ordered to give determinism on items to append (not random)
14
+ *
15
+ * @param originalNetworks - The original list of network configurations.
16
+ * @param networksToAdd - The list of network configurations to add.
17
+ * @param maxSize - The maximum allowed size of the list. Defaults to MAX_NETWORKS_SIZE.
18
+ * @returns The networks to add, sorted by chainId.
19
+ */
20
+ export declare const getBoundedNetworksToAdd: (originalNetworks: NetworkConfiguration[], networksToAdd: NetworkConfiguration[], maxSize?: number) => NetworkConfiguration[];
21
+ //# sourceMappingURL=add-network-utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-network-utils.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/add-network-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qCAAqC;AAEzE,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,qBACrB,MAAM,WACf,MAAM,WAGhB,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB,qBAChB,oBAAoB,EAAE,iBACzB,oBAAoB,EAAE,6CAiBtC,CAAC"}
@@ -0,0 +1,30 @@
1
+ export const MAX_NETWORKS_SIZE = 50;
2
+ /**
3
+ * Calculates the available space to add new networks
4
+ * exported for testability.
5
+ * @param originalListSize - size of original list
6
+ * @param maxSize - max size
7
+ * @returns a positive number on the available space
8
+ */
9
+ export const calculateAvailableSpaceToAdd = (originalListSize, maxSize) => {
10
+ return Math.max(0, maxSize - originalListSize);
11
+ };
12
+ /**
13
+ * Returns a bounded number of networks to add (set by a max bound)
14
+ * The items will be ordered to give determinism on items to append (not random)
15
+ *
16
+ * @param originalNetworks - The original list of network configurations.
17
+ * @param networksToAdd - The list of network configurations to add.
18
+ * @param maxSize - The maximum allowed size of the list. Defaults to MAX_NETWORKS_SIZE.
19
+ * @returns The networks to add, sorted by chainId.
20
+ */
21
+ export const getBoundedNetworksToAdd = (originalNetworks, networksToAdd, maxSize = MAX_NETWORKS_SIZE) => {
22
+ const availableSpace = calculateAvailableSpaceToAdd(originalNetworks.length, maxSize);
23
+ const numberOfNetworksToAppend = Math.min(availableSpace, networksToAdd.length);
24
+ // Order and slice the networks to append
25
+ // Ordering so we have some determinism on the order of items
26
+ return networksToAdd
27
+ .sort((a, b) => a.chainId.localeCompare(b.chainId))
28
+ .slice(0, numberOfNetworksToAppend);
29
+ };
30
+ //# sourceMappingURL=add-network-utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-network-utils.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/add-network-utils.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAEpC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,gBAAwB,EACxB,OAAe,EACf,EAAE;IACF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,gBAAgB,CAAC,CAAC;AACjD,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,gBAAwC,EACxC,aAAqC,EACrC,OAAO,GAAG,iBAAiB,EAC3B,EAAE;IACF,MAAM,cAAc,GAAG,4BAA4B,CACjD,gBAAgB,CAAC,MAAM,EACvB,OAAO,CACR,CAAC;IACF,MAAM,wBAAwB,GAAG,IAAI,CAAC,GAAG,CACvC,cAAc,EACd,aAAa,CAAC,MAAM,CACrB,CAAC;IAEF,yCAAyC;IACzC,6DAA6D;IAC7D,OAAO,aAAa;SACjB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;SAClD,KAAK,CAAC,CAAC,EAAE,wBAAwB,CAAC,CAAC;AACxC,CAAC,CAAC","sourcesContent":["import type { NetworkConfiguration } from '@metamask/network-controller';\n\nexport const MAX_NETWORKS_SIZE = 50;\n\n/**\n * Calculates the available space to add new networks\n * exported for testability.\n * @param originalListSize - size of original list\n * @param maxSize - max size\n * @returns a positive number on the available space\n */\nexport const calculateAvailableSpaceToAdd = (\n originalListSize: number,\n maxSize: number,\n) => {\n return Math.max(0, maxSize - originalListSize);\n};\n\n/**\n * Returns a bounded number of networks to add (set by a max bound)\n * The items will be ordered to give determinism on items to append (not random)\n *\n * @param originalNetworks - The original list of network configurations.\n * @param networksToAdd - The list of network configurations to add.\n * @param maxSize - The maximum allowed size of the list. Defaults to MAX_NETWORKS_SIZE.\n * @returns The networks to add, sorted by chainId.\n */\nexport const getBoundedNetworksToAdd = (\n originalNetworks: NetworkConfiguration[],\n networksToAdd: NetworkConfiguration[],\n maxSize = MAX_NETWORKS_SIZE,\n) => {\n const availableSpace = calculateAvailableSpaceToAdd(\n originalNetworks.length,\n maxSize,\n );\n const numberOfNetworksToAppend = Math.min(\n availableSpace,\n networksToAdd.length,\n );\n\n // Order and slice the networks to append\n // Ordering so we have some determinism on the order of items\n return networksToAdd\n .sort((a, b) => a.chainId.localeCompare(b.chainId))\n .slice(0, numberOfNetworksToAppend);\n};\n"]}
@@ -3,22 +3,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.startNetworkSyncing = void 0;
6
+ exports.performMainNetworkSync = exports.dispatchUpdateNetwork = exports.startNetworkSyncing = exports.isMainNetworkSyncInProgress = void 0;
7
7
  const loglevel_1 = __importDefault(require("loglevel"));
8
+ const add_network_utils_1 = require("./add-network-utils.cjs");
9
+ const services_1 = require("./services.cjs");
10
+ const sync_all_1 = require("./sync-all.cjs");
8
11
  const sync_mutations_1 = require("./sync-mutations.cjs");
12
+ const update_network_utils_1 = require("./update-network-utils.cjs");
13
+ /**
14
+ * Global in-mem cache to signify that the network syncing is in progress
15
+ * Ensures that listeners do not fire during main sync (prevent double requests)
16
+ */
17
+ // Exported to help testing
18
+ // eslint-disable-next-line import/no-mutable-exports
19
+ exports.isMainNetworkSyncInProgress = false;
9
20
  /**
10
21
  * Initialize and setup events to listen to for network syncing
22
+ * We will be listening to:
23
+ * - Remove Event, to indicate that we need to remote network from remote
24
+ *
25
+ * We will not be listening to:
26
+ * - Add/Update events are not required, as we can sync these during the main sync
27
+ *
11
28
  * @param props - parameters used for initializing and enabling network syncing
12
29
  */
13
30
  function startNetworkSyncing(props) {
14
- const { messenger, getStorageConfig } = props;
31
+ const { messenger, getStorageConfig, isMutationSyncBlocked } = props;
15
32
  try {
16
- messenger.subscribe('NetworkController:networkAdded',
33
+ messenger.subscribe('NetworkController:networkRemoved',
17
34
  // eslint-disable-next-line @typescript-eslint/no-misused-promises
18
35
  async (networkConfiguration) => {
19
36
  try {
37
+ // If blocked (e.g. we have not yet performed a main-sync), then we should not perform any mutations
38
+ if (isMutationSyncBlocked()) {
39
+ return;
40
+ }
41
+ // As main sync is in progress, it will already local and remote networks
42
+ // So no need to re-process again.
43
+ if (exports.isMainNetworkSyncInProgress) {
44
+ return;
45
+ }
20
46
  const opts = await getStorageConfig();
21
- await (0, sync_mutations_1.addNetwork)(networkConfiguration, opts);
47
+ if (!opts) {
48
+ return;
49
+ }
50
+ await (0, sync_mutations_1.deleteNetwork)(networkConfiguration, opts);
22
51
  }
23
52
  catch {
24
53
  // Silently fail sync
@@ -28,38 +57,137 @@ function startNetworkSyncing(props) {
28
57
  catch (e) {
29
58
  loglevel_1.default.warn('NetworkSyncing, event subscription failed', e);
30
59
  }
60
+ }
61
+ exports.startNetworkSyncing = startNetworkSyncing;
62
+ /**
63
+ * method that will dispatch the `NetworkController:updateNetwork` action.
64
+ * transforms and corrects the network configuration (and RPCs) we pass through.
65
+ * @param props - properties
66
+ * @param props.messenger - messenger to call the action
67
+ * @param props.originalNetworkConfiguration - original network config (from network controller state)
68
+ * @param props.newNetworkConfiguration - new network config (from remote)
69
+ * @param props.selectedNetworkClientId - currently selected network client id
70
+ */
71
+ const dispatchUpdateNetwork = async (props) => {
72
+ const { messenger, originalNetworkConfiguration, newNetworkConfiguration, selectedNetworkClientId, } = props;
73
+ const { updateNetworkFields, newSelectedRpcEndpointIndex } = (0, update_network_utils_1.createUpdateNetworkProps)({
74
+ originalNetworkConfiguration,
75
+ newNetworkConfiguration,
76
+ selectedNetworkClientId,
77
+ });
78
+ await messenger.call('NetworkController:updateNetwork', updateNetworkFields.chainId, updateNetworkFields, { replacementSelectedRpcEndpointIndex: newSelectedRpcEndpointIndex });
79
+ };
80
+ exports.dispatchUpdateNetwork = dispatchUpdateNetwork;
81
+ /**
82
+ * Action to perform the main network sync.
83
+ * It will fetch local networks and remote networks, then determines which networks (local and remote) to add/update
84
+ * @param props - parameters used for this main sync
85
+ */
86
+ async function performMainNetworkSync(props) {
87
+ const { messenger, getStorageConfig, maxNetworksToAdd, onNetworkAdded, onNetworkRemoved, onNetworkUpdated, } = props;
88
+ // Edge-Case, we do not want to re-run the main-sync if it already is in progress
89
+ /* istanbul ignore if - this is not testable */
90
+ if (exports.isMainNetworkSyncInProgress) {
91
+ return;
92
+ }
93
+ exports.isMainNetworkSyncInProgress = true;
31
94
  try {
32
- messenger.subscribe('NetworkController:networkDeleted',
33
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
34
- async (networkConfiguration) => {
35
- try {
36
- const opts = await getStorageConfig();
37
- await (0, sync_mutations_1.deleteNetwork)(networkConfiguration, opts);
95
+ const opts = await getStorageConfig();
96
+ if (!opts) {
97
+ return;
98
+ }
99
+ const networkControllerState = messenger.call('NetworkController:getState');
100
+ const localNetworks = Object.values(networkControllerState.networkConfigurationsByChainId ?? {});
101
+ const remoteNetworks = await (0, services_1.getAllRemoteNetworks)(opts);
102
+ const networkChanges = (0, sync_all_1.findNetworksToUpdate)({
103
+ localNetworks,
104
+ remoteNetworks,
105
+ });
106
+ loglevel_1.default.debug('performMainNetworkSync() - Network Syncing Started', {
107
+ localNetworks,
108
+ remoteNetworks,
109
+ networkChanges,
110
+ });
111
+ // Update Remote
112
+ if (networkChanges?.remoteNetworksToUpdate &&
113
+ networkChanges.remoteNetworksToUpdate.length > 0) {
114
+ await (0, sync_mutations_1.batchUpdateNetworks)(networkChanges?.remoteNetworksToUpdate, opts);
115
+ }
116
+ // Add missing local networks
117
+ const boundedNetworkedToAdd = networkChanges?.missingLocalNetworks &&
118
+ (0, add_network_utils_1.getBoundedNetworksToAdd)(localNetworks, networkChanges.missingLocalNetworks, maxNetworksToAdd);
119
+ if (boundedNetworkedToAdd && boundedNetworkedToAdd.length > 0) {
120
+ const errors = [];
121
+ boundedNetworkedToAdd.forEach((n) => {
122
+ try {
123
+ messenger.call('NetworkController:addNetwork', n);
124
+ onNetworkAdded?.(n.chainId);
125
+ }
126
+ catch (e) {
127
+ /* istanbul ignore next - allocates logs, do not need to test */
128
+ errors.push(e);
129
+ // Silently fail, we can try this again on next main sync
130
+ }
131
+ });
132
+ /* istanbul ignore if - only logs errors, not useful to test */
133
+ if (errors.length > 0) {
134
+ loglevel_1.default.error('performMainNetworkSync() - NetworkController:addNetwork failures', errors);
38
135
  }
39
- catch {
40
- // Silently fail sync
136
+ }
137
+ // Update local networks
138
+ if (networkChanges?.localNetworksToUpdate &&
139
+ networkChanges.localNetworksToUpdate.length > 0) {
140
+ const errors = [];
141
+ for (const n of networkChanges.localNetworksToUpdate) {
142
+ try {
143
+ await (0, exports.dispatchUpdateNetwork)({
144
+ messenger,
145
+ originalNetworkConfiguration: networkControllerState.networkConfigurationsByChainId[n.chainId],
146
+ newNetworkConfiguration: n,
147
+ selectedNetworkClientId: networkControllerState.selectedNetworkClientId,
148
+ });
149
+ onNetworkUpdated?.(n.chainId);
150
+ }
151
+ catch (e) {
152
+ /* istanbul ignore next - allocates logs, do not need to test */
153
+ errors.push(e);
154
+ // Silently fail, we can try this again on next main sync
155
+ }
41
156
  }
42
- });
43
- }
44
- catch (e) {
45
- loglevel_1.default.warn('NetworkSyncing, event subscription failed', e);
46
- }
47
- try {
48
- messenger.subscribe('NetworkController:networkChanged',
49
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
50
- async (networkConfiguration) => {
51
- try {
52
- const opts = await getStorageConfig();
53
- await (0, sync_mutations_1.updateNetwork)(networkConfiguration, opts);
157
+ /* istanbul ignore if - only logs errors, not useful to test */
158
+ if (errors.length > 0) {
159
+ loglevel_1.default.error('performMainNetworkSync() - NetworkController:updateNetwork failed', errors);
54
160
  }
55
- catch {
56
- // Silently fail sync
161
+ }
162
+ // Remove local networks
163
+ if (networkChanges?.localNetworksToRemove &&
164
+ networkChanges.localNetworksToRemove.length > 0) {
165
+ const errors = [];
166
+ networkChanges.localNetworksToRemove.forEach((n) => {
167
+ try {
168
+ messenger.call('NetworkController:removeNetwork', n.chainId);
169
+ onNetworkRemoved?.(n.chainId);
170
+ }
171
+ catch (e) {
172
+ /* istanbul ignore next - allocates logs, do not need to test */
173
+ errors.push(e);
174
+ // Silently fail, we can try this again on next main sync
175
+ }
176
+ });
177
+ /* istanbul ignore if - only logs errors, not useful to test */
178
+ if (errors.length > 0) {
179
+ loglevel_1.default.error('performMainNetworkSync() - NetworkController:removeNetwork failed', errors);
57
180
  }
58
- });
181
+ }
59
182
  }
60
183
  catch (e) {
61
- loglevel_1.default.warn('NetworkSyncing, event subscription failed', e);
184
+ /* istanbul ignore next - only logs errors, not useful to test */
185
+ loglevel_1.default.error('performMainNetworkSync() failed', e);
186
+ // Silently fail sync
187
+ }
188
+ finally {
189
+ exports.isMainNetworkSyncInProgress = false;
62
190
  }
63
191
  }
64
- exports.startNetworkSyncing = startNetworkSyncing;
192
+ exports.performMainNetworkSync = performMainNetworkSync;
65
193
  //# sourceMappingURL=controller-integration.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"controller-integration.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/controller-integration.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA2B;AAI3B,yDAA4E;AAO5E;;;GAGG;AACH,SAAgB,mBAAmB,CAAC,KAA+B;IACjE,MAAM,EAAE,SAAS,EAAE,gBAAgB,EAAE,GAAG,KAAK,CAAC;IAE9C,IAAI;QACF,SAAS,CAAC,SAAS,CACjB,gCAAgC;QAChC,kEAAkE;QAClE,KAAK,EAAE,oBAAoB,EAAE,EAAE;YAC7B,IAAI;gBACF,MAAM,IAAI,GAAG,MAAM,gBAAgB,EAAE,CAAC;gBACtC,MAAM,IAAA,2BAAU,EAAC,oBAAoB,EAAE,IAAI,CAAC,CAAC;aAC9C;YAAC,MAAM;gBACN,qBAAqB;aACtB;QACH,CAAC,CACF,CAAC;KACH;IAAC,OAAO,CAAC,EAAE;QACV,kBAAG,CAAC,IAAI,CAAC,2CAA2C,EAAE,CAAC,CAAC,CAAC;KAC1D;IAED,IAAI;QACF,SAAS,CAAC,SAAS,CACjB,kCAAkC;QAClC,kEAAkE;QAClE,KAAK,EAAE,oBAAoB,EAAE,EAAE;YAC7B,IAAI;gBACF,MAAM,IAAI,GAAG,MAAM,gBAAgB,EAAE,CAAC;gBACtC,MAAM,IAAA,8BAAa,EAAC,oBAAoB,EAAE,IAAI,CAAC,CAAC;aACjD;YAAC,MAAM;gBACN,qBAAqB;aACtB;QACH,CAAC,CACF,CAAC;KACH;IAAC,OAAO,CAAC,EAAE;QACV,kBAAG,CAAC,IAAI,CAAC,2CAA2C,EAAE,CAAC,CAAC,CAAC;KAC1D;IAED,IAAI;QACF,SAAS,CAAC,SAAS,CACjB,kCAAkC;QAClC,kEAAkE;QAClE,KAAK,EAAE,oBAAoB,EAAE,EAAE;YAC7B,IAAI;gBACF,MAAM,IAAI,GAAG,MAAM,gBAAgB,EAAE,CAAC;gBACtC,MAAM,IAAA,8BAAa,EAAC,oBAAoB,EAAE,IAAI,CAAC,CAAC;aACjD;YAAC,MAAM;gBACN,qBAAqB;aACtB;QACH,CAAC,CACF,CAAC;KACH;IAAC,OAAO,CAAC,EAAE;QACV,kBAAG,CAAC,IAAI,CAAC,2CAA2C,EAAE,CAAC,CAAC,CAAC;KAC1D;AACH,CAAC;AArDD,kDAqDC","sourcesContent":["import log from 'loglevel';\n\nimport type { UserStorageBaseOptions } from '../services';\nimport type { UserStorageControllerMessenger } from '../UserStorageController';\nimport { addNetwork, deleteNetwork, updateNetwork } from './sync-mutations';\n\ntype SetupNetworkSyncingProps = {\n messenger: UserStorageControllerMessenger;\n getStorageConfig: () => Promise<UserStorageBaseOptions>;\n};\n\n/**\n * Initialize and setup events to listen to for network syncing\n * @param props - parameters used for initializing and enabling network syncing\n */\nexport function startNetworkSyncing(props: SetupNetworkSyncingProps) {\n const { messenger, getStorageConfig } = props;\n\n try {\n messenger.subscribe(\n 'NetworkController:networkAdded',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (networkConfiguration) => {\n try {\n const opts = await getStorageConfig();\n await addNetwork(networkConfiguration, opts);\n } catch {\n // Silently fail sync\n }\n },\n );\n } catch (e) {\n log.warn('NetworkSyncing, event subscription failed', e);\n }\n\n try {\n messenger.subscribe(\n 'NetworkController:networkDeleted',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (networkConfiguration) => {\n try {\n const opts = await getStorageConfig();\n await deleteNetwork(networkConfiguration, opts);\n } catch {\n // Silently fail sync\n }\n },\n );\n } catch (e) {\n log.warn('NetworkSyncing, event subscription failed', e);\n }\n\n try {\n messenger.subscribe(\n 'NetworkController:networkChanged',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (networkConfiguration) => {\n try {\n const opts = await getStorageConfig();\n await updateNetwork(networkConfiguration, opts);\n } catch {\n // Silently fail sync\n }\n },\n );\n } catch (e) {\n log.warn('NetworkSyncing, event subscription failed', e);\n }\n}\n"]}
1
+ {"version":3,"file":"controller-integration.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/controller-integration.ts"],"names":[],"mappings":"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","sourcesContent":["import type { NetworkConfiguration } from '@metamask/network-controller';\nimport log from 'loglevel';\n\nimport type { UserStorageBaseOptions } from '../services';\nimport type { UserStorageControllerMessenger } from '../UserStorageController';\nimport { getBoundedNetworksToAdd } from './add-network-utils';\nimport { getAllRemoteNetworks } from './services';\nimport { findNetworksToUpdate } from './sync-all';\nimport { batchUpdateNetworks, deleteNetwork } from './sync-mutations';\nimport { createUpdateNetworkProps } from './update-network-utils';\n\ntype StartNetworkSyncingProps = {\n messenger: UserStorageControllerMessenger;\n getStorageConfig: () => Promise<UserStorageBaseOptions | null>;\n isMutationSyncBlocked: () => boolean;\n};\n\ntype PerformMainNetworkSyncProps = {\n messenger: UserStorageControllerMessenger;\n getStorageConfig: () => Promise<UserStorageBaseOptions | null>;\n maxNetworksToAdd?: number;\n onNetworkAdded?: (chainId: string) => void;\n onNetworkUpdated?: (chainId: string) => void;\n onNetworkRemoved?: (chainId: string) => void;\n};\n\n/**\n * Global in-mem cache to signify that the network syncing is in progress\n * Ensures that listeners do not fire during main sync (prevent double requests)\n */\n// Exported to help testing\n// eslint-disable-next-line import/no-mutable-exports\nexport let isMainNetworkSyncInProgress = false;\n\n/**\n * Initialize and setup events to listen to for network syncing\n * We will be listening to:\n * - Remove Event, to indicate that we need to remote network from remote\n *\n * We will not be listening to:\n * - Add/Update events are not required, as we can sync these during the main sync\n *\n * @param props - parameters used for initializing and enabling network syncing\n */\nexport function startNetworkSyncing(props: StartNetworkSyncingProps) {\n const { messenger, getStorageConfig, isMutationSyncBlocked } = props;\n try {\n messenger.subscribe(\n 'NetworkController:networkRemoved',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (networkConfiguration) => {\n try {\n // If blocked (e.g. we have not yet performed a main-sync), then we should not perform any mutations\n if (isMutationSyncBlocked()) {\n return;\n }\n\n // As main sync is in progress, it will already local and remote networks\n // So no need to re-process again.\n if (isMainNetworkSyncInProgress) {\n return;\n }\n\n const opts = await getStorageConfig();\n if (!opts) {\n return;\n }\n await deleteNetwork(networkConfiguration, opts);\n } catch {\n // Silently fail sync\n }\n },\n );\n } catch (e) {\n log.warn('NetworkSyncing, event subscription failed', e);\n }\n}\n\n/**\n * method that will dispatch the `NetworkController:updateNetwork` action.\n * transforms and corrects the network configuration (and RPCs) we pass through.\n * @param props - properties\n * @param props.messenger - messenger to call the action\n * @param props.originalNetworkConfiguration - original network config (from network controller state)\n * @param props.newNetworkConfiguration - new network config (from remote)\n * @param props.selectedNetworkClientId - currently selected network client id\n */\nexport const dispatchUpdateNetwork = async (props: {\n messenger: UserStorageControllerMessenger;\n originalNetworkConfiguration: NetworkConfiguration;\n newNetworkConfiguration: NetworkConfiguration;\n selectedNetworkClientId: string;\n}) => {\n const {\n messenger,\n originalNetworkConfiguration,\n newNetworkConfiguration,\n selectedNetworkClientId,\n } = props;\n\n const { updateNetworkFields, newSelectedRpcEndpointIndex } =\n createUpdateNetworkProps({\n originalNetworkConfiguration,\n newNetworkConfiguration,\n selectedNetworkClientId,\n });\n\n await messenger.call(\n 'NetworkController:updateNetwork',\n updateNetworkFields.chainId,\n updateNetworkFields,\n { replacementSelectedRpcEndpointIndex: newSelectedRpcEndpointIndex },\n );\n};\n\n/**\n * Action to perform the main network sync.\n * It will fetch local networks and remote networks, then determines which networks (local and remote) to add/update\n * @param props - parameters used for this main sync\n */\nexport async function performMainNetworkSync(\n props: PerformMainNetworkSyncProps,\n) {\n const {\n messenger,\n getStorageConfig,\n maxNetworksToAdd,\n onNetworkAdded,\n onNetworkRemoved,\n onNetworkUpdated,\n } = props;\n\n // Edge-Case, we do not want to re-run the main-sync if it already is in progress\n /* istanbul ignore if - this is not testable */\n if (isMainNetworkSyncInProgress) {\n return;\n }\n\n isMainNetworkSyncInProgress = true;\n try {\n const opts = await getStorageConfig();\n if (!opts) {\n return;\n }\n\n const networkControllerState = messenger.call('NetworkController:getState');\n const localNetworks = Object.values(\n networkControllerState.networkConfigurationsByChainId ?? {},\n );\n\n const remoteNetworks = await getAllRemoteNetworks(opts);\n const networkChanges = findNetworksToUpdate({\n localNetworks,\n remoteNetworks,\n });\n\n log.debug('performMainNetworkSync() - Network Syncing Started', {\n localNetworks,\n remoteNetworks,\n networkChanges,\n });\n\n // Update Remote\n if (\n networkChanges?.remoteNetworksToUpdate &&\n networkChanges.remoteNetworksToUpdate.length > 0\n ) {\n await batchUpdateNetworks(networkChanges?.remoteNetworksToUpdate, opts);\n }\n\n // Add missing local networks\n const boundedNetworkedToAdd =\n networkChanges?.missingLocalNetworks &&\n getBoundedNetworksToAdd(\n localNetworks,\n networkChanges.missingLocalNetworks,\n maxNetworksToAdd,\n );\n if (boundedNetworkedToAdd && boundedNetworkedToAdd.length > 0) {\n const errors: unknown[] = [];\n boundedNetworkedToAdd.forEach((n) => {\n try {\n messenger.call('NetworkController:addNetwork', n);\n onNetworkAdded?.(n.chainId);\n } catch (e) {\n /* istanbul ignore next - allocates logs, do not need to test */\n errors.push(e);\n // Silently fail, we can try this again on next main sync\n }\n });\n\n /* istanbul ignore if - only logs errors, not useful to test */\n if (errors.length > 0) {\n log.error(\n 'performMainNetworkSync() - NetworkController:addNetwork failures',\n errors,\n );\n }\n }\n\n // Update local networks\n if (\n networkChanges?.localNetworksToUpdate &&\n networkChanges.localNetworksToUpdate.length > 0\n ) {\n const errors: unknown[] = [];\n for (const n of networkChanges.localNetworksToUpdate) {\n try {\n await dispatchUpdateNetwork({\n messenger,\n originalNetworkConfiguration:\n networkControllerState.networkConfigurationsByChainId[n.chainId],\n newNetworkConfiguration: n,\n selectedNetworkClientId:\n networkControllerState.selectedNetworkClientId,\n });\n onNetworkUpdated?.(n.chainId);\n } catch (e) {\n /* istanbul ignore next - allocates logs, do not need to test */\n errors.push(e);\n // Silently fail, we can try this again on next main sync\n }\n }\n\n /* istanbul ignore if - only logs errors, not useful to test */\n if (errors.length > 0) {\n log.error(\n 'performMainNetworkSync() - NetworkController:updateNetwork failed',\n errors,\n );\n }\n }\n\n // Remove local networks\n if (\n networkChanges?.localNetworksToRemove &&\n networkChanges.localNetworksToRemove.length > 0\n ) {\n const errors: unknown[] = [];\n networkChanges.localNetworksToRemove.forEach((n) => {\n try {\n messenger.call('NetworkController:removeNetwork', n.chainId);\n onNetworkRemoved?.(n.chainId);\n } catch (e) {\n /* istanbul ignore next - allocates logs, do not need to test */\n errors.push(e);\n // Silently fail, we can try this again on next main sync\n }\n });\n\n /* istanbul ignore if - only logs errors, not useful to test */\n if (errors.length > 0) {\n log.error(\n 'performMainNetworkSync() - NetworkController:removeNetwork failed',\n errors,\n );\n }\n }\n } catch (e) {\n /* istanbul ignore next - only logs errors, not useful to test */\n log.error('performMainNetworkSync() failed', e);\n // Silently fail sync\n } finally {\n isMainNetworkSyncInProgress = false;\n }\n}\n"]}
@@ -1,13 +1,55 @@
1
+ import type { NetworkConfiguration } from "@metamask/network-controller";
1
2
  import type { UserStorageBaseOptions } from "../services.cjs";
2
3
  import type { UserStorageControllerMessenger } from "../UserStorageController.cjs";
3
- type SetupNetworkSyncingProps = {
4
+ type StartNetworkSyncingProps = {
4
5
  messenger: UserStorageControllerMessenger;
5
- getStorageConfig: () => Promise<UserStorageBaseOptions>;
6
+ getStorageConfig: () => Promise<UserStorageBaseOptions | null>;
7
+ isMutationSyncBlocked: () => boolean;
8
+ };
9
+ type PerformMainNetworkSyncProps = {
10
+ messenger: UserStorageControllerMessenger;
11
+ getStorageConfig: () => Promise<UserStorageBaseOptions | null>;
12
+ maxNetworksToAdd?: number;
13
+ onNetworkAdded?: (chainId: string) => void;
14
+ onNetworkUpdated?: (chainId: string) => void;
15
+ onNetworkRemoved?: (chainId: string) => void;
6
16
  };
17
+ /**
18
+ * Global in-mem cache to signify that the network syncing is in progress
19
+ * Ensures that listeners do not fire during main sync (prevent double requests)
20
+ */
21
+ export declare let isMainNetworkSyncInProgress: boolean;
7
22
  /**
8
23
  * Initialize and setup events to listen to for network syncing
24
+ * We will be listening to:
25
+ * - Remove Event, to indicate that we need to remote network from remote
26
+ *
27
+ * We will not be listening to:
28
+ * - Add/Update events are not required, as we can sync these during the main sync
29
+ *
9
30
  * @param props - parameters used for initializing and enabling network syncing
10
31
  */
11
- export declare function startNetworkSyncing(props: SetupNetworkSyncingProps): void;
32
+ export declare function startNetworkSyncing(props: StartNetworkSyncingProps): void;
33
+ /**
34
+ * method that will dispatch the `NetworkController:updateNetwork` action.
35
+ * transforms and corrects the network configuration (and RPCs) we pass through.
36
+ * @param props - properties
37
+ * @param props.messenger - messenger to call the action
38
+ * @param props.originalNetworkConfiguration - original network config (from network controller state)
39
+ * @param props.newNetworkConfiguration - new network config (from remote)
40
+ * @param props.selectedNetworkClientId - currently selected network client id
41
+ */
42
+ export declare const dispatchUpdateNetwork: (props: {
43
+ messenger: UserStorageControllerMessenger;
44
+ originalNetworkConfiguration: NetworkConfiguration;
45
+ newNetworkConfiguration: NetworkConfiguration;
46
+ selectedNetworkClientId: string;
47
+ }) => Promise<void>;
48
+ /**
49
+ * Action to perform the main network sync.
50
+ * It will fetch local networks and remote networks, then determines which networks (local and remote) to add/update
51
+ * @param props - parameters used for this main sync
52
+ */
53
+ export declare function performMainNetworkSync(props: PerformMainNetworkSyncProps): Promise<void>;
12
54
  export {};
13
55
  //# sourceMappingURL=controller-integration.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"controller-integration.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/controller-integration.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAoB;AAC1D,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAG/E,KAAK,wBAAwB,GAAG;IAC9B,SAAS,EAAE,8BAA8B,CAAC;IAC1C,gBAAgB,EAAE,MAAM,OAAO,CAAC,sBAAsB,CAAC,CAAC;CACzD,CAAC;AAEF;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,QAqDlE"}
1
+ {"version":3,"file":"controller-integration.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/network-syncing/controller-integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qCAAqC;AAGzE,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAoB;AAC1D,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAO/E,KAAK,wBAAwB,GAAG;IAC9B,SAAS,EAAE,8BAA8B,CAAC;IAC1C,gBAAgB,EAAE,MAAM,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAAC;IAC/D,qBAAqB,EAAE,MAAM,OAAO,CAAC;CACtC,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,SAAS,EAAE,8BAA8B,CAAC;IAC1C,gBAAgB,EAAE,MAAM,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAAC;IAC/D,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3C,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7C,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9C,CAAC;AAEF;;;GAGG;AAGH,eAAO,IAAI,2BAA2B,SAAQ,CAAC;AAE/C;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,QAgClE;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,UAAiB;IACjD,SAAS,EAAE,8BAA8B,CAAC;IAC1C,4BAA4B,EAAE,oBAAoB,CAAC;IACnD,uBAAuB,EAAE,oBAAoB,CAAC;IAC9C,uBAAuB,EAAE,MAAM,CAAC;CACjC,kBAqBA,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,sBAAsB,CAC1C,KAAK,EAAE,2BAA2B,iBAgJnC"}