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 1.0.3 to 1.1.0

392

index.js

@@ -1,46 +0,23 @@

const SHA3 = require('sha3'),
ec = require('elliptic').ec('secp256k1');
assert = require('assert'),
rlp = require('rlp'),
BN = require('bn.js');
const SHA3 = require('sha3')
const ec = require('elliptic').ec('secp256k1')
const assert = require('assert')
const rlp = require('rlp')
const BN = require('bn.js')
//the max interger that this VM can handle
const MAX_INTEGER = exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16);
exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)
exports.TWO_POW256 = new BN('115792089237316195423570985008687907853269984665640564039457584007913129639936')
const TWO_POW256 = exports.TWO_POW256 = new BN('115792089237316195423570985008687907853269984665640564039457584007913129639936');
//hex string of SHA3-256 hash of `null`
exports.SHA3_NULL = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
exports.SHA3_NULL = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
//SHA3-256 hash of the rlp of []
exports.SHA3_RLP_ARRAY = '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347';
exports.SHA3_RLP_ARRAY = '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'
//SHA3-256 hash of the rlp of `null`
exports.SHA3_RLP = '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421';
exports.SHA3_RLP = '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'
const ETH_UNITS = exports.ETH_UNITS = [
'wei',
'Kwei',
'Mwei',
'Gwei',
'szabo',
'finney',
'ether',
'grand',
'Mether',
'Gether',
'Tether',
'Pether',
'Eether',
'Zether',
'Yether',
'Nether',
'Dether',
'Vether',
'Uether'
];
exports.BN = BN
exports.elliptic = ec
exports.BN = BN;
/**

@@ -53,6 +30,4 @@ * Returns a buffer filled with 0s

exports.zeros = function(bytes) {
var buf = new Buffer(bytes);
buf.fill(0);
return buf;
};
return new Buffer(bytes).fill(0)
}

@@ -66,13 +41,10 @@ /**

*/
var pad = exports.pad = function(msg, length) {
var buf;
exports.pad = function(msg, length) {
if (msg.length < length) {
buf = new Buffer(length);
buf.fill(0);
msg.copy(buf, length - msg.length);
return buf;
} else {
return msg.slice(-length);
}
};
var buf = exports.zeros(length)
msg.copy(buf, length - msg.length)
return buf
}
return msg.slice(-length)
}

@@ -86,9 +58,9 @@ /**

exports.unpad = function(a) {
var first = a[0];
var first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1);
first = a[0];
a = a.slice(1)
first = a[0]
}
return a;
};
return a
}

@@ -102,10 +74,10 @@ /**

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);
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) {
hex = '0' + hex;
hex = '0' + hex
}
return hex;
};
return hex
}

@@ -119,5 +91,5 @@ /**

exports.intToBuffer = function(i) {
var hex = exports.intToHex(i);
return new Buffer(hex, 'hex');
};
var hex = exports.intToHex(i)
return new Buffer(hex, 'hex')
}

@@ -131,7 +103,7 @@ /**

exports.bufferToInt = function(buf) {
if (buf.length === 0) {
return 0;
}
return parseInt(buf.toString('hex'), 16);
};
if (buf.length === 0)
return 0
return parseInt(buf.toString('hex'), 16)
}

@@ -145,9 +117,8 @@ /**

exports.fromSigned = function(num) {
if (num.length === 32 && num[0] >= 128) {
return new BN(num).sub(TWO_POW256);
} else {
return new BN(num);
}
};
if (num.length === 32 && num[0] >= 128)
return new BN(num).sub(exports.TWO_POW256)
return new BN(num)
}
/**

@@ -160,8 +131,7 @@ * Converts a bignum to an unsigned interger and returns it as a buffer

exports.toUnsigned = function(num) {
if (num.cmp(new BN(0)) === -1) {
return new Buffer(num.add(TWO_POW256).toArray());
} else {
return new Buffer(num.toArray());
}
};
if (num.cmpn(0) === -1)
return new Buffer(num.add(exports.TWO_POW256).toArray())
return new Buffer(num.toArray())
}

@@ -171,9 +141,9 @@ exports.sha3 = function(a, bytes) {

var h = new SHA3.SHA3Hash(bytes);
if (a) {
h.update(a);
}
return new Buffer(h.digest('hex'), 'hex');
};
var h = new SHA3.SHA3Hash(bytes)
if (a)
h.update(a)
return new Buffer(h.digest('hex'), 'hex')
}
/**

@@ -185,7 +155,7 @@ * Returns the ethereum address of a given public key

*/
var pubToAddress = exports.pubToAddress = exports.publicToAddress = function(pubKey) {
var hash = new SHA3.SHA3Hash(256);
hash.update(pubKey.slice(-64));
return new Buffer(hash.digest('hex').slice(-40), 'hex');
};
exports.pubToAddress = exports.publicToAddress = function(pubKey) {
var hash = new SHA3.SHA3Hash(256)
hash.update(pubKey.slice(-64))
return new Buffer(hash.digest('hex').slice(-40), 'hex')
}

