Skip to content

Commit

Permalink
Web3 Eth Personal (#4781)
Browse files Browse the repository at this point in the history
* Add @ethereumjs/tx dependency

* Update Web3BaseProvider to SupportedProviders for Web3Eth constructor

* WIP eth-tx

* Add support for undefined values for convertToValidType

* Remove unused Web3BaseProvider

* WIP eth-tx utils

* Export privateKeyToAddress

* Add web3-eth-accounts dependency

* WIP web3-eth-tx util methods

* Replace inline errors with error constructors

* Change types for transaction r and s properties. Correct hardforks in detectTransactionType

* Init formatTransaction tests and fixture

* Init detectTransactionType and fixture

* Add more descriptive error messages

* Logic fixes for validateTransactionForSigning

* Init validateTransactionForSigning tests and fixtures

* Add esModuleInterop: true to tsconfig

* Small bug fixes and added TODOs

* Add parent describe to detect_transaction_type test

* Add parent describe to format_transaction test

* Add web3-providers-http as dev dependency for tests

* Init populate_transaction tests

* Move types from eth_tx.ts to types.ts

* Remove TODOs

* Add missing ,

* Remove TODO

* Remove TODO

* Fix transaction type import issues

* Update convertToValidType test data for undefined

* Update override method tests

* Update packages/web3-eth/src/eth_tx.ts

Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com>

* Move getBlock to after type check for populateTransaction

* Replace N/A with name of error for error.msg

* Assign formattedTransaction type Transaction<ReturnType>

* convertToValidType now throws error for value === undefined

* NumberType extends Numbers

* Transaction type related changes

* Refactor DesiredType logic

* Convert to deep copy for formatTransaction method

* skip override method test - needs to be refactored. General formatting

* Skip override method test - needs to be refactored. Set input.type > 0x7f

* Refactor formatTransaction

* Add error codes to web3-eth errors

* Refactor validateGate if statements into readable consts

* Update comment

* Add link to error message from 1.x

* Fix bug with is gas consts in validateGas

* Init InvalidConvertibleValueError

* Replace error with InvalidConvertibleValueError

* Update tests for formatting undefined

* Update expected errors for validateGas tests

* No longer default tx.type if undefined

* Refactor detectTransactionType

* Fix type error for return in detectTransactionType

* Init rpc_method_wrappers.ts

* Remove Web3Eth import

* Refactor use of web3Context.defaults

* Restore Formatted transaction types

* Init web3_rpc_method_wrappers tests

* Refactor web3_eth_methods_with_parameters test

* Replace if X === undefined checks with ?

* Un-export consts that aren't used

* Add defaultTransactionType and defaultMaxPriorityFeePerGas

* Update defaults for chain and hardfork to mainnet and london

* Update to use web3Context.default chain and hardfork. Init tests for defaults

* Update test to account for added defaults

* Refactor validateGas to use helper methods

* remove TODO

* Init error TransactionGasMismatchError

* Fix tests and refactor transaction validator helper methods

* Move validation methods to validation.ts

* Add input to Transaction type

* Add @ethereumjs/common dependency

* yarn format

* Remove null for defaultTransactionType

* Add default for defaultTransactionType

* Update default for defaultTransactionType

* Bug fixes, refactors, and init prepareTransactionForSigning and tests

* Remove unused test code

* revert transaction data and value to default to 0x

* Fix failing populate_transaction tests

* Add defaultNetworkId to web3_config

* Add TODO for failing prepare_transaction_for_signing test

* Remove TODO

* Init TransactionDataAndInputError

* Add else if to populateTransaction - data

* Refactor populateTransaction - chainId

* Comment out unused ifs

* Remove populateTransaction - gas

* Remove populateTransaction - hexTxType

* Replace use of ValidReturnTypes[ValidTypes.HexString] with HexString

* Remove toHex import

* Remove | null for Web3ConfigOptions defaultChain and defaultHardfork

* Refactor getEthereumjsTransactionOptions

* Remove no longer needed populateTransaction - gas test

* Update packages/web3-eth/src/validation.ts

* Remove unnecessary rpc method wrappers

* Web3Eth now extends Web3Context instead of instantiating it

* Init getPendingTransactions

* Init requestAccounts

* Add EIP-1102 as a comment for requestAccounts

* Init getChainId

* Init getProof

* Init Web3EthExecutionAPI

* Fix imports for AccountObject in fixtures

* Add formatting to getPendingTransactions. Move formatTransaction to seperate file

* Add TODO to investigate transaction.data

* Add formatting to getChainId response

* Init getNodeInfo

* Revert esModuleInterop change

* Combine networkId and chainId if statements

* yarn format

* Add Partial to type of transaction for eth_sendTransaction

* Init transactionReceiptPollingInterval and transactionConfirmationPollingInterval

* Add TODO and Partial to transaction type for sendTransaction

* WIP sendTransaction and PromiEvent integration

* Add eslint-disable-next-line

* Add eslint-disable-next-line

* Move TransactionEvents

* eslint fixes

* Update sendSignedTransaction to use PromiEvent

* Init signTransaction

* Refactor TransactionCall

* Comment out validation for call

* Init TransactionCall type for web3-eth types

* Remove as BaseTransaction from isTransactionCall

* Implement call for rpc_method_wrappers

* Uncomment sendTransaction, signTransaction, and call

* ✨ Add skeleton for the web3-eth-personal

* 🎨 Add initial structure

* 🎨 Update web3-eth packageto export utility functions

* ✨ Add eth-personal api implementation

* ✅ Add unit tests for web3-eth-personal

* Apply suggestions from code review

Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>

* 🎨 Update the code as per feebdack

Co-authored-by: Wyatt Barnes <me@wyatt.email>
Co-authored-by: jdevcs <86780488+jdevcs@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 4, 2022
1 parent 30b8672 commit 39f8fd5
Show file tree
Hide file tree
Showing 30 changed files with 2,718 additions and 254 deletions.
3 changes: 3 additions & 0 deletions packages/web3-eth-personal/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
jest.config.js
.eslintrc.js
7 changes: 7 additions & 0 deletions packages/web3-eth-personal/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: '../../.eslintrc.js',
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
};
Empty file.
26 changes: 26 additions & 0 deletions packages/web3-eth-personal/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.babelrc
.eslintignore
.eslintrc.json
.eslintrc.js
.gitignore
.lintstagedrc.json
.nycrc
.prettierrc.json
.prettierignore
cypress.json
index.html
Jenkinsfile*
*.log

