Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ethereumjs-util

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ethereumjs-util - npm Package Compare versions

Comparing version 2.4.0 to 2.5.0

docs/index.md

158

index.js
const SHA3 = require('sha3')
const ec = require('elliptic').ec('secp256k1')
const secp256k1 = require('secp256k1')
const assert = require('assert')

@@ -8,19 +8,69 @@ const rlp = require('rlp')

// the max interger that this VM can handle
/**
* the max interger that this VM can handle (a ```BN```)
* @var {BN} MAX_INTEGER
*/
exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)
exports.TWO_POW256 = new BN('115792089237316195423570985008687907853269984665640564039457584007913129639936')
// hex string of SHA3-256 hash of `null`
exports.SHA3_NULL = new Buffer('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', 'hex')
/**
* 2^256 (a ```BN```)
* @var {BN} TWO_POW256
*/
exports.TWO_POW256 = new BN('10000000000000000000000000000000000000000000000000000000000000000', 16)
// SHA3-256 hash of the rlp of []
exports.SHA3_RLP_ARRAY = new Buffer('1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', 'hex')
/**
* SHA3-256 hash of null (a ```String```)
* @var {String} SHA3_NULL_S
*/
exports.SHA3_NULL_S = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
// SHA3-256 hash of the rlp of `null`
exports.SHA3_RLP = new Buffer('56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', 'hex')
/**
* SHA3-256 hash of null (a ```Buffer```)
* @var {Buffer} SHA3_NULL
*/
exports.SHA3_NULL = new Buffer(exports.SHA3_NULL_S, 'hex')
/**
* SHA3-256 of an RLP of an empty array (a ```String```)
* @var {String} SHA3_RLP_ARRAY_S
*/
exports.SHA3_RLP_ARRAY_S = '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'
/**
* SHA3-256 of an RLP of an empty array (a ```Buffer```)
* @var {Buffer} SHA3_RLP_ARRAY
*/
exports.SHA3_RLP_ARRAY = new Buffer(exports.SHA3_RLP_ARRAY_S, 'hex')
/**
* SHA3-256 hash of the RLP of null (a ```String```)
* @var {String} SHA3_RLP_S
*/
exports.SHA3_RLP_S = '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'
/**
* SHA3-256 hash of the RLP of null (a ```Buffer```)
* @var {Buffer} SHA3_RLP
*/
exports.SHA3_RLP = new Buffer(exports.SHA3_RLP_S, 'hex')
/**
* [`BN`](https://github.com/indutny/bn.js)
* @var {Function}
*/
exports.BN = BN
/**
* [`rlp`](https://github.com/wanderer/rlp)
* @var {Function}
*/
exports.rlp = rlp
/**
* [`secp256k1`](https://github.com/cryptocoinjs/secp256k1-node/)
* @var {Object}
*/
exports.secp256k1 = secp256k1
/**
* Returns a buffer filled with 0s

@@ -40,4 +90,4 @@ * @method zeros

* @method pad
* @param {Buffer|Array} array
* @param {Integer} length the number of bytes the output should be
* @param {Buffer|Array} msg the value to pad
* @param {Integer} length the number of bytes the output should be
* @return {Buffer|Array}

@@ -58,4 +108,4 @@ */

* @method rpad
* @param {Buffer|Array} array
* @param {Integer} length the number of bytes the output should be
* @param {Buffer|Array} msg the value to pad
* @param {Integer} length the number of bytes the output should be
* @return {Buffer|Array}

@@ -75,3 +125,2 @@ */

* Trims leading zeros from a buffer or an array
*
* @method unpad

