Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- **BREAKING:** Add `GeolocationController` and `GeolocationApiService` to the default initialization configurations ([#11058](https://github.com/MetaMask/core/pull/11058))
- Optional `instanceOptions.geolocationApiService` accepts `env`, `fetch`, `ttlMs`, and `policyOptions`. Defaults to production and `globalThis.fetch`.
- `GeolocationController` delegates `GeolocationApiService:fetchGeolocationData`.
- Hosts that already register `GeolocationController` or `GeolocationApiService` on the wallet root messenger must remove them before upgrading, or messenger registration will collide.
- Hosts no longer need to register `GeolocationController` when `AnalyticsController` `isGeolocationEnabled` is `true`.

### Changed

- Add `@metamask/geolocation-controller` as a dependency ([#11058](https://github.com/MetaMask/core/pull/11058))

## [19.1.0]

### Added
Expand Down
1 change: 1 addition & 0 deletions packages/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@metamask/connectivity-controller": "^1.0.0",
"@metamask/controller-utils": "^13.0.0",
"@metamask/gas-fee-controller": "^27.0.0",
"@metamask/geolocation-controller": "^2.0.0",
"@metamask/keyring-controller": "^28.2.0",
"@metamask/logging-controller": "^10.0.0",
"@metamask/messenger": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export type AnalyticsControllerInstanceOptions = {
isPreConsentQueueEnabled?: AnalyticsControllerOptions['isPreConsentQueueEnabled'];

/**
* When omitted, the controller defaults to `false`. When enabled, hosts must
* register `GeolocationController` on the wallet root messenger.
* When omitted, the controller defaults to `false`. When enabled, wallet
* resolves location through the default `GeolocationController`.
*/
isGeolocationEnabled?: AnalyticsControllerOptions['isGeolocationEnabled'];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Env, GeolocationApiService } from '@metamask/geolocation-controller';
import { Messenger } from '@metamask/messenger';
import type { Mock } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

import { defaultConfigurations } from '../../defaults.js';
import type {
DefaultActions,
DefaultEvents,
RootMessenger,
} from '../../defaults.js';
import { geolocationApiService } from './geolocation-api-service.js';

function getRootMessenger(): RootMessenger<DefaultActions, DefaultEvents> {
return new Messenger({ namespace: 'Root' });
}

function makeFetchMock(body: unknown): Mock {
return vi
.fn()
.mockResolvedValue(
new globalThis.Response(JSON.stringify(body), { status: 200 }),
);
}

describe('geolocationApiService', () => {
it('is registered as a default initialization configuration', () => {
expect(Object.values(defaultConfigurations)).toContain(
geolocationApiService,
);
});

it('initializes a GeolocationApiService', () => {
const messenger = geolocationApiService.getMessenger(getRootMessenger());

const instance = geolocationApiService.init({
state: undefined,
messenger,
options: {},
});

expect(instance).toBeInstanceOf(GeolocationApiService);
});

it('uses the provided env to determine the API URL', async () => {
const fetchMock = makeFetchMock({
country: 'US',
region: 'NY',
timezone: 'America/New_York',
});
const messenger = geolocationApiService.getMessenger(getRootMessenger());

const instance = geolocationApiService.init({
state: undefined,
messenger,
options: { env: Env.DEV, fetch: fetchMock },
});

await instance.fetchGeolocationData();

const [url] = fetchMock.mock.calls[0] as [string];
expect(url).toContain('dev-api.cx.metamask.io');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Env, GeolocationApiService } from '@metamask/geolocation-controller';
import type { GeolocationApiServiceMessenger } from '@metamask/geolocation-controller';
import { Messenger } from '@metamask/messenger';

import type { InitializationConfiguration } from '../../types.js';

export type { GeolocationApiServiceInstanceOptions } from './types.js';

export const geolocationApiService: InitializationConfiguration<
GeolocationApiService,
GeolocationApiServiceMessenger
> = {
name: 'GeolocationApiService',
init: ({ messenger, options }) =>
new GeolocationApiService({
messenger,
env: options.env ?? Env.PRD,
fetch: options.fetch,
ttlMs: options.ttlMs,
policyOptions: options.policyOptions,
}),
getMessenger: (parent) =>
new Messenger({
namespace: 'GeolocationApiService',
parent,
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type {
Env,
GeolocationApiService,
} from '@metamask/geolocation-controller';

type GeolocationApiServiceOptions = ConstructorParameters<
typeof GeolocationApiService
>[0];

/**
* Per-instance options for the wallet's `GeolocationApiService`.
*
* All fields are optional. The service defaults to production and
* `globalThis.fetch`.
*/
export type GeolocationApiServiceInstanceOptions = {
env?: Env;
fetch?: GeolocationApiServiceOptions['fetch'];
ttlMs?: GeolocationApiServiceOptions['ttlMs'];
policyOptions?: GeolocationApiServiceOptions['policyOptions'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
GeolocationController,
getDefaultGeolocationControllerState,
} from '@metamask/geolocation-controller';
import { Messenger } from '@metamask/messenger';
import { describe, expect, it, vi } from 'vitest';

import { defaultConfigurations } from '../../defaults.js';
import type {
DefaultActions,
DefaultEvents,
RootMessenger,
} from '../../defaults.js';
import { geolocationApiService } from '../geolocation-api-service/geolocation-api-service.js';
import { geolocationController } from './geolocation-controller.js';

function getRootMessenger(): RootMessenger<DefaultActions, DefaultEvents> {
return new Messenger({ namespace: 'Root' });
}

describe('geolocationController', () => {
it('is registered as a default initialization configuration', () => {
expect(Object.values(defaultConfigurations)).toContain(
geolocationController,
);
});

it('initializes a GeolocationController with default state', () => {
const messenger = geolocationController.getMessenger(getRootMessenger());

const instance = geolocationController.init({
state: undefined,
messenger,
options: {},
});

expect(instance).toBeInstanceOf(GeolocationController);
expect(instance.state).toStrictEqual(
getDefaultGeolocationControllerState(),
);
});

it('delegates GeolocationApiService:fetchGeolocationData', () => {
const parent = getRootMessenger();
const delegateSpy = vi.spyOn(parent, 'delegate');

const messenger = geolocationController.getMessenger(parent);

expect(delegateSpy).toHaveBeenCalledWith({
messenger,
actions: ['GeolocationApiService:fetchGeolocationData'],
});
});

it('resolves geolocation through the wallet-owned API service', async () => {
const rootMessenger = getRootMessenger();
const fetchMock = vi.fn().mockResolvedValue(
new globalThis.Response(
JSON.stringify({
country: 'US',
region: 'NY',
timezone: 'America/New_York',
}),
{ status: 200 },
),
);

geolocationApiService.init({
state: undefined,
messenger: geolocationApiService.getMessenger(rootMessenger),
options: { fetch: fetchMock },
});

geolocationController.init({
state: undefined,
messenger: geolocationController.getMessenger(rootMessenger),
options: {},
});

const result = await rootMessenger.call(
'GeolocationController:getGeolocationData',
);

expect(result).toStrictEqual({
country: 'US',
region: 'NY',
timezone: 'America/New_York',
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { GeolocationController } from '@metamask/geolocation-controller';
import type { GeolocationControllerMessenger } from '@metamask/geolocation-controller';
import { Messenger } from '@metamask/messenger';

import type { InitializationConfiguration } from '../../types.js';

export const geolocationController: InitializationConfiguration<
GeolocationController,
GeolocationControllerMessenger
> = {
name: 'GeolocationController',
reference: GeolocationController,
init: ({ state, messenger }) =>
new GeolocationController({
messenger,
state,
}),
getMessenger: (parent) => {
const messenger: GeolocationControllerMessenger = new Messenger({
namespace: 'GeolocationController',
parent,
});

parent.delegate({
messenger,
actions: ['GeolocationApiService:fetchGeolocationData'],
});

return messenger;
},
};
2 changes: 2 additions & 0 deletions packages/wallet/src/initialization/instances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export { configRegistryApiService } from './config-registry-api-service/config-r
export { configRegistryController } from './config-registry-controller/config-registry-controller.js';
export { connectivityController } from './connectivity-controller/connectivity-controller.js';
export { gasFeeController } from './gas-fee-controller/gas-fee-controller.js';
export { geolocationApiService } from './geolocation-api-service/geolocation-api-service.js';
export { geolocationController } from './geolocation-controller/geolocation-controller.js';
export { keyringController } from './keyring-controller/keyring-controller.js';
export { loggingController } from './logging-controller/logging-controller.js';
export { networkController } from './network-controller/network-controller.js';
Expand Down
2 changes: 2 additions & 0 deletions packages/wallet/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { ConfigRegistryApiServiceInstanceOptions } from './initialization/i
import type { ConfigRegistryControllerInstanceOptions } from './initialization/instances/config-registry-controller/types.js';
import type { ConnectivityControllerInstanceOptions } from './initialization/instances/connectivity-controller/types.js';
import type { GasFeeControllerInstanceOptions } from './initialization/instances/gas-fee-controller/types.js';
import type { GeolocationApiServiceInstanceOptions } from './initialization/instances/geolocation-api-service/types.js';
import type { KeyringControllerInstanceOptions } from './initialization/instances/keyring-controller/types.js';
import type { NetworkControllerInstanceOptions } from './initialization/instances/network-controller/types.js';
import type { PasskeyControllerInstanceOptions } from './initialization/instances/passkey-controller/types.js';
Expand Down Expand Up @@ -42,6 +43,7 @@ export type InstanceSpecificOptions = {
configRegistryController?: ConfigRegistryControllerInstanceOptions;
connectivityController: ConnectivityControllerInstanceOptions;
gasFeeController: GasFeeControllerInstanceOptions;
geolocationApiService?: GeolocationApiServiceInstanceOptions;
keyringController?: KeyringControllerInstanceOptions;
networkController: NetworkControllerInstanceOptions;
remoteFeatureFlagController: RemoteFeatureFlagControllerInstanceOptions;
Expand Down
3 changes: 3 additions & 0 deletions packages/wallet/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
},
{
"path": "../utils/tsconfig.build.json"
},
{
"path": "../geolocation-controller/tsconfig.build.json"
}
],
"include": ["../../types", "./src"]
Expand Down
3 changes: 3 additions & 0 deletions packages/wallet/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
},
{
"path": "../utils"
},
{
"path": "../geolocation-controller"
}
],
"include": ["../../types", "./src"]
Expand Down
3 changes: 3 additions & 0 deletions packages/wallet/tsconfig.lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
},
{
"path": "../utils/tsconfig.lint.json"
},
{
"path": "../geolocation-controller/tsconfig.lint.json"
}
]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9710,6 +9710,7 @@ __metadata:
"@metamask/connectivity-controller": "npm:^1.0.0"
"@metamask/controller-utils": "npm:^13.0.0"
"@metamask/gas-fee-controller": "npm:^27.0.0"
"@metamask/geolocation-controller": "npm:^2.0.0"
"@metamask/keyring-controller": "npm:^28.2.0"
"@metamask/logging-controller": "npm:^10.0.0"
"@metamask/messenger": "npm:^3.0.0"
Expand Down
Loading