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

Ethers audit #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9,418 changes: 4,806 additions & 4,612 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 17 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
{
"name": "@maticnetwork/maticjs-ethers",
"version": "1.0.3",
"version": "1.0.4",
"description": "ethers plugin for matic.js",
"main": "dist/npm.export.js",
"types": "dist/ts/index.d.ts",
"scripts": {
"build": "npm run build:dev",
"build:test": "npm run build:pack && npm run test",
"build:link": "npm run build && npm link",
"build:webpack": "npm run lint && webpack --config webpack/webpack.node.config.js",
"build:pack": "npm run build:dev && npm pack",
"build:dev": "cross-env NODE_ENV=development npm run build:webpack",
"build:prod": "cross-env NODE_ENV=production npm run build:webpack",
"clean": "rimraf dist",
"build:link": "webpack && npm link",
"build:dev": "webpack",
"build:prod": "webpack --env build",
"deploy": "npm run build:dev && npm run build:prod",
"prepublishOnly": "npm run deploy",
"lint": "tslint src/**/*.ts",
"lint:fix": "tslint src/**/*.ts --fix",
"debug": "npm run build:pack && cd test && npm run install:lib:debug",
"test": "cd test && npm run install:lib:test"
"debug": "webpack && cd test && npm run link:lib:debug"
},
"repository": {
"type": "git",
Expand All @@ -30,20 +26,21 @@
},
"homepage": "https://github.com/maticnetwork/maticjs-ethers#readme",
"dependencies": {
"ethers": "^5.5.1"
"bignumber.js": "^9.1.2",
"ethers": "^6.13.2"
},
"peerDependencies": {
"@maticnetwork/maticjs": "^3.2.5"
"@maticnetwork/maticjs": "^3.9.2"
},
"devDependencies": {
"@maticnetwork/maticjs": "^3.2.5",
"copy-webpack-plugin": "^6.1.1",
"@maticnetwork/maticjs": "^3.9.2",
"copy-webpack-plugin": "^12.0.2",
"cross-env": "^7.0.3",
"ts-loader": "^7.0.1",
"ts-loader": "^8.0.0",
"tslint": "^6.1.3",
"typescript": "^3.8.3",
"webpack": "^4.43.0",
"webpack-cli": "^4.2.0",
"webpack-merge": "^4.2.2"
"typescript": "^4.9.5",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"yargs": "^17.7.2"
}
}
}
16 changes: 11 additions & 5 deletions src/ethers/ethjs_method.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { BaseContractMethod, ITransactionRequestConfig, ITransactionWriteResult, Logger } from "@maticnetwork/maticjs";
import { BigNumber, Contract, PopulatedTransaction } from "ethers";
import { BaseContractMethod, ITransactionRequestConfig, Logger } from "@maticnetwork/maticjs";
import { Contract } from "ethers";
import { TransactionWriteResult } from "../helpers";
import BigNumber from "bignumber.js";

export class ContractMethod extends BaseContractMethod {
constructor(logger: Logger, private contract_: Contract, private methodName, private args) {
super(logger);
}

get address() {
return this.contract_.address;
return this.contract_.target as string;
}

// This function is only available in ethers implementation. and not in maticjs-web3.
getAddress() {
return this.contract_.getAddress();
}

toBigNumber(value) {
return value ? BigNumber.from(value) : value;
return value ? new BigNumber(value) : value;
}

private toConfig_(config: ITransactionRequestConfig = {}) {
Expand All @@ -30,7 +36,7 @@ export class ContractMethod extends BaseContractMethod {
maxFeePerGas: toBigNumber(config.maxFeePerGas),
maxPriorityFeePerGas: toBigNumber(config.maxPriorityFeePerGas),

} as PopulatedTransaction;
};
}

