Skip to content

Commit

Permalink
chore: remove @metamask/ethjs package (#29620)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

This PR introduces the following changes:
	•	Removes the `@metamask/ethjs` package.
• Replaces the global variable `global.eth` with the existing
`global.ethereumProvider` (an instance of `StreamProvider` from
`web3-stream-provider` package).
• Updates all instances of `getCode` to use a manual `sendAsync` call
for the `eth_getCode` method, as StreamProvider does not include
getCode, unlike ethjs.
• Substitutes the use of ethjs contracts with Contract instances from
the @ethersproject/contracts library.
• Removal of `@metamask/ethjs-contract` and `@metamask/ethjs-query`
packages.

The purpose of this PR is to simplify and break down the process of
replacing web3-stream-provider
([issue](#28774)),
which uses the legacy sendAsync method and is not EIP-1193 compliant,
with StreamProvider from @metamask/providers, which fully adheres to the
EIP-1193 Specs.
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29620?quickstart=1)

## **Related issues**

Fixes: partially completes
#28774

## **Manual testing steps**

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: MetaMask Bot <[email protected]>
  • Loading branch information
cryptodev-2s and metamaskbot authored Jan 14, 2025
1 parent 140b5d8 commit b52121e
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 513 deletions.
44 changes: 29 additions & 15 deletions app/scripts/lib/ens-ipfs/resolver.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import namehash from 'eth-ens-namehash';
import Eth from '@metamask/ethjs-query';
import EthContract from '@metamask/ethjs-contract';
import contentHash from '@ensdomains/content-hash';
import { Web3Provider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import registryAbi from './contracts/registry';
import resolverAbi from './contracts/resolver';

export default async function resolveEnsToIpfsContentId({ provider, name }) {
const eth = new Eth(provider);
const hash = namehash.hash(name);
const contract = new EthContract(eth);

// lookup registry
const chainId = Number.parseInt(await eth.net_version(), 10);
const chainId = Number.parseInt(
await provider.request({ method: 'net_version' }),
10,
);
const registryAddress = getRegistryForChainId(chainId);
if (!registryAddress) {
throw new Error(
`EnsIpfsResolver - no known ens-ipfs registry for chainId "${chainId}"`,
);
}
const Registry = contract(registryAbi).at(registryAddress);
const web3Provider = new Web3Provider(provider);
const registryContract = new Contract(
registryAddress,
registryAbi,
web3Provider,
);
// lookup resolver
const resolverLookupResult = await Registry.resolver(hash);
const resolverAddress = resolverLookupResult[0];
const resolverAddress = await registryContract.resolver(hash);
if (hexValueIsEmpty(resolverAddress)) {
throw new Error(`EnsIpfsResolver - no resolver found for name "${name}"`);
}
const Resolver = contract(resolverAbi).at(resolverAddress);
const resolverContract = new Contract(
resolverAddress,
resolverAbi,
web3Provider,
);

const isEIP1577Compliant = await Resolver.supportsInterface('0xbc1c58d1');
const isLegacyResolver = await Resolver.supportsInterface('0xd8389dc5');
if (isEIP1577Compliant[0]) {
const contentLookupResult = await Resolver.contenthash(hash);
const isEIP1577Compliant = await resolverContract.supportsInterface(
'0xbc1c58d1',
);
const isLegacyResolver = await resolverContract.supportsInterface(
'0xd8389dc5',
);
if (isEIP1577Compliant) {
const contentLookupResult = await resolverContract.contenthash(hash);
const rawContentHash = contentLookupResult[0];
let decodedContentHash = contentHash.decode(rawContentHash);
const type = contentHash.getCodec(rawContentHash);
Expand All @@ -41,9 +55,9 @@ export default async function resolveEnsToIpfsContentId({ provider, name }) {

return { type, hash: decodedContentHash };
}
if (isLegacyResolver[0]) {
if (isLegacyResolver) {
// lookup content id
const contentLookupResult = await Resolver.content(hash);
const contentLookupResult = await resolverContract.content(hash);
const content = contentLookupResult[0];
if (hexValueIsEmpty(content)) {
throw new Error(
Expand Down
5 changes: 2 additions & 3 deletions app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EthQuery, { Provider } from '@metamask/eth-query';
import type { Provider } from '@metamask/network-controller';
import { FetchGasFeeEstimateOptions } from '@metamask/gas-fee-controller';
import { BigNumber } from 'bignumber.js';
import { isHexString } from 'ethereumjs-util';
Expand Down Expand Up @@ -799,15 +799,14 @@ async function buildEventFragmentProperties({
id,
userFeeLevel,
} = transactionMeta;
const query = new EthQuery(transactionMetricsRequest.provider);
const source = referrer === ORIGIN_METAMASK ? 'user' : 'dapp';

const gasFeeSelected =
userFeeLevel === 'dappSuggested' ? 'dapp_proposed' : userFeeLevel;

const { assetType, tokenStandard } = await determineTransactionAssetType(
transactionMeta,
query,
transactionMetricsRequest.provider,
transactionMetricsRequest.getTokenStandardAndDetails,
);

Expand Down
18 changes: 7 additions & 11 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import LatticeKeyring from 'eth-lattice-keyring';
import { rawChainData } from 'eth-chainlist';
import { MetaMaskKeyring as QRHardwareKeyring } from '@keystonehq/metamask-airgapped-keyring';
import EthQuery from '@metamask/eth-query';
import EthJSQuery from '@metamask/ethjs-query';
import { nanoid } from 'nanoid';
import { captureException } from '@sentry/browser';
import { AddressBookController } from '@metamask/address-book-controller';
Expand Down Expand Up @@ -5406,16 +5405,13 @@ export default class MetamaskController extends EventEmitter {

async estimateGas(estimateGasParams) {
return new Promise((resolve, reject) => {
return new EthJSQuery(this.provider).estimateGas(
estimateGasParams,
(err, res) => {
if (err) {
return reject(err);
}

return resolve(res.toString(16));
},
);
this.provider
.request({
method: 'eth_estimateGas',
params: [estimateGasParams],
})
.then((result) => resolve(result.toString(16)))
.catch((err) => reject(err));
});
}

Expand Down
2 changes: 0 additions & 2 deletions app/scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'react-devtools';
import PortStream from 'extension-port-stream';
import browser from 'webextension-polyfill';

import Eth from '@metamask/ethjs';
import EthQuery from '@metamask/eth-query';
import StreamProvider from 'web3-stream-provider';
import log from 'loglevel';
Expand Down Expand Up @@ -363,7 +362,6 @@ function setupWeb3Connection(connectionStream) {
providerStream.on('error', console.error.bind(console));
global.ethereumProvider = providerStream;
global.ethQuery = new EthQuery(providerStream);
global.eth = new Eth(providerStream);
}

/**
Expand Down
107 changes: 39 additions & 68 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
"ethers>@ethersproject/keccak256": {
"packages": {
"@ethersproject/bytes": true,
"@metamask/ethjs>js-sha3": true
"eth-ens-namehash>js-sha3": true
}
},
"ethers>@ethersproject/logger": {
Expand Down Expand Up @@ -971,7 +971,7 @@
},
"packages": {
"@ethereumjs/tx>@ethereumjs/util": true,
"@metamask/ethjs>@metamask/ethjs-unit": true,
"@metamask/controller-utils>@metamask/ethjs-unit": true,
"@metamask/utils": true,
"@metamask/controller-utils>@spruceid/siwe-parser": true,
"bn.js": true,
Expand Down Expand Up @@ -1211,8 +1211,8 @@
"packages": {
"@babel/runtime": true,
"@metamask/eth-token-tracker>@metamask/eth-block-tracker": true,
"@metamask/ethjs-contract": true,
"@metamask/ethjs-query": true,
"eth-method-registry>@metamask/ethjs-contract": true,
"eth-method-registry>@metamask/ethjs-query": true,
"@metamask/safe-event-emitter": true,
"bn.js": true,
"@metamask/eth-token-tracker>deep-equal": true,
Expand All @@ -1239,80 +1239,56 @@
"URL": true
}
},
"@metamask/ethjs": {
"globals": {
"clearInterval": true,
"setInterval": true
},
"packages": {
"@metamask/ethjs-contract": true,
"@metamask/ethjs>@metamask/ethjs-filter": true,
"@metamask/ethjs>@metamask/ethjs-provider-http": true,
"@metamask/ethjs-query": true,
"@metamask/ethjs>@metamask/ethjs-unit": true,
"@metamask/ethjs>@metamask/ethjs-util": true,
"@metamask/ethjs>@metamask/number-to-bn": true,
"bn.js": true,
"browserify>buffer": true,
"@metamask/ethjs>ethjs-abi": true,
"@metamask/ethjs>js-sha3": true
}
},
"@metamask/ethjs-contract": {
"eth-method-registry>@metamask/ethjs-contract": {
"packages": {
"@babel/runtime": true,
"@metamask/ethjs>@metamask/ethjs-filter": true,
"@metamask/ethjs>@metamask/ethjs-util": true,
"@metamask/ethjs>ethjs-abi": true,
"@metamask/ethjs>js-sha3": true,
"eth-method-registry>@metamask/ethjs-contract>@metamask/ethjs-filter": true,
"eth-method-registry>@metamask/ethjs-contract>@metamask/ethjs-util": true,
"eth-method-registry>@metamask/ethjs-contract>ethjs-abi": true,
"eth-ens-namehash>js-sha3": true,
"promise-to-callback": true
}
},
"@metamask/ethjs>@metamask/ethjs-filter": {
"eth-method-registry>@metamask/ethjs-contract>@metamask/ethjs-filter": {
"globals": {
"clearInterval": true,
"setInterval": true
}
},
"@metamask/ethjs-query>@metamask/ethjs-format": {
"packages": {
"@metamask/ethjs>@metamask/ethjs-util": true,
"@metamask/ethjs>@metamask/number-to-bn": true,
"@metamask/ethjs-query>@metamask/ethjs-format>ethjs-schema": true,
"@metamask/ethjs>@metamask/ethjs-util>strip-hex-prefix": true
}
},
"@metamask/ethjs>@metamask/ethjs-provider-http": {
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format": {
"packages": {
"@metamask/ethjs>@metamask/ethjs-provider-http>xhr2": true
"eth-method-registry>@metamask/ethjs-contract>@metamask/ethjs-util": true,
"@metamask/controller-utils>@metamask/ethjs-unit>@metamask/number-to-bn": true,
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>ethjs-schema": true,
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>strip-hex-prefix": true
}
},
"@metamask/ethjs-query": {
"eth-method-registry>@metamask/ethjs-query": {
"globals": {
"console": true
},
"packages": {
"@metamask/ethjs-query>@metamask/ethjs-format": true,
"@metamask/ethjs-query>@metamask/ethjs-rpc": true,
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format": true,
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-rpc": true,
"promise-to-callback": true
}
},
"@metamask/ethjs-query>@metamask/ethjs-rpc": {
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-rpc": {
"packages": {
"promise-to-callback": true
}
},
"@metamask/ethjs>@metamask/ethjs-unit": {
"@metamask/controller-utils>@metamask/ethjs-unit": {
"packages": {
"@metamask/ethjs>@metamask/number-to-bn": true,
"@metamask/controller-utils>@metamask/ethjs-unit>@metamask/number-to-bn": true,
"bn.js": true
}
},
"@metamask/ethjs>@metamask/ethjs-util": {
"eth-method-registry>@metamask/ethjs-contract>@metamask/ethjs-util": {
"packages": {
"browserify>buffer": true,
"@metamask/ethjs>@metamask/ethjs-util>is-hex-prefixed": true,
"@metamask/ethjs>@metamask/ethjs-util>strip-hex-prefix": true
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>is-hex-prefixed": true,
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>strip-hex-prefix": true
}
},
"@metamask/gas-fee-controller": {
Expand Down Expand Up @@ -1512,10 +1488,10 @@
"uuid": true
}
},
"@metamask/ethjs>@metamask/number-to-bn": {
"@metamask/controller-utils>@metamask/ethjs-unit>@metamask/number-to-bn": {
"packages": {
"bn.js": true,
"@metamask/ethjs>@metamask/ethjs-util>strip-hex-prefix": true
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>strip-hex-prefix": true
}
},
"@metamask/object-multiplex": {
Expand Down Expand Up @@ -3634,7 +3610,7 @@
"packages": {
"bn.js": true,
"buffer": true,
"@metamask/ethjs>js-sha3": true
"eth-ens-namehash>js-sha3": true
}
},
"eth-ens-namehash": {
Expand All @@ -3644,7 +3620,7 @@
"packages": {
"browserify>buffer": true,
"eth-ens-namehash>idna-uts46-hx": true,
"@metamask/ethjs>js-sha3": true
"eth-ens-namehash>js-sha3": true
}
},
"eth-lattice-keyring": {
Expand All @@ -3669,8 +3645,8 @@
},
"eth-method-registry": {
"packages": {
"@metamask/ethjs-contract": true,
"@metamask/ethjs-query": true
"eth-method-registry>@metamask/ethjs-contract": true,
"eth-method-registry>@metamask/ethjs-query": true
}
},
"@ethereumjs/tx>ethereum-cryptography": {
Expand Down Expand Up @@ -3821,12 +3797,12 @@
"ethers>@ethersproject/wordlists": true
}
},
"@metamask/ethjs>ethjs-abi": {
"eth-method-registry>@metamask/ethjs-contract>ethjs-abi": {
"packages": {
"bn.js": true,
"browserify>buffer": true,
"@metamask/ethjs>js-sha3": true,
"@metamask/ethjs>ethjs-abi>number-to-bn": true
"eth-ens-namehash>js-sha3": true,
"eth-method-registry>@metamask/ethjs-contract>ethjs-abi>number-to-bn": true
}
},
"webpack>events": {
Expand Down Expand Up @@ -3952,7 +3928,7 @@
"eth-lattice-keyring>gridplus-sdk>elliptic": true,
"eth-lattice-keyring>gridplus-sdk>eth-eip712-util-browser": true,
"ethers>@ethersproject/sha2>hash.js": true,
"@metamask/ethjs>js-sha3": true,
"eth-ens-namehash>js-sha3": true,
"lodash": true,
"eth-lattice-keyring>rlp": true,
"ganache>secp256k1": true,
Expand Down Expand Up @@ -4173,7 +4149,7 @@
"browserify>buffer": true
}
},
"@metamask/ethjs>js-sha3": {
"eth-ens-namehash>js-sha3": {
"globals": {
"define": true
},
Expand Down Expand Up @@ -4515,10 +4491,10 @@
"fetch": true
}
},
"@metamask/ethjs>ethjs-abi>number-to-bn": {
"eth-method-registry>@metamask/ethjs-contract>ethjs-abi>number-to-bn": {
"packages": {
"bn.js": true,
"@metamask/ethjs>@metamask/ethjs-util>strip-hex-prefix": true
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>strip-hex-prefix": true
}
},
"string.prototype.matchall>es-abstract>object-inspect": {
Expand Down Expand Up @@ -5352,9 +5328,9 @@
"koa>content-disposition>safe-buffer": true
}
},
"@metamask/ethjs>@metamask/ethjs-util>strip-hex-prefix": {
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>strip-hex-prefix": {
"packages": {
"@metamask/ethjs>@metamask/ethjs-util>is-hex-prefixed": true
"eth-method-registry>@metamask/ethjs-query>@metamask/ethjs-format>is-hex-prefixed": true
}
},
"react-markdown>style-to-object": {
Expand Down Expand Up @@ -5643,11 +5619,6 @@
"string.prototype.matchall>es-abstract>gopd": true,
"koa>is-generator-function>has-tostringtag": true
}
},
"@metamask/ethjs>@metamask/ethjs-provider-http>xhr2": {
"globals": {
"XMLHttpRequest": true
}
}
}
}
Loading

0 comments on commit b52121e

Please sign in to comment.