@@ -199,6 +169,6 @@ /**

var privateToPublic = exports.privateToPublic = function(privateKey){
privateKey = new BN(privateKey);
var key = ec.keyFromPrivate(privateKey).getPublic().toJSON();
key = new Buffer(key[0].toArray().concat(key[1].toArray()));
return key;
privateKey = new BN(privateKey)
var key = ec.keyFromPrivate(privateKey).getPublic().toJSON()
key = new Buffer(key[0].toArray().concat(key[1].toArray()))
return key
}

@@ -213,3 +183,3 @@

exports.privateToAddress = function(privateKey){
return pubToAddress(privateToPublic(privateKey));
return exports.publicToAddress(privateToPublic(privateKey))
}

@@ -224,12 +194,10 @@

exports.generateAddress = function(from, nonce) {
nonce = new Buffer(new BN(nonce).subn(1).toArray())
nonce = new Buffer(new BN(nonce).sub(new BN(1)).toArray());
if (nonce.toString('hex') === '00') {
if (nonce.toString('hex') === '00')
nonce = 0;
}
var hash = new SHA3.SHA3Hash(256);
hash.update(rlp.encode([new Buffer(from, 'hex'), nonce]));
return new Buffer(hash.digest('hex').slice(24), 'hex');
};
var hash = exports.sha3(rlp.encode([new Buffer(from, 'hex'), nonce]))
return hash.slice(12)
}

@@ -244,22 +212,21 @@ /**

self.raw = [];
self._fields = [];
self.raw = []
self._fields = []
self.toJSON = function(label){
if (label) {
var obj = {};
var obj = {}
for (var prop in this) {
if (typeof this[prop] !== 'function' && prop !== 'raw' && prop !== '_fields')
obj[prop] = this[prop].toString('hex');
obj[prop] = this[prop].toString('hex')
}
return obj;
} else {
return baToJSON(this.raw);
return obj
}
};
return exports.baToJSON(this.raw)
}
fields.forEach(function(field, i) {
self._fields.push(field.name);
self._fields.push(field.name)
Object.defineProperty(self, field.name, {

@@ -269,64 +236,58 @@ enumerable: true,

get: function() {
return this.raw[i];
return this.raw[i]
},
set: function(v) {
if (!Buffer.isBuffer(v)) {
if (typeof v === 'string') {
v= stripHex(v);
v = new Buffer(v, 'hex');
} else if (typeof v === 'number') {
v = exports.intToBuffer(v);
} else if (v === null || v === undefined) {
v = new Buffer([]);
} else if (v.toArray) {
v = new Buffer(v.toArray());
} else {
throw new Error('invalid type');
}
if (typeof v === 'string')
v = new Buffer(exports.stripHex(v), 'hex')
else if (typeof v === 'number')
v = exports.intToBuffer(v)
else if (v === null || v === undefined)
v = new Buffer([])
else if (v.toArray)
v = new Buffer(v.toArray())
else
throw new Error('invalid type')
}
if(v.toString('hex') === '00' && field.noZero) v = new Buffer([]);
if(v.toString('hex') === '00' && field.noZero)
v = new Buffer([])
if(field.word && new BN(v).cmp(MAX_INTEGER) === 1){
throw('to large of value');
}
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 = pad(v, field.length);
}
if(!(field.empty && v.length === 0) && field.pad && v.length < field.length)
v = exports.pad(v, field.length)
if (!(field.empty && v.length === 0) && field.length) {
assert(field.length === v.length, 'The field ' + field.name + 'must have byte length of ' + field.length);
}
if (!(field.empty && v.length === 0) && field.length)
assert(field.length === v.length, 'The field ' + field.name + 'must have byte length of ' + field.length)
this.raw[i] = v;
this.raw[i] = v
}
});
})
if(field.default){
self[field.name] = field.default;
}
if(field.default)
self[field.name] = field.default
})
});
if (data) {
if (Buffer.isBuffer(data)) {
data = rlp.decode(data);
}
if (Buffer.isBuffer(data))
data = rlp.decode(data)
if (Array.isArray(data)) {
if(data.length > self._fields.length) throw('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;
});
self[self._fields[i]] = typeof d === 'string' ? new Buffer(d, 'hex') : d
})
} else {
for (var prop in data) {
if (self._fields.indexOf(prop) !== -1) {
self[prop] = data[prop];
}
if (self._fields.indexOf(prop) !== -1)
self[prop] = data[prop]
}
}
}
};
}

@@ -340,18 +301,16 @@ /**

if (Buffer.isBuffer(ba)) {
if (ba.length === 0) {
console.log('new Buffer(0)');
} else {
console.log('new Buffer(\'' + ba.toString('hex') + '\', \'hex\')');
}
if (ba.length === 0)
console.log('new Buffer(0)')
else
console.log('new Buffer(\'' + ba.toString('hex') + '\', \'hex\')')
} else if (ba instanceof Array) {
console.log('[');
console.log('[')
for (var i = 0; i < ba.length; i++) {
exports.printBA(ba[i]);
console.log(',');
exports.printBA(ba[i])
console.log(',')
}
console.log(']');
} else {
console.log(ba);
}
};
console.log(']')
} else
console.log(ba)
}

@@ -363,100 +322,22 @@ /**

*/
const baToJSON = exports.baToJSON = function(ba) {
exports.baToJSON = function(ba) {
if (Buffer.isBuffer(ba)) {
return ba.toString('hex');
return ba.toString('hex')
} else if (ba instanceof Array) {
var array = [];
var array = []
for (var i = 0; i < ba.length; i++) {
array.push(exports.baToJSON(ba[i]));
array.push(exports.baToJSON(ba[i]))
}
return array;
return array
}
};
}
/// @returns ascii string representation of hex value prefixed with 0x
exports.toAscii = function(hex) {
// Find termination
var str = '';
var i = 0,
l = hex.length;
if (hex.substring(0, 2) === '0x')
i = 2;
for (; i < l; i += 2) {
var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) {
break;
}
str += String.fromCharCode(code);
}
return str;
};
/// @returns hex representation (prefixed by 0x) of ascii string
exports.fromAscii = function(str, pad) {
pad = pad === undefined ? 0 : pad;
var hex = this.toHex(str);
while (hex.length < pad * 2) {
hex += '00';
}
return '0x' + hex;
};
/// @returns decimal representaton of hex value prefixed by 0x
exports.toDecimal = function(val) {
// remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : '0';
return new BN(val, 16).toString(10);
};
// @returns hex representation (prefixed by 0x) of decimal value
exports.fromDecimal = function(val) {
return '0x' + new BN(val).toString(16);
};
exports.toHex = function(str) {
var hex = '';
for (var i = 0; i < str.length; i++) {
var n = str.charCodeAt(i).toString(16);
hex += n.length < 2 ? '0' + n : n;
}
return hex;
};
/// used to transform value/string to eth string
exports.toEth = function(str) {
var val = typeof str === 'string' ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
var unit = 0;
var units = ETH_UNITS;
while (val > 3000 && unit < units.length - 1) {
val /= 1000;
unit++;
}
var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
var replaceFunction = function($0, $1, $2) {
return $1 + ',' + $2;
};
while (true) {
var o = s;
s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
if (o === s)
break;
}
return s + ' ' + units[unit];
};
const isHexPrefixed = exports.isHexPrefixed = function(str){
return str.slice(0, 2) === '0x';
exports.isHexPrefixed = function(str){
return str.slice(0, 2) === '0x'
}
const stripHex = exports.stripHex = function(str){
exports.stripHex = function(str){
if (typeof str !== 'string')
return str
return isHexPrefixed ? str.slice(2) : str
return exports.isHexPrefixed(str) ? str.slice(2) : str
}

@@ -467,4 +348,3 @@

return str
return isHexPrefixed(str) ? '0x' + str : str
return exports.isHexPrefixed(str) ? '0x' + str : str
}
{
"name": "ethereumjs-util",
"version": "1.0.3",
"version": "1.1.0",
"description": "a collection of utility functions for Ethereum",

@@ -5,0 +5,0 @@ "main": "index.js",

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