encodeABI() {
Expand Down
87 changes: 52 additions & 35 deletions src/ethers/web3_client.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import { providers, Wallet, utils, Contract, ethers, BigNumber } from "ethers";
import {
Wallet, Contract, ethers,
JsonRpcProvider,
JsonRpcSigner,
BrowserProvider,
AbiCoder
} from "ethers";
import BigNumber from "bignumber.js";
import { EthJsContract } from "./ethjs_contract";
import { doNothing } from "../helpers";
import { BaseWeb3Client, IBlockWithTransaction, IError, IJsonRpcRequestPayload, IJsonRpcResponse, ITransactionRequestConfig, ITransactionWriteResult } from "@maticnetwork/maticjs";
import {
BaseWeb3Client, IError, IJsonRpcRequestPayload, IJsonRpcResponse,
ITransactionRequestConfig, ITransactionWriteResult
} from "@maticnetwork/maticjs";
import { ethBlockToMaticBlock, ethReceiptToMaticReceipt, ethTxToMaticTx } from "../utils";

type ETHER_PROVIDER = providers.JsonRpcProvider;
type ETHER_SIGNER = providers.JsonRpcSigner;
type ETHER_PROVIDER = JsonRpcProvider;
type ETHER_SIGNER = JsonRpcSigner;
type WEB3_PROVIDER = BrowserProvider;

export class EtherWeb3Client extends BaseWeb3Client {
name = 'ETHER';
provider: ETHER_PROVIDER;
provider: ETHER_PROVIDER | WEB3_PROVIDER;
signer: ETHER_SIGNER;

constructor(provider: ETHER_PROVIDER | Wallet, logger) {
super(logger);
if ((provider as ETHER_PROVIDER)._isProvider) {
this.provider = provider as ETHER_PROVIDER;
this.signer = this.provider.getSigner();
if (!this.signer || !this.signer._address) {
this.signer = (provider as any);
}
}
else {
this.signer = (provider as any);
this.provider = ((provider as Wallet).provider) as any;

if (provider instanceof BrowserProvider) {
this.provider = provider;
provider.getSigner().then((signer) => {
this.signer = signer;
});
} else if (provider instanceof JsonRpcProvider) {
this.provider = provider;
this.signer = provider as any;
} else {
this.signer = provider as any;
this.provider = provider.provider || provider as any;
}
}

Expand All @@ -40,12 +53,14 @@ export class EtherWeb3Client extends BaseWeb3Client {
const provider = this.provider;
return provider.send(
'eth_getBlockByNumber',
[utils.hexValue(blockHashOrBlockNumber), true]
[ethers.toQuantity(blockHashOrBlockNumber), true]
).then(rawBlock => {
const block = provider.formatter.blockWithTransactions(rawBlock);
block['stateRoot'] = provider.formatter.hash(rawBlock.stateRoot);
block['transactionsRoot'] = provider.formatter.hash(rawBlock.transactionsRoot);
block['receiptsRoot'] = provider.formatter.hash(rawBlock.receiptsRoot);
const block = {
...rawBlock,
stateRoot: ethers.zeroPadValue(rawBlock.stateRoot, 32),
transactionsRoot: ethers.zeroPadValue(rawBlock.transactionsRoot, 32),
receiptsRoot: ethers.zeroPadValue(rawBlock.receiptsRoot, 32),
};

block.transactions = block.transactions.map(tx => {
return ethTxToMaticTx(tx as any);
Expand All @@ -57,7 +72,9 @@ export class EtherWeb3Client extends BaseWeb3Client {


getChainId() {
return this.signer.getChainId();
return this.signer.provider.getNetwork().then(network => {
return new BigNumber(network.chainId.toString()).toNumber();
});
}

getBalance(address) {
Expand Down Expand Up @@ -102,26 +119,26 @@ export class EtherWeb3Client extends BaseWeb3Client {
}

getGasPrice() {
return this.provider.getGasPrice().then(result => {
return result.toString();
return this.provider.getFeeData().then(result => {
return result.gasPrice.toString();
});
}

estimateGas(config) {
return this.provider.estimateGas(
this.toEthTxConfig_(config)
).then(value => {
return value.toNumber();
return new BigNumber(value.toString()).toNumber();
});
}

encodeParameters(params: any[], types: any[]) {
return utils.defaultAbiCoder.encode(types, params);
return AbiCoder.defaultAbiCoder().encode(types, params);
}

toHex(value, returnType) {

if (utils.isAddress(value)) {
if (ethers.isAddress(value)) {
return returnType ? 'address' : '0x' + value.toLowerCase().replace(/^0x/i, '');
}

Expand All @@ -134,44 +151,44 @@ export class EtherWeb3Client extends BaseWeb3Client {
}

if (typeof value === 'object' && !!value && !BigNumber.isBigNumber(value)) {
return returnType ? 'string' : utils.hexlify(JSON.stringify(value));
return returnType ? 'string' : ethers.toBeHex(JSON.stringify(value));
}

// if its a negative number, pass it through numberToHex
if (typeof value === 'string') {
if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) {
return returnType ? 'int256' : utils.hexlify(value);
return returnType ? 'int256' : ethers.toBeHex(value);
} else if (value.indexOf('0x') === 0 || value.indexOf('0X') === 0) {
return returnType ? 'bytes' : value;
} else if (!isFinite(value as any)) {
return returnType ? 'string' : utils.hexlify(value);
return returnType ? 'string' : ethers.toBeHex(value);
}
}

return returnType ? (value < 0 ? 'int256' : 'uint256') : utils.hexlify(value);
return returnType ? (value < 0 ? 'int256' : 'uint256') : ethers.toBeHex(value);
}

hexToNumber(value) {
return BigNumber.from(value).toNumber();
return new BigNumber(value).toNumber();
}

hexToNumberString(value) {
return BigNumber.from(value).toString();
return new BigNumber(value).toString();
}

signTypedData(signer, typedData) {
const { domain, types, message: value } = typedData;
if (types.EIP712Domain) {
delete types.EIP712Domain;
}
return this.signer._signTypedData(domain, types, value);
return this.signer.signTypedData(domain, types, value);
}

etheriumSha3(...value) {
const types = value.map(val => {
return this.toHex(val, true);
});
return utils.solidityKeccak256(types, value);
return ethers.solidityPackedKeccak256(types, value);
}

sendRPCRequest(request: IJsonRpcRequestPayload) {
Expand Down Expand Up @@ -236,7 +253,7 @@ export class EtherWeb3Client extends BaseWeb3Client {
}

decodeParameters(hexString, types: any[]) {
return utils.defaultAbiCoder.decode(types, hexString) as any;
return AbiCoder.defaultAbiCoder().decode(types, hexString) as any;
}

}
6 changes: 3 additions & 3 deletions src/helpers/transaction_write_result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ITransactionWriteResult } from "@maticnetwork/maticjs";
import { providers } from "ethers";
import { TransactionReceipt, TransactionResponse } from "ethers";
import { ethReceiptToMaticReceipt } from "../utils";
import { doNothing } from "./do_nothing";

Expand All @@ -11,15 +11,15 @@ export class TransactionWriteResult implements ITransactionWriteResult {
onTransactionReceipt: Function;

getReceipt() {
return new Promise<providers.TransactionReceipt>((res, rej) => {
return new Promise<TransactionReceipt>((res, rej) => {
this.onTransactionReceipt = res;
this.onTransactionError = rej;
}).then(receipt => {
return ethReceiptToMaticReceipt(receipt);
});
}

constructor(private promise: Promise<providers.TransactionResponse>) {
constructor(private promise: Promise<TransactionResponse>) {
promise.then(response => {
this.onTransactionHash(response.hash);
setTimeout(() => {
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ITransactionRequestConfig, IPlugin, ITransactionResult, Converter, utils } from "@maticnetwork/maticjs";
import { BigNumber, ethers } from "ethers";
import { IPlugin } from "@maticnetwork/maticjs";
import { EtherWeb3Client } from "./ethers";
import { MaticBigNumber } from "./utils";

import BigNumber from "bignumber.js";

export class Web3ClientPlugin implements IPlugin {
setup(matic) {
Expand All @@ -17,4 +16,4 @@ export class Web3ClientPlugin implements IPlugin {
export * from "./ethers";

/* tslint:disable-next-line */
export default Web3ClientPlugin;
export default Web3ClientPlugin;
16 changes: 9 additions & 7 deletions src/utils/eth_block_to_matic_block.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { IBlock } from "@maticnetwork/maticjs";
import { providers } from "ethers";
import { Block } from "ethers";
import BigNumber from "bignumber.js";

export const ethBlockToMaticBlock = (block: providers.Block) => {
block.gasUsed = block.gasUsed.toNumber() as any;
block.gasLimit = block.gasLimit.toNumber() as any;
if ((block as any).baseFeePerGas) {
(block as any).baseFeePerGas = block.baseFeePerGas.toHexString();
export const ethBlockToMaticBlock = (block: Block) => {
const newBlock = block.toJSON();
newBlock.gasUsed = new BigNumber(newBlock.gasUsed.toString()).toNumber() as any;
newBlock.gasLimit = new BigNumber(newBlock.gasLimit.toString()).toNumber() as any;
if ((newBlock as any).baseFeePerGas) {
(newBlock as any).baseFeePerGas = new BigNumber(newBlock.baseFeePerGas.toString()).toNumber() as any;
}
return block as any as IBlock;
return newBlock as IBlock;
};
11 changes: 5 additions & 6 deletions src/utils/eth_receipt_to_matic_receipt.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ITransactionReceipt } from "@maticnetwork/maticjs";
import { providers } from "ethers";

export const ethReceiptToMaticReceipt = (receipt: providers.TransactionReceipt) => {
import { TransactionReceipt } from "ethers";
import BigNumber from "bignumber.js";

export const ethReceiptToMaticReceipt = (receipt: TransactionReceipt) => {
const maticReceipt: ITransactionReceipt = receipt as any;

maticReceipt.gasUsed = receipt.gasUsed.toNumber();
maticReceipt.cumulativeGasUsed = receipt.cumulativeGasUsed.toNumber();
maticReceipt.gasUsed = new BigNumber(receipt.gasUsed.toString()).toNumber();
maticReceipt.cumulativeGasUsed = new BigNumber(receipt.cumulativeGasUsed.toString()).toNumber();

return maticReceipt;
};
11 changes: 6 additions & 5 deletions src/utils/eth_tx_to_matic_tx.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IBlock, ITransactionData, ITransactionResult } from "@maticnetwork/maticjs";
import { providers } from "ethers";
import { ITransactionData } from "@maticnetwork/maticjs";
import BigNumber from "bignumber.js";
import { TransactionResponse } from "ethers";

export const ethTxToMaticTx = (tx: providers.TransactionResponse) => {
export const ethTxToMaticTx = (tx: TransactionResponse) => {
const maticTx: ITransactionData = tx as any;
maticTx.gasPrice = tx.gasPrice.toString();
maticTx.gas = tx.gasLimit.toNumber();
(maticTx as any).gasLimit = tx.gasLimit.toNumber();
maticTx.gas = new BigNumber(tx.gasLimit.toString()).toNumber();
(maticTx as any).gasLimit = new BigNumber(tx.gasLimit.toString()).toNumber();
maticTx.value = tx.value.toString();
maticTx.transactionHash = tx.hash;

Expand Down
Loading
Loading