Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add app metadata controller #5325

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions packages/app-metadata-controller/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2018 MetaMask

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15 changes: 15 additions & 0 deletions packages/app-metadata-controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `@metamask/app-metadata-controller`

Manages the Metadata for the App

## Installation

`yarn add @metamask/app-metadata-controller`

or

`npm install @metamask/app-metadata-controller`

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
26 changes: 26 additions & 0 deletions packages/app-metadata-controller/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

const merge = require('deepmerge');
const path = require('path');

const baseConfig = require('../../jest.config.packages');

const displayName = path.basename(__dirname);

module.exports = merge(baseConfig, {
// The display name when running multiple projects
displayName,

// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
});
73 changes: 73 additions & 0 deletions packages/app-metadata-controller/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "@metamask/app-metadata-controller",
"version": "1.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set the version to 0.0.0 until the first release?

Suggested change
"version": "1.0.0",
"version": "0.0.0",

"description": "Manages requests that for app metadata",
"keywords": [
"MetaMask",
"Ethereum"
],
"homepage": "https://github.com/MetaMask/core/tree/main/packages/app-metadata-controller#readme",
"bugs": {
"url": "https://github.com/MetaMask/core/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.cts",
"files": [
"dist/"
],
"scripts": {
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/app-metadata-controller",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/app-metadata-controller",
"publish:preview": "yarn npm publish --tag preview",
"since-latest-release": "../../scripts/since-latest-release.sh",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"dependencies": {
"@metamask/base-controller": "^8.0.0",
"@metamask/rpc-errors": "^7.0.2",
"@metamask/utils": "^11.1.0",
"nanoid": "^3.3.8"
Comment on lines +50 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these dependencies used?

Suggested change
"@metamask/base-controller": "^8.0.0",
"@metamask/rpc-errors": "^7.0.2",
"@metamask/utils": "^11.1.0",
"nanoid": "^3.3.8"
"@metamask/base-controller": "^8.0.0"

},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@types/jest": "^27.4.1",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"sinon": "^9.2.4",
"ts-jest": "^27.1.4",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
"typescript": "~5.2.2"
},
"engines": {
"node": "^18.18 || >=20"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
154 changes: 154 additions & 0 deletions packages/app-metadata-controller/src/AppMetadataController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { Messenger } from '@metamask/base-controller';
import AppMetadataController, {
getDefaultAppMetadataControllerState,
type AppMetadataControllerOptions,
} from './AppMetadataController';

describe('AppMetadataController', () => {
describe('constructor', () => {
it('accepts initial state and does not modify it if currentMigrationVersion and platform.getVersion() match respective values in state', async () => {
const initState = {
currentAppVersion: '1',
previousAppVersion: '1',
previousMigrationVersion: 1,
currentMigrationVersion: 1,
};
withController(
{
state: initState,
currentMigrationVersion: 1,
currentAppVersion: '1',
},
({ controller }) => {
expect(controller.state).toStrictEqual(initState);
},
);
});

it('sets default state and does not modify it', () => {
withController({ state: {} }, ({ controller }) => {
expect(controller.state).toStrictEqual(
getDefaultAppMetadataControllerState(),
);
});
});

it('sets default state and does not modify it if options version parameters match respective default values', () => {
withController(
{
state: {},
currentMigrationVersion: 0,
currentAppVersion: '',
},
({ controller }) => {
expect(controller.state).toStrictEqual(
getDefaultAppMetadataControllerState(),
);
},
);
});

it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', () => {
withController(
{
state: {},
currentMigrationVersion: 0,
currentAppVersion: '1',
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentAppVersion: '1',
});
},
);
});

it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', () => {
withController(
{
state: {
currentAppVersion: '2',
previousAppVersion: '1',
},
currentAppVersion: '3',
currentMigrationVersion: 0,
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentAppVersion: '3',
previousAppVersion: '2',
});
},
);
});

it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', () => {
withController(
{
state: {},
currentMigrationVersion: 1,
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentMigrationVersion: 1,
});
},
);
});

it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', () => {
withController(
{
state: {
currentMigrationVersion: 2,
previousMigrationVersion: 1,
},
currentMigrationVersion: 3,
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentMigrationVersion: 3,
previousMigrationVersion: 2,
});
},
);
});
});
});

type WithControllerOptions = Partial<AppMetadataControllerOptions>;

type WithControllerCallback<ReturnValue> = ({
controller,
}: {
controller: AppMetadataController;
}) => ReturnValue;

type WithControllerArgs<ReturnValue> =
| [WithControllerCallback<ReturnValue>]
| [WithControllerOptions, WithControllerCallback<ReturnValue>];

function withController<ReturnValue>(
...args: WithControllerArgs<ReturnValue>
): ReturnValue {
const [options = {}, fn] = args.length === 2 ? args : [{}, args[0]];

const messenger = new Messenger<never, never>();

const appMetadataControllerMessenger = messenger.getRestricted({
name: 'AppMetadataController',
allowedActions: [],
allowedEvents: [],
});

return fn({
controller: new AppMetadataController({
messenger: appMetadataControllerMessenger,
...options,
}),
});
}
Loading
Loading