@@ -90,3 +139,7 @@ * @param {Buffer|Array|String}

}
/**
* Attempts to turn a value into a Buffer. Attempts to turn a value into a Buffer. Supports Buffer, string, number, null/undefined, BN.js or other objects with a toArray() method.
* @method toBuffer
* @param {*} v the value
*/
exports.toBuffer = function (v) {

@@ -134,5 +187,5 @@ if (!Buffer.isBuffer(v)) {

/**
* Converts an integer to a buffer
* Converts an `Integer` to a `Buffer`
* @method intToBuffer
* @param {Number}
* @param {Integer}
* @return {Buffer}

@@ -146,5 +199,5 @@ */

/**
* Converts a buffer to an Interger
* Converts a `Buffer` to an `Interger`
* @method bufferToInt
* @par[MaÅam {B[M`Êuffer}
* @param {Buffer}
* @return {Number}

@@ -162,3 +215,3 @@ */

/**
* interpets a buffer as a signed integer and returns a bignum
* interpets a `Buffer` as a signed integer and returns a `bignum`
* @method fromSigned

@@ -169,4 +222,5 @@ * @param {Buffer} num

exports.fromSigned = function (num) {
// Could use num.testn(255), but this is faster:
if (num.length === 32 && num[0] >= 128) {
return new BN(num).sub(exports.TWO_POW256)
return new BN(num).inotn(256).iaddn(1).ineg()
}

@@ -178,3 +232,3 @@

/**
* Converts a bignum to an unsigned interger and returns it as a buffer
* Converts a `Bignum` to an unsigned interger and returns it as a `Buffer`
* @method toUnsigned

@@ -186,3 +240,3 @@ * @param {Bignum} num

if (num.isNeg()) {
return new Buffer(num.add(exports.TWO_POW256).toArray())
return new Buffer(num.abs().inotn(256).iaddn(1).toArray())
}

@@ -197,3 +251,3 @@

* @param {Buffer|Array|String|Number} a the input data
* @param {Number} bytes the SHA width
* @param {Number} [bytes=256] the SHA width
* @return {Buffer}

@@ -271,5 +325,4 @@ */

privateKey = exports.toBuffer(privateKey)
privateKey = new BN(privateKey)
var key = ec.keyFromPrivate(privateKey).getPublic().toJSON()
return Buffer.concat([exports.pad(key[0], 32), exports.pad(key[1], 32)])
// skip the type flag and use the X, Y points
return secp256k1.publicKeyConvert(secp256k1.publicKeyCreate(privateKey), false).slice(1)
}

@@ -288,6 +341,7 @@

/**
* Generates a new address
* Generates an address of a newly created contract
* @method generateAddress
* @param {Buffer} from the address which is creating this new address
* @param {Buffer} nonce the nonce of the from account
* @return {Buffer}
*/

@@ -306,3 +360,19 @@ exports.generateAddress = function (from, nonce) {

// Returns a Boolean on whether or not the a sting starts with 0x
/**
* Returns true if the supplied address belongs to a precompiled account
* @method isPrecompiled
* @param {Buffer|String}
* @return {Boolean}
*/
exports.isPrecompiled = function (address) {
var a = exports.unpad(address)
return a.length === 1 && a[0] > 0 && a[0] < 5
}
/**
* Returns a `Boolean` on whether or not the a `Sting` starts with "0x"
* @method isHexPrefixed
* @param {String} str
* @return {Boolean}
*/
exports.isHexPrefixed = function (str) {

@@ -312,3 +382,8 @@ return str.slice(0, 2) === '0x'

// Removes 0x from a given String
/**
* Removes "0x" from a given `String`
* @method stripHexPrefix
* @param {String} str
* @return {String}
*/
exports.stripHexPrefix = function (str) {

@@ -321,3 +396,8 @@ if (typeof str !== 'string') {

// Adds 0x to a given string if it does not already start with 0x
/**
* Adds "0x" to a given string if it does not already start with "0x"
* @method addHexPrefix
* @param {String}
* @return {String}
*/
exports.addHexPrefix = function (str) {

@@ -332,6 +412,11 @@ if (typeof str !== 'string') {

/**
* defines properties on a `Object`
* Defines properties on a Object. It make the assumption that underlying data is binary
* @method defineProperties
* @para[M`Êm {Object} self the `Object` to define properties on
* @param {Array} fields an array fields to define
* @param {Object} self the `Object` to define properties on
* @param {Array} fields an array fields to define. Fields can contain:
* * `name` - the name of the properties
* * `length` - the number of bytes the field can have
* * `allowLess` - if the field can be less than the length
* * `allowEmpty`
* @param {*} Data data to be validated against the definitions
*/

@@ -459,2 +544,7 @@ exports.defineProperties = function (self, fields, data) {

/**
* Pads a String to have an even length
* @method padToEven
* @param {String}
*/
exports.padToEven = function (a) {

@@ -461,0 +551,0 @@ if (a.length % 2) a = '0' + a

{
"name": "ethereumjs-util",
"version": "2.4.0",
"version": "2.5.0",
"description": "a collection of utility functions for Ethereum",

@@ -13,7 +13,8 @@ "main": "index.js",

"test:browser": "karma start karma.conf.js",
"test:node": "istanbul test _mocha -- --reporter spec"
"test:node": "istanbul test _mocha -- --reporter spec",
"build:docs": "documentation --github -f md ./index.js > ./docs/index.md"
},
"repository": {
"type": "git",
"url": "https://github.com/ethereum/ethereumjs-utils.git"
"url": "https://github.com/ethereumjs/ethereumjs-util.git"
},

@@ -25,12 +26,15 @@ "keywords": [

"author": "mjbecze <mjbecze@gmail.com>",
"contributors": [
"Alex Beregszaszi <alex@rtfs.hu>"
],
"license": "MPL-2.0",
"bugs": {
"url": "https://github.com/ethereum/ethereumjs-utils/issues"
"url": "https://github.com/ethereumjs/ethereumjs-util/issues"
},
"homepage": "https://github.com/ethereum/ethereumjs-utils",
"homepage": "https://github.com/ethereumjs/ethereumjs-util",
"dependencies": {
"bn.js": "^4.4.0",
"bn.js": "^4.6.2",
"browserify-sha3": "^0.0.1",
"elliptic": "^6.0.2",
"rlp": "^2.0.0",
"secp256k1": "^2.0.7",
"sha3": "^1.1.0"

@@ -54,2 +58,3 @@ },

"coveralls": "^2.11.4",
"documentation": "^3.0.4",
"istanbul": "^0.4.1",

@@ -56,0 +61,0 @@ "karma": "^0.13.15",

# SYNOPSIS
[![NPM Package](https://img.shields.io/npm/v/ethereumjs-util.svg?style=flat-square)](https://www.npmjs.org/package/ethereumjs-util)
[![Build Status](https://img.shields.io/travis/ethereum/ethereumjs-util.svg?branch=master&style=flat-square)](https://travis-ci.org/ethereum/ethereumjs-util)
[![Coverage Status](https://img.shields.io/coveralls/ethereum/ethereumjs-util.svg?style=flat-square)](https://coveralls.io/r/ethereum/ethereumjs-util)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ethereum/ethereumjs-lib?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) or #ethereumjs on freenode
[![Build Status](https://img.shields.io/travis/ethereumjs/ethereumjs-util.svg?branch=master&style=flat-square)](https://travis-ci.org/ethereumjs/ethereumjs-util)
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/ethereumjs-util.svg?style=flat-square)](https://coveralls.io/r/ethereumjs/ethereumjs-util)
[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode

@@ -14,198 +14,5 @@ [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

# API
## properties
- `MAX_INTEGER` - The max integer that the VM can handle
- `TWO_POW256` - 2^256
- `SHA3_NULL` - SHA3-256 hash of `null`
- `SHA3_RLP_ARRAY` - SHA3-256 of an RLP of an empty array
- `SHA3_RLP` - SHA3-256 hash of the RLP of `null`
- [`BN`](https://github.com/indutny/bn.js)
- [`rlp`](https://github.com/wanderer/rlp)
[./docs/](./docs/index.md)
## methods
### `zeros(number)`
Returns buffer filled with 0's
**Parameters**
- `number` - the number of bytes to return
**Return:** `Buffer`
### `pad(val, length)`
Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes
**Parameters**
- `val` - the value to pad
- `length` - the of the resulting value
**Return:** `Array` or `Buffer`
### `rpad(val, length)`
Pads an `Array` or `Buffer` with trailing zeros till it has `length` bytes
**Parameters**
- `val` - the value to pad
- `length` - the of the resulting value
**Return:** `Array` or `Buffer`
### `unpad(val)`
Trims leading zeros from a `Buffer` or an `Array`
**Parameters**
- `val` - a `Buffer` or an `Array` to unpad
**Return:** `Buffer` or `Array`
### `toBuffer(val)`
Attempts to turn a value into a Buffer. Attempts to turn a value into a Buffer. Supports Buffer, string, number, null/undefined, BN.js or other objects with a toArray() method.
**Parameters**
- `val` the object to be converted
**Return:** `Buffer`
### `intToHex(int)`
Converts an `Integer` into a hex `String`
**Parameters**
- `int`
**Return:** `String`
### `intToBuffer(int)`
Converts an `Integer` to a `Buffer`
**Parameters**
- `int`
**Return:** `Buffer`
### `bufferToInt(buf)`
Converts a `Buffer` to an `Integer`
**Parameters**
- `buf`
**Return:** `Interger`
### `fromSigned(buf)`
Interprets a `Buffer` as a signed `Integer`
**Parameters**
- `buf`
**Return:** [`BN.js`](https://github.com/indutny/bn.js)
### `toUnsigned(num)`
Converts a [`BN.js`](https://github.com/indutny/bn.js) to an unsigned integer
**Parameters**
- `num` - a [`BN.js`](https://github.com/indutny/bn.js)
**Return:** `buffer`
### `publicToAddress(pubKey)`
Returns the ethereum address of a given public key
**Parameters**
- `pubKey` - the public key as a `Buffer`
**Return:** : `Buffer`
### `privateToAddress(privateKey)`
Returns the ethereum address of a given private key
**Parameters**
- `privateKey` - the private key as a `Buffer`
**Return:** `Buffer`
### `privateToPublic(privateKey)`
Returns the ethereum public key of a given private key
**Parameters**
- `privateKey` - the private key as a `Buffer`
**Return:** `Buffer`
### `generateAddress(from, nonce)`
Generates an address of a newly created contract. Don't forget to increment the nonce to get the correct address.
**Parameters**
- `from` - the address of the account creating the contract
- `nonce` - the creating accounts nonce
**Return:** `Buffer`
### `sha3(a, bytes)`
Returns a sha3 of `a` of the length of `bytes`
**Parameters**
- `a` - the value to hash
- `bytes` - how many bytes the hash should be
**Return:** `Buffer`
### `sha256(a, bytes)`
Returns a sha256 of `a`
**Parameters**
- `a` - the value to hash
**Return:** `Buffer`
### `ripemd160(a, bytes)`
Returns a ripemd160 of `a`
**Parameters**
- `a` - the value to hash
- `padded` - pad the hash to 256 bits with zeroes
**Return:** `Buffer`
### `rlphash(a)`
Returns a sha3 of the RLP encoded version of `a`
**Parameters**
- `a` - the value to encode and hash
**Return:** `Buffer`
### `printBA(ba)`
Print a Buffer Array
**Parameters**
- `ba` - an `Array` of `Buffers`
**Return:** a Buffer Array
### `baToJSON(ba)`
Converts a buffer array to JSON
**Parameters**
- `ba` - an `Array` of `Buffers`
**Return:** a JSON Object
### `isHexPrefixed(string)`
Returns a Boolean on whether or not the String starts with `0x`
**Parameters**
- `string` - a `String`
**Return:** `Boolean`
### `stripHexPrefix(string)`
Removes `0x` from a given String
**Parameters**
- `string` - a `String`
**Return:** `String`
### `addHexPrefix(string)`
Adds `0x` to a given string if it does not already start with `0x`
**Parameters**
- `string` - a `String`
**Return:** `string`
### `defineProperties(self, fields)`
Defines properties on a `Object`. It make the assumption that underlying data is binary.
**Parameters**
- `self` - the `Object` to define properties on
- `fields` - an array fields to define. Fields can contain:
- `name` the name of the properties
- `length` the number of bytes the field can have
- `allowLess` if the field can be less than the `length`
- `allowEmpty`
### `validate(fields, data)`
Validate defined fields
**Parameters**
- `fields`
- `data`
**Return:** `Boolean`
# LICENSE
MPL-2.0

@@ -177,4 +177,23 @@ var assert = require('assert')

describe('privateToPublic', function () {
it('should produce a public key given a private key', function () {
var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d'
var privateKey = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95])
var r = ethUtils.privateToPublic(privateKey).toString('hex')
assert.equal(r.toString('hex'), pubKey)
})
it('shouldn\'t produce a public key given an invalid private key', function () {
var privateKey1 = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95, 42])
var privateKey2 = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28])
assert.throws(function () {
ethUtils.privateToPublic(privateKey1)
})
assert.throws(function () {
ethUtils.privateToPublic(privateKey2)
})
})
})
describe('privateToAddress', function () {
it('should produce an address given a public key', function () {
it('should produce an address given a private key', function () {
var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a'

@@ -208,1 +227,15 @@ // Our private key

})
describe('isPrecompiled', function () {
it('should return true', function () {
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000001'), true)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000002'), true)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000003'), true)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000004'), true)
})
it('should return false', function () {
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000000'), false)
assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000005'), false)
assert.equal(ethUtils.isPrecompiled('1000000000000000000000000000000000000000'), false)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc