Skip to content

Commit

Permalink
refactor(keyring-api): fix new eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ccharly committed Sep 18, 2024
1 parent 7dd0958 commit 65351c4
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/keyring-api/src/KeyringClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type Sender = {
};

export class KeyringClient implements Keyring {
#sender: Sender;
readonly #sender: Sender;

/**
* Create a new instance of `KeyringClient`.
Expand Down
4 changes: 2 additions & 2 deletions packages/keyring-api/src/KeyringSnapRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { KeyringClient } from './KeyringClient';
* to a snap through the snap JSON-RPC API.
*/
export class SnapRpcSender implements Sender {
#origin: string;
readonly #origin: string;

#provider: MetaMaskInpageProvider;
readonly #provider: MetaMaskInpageProvider;

/**
* Create a new instance of `SnapRpcSender`.
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/btc/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BtcP2wpkhAccount } from './types';
import type { KeyringAccount } from '../api';
import type { Extends } from '../utils';
import { expectTrue } from '../utils';
import type { BtcP2wpkhAccount } from './types';

// `BtcP2wpkhAccount` extends `KeyringAccount`
expectTrue<Extends<BtcP2wpkhAccount, KeyringAccount>>();
1 change: 1 addition & 0 deletions packages/keyring-api/src/eth/erc4337/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EthAddressStruct, EthBytesStruct, EthUint256Struct } from '../types';

/**
* Struct of a UserOperation as defined by ERC-4337.
*
* @see https://eips.ethereum.org/EIPS/eip-4337#definitions
*/
export const EthUserOperationStruct = object({
Expand Down
4 changes: 2 additions & 2 deletions packages/keyring-api/src/eth/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expectAssignable, expectNotAssignable } from 'tsd';

import type { EthEoaAccount, EthErc4337Account } from './types';
import { EthMethod } from './types';
import type { KeyringAccount } from '../api';
import { EthAccountType } from '../api';
import type { Extends } from '../utils';
import { expectTrue } from '../utils';
import type { EthEoaAccount, EthErc4337Account } from './types';
import { EthMethod } from './types';

const id = '606a7759-b0fb-48e4-9874-bab62ff8e7eb';
const address = '0x000';
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/eth/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BtcAccountType, EthAccountType } from '../api';
import { isEvmAccountType } from './utils';
import { BtcAccountType, EthAccountType } from '../api';

describe('isEvmAccountType', () => {
it.each([
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/internal/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@metamask/superstruct';
import { JsonStruct } from '@metamask/utils';

import { KeyringRpcMethod } from './rpc';
import {
BalanceStruct,
CaipAssetTypeStruct,
Expand All @@ -19,7 +20,6 @@ import {
} from '../api';
import { object } from '../superstruct';
import { UuidStruct } from '../utils';
import { KeyringRpcMethod } from './rpc';

const CommonHeader = {
jsonrpc: literal('2.0'),
Expand Down
4 changes: 2 additions & 2 deletions packages/keyring-api/src/internal/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { is } from '@metamask/superstruct';

import { EthAccountType } from '../api';
import { KeyringEvent } from '../events';
import {
AccountCreatedEventStruct,
AccountDeletedEventStruct,
AccountUpdatedEventStruct,
RequestApprovedEventStruct,
RequestRejectedEventStruct,
} from './events';
import { EthAccountType } from '../api';
import { KeyringEvent } from '../events';

describe('events', () => {
describe('AccountCreatedEventStruct', () => {
Expand Down
35 changes: 12 additions & 23 deletions packages/keyring-api/src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,20 @@ export const InternalAccountMetadataStruct = object({
}),
});

/**
* Creates an `InternalAccount` from an existing account `superstruct` object.
*
* @param accountStruct - An account `superstruct` object.
* @returns The `InternalAccount` associated to `accountStruct`.
*/
function asInternalAccountStruct<Account, AccountSchema>(
accountStruct: Struct<Account, AccountSchema>,
) {
return object({
...accountStruct.schema,
...InternalAccountMetadataStruct.schema,
});
}

export const InternalEthEoaAccountStruct =
asInternalAccountStruct(EthEoaAccountStruct);
export const InternalEthEoaAccountStruct = object({
...EthEoaAccountStruct.schema,
...InternalAccountMetadataStruct.schema,
});

export const InternalEthErc4337AccountStruct = asInternalAccountStruct(
EthErc4337AccountStruct,
);
export const InternalEthErc4337AccountStruct = object({
...EthErc4337AccountStruct.schema,
...InternalAccountMetadataStruct.schema,
});

export const InternalBtcP2wpkhAccountStruct = asInternalAccountStruct(
BtcP2wpkhAccountStruct,
);
export const InternalBtcP2wpkhAccountStruct = object({
...BtcP2wpkhAccountStruct.schema,
...InternalAccountMetadataStruct.schema,
});

export type InternalEthEoaAccount = Infer<typeof InternalEthEoaAccountStruct>;

Expand Down
26 changes: 13 additions & 13 deletions packages/keyring-api/src/rpc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ async function dispatchRequest(
assert(request, JsonRpcRequestStruct);

switch (request.method) {
case KeyringRpcMethod.ListAccounts: {
case `${KeyringRpcMethod.ListAccounts}`: {
assert(request, ListAccountsRequestStruct);
return keyring.listAccounts();
}

case KeyringRpcMethod.GetAccount: {
case `${KeyringRpcMethod.GetAccount}`: {
assert(request, GetAccountRequestStruct);
return keyring.getAccount(request.params.id);
}

case KeyringRpcMethod.CreateAccount: {
case `${KeyringRpcMethod.CreateAccount}`: {
assert(request, CreateAccountRequestStruct);
return keyring.createAccount(request.params.options);
}

case KeyringRpcMethod.GetAccountBalances: {
case `${KeyringRpcMethod.GetAccountBalances}`: {
if (keyring.getAccountBalances === undefined) {
throw new MethodNotSupportedError(request.method);
}
Expand All @@ -73,62 +73,62 @@ async function dispatchRequest(
);
}

case KeyringRpcMethod.FilterAccountChains: {
case `${KeyringRpcMethod.FilterAccountChains}`: {
assert(request, FilterAccountChainsStruct);
return keyring.filterAccountChains(
request.params.id,
request.params.chains,
);
}

case KeyringRpcMethod.UpdateAccount: {
case `${KeyringRpcMethod.UpdateAccount}`: {
assert(request, UpdateAccountRequestStruct);
return keyring.updateAccount(request.params.account);
}

case KeyringRpcMethod.DeleteAccount: {
case `${KeyringRpcMethod.DeleteAccount}`: {
assert(request, DeleteAccountRequestStruct);
return keyring.deleteAccount(request.params.id);
}

case KeyringRpcMethod.ExportAccount: {
case `${KeyringRpcMethod.ExportAccount}`: {
if (keyring.exportAccount === undefined) {
throw new MethodNotSupportedError(request.method);
}
assert(request, ExportAccountRequestStruct);
return keyring.exportAccount(request.params.id);
}

case KeyringRpcMethod.ListRequests: {
case `${KeyringRpcMethod.ListRequests}`: {
if (keyring.listRequests === undefined) {
throw new MethodNotSupportedError(request.method);
}
assert(request, ListRequestsRequestStruct);
return keyring.listRequests();
}

case KeyringRpcMethod.GetRequest: {
case `${KeyringRpcMethod.GetRequest}`: {
if (keyring.getRequest === undefined) {
throw new MethodNotSupportedError(request.method);
}
assert(request, GetRequestRequestStruct);
return keyring.getRequest(request.params.id);
}

case KeyringRpcMethod.SubmitRequest: {
case `${KeyringRpcMethod.SubmitRequest}`: {
assert(request, SubmitRequestRequestStruct);
return keyring.submitRequest(request.params);
}

case KeyringRpcMethod.ApproveRequest: {
case `${KeyringRpcMethod.ApproveRequest}`: {
if (keyring.approveRequest === undefined) {
throw new MethodNotSupportedError(request.method);
}
assert(request, ApproveRequestRequestStruct);
return keyring.approveRequest(request.params.id, request.params.data);
}

case KeyringRpcMethod.RejectRequest: {
case `${KeyringRpcMethod.RejectRequest}`: {
if (keyring.rejectRequest === undefined) {
throw new MethodNotSupportedError(request.method);
}
Expand Down
1 change: 1 addition & 0 deletions packages/keyring-api/src/superstruct.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { expectAssignable, expectNotAssignable } from 'tsd';

import { exactOptional, object } from '.';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const exactOptionalObject = object({
a: number(),
b: optional(string()),
Expand Down
1 change: 1 addition & 0 deletions packages/keyring-api/src/superstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
Simplify,
} from '@metamask/superstruct';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
declare const ExactOptionalSymbol: unique symbol;

export type ExactOptionalTag = {
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const UrlStruct = define<string>('Url', (value: unknown) => {
try {
const url = new URL(value as string);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch (_) {
} catch {
return false;
}
});
Expand Down

0 comments on commit 65351c4

Please sign in to comment.