Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Upgrade to ethereumjs-config v2 #289

Merged
merged 3 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: '@ethereumjs/eslint-config-defaults',
env: {
mocha: true
}
}
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: npm install
- run: npm run test

- name: Upload coverage to Coveralls
uses: coverallsapp/github-action@master
- uses: codecov/codecov-action@v1
if: matrix.node-version == '12.x'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
file: ./coverage/lcov.info
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SYNOPSIS

[![NPM Package](https://img.shields.io/npm/v/ethereumjs-util.svg)](https://www.npmjs.org/package/ethereumjs-util)
[![Actions Status](https://github.com/ethereumjs/ethereumjs-util/workflows/Build/badge.svg)](https://github.com/ethereumjs/ethereumjs-util/actions)
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/ethereumjs-util.svg)](https://coveralls.io/r/ethereumjs/ethereumjs-util)
[![NPM Status][npm-badge]][npm-link]
[![Actions Status][actions-badge]][actions-link]
[![Coverage Status][coverage-badge]][coverage-link]
[![Discord][discord-badge]][discord-link]

A collection of utility functions for Ethereum. It can be used in Node.js and in the browser with [browserify](http://browserify.org/).
Expand Down Expand Up @@ -71,7 +71,7 @@ The following methods are available provided by [ethjs-util](https://github.com/
- toAscii
- getKeys

Import can be done directly by function name analogous to the build-in function import:
They can be imported by name:

```js
import { intToHex, stripHexPrefix } from 'ethereumjs-util'
Expand All @@ -94,5 +94,11 @@ If you want to join for work or do improvements on the libraries have a look at

MPL-2.0

[npm-badge]: https://img.shields.io/npm/v/ethereumjs-util.svg
[npm-link]: https://www.npmjs.org/package/ethereumjs-util
[actions-badge]: https://github.com/ethereumjs/ethereumjs-util/workflows/Build/badge.svg
[actions-link]: https://github.com/ethereumjs/ethereumjs-util/actions
[coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-util/branch/master/graph/badge.svg
[coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-util
[discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue
[discord-link]: https://discord.gg/TNwARpR
7 changes: 7 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
coverage:
status:
project:
default:
target: auto
threshold: 2%
base: auto
8 changes: 4 additions & 4 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module.exports = function(config) {
config.set({
frameworks: ['mocha', 'karma-typescript'],
exclude: ["src/@types/**"], // ref: https://github.com/monounity/karma-typescript/issues/254
files: ['src/**/*.ts', 'test/**/*.ts'],
preprocessors: {
'**/*.ts': ['karma-typescript'],
'**/*.ts': ['karma-typescript']
},
plugins: ['karma-mocha', 'karma-typescript', 'karma-chrome-launcher', 'karma-firefox-launcher'],
karmaTypescriptConfig: {
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
entrypoints: /\.spec\.ts$/
},
tsconfig: './tsconfig.json'
},
colors: true,
reporters: ['progress', 'karma-typescript'],
Expand All @@ -19,6 +19,6 @@ module.exports = function(config) {
concurrency: Infinity,
// Fail after timeout
browserDisconnectTimeout: 100000,
browserNoActivityTimeout: 100000,
browserNoActivityTimeout: 100000
})
}
25 changes: 10 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@
"dist"
],
"scripts": {
"build": "ethereumjs-config-build",
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run test && npm run build",
"docs:build": "npx typedoc --options typedoc.js",
"format": "ethereumjs-config-format",
"format:fix": "ethereumjs-config-format-fix",
"lint": "ethereumjs-config-lint",
"lint:fix": "ethereumjs-config-lint-fix",
"test": "npm run lint && npm run test:node && npm run test:browser",
"test:browser": "karma start karma.conf.js",
"test:node": "nyc --reporter=lcov mocha --require ts-node/register 'test/*.spec.ts'",
"tsc": "ethereumjs-config-tsc",
"tslint": "ethereumjs-config-tslint",
"tslint:fix": "ethereumjs-config-tslint-fix"
"tsc": "ethereumjs-config-tsc"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -89,18 +85,19 @@
},
"homepage": "https://github.com/ethereumjs/ethereumjs-util",
"dependencies": {
"@types/bn.js": "^4.11.3",
"@types/bn.js": "^5.1.0",
"bn.js": "^5.1.2",
"create-hash": "^1.1.2",
"ethereum-cryptography": "^0.1.3",
"ethjs-util": "0.1.6",
"rlp": "^2.2.4"
},
"devDependencies": {
"@ethereumjs/config-prettier": "^1.1.0",
"@ethereumjs/config-tsc": "^1.1.0",
"@ethereumjs/config-tslint": "^1.1.0",
"@types/mocha": "^5.2.7",
"@ethereumjs/config-coverage": "^2.0.0",
"@ethereumjs/config-typescript": "^2.0.0",
"@ethereumjs/eslint-config-defaults": "^2.0.0",
"@types/assert": "^1.5.4",
"@types/mocha": "^8.2.0",
"@types/node": "^11.9.0",
"@types/secp256k1": "^4.0.1",
"husky": "^2.1.0",
Expand All @@ -109,14 +106,12 @@
"karma-firefox-launcher": "^1.0.0",
"karma-mocha": "^2.0.0",
"karma-typescript": "^4.1.1",
"mocha": "^6.0.0",
"mocha": "^8.2.1",
"nyc": "^15.0.0",
"prettier": "^1.15.3",
"ts-node": "^8.6.2",
"tslint": "^5.12.0",
"typedoc": "next",
"typedoc-plugin-markdown": "^2.2.16",
"typescript": "^3.8.3",
"typestrict": "^1.0.2"
"typescript": "^3.8.3"
}
}
2 changes: 1 addition & 1 deletion prettier.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('@ethereumjs/config-prettier')
module.exports = require('@ethereumjs/eslint-config-defaults/prettier.config.js')
37 changes: 16 additions & 21 deletions src/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert'
import * as BN from 'bn.js'
import assert from 'assert'
import BN from 'bn.js'
import * as rlp from 'rlp'
import { stripHexPrefix } from 'ethjs-util'
import { KECCAK256_RLP, KECCAK256_NULL } from './constants'
Expand All @@ -12,7 +12,7 @@ const {
privateKeyVerify,
publicKeyCreate,
publicKeyVerify,
publicKeyConvert,
publicKeyConvert
} = require('ethereum-cryptography/secp256k1')

export interface AccountData {
Expand All @@ -35,7 +35,7 @@ export class Account {
nonce ? new BN(toBuffer(nonce)) : undefined,
balance ? new BN(toBuffer(balance)) : undefined,
stateRoot ? toBuffer(stateRoot) : undefined,
codeHash ? toBuffer(codeHash) : undefined,
codeHash ? toBuffer(codeHash) : undefined
)
}

Expand All @@ -52,12 +52,7 @@ export class Account {
public static fromValuesArray(values: Buffer[]) {
const [nonce, balance, stateRoot, codeHash] = values

return new Account(
nonce ? new BN(nonce) : undefined,
balance ? new BN(balance) : undefined,
stateRoot,
codeHash,
)
return new Account(new BN(nonce), new BN(balance), stateRoot, codeHash)
}

/**
Expand All @@ -68,7 +63,7 @@ export class Account {
nonce = new BN(0),
balance = new BN(0),
stateRoot = KECCAK256_RLP,
codeHash = KECCAK256_NULL,
codeHash = KECCAK256_NULL
) {
this.nonce = nonce
this.balance = balance
Expand Down Expand Up @@ -169,7 +164,7 @@ export const toChecksumAddress = function(hexAddress: string, eip1191ChainId?: n
*/
export const isValidChecksumAddress = function(
hexAddress: string,
eip1191ChainId?: number,
eip1191ChainId?: number
): boolean {
return isValidAddress(hexAddress) && toChecksumAddress(hexAddress, eip1191ChainId) === hexAddress
}
Expand Down Expand Up @@ -209,7 +204,7 @@ export const generateAddress2 = function(from: Buffer, salt: Buffer, initCode: B
assert(salt.length === 32)

const address = keccak256(
Buffer.concat([Buffer.from('ff', 'hex'), from, salt, keccak256(initCode)]),
Buffer.concat([Buffer.from('ff', 'hex'), from, salt, keccak256(initCode)])
)

return address.slice(-20)
Expand Down Expand Up @@ -259,14 +254,6 @@ export const pubToAddress = function(pubKey: Buffer, sanitize: boolean = false):
}
export const publicToAddress = pubToAddress

/**
* Returns the ethereum address of a given private key.
* @param privateKey A private key must be 256 bits wide
*/
export const privateToAddress = function(privateKey: Buffer): Buffer {
return publicToAddress(privateToPublic(privateKey))
}

/**
* Returns the ethereum public key of a given private key.
* @param privateKey A private key must be 256 bits wide
Expand All @@ -277,6 +264,14 @@ export const privateToPublic = function(privateKey: Buffer): Buffer {
return Buffer.from(publicKeyCreate(privateKey, false)).slice(1)
}

/**
* Returns the ethereum address of a given private key.
* @param privateKey A private key must be 256 bits wide
*/
export const privateToAddress = function(privateKey: Buffer): Buffer {
return publicToAddress(privateToPublic(privateKey))
}

/**
* Converts a public key to the Ethereum format.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
pubToAddress,
privateToAddress,
generateAddress,
generateAddress2,
generateAddress2
} from './account'

export class Address {
Expand Down
74 changes: 37 additions & 37 deletions src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as BN from 'bn.js'
import BN from 'bn.js'
import { intToBuffer, stripHexPrefix, padToEven, isHexString, isHexPrefixed } from 'ethjs-util'
import { TransformableToArray, TransformableToBuffer } from './types'
import { assertIsBuffer, assertIsArray, assertIsHexString } from './helpers'
Expand All @@ -11,6 +11,31 @@ export const zeros = function(bytes: number): Buffer {
return Buffer.allocUnsafe(bytes).fill(0)
}

/**
* Pads a `Buffer` with zeros till it has `length` bytes.
* Truncates the beginning or end of input if its length exceeds `length`.
* @param msg the value to pad (Buffer)
* @param length the number of bytes the output should be
* @param right whether to start padding form the left or right
* @return (Buffer)
*/
const setLength = function(msg: Buffer, length: number, right: boolean) {
const buf = zeros(length)
if (right) {
if (msg.length < length) {
msg.copy(buf)
return buf
}
return msg.slice(0, length)
} else {
if (msg.length < length) {
msg.copy(buf, length - msg.length)
return buf
}
return msg.slice(-length)
}
}

/**
* Left Pads a `Buffer` with leading zeros till it has `length` bytes.
* Or it truncates the beginning if it exceeds.
Expand All @@ -36,28 +61,17 @@ export const setLengthRight = function(msg: Buffer, length: number) {
}

/**
* Pads a `Buffer` with zeros till it has `length` bytes.
* Truncates the beginning or end of input if its length exceeds `length`.
* @param msg the value to pad (Buffer)
* @param length the number of bytes the output should be
* @param right whether to start padding form the left or right
* @return (Buffer)
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
* @param a (Buffer|Array|String)
* @return (Buffer|Array|String)
*/
const setLength = function(msg: Buffer, length: number, right: boolean) {
const buf = zeros(length)
if (right) {
if (msg.length < length) {
msg.copy(buf)
return buf
}
return msg.slice(0, length)
} else {
if (msg.length < length) {
msg.copy(buf, length - msg.length)
return buf
}
return msg.slice(-length)
const stripZeros = function(a: any): Buffer | number[] | string {
let first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1)
first = a[0]
}
return a
}

/**
Expand Down Expand Up @@ -91,20 +105,6 @@ export const unpadHexString = function(a: string): string {
return stripZeros(a) as string
}

/**
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
* @param a (Buffer|Array|String)
* @return (Buffer|Array|String)
*/
const stripZeros = function(a: any): Buffer | number[] | string {
let first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1)
first = a[0]
}
return a
}

/**
* Attempts to turn a value into a `Buffer`.
* Inputs supported: `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` or `toBuffer()` method.
Expand All @@ -121,7 +121,7 @@ export const toBuffer = function(
| TransformableToArray
| TransformableToBuffer
| null
| undefined,
| undefined
): Buffer {
if (v === null || v === undefined) {
return Buffer.allocUnsafe(0)
Expand All @@ -138,7 +138,7 @@ export const toBuffer = function(
if (typeof v === 'string') {
if (!isHexString(v)) {
throw new Error(
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`,
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`
)
}
return Buffer.from(padToEven(stripHexPrefix(v)), 'hex')
Expand Down
6 changes: 3 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const Buffer = require('buffer').Buffer
import * as BN from 'bn.js'
import BN from 'bn.js'

/**
* The max integer that this VM can handle
*/
export const MAX_INTEGER: BN = new BN(
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
16,
16
)

/**
* 2^256
*/
export const TWO_POW256: BN = new BN(
'10000000000000000000000000000000000000000000000000000000000000000',
16,
16
)

/**
Expand Down
Loading