Skip to content

Commit

Permalink
Merge branch '4.x' into ok/7017-Research-Snyk-Advisor-score-to-make-i…
Browse files Browse the repository at this point in the history
…mprovements
  • Loading branch information
avkos committed May 27, 2024
2 parents 98ed885 + 22c07ad commit b2a8767
Show file tree
Hide file tree
Showing 43 changed files with 2,556 additions and 26 deletions.
11 changes: 10 additions & 1 deletion packages/web3-eth-ens/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ Documentation:

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)

## [Unreleased]
## [Unreleased]

### Added

- `getText` now supports first param Address
- `getName` has optional second param checkInterfaceSupport

### Fixed

- `getName` reverse resolution
11 changes: 7 additions & 4 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
TransactionReceipt,
Web3NetAPI,
} from 'web3-types';
import { isAddress } from 'web3-validator';
import { PublicResolverAbi } from './abi/ens/PublicResolver.js';
import { networkIds, registryAddresses } from './config.js';
import { Registry } from './registry.js';
Expand Down Expand Up @@ -174,17 +175,19 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
* @param key - The key to resolve https://github.com/ethereum/ercs/blob/master/ERCS/erc-634.md#global-keys
* @returns - The value content stored in the resolver for the specified key
*/
public async getText(ENSName: string, key: string): Promise<string> {
return this._resolver.getText(ENSName, key);
public async getText(ENSNameOrAddr: string | Address, key: string): Promise<string> {
if(isAddress(ENSNameOrAddr))
return this._resolver.getText(await(this._resolver.getName(ENSNameOrAddr,false)), key);
return this._resolver.getText(ENSNameOrAddr, key);
}

/**
* Resolves the name of an ENS node.
* @param ENSName - The node to resolve
* @returns - The name
*/
public async getName(ENSName: string): Promise<string> {
return this._resolver.getName(ENSName);
public async getName(ENSName: string, checkInterfaceSupport = true): Promise<string> {
return this._resolver.getName(ENSName, checkInterfaceSupport);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ export class Resolver {
}

public async getName(
address: string
address: string,
checkInterfaceSupport = true
) {
const resolverContract = await this.getResolverContractAdapter(address);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.name);
const reverseName = `${address.toLowerCase().substring(2)}.addr.reverse`;

const resolverContract = await this.getResolverContractAdapter(reverseName);

if(checkInterfaceSupport)
await this.checkInterfaceSupport(resolverContract, methodsInInterface.name);

return resolverContract.methods
.name(namehash(address)).call()
.name(namehash(reverseName)).call()
}
}
7 changes: 5 additions & 2 deletions packages/web3-eth-ens/test/unit/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ describe('resolver', () => {

describe('name', () => {
it('getName', async () => {
const address = "0x314159265dd8dbb310642f98f50c066173c1259b";

const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
Expand All @@ -255,12 +257,13 @@ describe('resolver', () => {
});
});

await resolver.getName(ENS_NAME);
await resolver.getName(address);
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.name],
);

expect(nameMock).toHaveBeenCalledWith(namehash(ENS_NAME));
const reverseName = `${address.toLowerCase().substring(2)}.addr.reverse`;
expect(nameMock).toHaveBeenCalledWith(namehash(reverseName));
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Web3RequestManager } from 'web3-core';
import { ethRpcMethods } from '../../../src/index';

jest.mock('web3-validator');

describe('getUncleByBlockNumberAndIndex', () => {
let requestManagerSendSpy: jest.Mock;
let requestManager: Web3RequestManager;

beforeAll(() => {
requestManager = new Web3RequestManager('http://127.0.0.1:8545');
requestManagerSendSpy = jest.fn();
requestManager.send = requestManagerSendSpy;
});

it('should call requestManager.send with eth_getUncleByBlockNumberAndIndex method', async () => {
await ethRpcMethods.getUncleByBlockNumberAndIndex(requestManager, 0, '1' );
expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'eth_getUncleByBlockNumberAndIndex',
params: [0,'1'],
});
},
);

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Web3RequestManager } from 'web3-core';
import { EthPersonalAPI } from 'web3-types';
import { personalRpcMethods } from '../../../src/index';

describe('Eth Personal', () => {
let requestManagerSendSpy: jest.Mock;
let requestManager: Web3RequestManager<EthPersonalAPI>;

beforeAll(() => {
requestManager = new Web3RequestManager<EthPersonalAPI>('http://127.0.0.1:8545');
requestManagerSendSpy = jest.fn();
requestManager.send = requestManagerSendSpy;
});

it('should call requestManager.send with personal_listAccounts method', async () => {
await personalRpcMethods.getAccounts(requestManager);
expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_listAccounts',
params: [],
});
});

it('should call requestManager.send with personal_newAccount method', async () => {
const pass = "ABC123";
await personalRpcMethods.newAccount(requestManager, pass);
expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_newAccount',
params: [pass],
});
});

it('should call requestManager.send with personal_unlockAccount method', async () => {
const pass = "ABC123";
const address = "0x4106486FB42F3Abf07CC07ef5DEE38f60319e789";
const duration = 100;
await personalRpcMethods.unlockAccount(requestManager, address, pass, duration);

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_unlockAccount',
params: [address, pass, duration],
});
});

it('should call requestManager.send with personal_lockAccount method', async () => {
const address = "0x4106486FB42F3Abf07CC07ef5DEE38f60319e789";

await personalRpcMethods.lockAccount(requestManager, address );

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_lockAccount',
params: [address],
});
});

it('should call requestManager.send with personal_importRawKey method', async () => {
const passPhrase = "123456";
const keyData = "abe40cb08850da918ee951b237fa87946499b2d8643e4aa12b0610b050c731f6";
await personalRpcMethods.importRawKey(requestManager, keyData, passPhrase );

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_importRawKey',
params: [keyData,passPhrase],
});
});

it('should call requestManager.send with personal_sendTransaction method', async () => {
const passPhrase = "123456";
const tx = {
from: "0x0d4aa485ecbc499c70860feb7e5aaeaf5fd8172e",
gasPrice: "20000",
gas: "21000",
to: "0x4106486FB42F3Abf07CC07ef5DEE38f60319e789",
value: "1000000",
data: "",
nonce: 0,
};
await personalRpcMethods.sendTransaction(requestManager, tx, passPhrase );

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_sendTransaction',
params: [tx,passPhrase],
});
});

it('should call requestManager.send with personal_signTransaction method', async () => {
const passPhrase = "123456";
const tx = {
from: "0x0d4aa485ecbc499c70860feb7e5aaeaf5fd8172e",
gasPrice: "20000",
gas: "21000",
to: "0x4106486FB42F3Abf07CC07ef5DEE38f60319e789",
value: "1000000",
data: "",
nonce: 0,
};
await personalRpcMethods.signTransaction(requestManager, tx, passPhrase );

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_signTransaction',
params: [tx,passPhrase],
});
});

it('should call requestManager.send with personal_sign method', async () => {
const data = "Hello world";
const address = "0x0D4Aa485ECbC499c70860fEb7e5AaeAf5fd8172E";
const pass = "123456";

await personalRpcMethods.sign(requestManager, data,address,pass);

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_sign',
params: [ data,address,pass],
});
});

it('should call requestManager.send with personal_ecRecover method', async () => {
const data = "Hello world";
const signature = "0x5d21d01b3198ac34d0585a9d76c4d1c8123e5e06746c8962318a1c08ffb207596e6fce4a6f377b7c0fc98c5f646cd73438c80e8a1a95cbec55a84c2889dca0301b";

await personalRpcMethods.ecRecover(requestManager,data, signature);

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'personal_ecRecover',
params: [ data, signature],
});
});
});
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/.eslintignore
7 changes: 7 additions & 0 deletions packages/web3-rpc-providers/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: '../../.eslintrc.js',
parserOptions: {
project: './tsconfig.esm.json',
tsconfigRootDir: __dirname,
},
};
Empty file.
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/.npmignore
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/.npmrc
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/.prettierignore
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/.prettierrc.json
38 changes: 38 additions & 0 deletions packages/web3-rpc-providers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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).

<!-- EXAMPLE
## [1.0.0]
### Added
- I've added feature XY (#1000)
### Changed
- I've cleaned up XY (#1000)
### Deprecated
- I've deprecated XY (#1000)
### Removed
- I've removed XY (#1000)
### Fixed
- I've fixed XY (#1000)
### Security
- I've improved the security in XY (#1000)
-->

## [Unreleased]
Loading

0 comments on commit b2a8767

Please sign in to comment.