ethereumjs-util
Advanced tools
Comparing version 1.3.8 to 1.4.0
215
index.js
@@ -7,13 +7,13 @@ const SHA3 = require('sha3') | ||
//the max interger that this VM can handle | ||
// the max interger that this VM can handle | ||
exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16) | ||
exports.TWO_POW256 = new BN('115792089237316195423570985008687907853269984665640564039457584007913129639936') | ||
//hex string of SHA3-256 hash of `null` | ||
// hex string of SHA3-256 hash of `null` | ||
exports.SHA3_NULL = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' | ||
//SHA3-256 hash of the rlp of [] | ||
// SHA3-256 hash of the rlp of [] | ||
exports.SHA3_RLP_ARRAY = '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347' | ||
//SHA3-256 hash of the rlp of `null` | ||
// SHA3-256 hash of the rlp of `null` | ||
exports.SHA3_RLP = '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' | ||
@@ -30,6 +30,6 @@ | ||
*/ | ||
exports.zeros = function(bytes) { | ||
var buf = new Buffer(bytes); | ||
buf.fill(0); | ||
return buf; | ||
exports.zeros = function (bytes) { | ||
var buf = new Buffer(bytes) | ||
buf.fill(0) | ||
return buf | ||
} | ||
@@ -44,3 +44,3 @@ | ||
*/ | ||
exports.pad = function(msg, length) { | ||
exports.pad = function (msg, length) { | ||
if (msg.length < length) { | ||
@@ -50,3 +50,3 @@ var buf = exports.zeros(length) | ||
return buf | ||
} | ||
} | ||
return msg.slice(-length) | ||
@@ -61,3 +61,3 @@ } | ||
*/ | ||
exports.unpad = function(a) { | ||
exports.unpad = exports.stripZeros = function (a) { | ||
var first = a[0] | ||
@@ -71,2 +71,20 @@ while (a.length > 0 && first.toString() === '0') { | ||
exports.toBuffer = function (v) { | ||
if (!Buffer.isBuffer(v)) { | ||
if (typeof v === 'string') { | ||
v = new Buffer(padToEven(exports.stripHexPrefix(v)), 'hex') | ||
} else if (typeof v === 'number') { | ||
v = exports.intToBuffer(v) | ||
} else if (v === null || v === undefined) { | ||
v = new Buffer([]) | ||
} else if (v.toArray) { | ||
// converts a BN to a Buffer | ||
v = new Buffer(v.toArray()) | ||
} else { | ||
throw new Error('invalid type') | ||
} | ||
} | ||
return v | ||
} | ||
/** | ||
@@ -78,8 +96,9 @@ * Converts an integer into a hex string | ||
*/ | ||
exports.intToHex = function(i) { | ||
exports.intToHex = function (i) { | ||
assert(i % 1 === 0, 'number is not a interger') | ||
assert(i >= 0, 'number must be positive') | ||
var hex = i.toString(16) | ||
if (hex.length % 2) | ||
if (hex.length % 2) { | ||
hex = '0' + hex | ||
} | ||
@@ -95,3 +114,3 @@ return hex | ||
*/ | ||
exports.intToBuffer = function(i) { | ||
exports.intToBuffer = function (i) { | ||
var hex = exports.intToHex(i) | ||
@@ -107,6 +126,7 @@ return new Buffer(hex, 'hex') | ||
*/ | ||
exports.bufferToInt = function(buf) { | ||
if (buf.length === 0) | ||
exports.bufferToInt = function (buf) { | ||
if (buf.length === 0) { | ||
return 0 | ||
} | ||
return parseInt(buf.toString('hex'), 16) | ||
@@ -121,5 +141,6 @@ } | ||
*/ | ||
exports.fromSigned = function(num) { | ||
if (num.length === 32 && num[0] >= 128) | ||
exports.fromSigned = function (num) { | ||
if (num.length === 32 && num[0] >= 128) { | ||
return new BN(num).sub(exports.TWO_POW256) | ||
} | ||
@@ -135,16 +156,17 @@ return new BN(num) | ||
*/ | ||
exports.toUnsigned = function(num) { | ||
if (num.cmpn(0) === -1) | ||
exports.toUnsigned = function (num) { | ||
if (num.cmpn(0) === -1) { | ||
return new Buffer(num.add(exports.TWO_POW256).toArray()) | ||
} | ||
return new Buffer(num.toArray()) | ||
} | ||
exports.sha3 = function(a, bytes) { | ||
if(!bytes) bytes = 256 | ||
exports.sha3 = function (a, bytes) { | ||
if (!bytes) bytes = 256 | ||
var h = new SHA3.SHA3Hash(bytes) | ||
if (a) | ||
if (a) { | ||
h.update(a) | ||
} | ||
return new Buffer(h.digest('hex'), 'hex') | ||
@@ -159,3 +181,3 @@ } | ||
*/ | ||
exports.pubToAddress = exports.publicToAddress = function(pubKey) { | ||
exports.pubToAddress = exports.publicToAddress = function (pubKey) { | ||
var hash = new SHA3.SHA3Hash(256) | ||
@@ -172,3 +194,3 @@ hash.update(pubKey.slice(-64)) | ||
*/ | ||
var privateToPublic = exports.privateToPublic = function(privateKey){ | ||
var privateToPublic = exports.privateToPublic = function (privateKey) { | ||
privateKey = new BN(privateKey) | ||
@@ -185,3 +207,3 @@ var key = ec.keyFromPrivate(privateKey).getPublic().toJSON() | ||
*/ | ||
exports.privateToAddress = function(privateKey){ | ||
exports.privateToAddress = function (privateKey) { | ||
return exports.publicToAddress(privateToPublic(privateKey)) | ||
@@ -196,7 +218,8 @@ } | ||
*/ | ||
exports.generateAddress = function(from, nonce) { | ||
exports.generateAddress = function (from, nonce) { | ||
nonce = new Buffer(new BN(nonce).subn(1).toArray()) | ||
if (nonce.toString('hex') === '00') | ||
nonce = 0; | ||
if (nonce.toString('hex') === '00') { | ||
nonce = 0 | ||
} | ||
@@ -207,2 +230,23 @@ var hash = exports.sha3(rlp.encode([new Buffer(from, 'hex'), nonce])) | ||
// Returns a Boolean on whether or not the a sting starts with 0x | ||
exports.isHexPrefixed = function (str) { | ||
return str.slice(0, 2) === '0x' | ||
} | ||
// Removes 0x from a given String | ||
exports.stripHexPrefix = function (str) { | ||
if (typeof str !== 'string') { | ||
return str | ||
} | ||
return exports.isHexPrefixed(str) ? str.slice(2) : str | ||
} | ||
// Adds 0x to a given string if it does not already start with 0x | ||
exports.addHexPrefix = function (str) { | ||
if (typeof str !== 'string') { | ||
return str | ||
} | ||
return exports.isHexPrefixed(str) ? '0x' + str : str | ||
} | ||
/** | ||
@@ -214,8 +258,7 @@ * defines properties on a `Object` | ||
*/ | ||
exports.defineProperties = function(self, fields, data) { | ||
exports.defineProperties = function (self, fields, data) { | ||
self.raw = [] | ||
self._fields = [] | ||
self.toJSON = function(label){ | ||
self.toJSON = function (label) { | ||
if (label) { | ||
@@ -225,4 +268,5 @@ var obj = {} | ||
for (var prop in this) { | ||
if (typeof this[prop] !== 'function' && prop !== 'raw' && prop !== '_fields') | ||
if (typeof this[prop] !== 'function' && prop !== 'raw' && prop !== '_fields') { | ||
obj[prop] = this[prop].toString('hex') | ||
} | ||
} | ||
@@ -235,3 +279,3 @@ return obj | ||
fields.forEach(function(field, i) { | ||
fields.forEach(function (field, i) { | ||
self._fields.push(field.name) | ||
@@ -241,33 +285,18 @@ Object.defineProperty(self, field.name, { | ||
configurable: true, | ||
get: function() { | ||
get: function () { | ||
return this.raw[i] | ||
}, | ||
set: function(v) { | ||
if (!Buffer.isBuffer(v)) { | ||
if (typeof v === 'string') | ||
v = new Buffer(padToEven(exports.stripHexPrefix(v)), 'hex') | ||
else if (typeof v === 'number') | ||
v = exports.intToBuffer(v) | ||
else if (v === null || v === undefined) | ||
v = new Buffer([]) | ||
else if (v.toArray) | ||
//converts a BN to a Buffer | ||
v = new Buffer(v.toArray()) | ||
else | ||
throw new Error('invalid type') | ||
} | ||
set: function (v) { | ||
v = exports.toBuffer(v) | ||
if(v.toString('hex') === '00' && field.noZero) | ||
if (v.toString('hex') === '00' && !field.allowZero) { | ||
v = new Buffer([]) | ||
} | ||
if(field.word && new BN(v).cmp(exports.MAX_INTEGER) === 1) | ||
throw(new Error('to large of value')) | ||
if(!(field.empty && v.length === 0) && field.pad && v.length < field.length) | ||
v = exports.pad(v, field.length) | ||
if(field.allowLess && field.length){ | ||
if (field.allowLess && field.length) { | ||
v = exports.stripZeros(v) | ||
assert(field.length >= v.length) | ||
} else if (!(field.empty && v.length === 0) && field.length) | ||
} else if (!(field.empty && v.length === 0) && field.length) { | ||
assert(field.length === v.length, 'The field ' + field.name + 'must have byte length of ' + field.length) | ||
} | ||
@@ -278,27 +307,32 @@ this.raw[i] = v | ||
if(field.default) | ||
if (field.default) { | ||
self[field.name] = field.default | ||
} | ||
}) | ||
if (data) { | ||
if(typeof data === 'string') | ||
if (typeof data === 'string') { | ||
data = new Buffer(exports.stripHexPrefix(data), 'hex') | ||
} | ||
if (Buffer.isBuffer(data)) | ||
if (Buffer.isBuffer(data)) { | ||
data = rlp.decode(data) | ||
} | ||
if (Array.isArray(data)) { | ||
if(data.length > self._fields.length) | ||
throw(new Error('wrong number of fields in data')) | ||
if (data.length > self._fields.length) { | ||
throw (new Error('wrong number of fields in data')) | ||
} | ||
//make sure all the items are buffers | ||
data.forEach(function(d, i) { | ||
self[self._fields[i]] = typeof d === 'string' ? new Buffer(d, 'hex') : d | ||
// make sure all the items are buffers | ||
data.forEach(function (d, i) { | ||
self[self._fields[i]] = typeof d === 'string' ? new Buffer(d, 'hex') : d | ||
}) | ||
} else if(typeof data === 'object') { | ||
} else if (typeof data === 'object') { | ||
for (var prop in data) { | ||
if (self._fields.indexOf(prop) !== -1) | ||
if (self._fields.indexOf(prop) !== -1) { | ||
self[prop] = data[prop] | ||
} | ||
} | ||
} else{ | ||
} else { | ||
throw new Error('invalid data') | ||
@@ -314,8 +348,9 @@ } | ||
*/ | ||
exports.printBA = function(ba) { | ||
exports.printBA = function (ba) { | ||
if (Buffer.isBuffer(ba)) { | ||
if (ba.length === 0) | ||
if (ba.length === 0) { | ||
console.log('new Buffer(0)') | ||
else | ||
console.log('new Buffer(\'' + ba.toString('hex') + '\', \'hex\')') | ||
} else { | ||
console.log("new Buffer('" + ba.toString('hex') + "', 'hex')") | ||
} | ||
} else if (ba instanceof Array) { | ||
@@ -328,4 +363,5 @@ console.log('[') | ||
console.log(']') | ||
} else | ||
} else { | ||
console.log(ba) | ||
} | ||
} | ||
@@ -338,3 +374,3 @@ | ||
*/ | ||
exports.baToJSON = function(ba) { | ||
exports.baToJSON = function (ba) { | ||
if (Buffer.isBuffer(ba)) { | ||
@@ -351,24 +387,5 @@ return ba.toString('hex') | ||
//Returns a Boolean on whether or not the a sting starts with 0x | ||
exports.isHexPrefixed = function(str){ | ||
return str.slice(0, 2) === '0x' | ||
} | ||
//Removes 0x from a given String | ||
exports.stripHexPrefix = function(str){ | ||
if (typeof str !== 'string') | ||
return str | ||
return exports.isHexPrefixed(str) ? str.slice(2) : str | ||
} | ||
//Adds 0x to a given string if it does not already start with 0x | ||
exports.addHexPrefix = function(str){ | ||
if (typeof str !== 'string') | ||
return str | ||
return exports.isHexPrefixed(str) ? '0x' + str : str | ||
} | ||
function padToEven(a){ | ||
if (a.length % 2) a = '0' + a; | ||
function padToEven (a) { | ||
if (a.length % 2) a = '0' + a | ||
return a | ||
} |
{ | ||
"name": "ethereumjs-util", | ||
"version": "1.3.8", | ||
"version": "1.4.0", | ||
"description": "a collection of utility functions for Ethereum", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha ./test/index.js", | ||
"test": "standard ./index.js && mocha ./test/", | ||
"compile": "browserify index.js -s ethUtils > ethUtils.js" | ||
@@ -25,3 +25,3 @@ }, | ||
"dependencies": { | ||
"bn.js": "^3.0.1", | ||
"bn.js": "^3.1.2", | ||
"browserify-sha3": "0.0.0", | ||
@@ -47,4 +47,5 @@ "elliptic": "^5.0.0", | ||
"devDependencies": { | ||
"mocha": "^2.1.0" | ||
"mocha": "^2.1.0", | ||
"standard": "^5.2.2" | ||
} | ||
} |
138
README.md
@@ -1,2 +0,4 @@ | ||
# SYNOPSIS [![Build Status](https://travis-ci.org/ethereum/ethereumjs-util.svg)](https://travis-ci.org/ethereum/ethereumjs-util) | ||
# SYNOPSIS | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
[![Build Status](https://travis-ci.org/ethereum/ethereumjs-util.svg)](https://travis-ci.org/ethereum/ethereumjs-util) | ||
A collection of utility functions for ethereum. It can be used in node.js or can be in the browser with browserify. | ||
@@ -20,75 +22,140 @@ | ||
### `zeros(number)` | ||
Returns buffer filled with 0's | ||
- `number` - the number bytes to to return | ||
Returns buffer filled with 0's | ||
**Parameters** | ||
- `number` - the number bytes to to return | ||
**Return:** `buffer` | ||
### `pad(val, length)` | ||
pads an `array` or `buffer` with leading zeros till it has `length` bytes | ||
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 | ||
- `length` - the of the resulting value | ||
**Return:** `array` or `buffer` | ||
### `unpad(val)` | ||
Trims leading zeros from a buffer or an array | ||
- `val` - a `buffer` to unpad | ||
Trims leading zeros from a buffer or an array | ||
**Parameters** | ||
- `val` - a `buffer` or and `Array` to unpad | ||
**Return:** `buffer` or and `Array` | ||
### `toBufer(val)` | ||
Attemps to turn a value into a Buffer | ||
**Parameters** | ||
- `val` the | ||
**Return:** `Buffer` | ||
### `intToHex(int)` | ||
Converts an `Integer` into a hex `String` | ||
- `int` | ||
Converts an `Integer` into a hex `String` | ||
**Parameters** | ||
- `int` | ||
**Return:** `String` | ||
### `intToBuffer(int)` | ||
Converts an `Integer` to a `Buffer` | ||
- `int` | ||
Converts an `Integer` to a `Buffer` | ||
**Parameters** | ||
- `int` | ||
**Return:** `Buffer` | ||
### `bufferToInt(buf)` | ||
converts a `Buffer` to an `Interger` | ||
- `buf` | ||
converts a `Buffer` to an `Interger` | ||
**Parameters** | ||
- `buf` | ||
**Return:** `Interger` | ||
### `fromSigned(buf)` | ||
interpets a `Buffer` as a signed `Integer` and returns a `Bignum` | ||
- `buf` | ||
interpets a `Buffer` as a signed `Integer` | ||
**Parameters** | ||
- `buf` | ||
**Return:** [`BN.js`](https://github.com/indutny/bn.js) | ||
### `toUnsigned(num)` | ||
Converts a `bignum` to an unsigned interger and returns it as a `buffer` | ||
- `num` - a `bignum` | ||
Converts a [`BN.js`](https://github.com/indutny/bn.js) to an unsigned interger | ||
**Parameters** | ||
- `num` - a [`BN.js`](https://github.com/indutny/bn.js) | ||
**Return:** `buffer` | ||
### `publicToAddress(pubKey)` | ||
Returns the ethereum address of a given public key | ||
- `pubKey` - the public key as a `buffer` | ||
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 | ||
- `privateKey` - the private key as a `buffer` | ||
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 | ||
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. | ||
Generates an address of a newly created contract. Don't forget to incerment the nonce to get the correct address. | ||
**Parameters** | ||
- `from` - the address of the account creating the contract | ||
- `nonce` - the creating accounts nonce | ||
- `nonce` - the creating accounts nonce | ||
**Return:** `Buffer` | ||
### `sha3(a, bytes)` | ||
Returns a sha3 of `a` of the length of `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 | ||
- `bytes` how many bytes the hash should be | ||
**Return:** `Buffer` | ||
### `printBA(ba)` | ||
Print a Buffer Array | ||
- `ba` - an `Array` of `Buffers` | ||
Print a Buffer Array | ||
**Parameters** | ||
- `ba` - an `Array` of `Buffers` | ||
**Return:** a Buffer Array | ||
### `baToJSON(ba)` | ||
converts a buffer array to JSON | ||
- `ba` - an `Array` of `Buffers` | ||
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 a sting starts with `0x` | ||
Returns a Boolean on whether or not the a sting starts with `0x` | ||
**Parameters** | ||
- String - a `String` | ||
**Return:** `String` | ||
### `stripHexPrefix(String)` | ||
Removes `0x` from a given 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` | ||
Adds `0x` to a given string if it does not already start with `0x` | ||
**Parameters** | ||
- `string` | ||
**Return:** `string` | ||
### `defineProperties(self, fields)` | ||
defines properties on a `Object` | ||
**Parameters** | ||
- `self` - the `Object` to define properties on | ||
@@ -99,5 +166,8 @@ - `fields` - an array fields to define | ||
Validate defined fields | ||
**Parameters** | ||
- `fields` | ||
- `data` | ||
- `data` | ||
**Return:** `Boolean` | ||
# TESTING | ||
@@ -104,0 +174,0 @@ Node.js Tests use Mocha. Test in the browser use Testling. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71361
8
440
176
2
2
Updatedbn.js@^3.1.2