Skip to content

Commit

Permalink
refactor: apply MetaMask eslint style into snap (#295)
Browse files Browse the repository at this point in the history
* feat: add new cd workflow

* feat: add code scan

* chore: update code scan in duel process

* chore: merge sonar scan in test workflow

* chore: add token for scan

* chore: restore report coverage from specific node version

* fix: update secret

* fix: missing node version on the cache key

* chore: update dependency for ui and snap

* fix: update yarn lock

* fix: update yarn.lock

* fix: remove unuse package in get-starknet

* fix: remove metamask/rpc-methods

* chore: remove metamask/rpc-methods

* chore: add eslint config

* fix: fix unit test and lint style

* chore: update config

* chore: update eslint style

* chore: update operator in starknetutils

* chore: update add erc20 token

* fix: update yarn lock

* chore: update yarn lock

* chore: update yarn.lock

* chore: update lint style for snap

* chore: update eslint style for wallet ui

* chore: update get-starknet eslint

* chore: update ignore pattern for snap

* chore: fix unit test of snap

* chore: remove infura url

* chore: update lint style

* chore: update ui lint style

* chore: add ApiParamsWithKeyDeriver type

* fix: move error object into the 2nd args of logger.error

* chore: remove readonly from private method

* chore: force rpc api to use ApiParamsWithKeyDeriver if it has  keyDeriver

* fix: fix unit test after adding ApiParamsWithKeyDeriver
  • Loading branch information
stanleyyconsensys authored Jul 25, 2024
1 parent b6f5879 commit 31f0f11
Show file tree
Hide file tree
Showing 191 changed files with 12,225 additions and 8,306 deletions.
43 changes: 43 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = {
root: true,

parserOptions: {
sourceType: 'module',
},

extends: ['@metamask/eslint-config'],

overrides: [
{
files: ['*.js'],
extends: ['@metamask/eslint-config-nodejs'],
},

{
files: ['*.ts', '*.tsx'],
extends: ['@metamask/eslint-config-typescript'],
},

{
files: ['*.test.ts', '*.test.js'],
extends: ['@metamask/eslint-config-jest'],
rules: {
'@typescript-eslint/no-shadow': [
'error',
{ allow: ['describe', 'expect', 'it'] },
],
},
},
],

ignorePatterns: [
'!.prettierrc.js',
'**/!.eslintrc.js',
'**/dist*/',
'**/*__GENERATED__*',
'**/build',
'**/public',
'**/.cache',
'**/example',
],
};
9 changes: 3 additions & 6 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// https://prettier.io/docs/en/options.html
// All of these are defaults except singleQuote, but we specify them
// for explicitness
module.exports = {
quoteProps: 'as-needed',
singleQuote: true,
printWidth: 120,
tabWidth: 2,
useTabs: false,
semi: true,
trailingComma: 'all',
bracketSpacing: true,
arrowParens: 'always',
};
546 changes: 546 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: '@yarnpkg/plugin-workspace-tools'
spec: "@yarnpkg/plugin-workspace-tools"
- path: .yarn/plugins/@yarnpkg/plugin-allow-scripts.cjs
spec: 'https://raw.githubusercontent.com/LavaMoat/LavaMoat/main/packages/yarn-plugin-allow-scripts/bundles/@yarnpkg/plugin-allow-scripts.js'
spec: "https://raw.githubusercontent.com/LavaMoat/LavaMoat/main/packages/yarn-plugin-allow-scripts/bundles/@yarnpkg/plugin-allow-scripts.js"
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-3.2.1.cjs
2 changes: 2 additions & 0 deletions packages/get-starknet/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
js/
49 changes: 38 additions & 11 deletions packages/get-starknet/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
extends: ['../../.eslintrc.js'],

parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
rules: {
tsconfigRootDir: __dirname,
},

overrides: [
{
files: ['snap.config.ts'],
extends: ['@metamask/eslint-config-nodejs'],
},
{
files: ['*.ts', '*.tsx'],
extends: ['@metamask/eslint-config-typescript'],
rules: {
'valid-jsdoc': 'off',
'jsdoc/require-description': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/match-description': 'off',
},
},
{
files: ['*.test.ts'],
extends: ['@metamask/eslint-config-jest'],
rules: {
'@typescript-eslint/no-shadow': ['error', { allow: ['describe', 'expect', 'it'] }],
'@typescript-eslint/unbound-method': 'off',
},
},

{
files: ['*.ts'],
rules: {
'import/no-nodejs-modules': 'off',
},
},
],

ignorePatterns: ['!.eslintrc.js', 'dist/', '**/test', '.nyc_output/', 'coverage/'],
};
19 changes: 15 additions & 4 deletions packages/get-starknet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@
"keywords": [],
"author": "Consensys",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"dotenv": "^16.4.5",
"eslint": "^8.13.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "~2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "^41.1.2",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"get-starknet-core": "^3.2.0",
"prettier": "^2.6.2",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"serve": "14.2.1",
"ts-loader": "^9.5.1",
Expand Down
1 change: 0 additions & 1 deletion packages/get-starknet/public/index.html