.nyc_output/
coverage/
benchmark/
.coverage/
cypress/
fixtures/
tmp/
logs/
test/
scripts/
browsertest/
examples/
2 changes: 2 additions & 0 deletions packages/web3-eth-personal/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
message = ":arrow_up: Version %s"
save-exact = false
42 changes: 42 additions & 0 deletions packages/web3-eth-personal/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Files
LICENSE
.gitkeep
mocha.opts
.DS_Store
REVISION

# rc files
.*rc
## ignore files
.*ignore

# Ignore extensions
*.png
*.jpg
*.svg
*.ai
*.sql
*.sh
*.html
*.info
*.xml
*.log
*.proto
*.csv
*.cmd
*.tsbuildinfo

## jest snapshot
*.snap

# project specific paths
coverage/
dist/
tmp/
browsertest.build/
.coverage
.coverage-unit
# Eth2 specs
*.yaml
*.yml
.tool-versions
7 changes: 7 additions & 0 deletions packages/web3-eth-personal/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 100,
"singleQuote": true,
"trailingComma": "all",
"useTabs": true,
"arrowParens": "avoid"
}
36 changes: 36 additions & 0 deletions packages/web3-eth-personal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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)
-->
53 changes: 53 additions & 0 deletions packages/web3-eth-personal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<p align="center">
<img src="assets/logo/web3js.jpg" width="500" alt="web3.js" />
</p>

# web3.js - Web3-Eth-Personal

