ethereumjs-util
Advanced tools
Comparing version 0.0.7 to 0.0.8
69
index.js
const SHA3 = require('sha3'), | ||
assert = require('assert'), | ||
rlp = require('rlp'), | ||
bignum = require('bignum'); | ||
BN = require('bn.js'); | ||
//the max interger that this VM can handle | ||
exports.MAX_INTEGER = bignum('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16); | ||
var MAX_INTEGER = exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16); | ||
var TWO_POW256 = exports.TWO_POW256 = bignum('115792089237316195423570985008687907853269984665640564039457584007913129639936'); | ||
var TWO_POW256 = exports.TWO_POW256 = new BN('115792089237316195423570985008687907853269984665640564039457584007913129639936'); | ||
@@ -43,2 +43,4 @@ //hex string of SHA3-256 hash of `null` | ||
exports.BN = BN; | ||
/** | ||
@@ -48,3 +50,3 @@ * Returns a buffer filled with 0s | ||
* @param {Integer} bytes the number of bytes the buffer should be | ||
* @return {Buffer} | ||
*[MaÆ @return {Buffer} | ||
*/ | ||
@@ -61,6 +63,6 @@ exports.zeros = function(bytes) { | ||
* @param {Buffer|Array} array | ||
* @param {Integer} length the number of bytes the output should be | ||
* @pa[MaÆram {Integer} length the number of bytes the output should be | ||
* @return {Buffer|Array} | ||
*/ | ||
exports.pad = function(msg, length) { | ||
var pad = exports.pad = function(msg, length) { | ||
var buf; | ||
@@ -122,3 +124,3 @@ if (msg.length < length) { | ||
* @method bufferToInt | ||
* @param {Buffer} | ||
* @par[MaÅam {B[M`Êuffer} | ||
* @return {Number} | ||
@@ -137,9 +139,9 @@ */ | ||
* @param {Buffer} num | ||
* @return {Bignum} | ||
* @return {BN} | ||
*/ | ||
exports.fromSigned = function(num) { | ||
if (num.length === 32 && num[0] >= 128) { | ||
return bignum.fromBuffer(num).sub(TWO_POW256); | ||
return new BN(num).sub(TWO_POW256); | ||
} else { | ||
return bignum.fromBuffer(num); | ||
return new BN(num); | ||
} | ||
@@ -155,6 +157,6 @@ }; | ||
exports.toUnsigned = function(num) { | ||
if (num.lt(0)) { | ||
return num.add(TWO_POW256).toBuffer(); | ||
if (num.cmp(new BN(0)) === -1) { | ||
return new Buffer(num.add(TWO_POW256).toArray()); | ||
} else { | ||
return num.toBuffer(); | ||
return new Buffer(num.toArray()); | ||
} | ||
@@ -193,3 +195,3 @@ }; | ||
nonce = bignum.fromBuffer(nonce).sub(1).toBuffer(); | ||
nonce = new Buffer(new BN(nonce).sub(new BN(1)).toArray()); | ||
if (nonce.toString('hex') === '00') { | ||
@@ -207,3 +209,3 @@ nonce = 0; | ||
* @method defineProperties | ||
* @param {Object} self the `Object` to define properties on | ||
* @para[M`Êm {Object} self the `Object` to define properties on | ||
* @param {Array} fields an array fields to define | ||
@@ -216,2 +218,17 @@ */ | ||
self.toJSON = function(label){ | ||
if (label) { | ||
var obj = {}; | ||
for (var prop in this) { | ||
if (typeof this[prop] !== 'function' && prop !== 'raw' && prop !== '_fields') | ||
obj[prop] = this[prop].toString('hex'); | ||
} | ||
return obj; | ||
} else { | ||
return baToJSON(this.raw); | ||
} | ||
}; | ||
fields.forEach(function(field, i) { | ||
@@ -233,4 +250,4 @@ self._fields.push(field.name); | ||
v = new Buffer([]); | ||
} else if (v.toBuffer) { | ||
v = v.toBuffer(); | ||
} else if (v.toArray) { | ||
v = new Buffer(v.toArray()); | ||
} else { | ||
@@ -241,3 +258,12 @@ throw new Error('invalid type'); | ||
if(field.word && new BN(v).cmp(MAX_INTEGER) === 1){ | ||
throw('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.length) { | ||
assert(field.length === v.length, 'The field ' + field.name + 'must have byte length of ' + field.length); | ||
@@ -262,2 +288,3 @@ } | ||
if (Array.isArray(data)) { | ||
if(data.length > self._fields.length) throw('wrong number of fields in data') | ||
//make sure all the items are buffers | ||
@@ -306,3 +333,3 @@ data.forEach(function(d, i) { | ||
*/ | ||
exports.baToJSON = function(ba) { | ||
const baToJSON = exports.baToJSON = function(ba) { | ||
if (Buffer.isBuffer(ba)) { | ||
@@ -319,4 +346,2 @@ return ba.toString('hex'); | ||
/// @returns ascii string representation of hex value prefixed with 0x | ||
@@ -358,3 +383,3 @@ exports.toAscii = function(hex) { | ||
val = val.length > 2 ? val.substring(2) : '0'; | ||
return bignum(val, 16).toString(10); | ||
return new BN(val, 16).toString(10); | ||
}; | ||
@@ -364,3 +389,3 @@ | ||
exports.fromDecimal = function(val) { | ||
return '0x' + bignum(val).toString(16); | ||
return '0x' + new BN(val).toString(16); | ||
}; | ||
@@ -367,0 +392,0 @@ |
{ | ||
"name": "ethereumjs-util", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "a collection of utility functions for Ethereum", | ||
@@ -25,4 +25,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"bignum": "^0.9.1", | ||
"bignum-browserify": "0.0.1", | ||
"bn.js": "^2.0.1", | ||
"crypto-js": "^3.1.2-5", | ||
"rlp": "0.0.12", | ||
@@ -32,4 +32,3 @@ "sha3": "^1.1.0" | ||
"browser": { | ||
"sha3": "./browser/sha3.js", | ||
"bignum": "bignum-browserify" | ||
"sha3": "./browser/sha3.js" | ||
}, | ||
@@ -36,0 +35,0 @@ "testling": { |
var assert = require('assert'); | ||
var ethUtils = require('../index.js'); | ||
var bignum = require('bignum'); | ||
var BN = require('bn.js'); | ||
@@ -77,3 +77,3 @@ describe('zeros function', function(){ | ||
var hex = 'ff00000000000000000000000000000000000000000000000000000000000000'; | ||
var num = bignum(neg); | ||
var num = new BN(neg); | ||
@@ -80,0 +80,0 @@ assert.equal(ethUtils.toUnsigned(num).toString('hex'), hex); |
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
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
16694
454
+ Addedbn.js@^2.0.1
+ Addedcrypto-js@^3.1.2-5
+ Addedbn.js@2.2.0(transitive)
+ Addedcrypto-js@3.3.0(transitive)
- Removedbignum@^0.9.1
- Removedbignum-browserify@0.0.1
- Removedbignum@0.9.3(transitive)
- Removedbignum-browserify@0.0.1(transitive)
- Removedjsbn@0.0.0(transitive)
- Removednan@1.8.4(transitive)