This file was deleted.

8 changes: 5 additions & 3 deletions packages/get-starknet/src/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { MetaMaskSnap } from './snap';
import {
import type {
Abi,
Account,
AllowArray,
CairoVersion,
Call,
Expand All @@ -15,9 +13,13 @@ import {
SignerInterface,
TypedData,
} from 'starknet';
import { Account } from 'starknet';

import type { MetaMaskSnap } from './snap';

export class MetaMaskAccount extends Account {
#snap: MetaMaskSnap;

#address: string;

constructor(
Expand Down
17 changes: 9 additions & 8 deletions packages/get-starknet/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MetaMaskSnap } from './snap';
import {
import type {
Abi,
ArraySignatureType,
Call,
Expand All @@ -9,12 +8,14 @@ import {
Signature,
SignerInterface,
TypedData,
ec,
num,
} from 'starknet';
import { ec, num as numUtils } from 'starknet';

import type { MetaMaskSnap } from './snap';

export class MetaMaskSigner implements SignerInterface {
#snap: MetaMaskSnap;

#address: string;

constructor(snap: MetaMaskSnap, address: string) {
Expand All @@ -28,7 +29,7 @@ export class MetaMaskSigner implements SignerInterface {

async signMessage(typedData: TypedData, accountAddress: string): Promise<Signature> {
const result = (await this.#snap.signMessage(typedData, false, accountAddress)) as ArraySignatureType;
return new ec.starkCurve.Signature(num.toBigInt(result[0]), num.toBigInt(result[1]));
return new ec.starkCurve.Signature(numUtils.toBigInt(result[0]), numUtils.toBigInt(result[1]));
}

async signTransaction(
Expand All @@ -42,16 +43,16 @@ export class MetaMaskSigner implements SignerInterface {
transactionsDetail,
abis,
)) as ArraySignatureType;
return new ec.starkCurve.Signature(num.toBigInt(result[0]), num.toBigInt(result[1]));
return new ec.starkCurve.Signature(numUtils.toBigInt(result[0]), numUtils.toBigInt(result[1]));
}

async signDeployAccountTransaction(transaction: DeployAccountSignerDetails): Promise<Signature> {
const result = (await this.#snap.signDeployAccountTransaction(this.#address, transaction)) as ArraySignatureType;
return new ec.starkCurve.Signature(num.toBigInt(result[0]), num.toBigInt(result[1]));
return new ec.starkCurve.Signature(numUtils.toBigInt(result[0]), numUtils.toBigInt(result[1]));
}

async signDeclareTransaction(transaction: DeclareSignerDetails): Promise<Signature> {
const result = (await this.#snap.signDeclareTransaction(this.#address, transaction)) as ArraySignatureType;
return new ec.starkCurve.Signature(num.toBigInt(result[0]), num.toBigInt(result[1]));
return new ec.starkCurve.Signature(numUtils.toBigInt(result[0]), numUtils.toBigInt(result[1]));
}
}
60 changes: 32 additions & 28 deletions packages/get-starknet/src/snap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AccContract, MetaMaskProvider, Network, RequestSnapResponse } from './type';
import {
import type {
Abi,
AllowArray,
Call,
Expand All @@ -14,9 +13,13 @@ import {
TypedData,
} from 'starknet';

import type { AccContract, MetaMaskProvider, Network, RequestSnapResponse } from './type';

export class MetaMaskSnap {
#provider: MetaMaskProvider;

#snapId: string;

#version: string;

constructor(snapId: string, version: string, provider: MetaMaskProvider) {
Expand Down Expand Up @@ -57,7 +60,7 @@ export class MetaMaskSnap {
signerAddress,
transactions,
transactionsDetail,
abis: abis,
abis,
...(await this.#getSnapParams()),
}),
},
Expand Down Expand Up @@ -136,7 +139,7 @@ export class MetaMaskSnap {
params: this.removeUndefined({
signerAddress,
typedDataMessage,
enableAuthorize: enableAuthorize,
enableAuthorize,
...(await this.#getSnapParams()),
}),
},
Expand Down Expand Up @@ -176,10 +179,10 @@ export class MetaMaskSnap {
params: {},
},
},
})) as unknown as Network[];
})) as Network[];

const network = response.find((n) => {
return n.chainId === chainId;
const network = response.find((item) => {
return item.chainId === chainId;
});

return network;
Expand All @@ -190,12 +193,7 @@ export class MetaMaskSnap {
return result[0];
}

async recoverAccounts(
chainId: string,
startScanIndex = 0,
maxScanned = 1,
maxMissed = 1,
): Promise<Array<AccContract>> {
async recoverAccounts(chainId: string, startScanIndex = 0, maxScanned = 1, maxMissed = 1): Promise<AccContract[]> {
return (await this.#provider.request({
method: 'wallet_invokeSnap',
params: {
Expand All @@ -210,7 +208,7 @@ export class MetaMaskSnap {
},
},
},
})) as Array<AccContract>;
})) as AccContract[];
}

async switchNetwork(chainId: string): Promise<boolean> {
Expand Down Expand Up @@ -275,7 +273,7 @@ export class MetaMaskSnap {
params: {},
},
},
})) as unknown as Network;
})) as Network;

return response;
}
Expand All @@ -287,30 +285,36 @@ export class MetaMaskSnap {
};
}

static async GetProvider(window: { ethereum?: unknown }) {
static async getProvider(window: {
ethereum?: {
detected?: MetaMaskProvider[];
providers?: MetaMaskProvider[];
};
}) {
const { ethereum } = window;
if (!ethereum) {
return null;
}
let providers = [ethereum];
let providers: MetaMaskProvider[] = [ethereum as unknown as MetaMaskProvider];

// ethereum.detected or ethereum.providers may exist when more than 1 wallet installed

//ethereum.detected or ethereum.providers may exist when more than 1 wallet installed
if (ethereum.hasOwnProperty('detected')) {
providers = ethereum['detected'];
} else if (ethereum.hasOwnProperty('providers')) {
providers = ethereum['providers'];
if (Object.prototype.hasOwnProperty.call(ethereum, 'detected')) {
providers = ethereum.detected as unknown as MetaMaskProvider[];
} else if (Object.prototype.hasOwnProperty.call(ethereum, 'providers')) {
providers = ethereum.providers as unknown as MetaMaskProvider[];
}

//delect provider by sending request
// detect provider by sending request
for (const provider of providers) {
if (provider && (await MetaMaskSnap.IsSupportSnap(provider as MetaMaskProvider))) {
if (provider && (await MetaMaskSnap.isSupportSnap(provider))) {
return provider;
}
}
return null;
}

static async IsSupportSnap(provider: MetaMaskProvider) {
static async isSupportSnap(provider: MetaMaskProvider) {
try {
await provider.request({
method: 'wallet_getSnaps',
Expand All @@ -328,7 +332,7 @@ export class MetaMaskSnap {
[this.#snapId]: { version: this.#version },
},
})) as RequestSnapResponse;
if (!response || !response[this.#snapId]?.enabled) {
if (!response?.[this.#snapId]?.enabled) {
return false;
}
return true;
Expand All @@ -346,13 +350,13 @@ export class MetaMaskSnap {
},
});
return true;
} catch (err) {
} catch (error) {
return false;
}
}

removeUndefined(obj: Record<string, unknown>) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== undefined));
return Object.fromEntries(Object.entries(obj).filter(([_, val]) => val !== undefined));
}
}
Loading

0 comments on commit 31f0f11

Please sign in to comment.