![ES Version](https://img.shields.io/badge/ES-2020-yellow)
![Node Version](https://img.shields.io/badge/node-14.x-green)
[![NPM Package][npm-image]][npm-url]
[![Dependency Status][deps-image]][deps-url]
[![Dev Dependency Status][deps-dev-image]][deps-dev-url]

This is a sub-package of [web3.js][repo].

`web3-eth-personal` Web3 module to interact with the Ethereum blockchain accounts stored in the node.

###### Get it from the NPM Registry

```bash
yarn add web3-eth-personal
```

## Getting Started

- :writing_hand: If you have questions [submit an issue](https://github.com/ChainSafe/web3.js/issues/new) or join us on [Discord](https://discord.gg/yjyvFRP)
![Discord](https://img.shields.io/discord/593655374469660673.svg?label=Discord&logo=discord)

## Prerequisites

- :gear: [NodeJS](https://nodejs.org/) (LTS/Fermium)
- :toolbox: [Yarn](https://yarnpkg.com/)/[Lerna](https://lerna.js.org/)

## Package.json Scripts

| Script | Description |
| ---------------- | -------------------------------------------------- |
| clean | Uses `rimraf` to remove `dist/` |
| build | Uses `tsc` to build package and dependent packages |
| lint | Uses `eslint` to lint package |
| lint:fix | Uses `eslint` to check and fix any warnings |
| format | Uses `prettier` to format the code |
| test | Uses `jest` to run unit tests |
| test:integration | Uses `jest` to run tests under `/test/integration` |
| test:unit | Uses `jest` to run tests under `/test/unit` |

[docs]: http://web3js.readthedocs.io/en/4.0/
[repo]: https://github.com/ethereum/web3.js
[npm-image]: https://img.shields.io/npm/v/web3-eth-personal.svg
[npm-url]: https://npmjs.org/packages/web3-eth-personal
[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-eth-personal
[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-eth-personal
[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-eth-personal
[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-eth-personal
1,516 changes: 1,516 additions & 0 deletions packages/web3-eth-personal/assets/logo/web3js.ai

Large diffs are not rendered by default.

Binary file added packages/web3-eth-personal/assets/logo/web3js.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions packages/web3-eth-personal/assets/logo/web3js.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions packages/web3-eth-personal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "web3-eth-personal",
"version": "4.0.0-alpha.0",
"description": "Web3 module to interact with the Ethereum blockchain accounts stored in the node.",
"main": "dist/index.js",
"repository": "https://github.com/ChainSafe/web3.js",
"author": "ChainSafe Systems",
"license": "LGPL-3.0",
"files": [
"dist/**/*"
],
"scripts": {
"clean": "rimraf dist",
"prebuild": "rimraf dist",
"build": "tsc --build",
"build:check": "node -e \"require('./dist')\"",
"lint": "eslint --ext .js,.ts .",
"lint:fix": "eslint --fix --ext .js,.ts .",
"format": "prettier --write '**/*'",
"test": "jest --config=./test/unit/jest.config.js",
"test:coverage": "jest --config=./test/unit/jest.config.js --coverage=true --coverage-reporters=text",
"test:ci": "jest --coverage=true --coverage-reporters=json --verbose",
"test:watch": "npm test -- --watch",
"test:unit": "jest --config=./test/unit/jest.config.js",
"test:integration": "jest --config=./test/integration/jest.config.js"
},
"dependencies": {
"web3-common": "1.0.0-alpha.0",
"web3-core": "4.0.0-alpha.0",
"web3-utils": "4.0.0-alpha.1",
"web3-eth": "4.0.0-alpha.1",
"web3-validator": "0.1.0-alpha.0"
},
"devDependencies": {
"@types/jest": "^27.0.3",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"eslint": "^8.3.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-web3-base": "0.1.0",
"eslint-plugin-import": "^2.25.3",
"jest": "^27.3.1",
"jest-extended": "^1.1.0",
"prettier": "^2.4.1",
"ts-jest": "^27.0.7",
"typescript": "^4.5.2"
}
}
14 changes: 14 additions & 0 deletions packages/web3-eth-personal/src/eth_personal_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Address, HexString } from 'web3-utils';
import { Transaction } from 'web3-eth';

export type EthPersonalAPI = {
personal_listAccounts: () => Address[];
personal_newAccount: (password: string) => Address;
personal_unlockAccount: (address: Address, password: string, unlockDuration: number) => boolean;
personal_lockAccount: (address: Address) => boolean;
personal_importRawKey: (keyData: HexString, passphrase: string) => boolean;
personal_sendTransaction: (tx: Transaction, passphrase: string) => HexString;
personal_signTransaction: (tx: Transaction, passphrase: string) => HexString;
personal_sign: (data: HexString, address: Address, passphrase: string) => HexString;
personal_ecRecover: (signedData: HexString, signature: HexString) => Address;
};
45 changes: 45 additions & 0 deletions packages/web3-eth-personal/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Web3Context } from 'web3-core';
import { Transaction } from 'web3-eth';
import { Address, HexString } from 'web3-utils';
import { EthPersonalAPI } from './eth_personal_api';
import * as rpcWrappers from './rpc_method_wrappers';

export class EthPersonal extends Web3Context<EthPersonalAPI> {
public async getAccounts() {
return rpcWrappers.getAccounts(this.requestManager);
}

public async newAccount(password: string) {
return rpcWrappers.newAccount(this.requestManager, password);
}

public async unlockAccount(address: Address, password: string, unlockDuration: number) {
return rpcWrappers.unlockAccount(this.requestManager, address, password, unlockDuration);
}

public async lockAccount(address: Address) {
return rpcWrappers.lockAccount(this.requestManager, address);
}

public async importRawKey(keyData: HexString, passphrase: string) {
return rpcWrappers.importRawKey(this.requestManager, keyData, passphrase);
}

public async sendTransaction(tx: Transaction, passphrase: string) {
return rpcWrappers.sendTransaction(this.requestManager, tx, passphrase);
}

public async signTransaction(tx: Transaction, passphrase: string) {
return rpcWrappers.signTransaction(this.requestManager, tx, passphrase);
}

public async sign(data: HexString, address: Address, passphrase: string) {
return rpcWrappers.sign(this.requestManager, data, address, passphrase);
}

public async ecRecover(signedData: HexString, signature: string) {
return rpcWrappers.ecRecover(this.requestManager, signedData, signature);
}
}

export default EthPersonal;
Loading

0 comments on commit 39f8fd5

Please sign in to comment.