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

refactor: add new package @metamask/keyring-utils #22

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ module.exports = {
'@typescript-eslint/naming-convention': 'warn',
},
},
// @metamask/keyring-utils
{
files: ['packages/keyring-utils/src/**/*.ts'],
extends: ['@metamask/eslint-config-typescript'],
parserOptions,
rules: {
// TODO: re-lint everything once the migration is done
'@typescript-eslint/no-explicit-any': 'off',
},
},
// @metamask/keyring-api
{
files: ['packages/keyring-api/src/**/*.ts'],
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This repository contains the following packages [^fn1]:
- [`@metamask/eth-snap-keyring`](packages/keyring-snap)
- [`@metamask/eth-trezor-keyring`](packages/keyring-eth-trezor)
- [`@metamask/keyring-api`](packages/keyring-api)
- [`@metamask/keyring-utils`](packages/keyring-utils)

<!-- end package list -->

Expand All @@ -31,6 +32,8 @@ linkStyle default opacity:0.5
eth_simple_keyring(["@metamask/eth-simple-keyring"]);
eth_trezor_keyring(["@metamask/eth-trezor-keyring"]);
eth_snap_keyring(["@metamask/eth-snap-keyring"]);
keyring_utils(["@metamask/keyring-utils"]);
keyring_api --> keyring_utils;
eth_snap_keyring --> keyring_api;
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"test:verbose": "yarn workspaces foreach --all --parallel --verbose run test:verbose"
},
"resolutions": {
"@metamask/keyring-utils@^0.0.1": "workspace:.",
"@types/node": "^20.12.12",
"@types/web": "0.0.69",
"@typescript/lib-dom": "npm:@types/[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@metamask/snaps-sdk": "^6.1.0",
"@metamask/superstruct": "^3.1.0",
"@metamask/utils": "^9.1.0",
"@types/uuid": "^9.0.8",
"bech32": "^2.0.0",
"deepmerge": "^4.2.2",
"uuid": "^9.0.1"
Expand All @@ -48,6 +47,7 @@
"@lavamoat/allow-scripts": "^3.0.4",
"@lavamoat/preinstall-always-fail": "^2.0.0",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/keyring-utils": "^0.0.1",
"@metamask/providers": "^17.1.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/JsonRpcRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { Infer } from '@metamask/superstruct';
import { JsonStruct } from '@metamask/utils';

import { exactOptional, object } from './superstruct';
import { exactOptional, object } from '@metamask/keyring-utils';

export const JsonRpcRequestStruct = object({
jsonrpc: literal('2.0'),
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/KeyringClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from './internal/api';
import { KeyringRpcMethod } from './internal/rpc';
import type { JsonRpcRequest } from './JsonRpcRequest';
import { strictMask } from './superstruct';
import { strictMask } from '@metamask/keyring-utils';

export type Sender = {
send(request: JsonRpcRequest): Promise<Json>;
Expand Down
3 changes: 1 addition & 2 deletions packages/keyring-api/src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { Infer } from '@metamask/superstruct';
import { array, enums, record, string } from '@metamask/superstruct';
import { JsonStruct } from '@metamask/utils';

import { object } from '../superstruct';
import { UuidStruct } from '../utils';
import { object, UuidStruct } from '@metamask/keyring-utils';

/**
* Supported Ethereum account types.
Expand Down
3 changes: 1 addition & 2 deletions packages/keyring-api/src/api/balance.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Infer } from '@metamask/superstruct';
import { string } from '@metamask/superstruct';

import { object } from '../superstruct';
import { StringNumberStruct } from '../utils';
import { object, StringNumberStruct } from '@metamask/keyring-utils';

export const BalanceStruct = object({
amount: StringNumberStruct,
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/api/caip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { is, type Infer } from '@metamask/superstruct';

import { definePattern } from '../superstruct';
import { definePattern } from '@metamask/keyring-utils';

const CAIP_ASSET_TYPE_REGEX =
/^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32}))\/(?<assetNamespace>[-a-z0-9]{3,8}):(?<assetReference>[-.%a-zA-Z0-9]{1,128})$/u;
Expand Down
3 changes: 1 addition & 2 deletions packages/keyring-api/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { Infer } from '@metamask/superstruct';
import { array, record, string, union } from '@metamask/superstruct';
import { JsonStruct } from '@metamask/utils';

import { exactOptional, object } from '../superstruct';
import { UuidStruct } from '../utils';
import { exactOptional, object, UuidStruct } from '@metamask/keyring-utils';

export const KeyringRequestStruct = object({
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/api/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Infer } from '@metamask/superstruct';
import { literal, string, union } from '@metamask/superstruct';
import { JsonStruct } from '@metamask/utils';

import { exactOptional, object } from '../superstruct';
import { exactOptional, object } from '@metamask/keyring-utils';

export const KeyringResponseStruct = union([
object({
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/btc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { string, array, enums, refine, literal } from '@metamask/superstruct';
import { bech32 } from 'bech32';

import { KeyringAccountStruct, BtcAccountType } from '../api';
import { object } from '../superstruct';
import { object } from '@metamask/keyring-utils';

export const BtcP2wpkhAddressStruct = refine(
string(),
Expand Down
3 changes: 1 addition & 2 deletions packages/keyring-api/src/eth/erc4337/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type Infer } from '@metamask/superstruct';

import { exactOptional, object } from '../../superstruct';
import { UrlStruct } from '../../utils';
import { exactOptional, object, UrlStruct } from '@metamask/keyring-utils';
import { EthAddressStruct, EthBytesStruct, EthUint256Struct } from '../types';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/eth/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UrlStruct } from '../utils';
import { UrlStruct } from '@metamask/keyring-utils';

describe('types', () => {
it('is a valid BundlerUrl', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/eth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Infer } from '@metamask/superstruct';
import { array, enums, literal } from '@metamask/superstruct';

import { EthAccountType, KeyringAccountStruct } from '../api';
import { object, definePattern } from '../superstruct';
import { object, definePattern } from '@metamask/keyring-utils';

export const EthBytesStruct = definePattern('EthBytes', /^0x[0-9a-f]*$/iu);

Expand Down
1 change: 0 additions & 1 deletion packages/keyring-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export * from './KeyringClient';
export * from './KeyringSnapRpcClient';
export * from './rpc-handler';
export * from './snap-utils';
export * from './superstruct';
3 changes: 1 addition & 2 deletions packages/keyring-api/src/internal/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
KeyringRequestStruct,
KeyringResponseStruct,
} from '../api';
import { object } from '../superstruct';
import { UuidStruct } from '../utils';
import { object, UuidStruct } from '@metamask/keyring-utils';
import { KeyringRpcMethod } from './rpc';

const CommonHeader = {
Expand Down
3 changes: 1 addition & 2 deletions packages/keyring-api/src/internal/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { JsonStruct } from '@metamask/utils';

import { KeyringAccountStruct } from '../api';
import { KeyringEvent } from '../events';
import { exactOptional, object } from '../superstruct';
import { UuidStruct } from '../utils';
import { exactOptional, object, UuidStruct } from '@metamask/keyring-utils';

export const AccountCreatedEventStruct = object({
method: literal(`${KeyringEvent.AccountCreated}`),
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { boolean, string, number } from '@metamask/superstruct';
import { BtcAccountType, EthAccountType, KeyringAccountStruct } from '../api';
import { BtcP2wpkhAccountStruct } from '../btc/types';
import { EthEoaAccountStruct, EthErc4337AccountStruct } from '../eth/types';
import { exactOptional, object } from '../superstruct';
import { exactOptional, object } from '@metamask/keyring-utils';

export type InternalAccountType = EthAccountType | BtcAccountType;

Expand Down
1 change: 1 addition & 0 deletions packages/keyring-api/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// checking for all *.d.ts files
"skipLibCheck": true
},
"references": [{ "path": "../keyring-utils/tsconfig.build.json" }],
"include": ["./src/**/*.ts"],
"exclude": ["./src/**/*.test.ts", "./src/**/*.test-d.ts"]
}
2 changes: 1 addition & 1 deletion packages/keyring-snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"@ethereumjs/tx": "^4.2.0",
"@metamask/eth-sig-util": "^7.0.3",
"@metamask/keyring-api": "^8.0.2",
"@metamask/keyring-api": "^8.1.0",
"@metamask/snaps-controllers": "^9.3.0",
"@metamask/snaps-sdk": "^6.1.0",
"@metamask/snaps-utils": "^7.8.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/keyring-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

[Unreleased]: https://github.com/MetaMask/accounts/
15 changes: 15 additions & 0 deletions packages/keyring-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# MetaMask Keyring utils

Various JavaScript/TypeScript utilities required by Keyrings.

## Installation

`yarn add @metamask/keyring-utils`

or

`npm install @metamask/keyring-utils`

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/accounts#readme).
28 changes: 28 additions & 0 deletions packages/keyring-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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,

coveragePathIgnorePatterns: ['./src/tests'],

// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
});
76 changes: 76 additions & 0 deletions packages/keyring-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "@metamask/keyring-utils",
"version": "0.0.1",
"description": "MetaMask Keyring utils",
"keywords": [
"metamask",
"keyring"
],
"homepage": "https://github.com/MetaMask/accounts/tree/main/packages/keyring-utils#readme",
"bugs": {
"url": "https://github.com/MetaMask/accounts/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/accounts.git"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsc --build tsconfig.build.json",
"build:clean": "rimraf dist && yarn build",
"build:docs": "typedoc",
"build:force": "tsc --build tsconfig.build.json --force",
"changelog:update": "../../scripts/update-changelog.sh @metamask/keyring-utils",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/keyring-utils",
"publish:preview": "yarn npm publish --tag preview",
"test": "yarn test:source && yarn test:types",
"test:clean": "jest --clearCache",
"test:source": "jest && jest-it-up",
"test:types": "tsd",
"test:verbose": "jest --verbose",
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/superstruct": "^3.1.0",
"@metamask/utils": "^9.1.0",
"@types/uuid": "^9.0.8",
"deepmerge": "^4.2.2",
"uuid": "^9.0.1"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^3.0.4",
"@lavamoat/preinstall-always-fail": "^2.0.0",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/providers": "^17.1.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
"depcheck": "^1.4.7",
"jest": "^28.1.3",
"jest-it-up": "^3.1.0",
"rimraf": "^5.0.7",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.2",
"tsd": "^0.31.0",
"typedoc": "^0.25.13",
"typescript": "~4.8.4"
},
"engines": {
"node": "^18.18 || >=20"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false
}
},
"tsd": {
"directory": "src"
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './types';
export * from './typing';
export * from './superstruct';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { define, type Infer } from '@metamask/superstruct';

import { definePattern } from '../superstruct';
import { definePattern } from './superstruct';

/**
* UUIDv4 struct.
Expand Down
13 changes: 13 additions & 0 deletions packages/keyring-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.packages.build.json",
"compilerOptions": {
"baseUrl": "./",
"outDir": "dist",
"rootDir": "src",
// FIXME: We should investigate how to get rid of this flag, as this will turn off type
// checking for all *.d.ts files
"skipLibCheck": true
},
"include": ["./src/**/*.ts"],
"exclude": ["./src/**/*.test.ts", "./src/**/*.test-d.ts"]
}
8 changes: 8 additions & 0 deletions packages/keyring-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.packages.json",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["./src"],
"exclude": ["./dist/**/*"]
}
6 changes: 6 additions & 0 deletions packages/keyring-utils/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"entryPoints": ["./src/index.ts"],
"excludePrivate": true,
"hideGenerator": true,
"out": "docs"
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
{ "path": "./packages/keyring-eth-ledger-bridge" },
{ "path": "./packages/keyring-eth-simple" },
{ "path": "./packages/keyring-eth-trezor" },
{ "path": "./packages/keyring-snap" }
{ "path": "./packages/keyring-snap" },
{ "path": "./packages/keyring-utils" }
],
"files": [],
"include": []
Expand Down
Loading
Loading