Skip to content

Commit fe518cf

Browse files
authored
Merge pull request #289 from ethereumjs/upgrade-config-2.0
Upgrade to ethereumjs-config v2
2 parents 0725f7e + 919ff36 commit fe518cf

26 files changed

+282
-273
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: '@ethereumjs/eslint-config-defaults',
3+
env: {
4+
mocha: true
5+
}
6+
}

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- run: npm install
2525
- run: npm run test
2626

27-
- name: Upload coverage to Coveralls
28-
uses: coverallsapp/github-action@master
27+
- uses: codecov/codecov-action@v1
28+
if: matrix.node-version == '12.x'
2929
with:
30-
github-token: ${{ secrets.GITHUB_TOKEN }}
30+
file: ./coverage/lcov.info

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SYNOPSIS
22

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

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

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

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

9595
MPL-2.0
9696

97+
[npm-badge]: https://img.shields.io/npm/v/ethereumjs-util.svg
98+
[npm-link]: https://www.npmjs.org/package/ethereumjs-util
99+
[actions-badge]: https://github.com/ethereumjs/ethereumjs-util/workflows/Build/badge.svg
100+
[actions-link]: https://github.com/ethereumjs/ethereumjs-util/actions
101+
[coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-util/branch/master/graph/badge.svg
102+
[coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-util
97103
[discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue
98104
[discord-link]: https://discord.gg/TNwARpR

codecov.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: auto
6+
threshold: 2%
7+
base: auto

karma.conf.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module.exports = function(config) {
22
config.set({
33
frameworks: ['mocha', 'karma-typescript'],
4-
exclude: ["src/@types/**"], // ref: https://github.com/monounity/karma-typescript/issues/254
54
files: ['src/**/*.ts', 'test/**/*.ts'],
65
preprocessors: {
7-
'**/*.ts': ['karma-typescript'],
6+
'**/*.ts': ['karma-typescript']
87
},
98
plugins: ['karma-mocha', 'karma-typescript', 'karma-chrome-launcher', 'karma-firefox-launcher'],
109
karmaTypescriptConfig: {
1110
bundlerOptions: {
12-
entrypoints: /\.spec\.ts$/,
11+
entrypoints: /\.spec\.ts$/
1312
},
13+
tsconfig: './tsconfig.json'
1414
},
1515
colors: true,
1616
reporters: ['progress', 'karma-typescript'],
@@ -19,6 +19,6 @@ module.exports = function(config) {
1919
concurrency: Infinity,
2020
// Fail after timeout
2121
browserDisconnectTimeout: 100000,
22-
browserNoActivityTimeout: 100000,
22+
browserNoActivityTimeout: 100000
2323
})
2424
}

package.json

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@
1111
"dist"
1212
],
1313
"scripts": {
14-
"build": "ethereumjs-config-build",
14+
"build": "ethereumjs-config-ts-build",
1515
"prepublishOnly": "npm run test && npm run build",
1616
"docs:build": "npx typedoc --options typedoc.js",
17-
"format": "ethereumjs-config-format",
18-
"format:fix": "ethereumjs-config-format-fix",
1917
"lint": "ethereumjs-config-lint",
2018
"lint:fix": "ethereumjs-config-lint-fix",
2119
"test": "npm run lint && npm run test:node && npm run test:browser",
2220
"test:browser": "karma start karma.conf.js",
2321
"test:node": "nyc --reporter=lcov mocha --require ts-node/register 'test/*.spec.ts'",
24-
"tsc": "ethereumjs-config-tsc",
25-
"tslint": "ethereumjs-config-tslint",
26-
"tslint:fix": "ethereumjs-config-tslint-fix"
22+
"tsc": "ethereumjs-config-tsc"
2723
},
2824
"husky": {
2925
"hooks": {
@@ -89,18 +85,19 @@
8985
},
9086
"homepage": "https://github.com/ethereumjs/ethereumjs-util",
9187
"dependencies": {
92-
"@types/bn.js": "^4.11.3",
88+
"@types/bn.js": "^5.1.0",
9389
"bn.js": "^5.1.2",
9490
"create-hash": "^1.1.2",
9591
"ethereum-cryptography": "^0.1.3",
9692
"ethjs-util": "0.1.6",
9793
"rlp": "^2.2.4"
9894
},
9995
"devDependencies": {
100-
"@ethereumjs/config-prettier": "^1.1.0",
101-
"@ethereumjs/config-tsc": "^1.1.0",
102-
"@ethereumjs/config-tslint": "^1.1.0",
103-
"@types/mocha": "^5.2.7",
96+
"@ethereumjs/config-coverage": "^2.0.0",
97+
"@ethereumjs/config-typescript": "^2.0.0",
98+
"@ethereumjs/eslint-config-defaults": "^2.0.0",
99+
"@types/assert": "^1.5.4",
100+
"@types/mocha": "^8.2.0",
104101
"@types/node": "^11.9.0",
105102
"@types/secp256k1": "^4.0.1",
106103
"husky": "^2.1.0",
@@ -109,14 +106,12 @@
109106
"karma-firefox-launcher": "^1.0.0",
110107
"karma-mocha": "^2.0.0",
111108
"karma-typescript": "^4.1.1",
112-
"mocha": "^6.0.0",
109+
"mocha": "^8.2.1",
113110
"nyc": "^15.0.0",
114111
"prettier": "^1.15.3",
115112
"ts-node": "^8.6.2",
116-
"tslint": "^5.12.0",
117113
"typedoc": "next",
118114
"typedoc-plugin-markdown": "^2.2.16",
119-
"typescript": "^3.8.3",
120-
"typestrict": "^1.0.2"
115+
"typescript": "^3.8.3"
121116
}
122117
}

prettier.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@ethereumjs/config-prettier')
1+
module.exports = require('@ethereumjs/eslint-config-defaults/prettier.config.js')

src/account.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as assert from 'assert'
2-
import * as BN from 'bn.js'
1+
import assert from 'assert'
2+
import BN from 'bn.js'
33
import * as rlp from 'rlp'
44
import { stripHexPrefix } from 'ethjs-util'
55
import { KECCAK256_RLP, KECCAK256_NULL } from './constants'
@@ -12,7 +12,7 @@ const {
1212
privateKeyVerify,
1313
publicKeyCreate,
1414
publicKeyVerify,
15-
publicKeyConvert,
15+
publicKeyConvert
1616
} = require('ethereum-cryptography/secp256k1')
1717

1818
export interface AccountData {
@@ -35,7 +35,7 @@ export class Account {
3535
nonce ? new BN(toBuffer(nonce)) : undefined,
3636
balance ? new BN(toBuffer(balance)) : undefined,
3737
stateRoot ? toBuffer(stateRoot) : undefined,
38-
codeHash ? toBuffer(codeHash) : undefined,
38+
codeHash ? toBuffer(codeHash) : undefined
3939
)
4040
}
4141

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

55-
return new Account(
56-
nonce ? new BN(nonce) : undefined,
57-
balance ? new BN(balance) : undefined,
58-
stateRoot,
59-
codeHash,
60-
)
55+
return new Account(new BN(nonce), new BN(balance), stateRoot, codeHash)
6156
}
6257

6358
/**
@@ -68,7 +63,7 @@ export class Account {
6863
nonce = new BN(0),
6964
balance = new BN(0),
7065
stateRoot = KECCAK256_RLP,
71-
codeHash = KECCAK256_NULL,
66+
codeHash = KECCAK256_NULL
7267
) {
7368
this.nonce = nonce
7469
this.balance = balance
@@ -169,7 +164,7 @@ export const toChecksumAddress = function(hexAddress: string, eip1191ChainId?: n
169164
*/
170165
export const isValidChecksumAddress = function(
171166
hexAddress: string,
172-
eip1191ChainId?: number,
167+
eip1191ChainId?: number
173168
): boolean {
174169
return isValidAddress(hexAddress) && toChecksumAddress(hexAddress, eip1191ChainId) === hexAddress
175170
}
@@ -209,7 +204,7 @@ export const generateAddress2 = function(from: Buffer, salt: Buffer, initCode: B
209204
assert(salt.length === 32)
210205

211206
const address = keccak256(
212-
Buffer.concat([Buffer.from('ff', 'hex'), from, salt, keccak256(initCode)]),
207+
Buffer.concat([Buffer.from('ff', 'hex'), from, salt, keccak256(initCode)])
213208
)
214209

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

262-
/**
263-
* Returns the ethereum address of a given private key.
264-
* @param privateKey A private key must be 256 bits wide
265-
*/
266-
export const privateToAddress = function(privateKey: Buffer): Buffer {
267-
return publicToAddress(privateToPublic(privateKey))
268-
}
269-
270257
/**
271258
* Returns the ethereum public key of a given private key.
272259
* @param privateKey A private key must be 256 bits wide
@@ -277,6 +264,14 @@ export const privateToPublic = function(privateKey: Buffer): Buffer {
277264
return Buffer.from(publicKeyCreate(privateKey, false)).slice(1)
278265
}
279266

267+
/**
268+
* Returns the ethereum address of a given private key.
269+
* @param privateKey A private key must be 256 bits wide
270+
*/
271+
export const privateToAddress = function(privateKey: Buffer): Buffer {
272+
return publicToAddress(privateToPublic(privateKey))
273+
}
274+
280275
/**
281276
* Converts a public key to the Ethereum format.
282277
*/

src/address.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
pubToAddress,
77
privateToAddress,
88
generateAddress,
9-
generateAddress2,
9+
generateAddress2
1010
} from './account'
1111

1212
export class Address {

src/bytes.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as BN from 'bn.js'
1+
import BN from 'bn.js'
22
import { intToBuffer, stripHexPrefix, padToEven, isHexString, isHexPrefixed } from 'ethjs-util'
33
import { TransformableToArray, TransformableToBuffer } from './types'
44
import { assertIsBuffer, assertIsArray, assertIsHexString } from './helpers'
@@ -11,6 +11,31 @@ export const zeros = function(bytes: number): Buffer {
1111
return Buffer.allocUnsafe(bytes).fill(0)
1212
}
1313

14+
/**
15+
* Pads a `Buffer` with zeros till it has `length` bytes.
16+
* Truncates the beginning or end of input if its length exceeds `length`.
17+
* @param msg the value to pad (Buffer)
18+
* @param length the number of bytes the output should be
19+
* @param right whether to start padding form the left or right
20+
* @return (Buffer)
21+
*/
22+
const setLength = function(msg: Buffer, length: number, right: boolean) {
23+
const buf = zeros(length)
24+
if (right) {
25+
if (msg.length < length) {
26+
msg.copy(buf)
27+
return buf
28+
}
29+
return msg.slice(0, length)
30+
} else {
31+
if (msg.length < length) {
32+
msg.copy(buf, length - msg.length)
33+
return buf
34+
}
35+
return msg.slice(-length)
36+
}
37+
}
38+
1439
/**
1540
* Left Pads a `Buffer` with leading zeros till it has `length` bytes.
1641
* Or it truncates the beginning if it exceeds.
@@ -36,28 +61,17 @@ export const setLengthRight = function(msg: Buffer, length: number) {
3661
}
3762

3863
/**
39-
* Pads a `Buffer` with zeros till it has `length` bytes.
40-
* Truncates the beginning or end of input if its length exceeds `length`.
41-
* @param msg the value to pad (Buffer)
42-
* @param length the number of bytes the output should be
43-
* @param right whether to start padding form the left or right
44-
* @return (Buffer)
64+
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
65+
* @param a (Buffer|Array|String)
66+
* @return (Buffer|Array|String)
4567
*/
46-
const setLength = function(msg: Buffer, length: number, right: boolean) {
47-
const buf = zeros(length)
48-
if (right) {
49-
if (msg.length < length) {
50-
msg.copy(buf)
51-
return buf
52-
}
53-
return msg.slice(0, length)
54-
} else {
55-
if (msg.length < length) {
56-
msg.copy(buf, length - msg.length)
57-
return buf
58-
}
59-
return msg.slice(-length)
68+
const stripZeros = function(a: any): Buffer | number[] | string {
69+
let first = a[0]
70+
while (a.length > 0 && first.toString() === '0') {
71+
a = a.slice(1)
72+
first = a[0]
6073
}
74+
return a
6175
}
6276

6377
/**
@@ -91,20 +105,6 @@ export const unpadHexString = function(a: string): string {
91105
return stripZeros(a) as string
92106
}
93107

94-
/**
95-
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
96-
* @param a (Buffer|Array|String)
97-
* @return (Buffer|Array|String)
98-
*/
99-
const stripZeros = function(a: any): Buffer | number[] | string {
100-
let first = a[0]
101-
while (a.length > 0 && first.toString() === '0') {
102-
a = a.slice(1)
103-
first = a[0]
104-
}
105-
return a
106-
}
107-
108108
/**
109109
* Attempts to turn a value into a `Buffer`.
110110
* Inputs supported: `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` or `toBuffer()` method.
@@ -121,7 +121,7 @@ export const toBuffer = function(
121121
| TransformableToArray
122122
| TransformableToBuffer
123123
| null
124-
| undefined,
124+
| undefined
125125
): Buffer {
126126
if (v === null || v === undefined) {
127127
return Buffer.allocUnsafe(0)
@@ -138,7 +138,7 @@ export const toBuffer = function(
138138
if (typeof v === 'string') {
139139
if (!isHexString(v)) {
140140
throw new Error(
141-
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`,
141+
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`
142142
)
143143
}
144144
return Buffer.from(padToEven(stripHexPrefix(v)), 'hex')

0 commit comments

Comments
 (0)