ethereumjs-util
Advanced tools
Comparing version 0.0.5 to 0.0.6
62
index.js
@@ -203,11 +203,9 @@ const SHA3 = require('sha3'), | ||
*/ | ||
exports.defineProperties = function(self, fields) { | ||
exports.defineProperties = function(self, fields, data) { | ||
self.raw = []; | ||
self._fields = []; | ||
fields.forEach(function(field, i) { | ||
if (!field.name) { | ||
field = { | ||
name: field | ||
}; | ||
} | ||
self._fields.push(field.name); | ||
Object.defineProperty(self, field.name, { | ||
@@ -223,8 +221,14 @@ enumerable: true, | ||
v = new Buffer(v, 'hex'); | ||
} else if (v !== null) { | ||
} else if (typeof v === 'number') { | ||
v = exports.intToBuffer(v); | ||
} else if (v === null) { | ||
v = new Buffer([]); | ||
} else if (v.toBuffer) { | ||
v = v.toBuffer(); | ||
} else { | ||
throw new Error('invalid type'); | ||
} | ||
} | ||
if (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); | ||
@@ -236,19 +240,27 @@ } | ||
}); | ||
if(field.default){ | ||
self[field.name] = field.default; | ||
} | ||
}); | ||
}; | ||
/** | ||
* Validate defined fields | ||
* @method validate | ||
* @param {Array} fields | ||
* @param {Object} data | ||
*/ | ||
exports.validate = function(fields, data) { | ||
var i = 0; | ||
fields.forEach(function(f) { | ||
if (f.name && f.length) { | ||
assert(data[i].length === f.length, 'invalid data for field: ' + f.name + ' needs length:' + f.length + 'got length: ' + data[i].length); | ||
if (data) { | ||
if (Buffer.isBuffer(data)) { | ||
data = rlp.decode(data); | ||
} | ||
i++; | ||
}); | ||
if (Array.isArray(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; | ||
}); | ||
} else { | ||
for (var prop in data) { | ||
if (self._fields.indexOf(prop) !== -1) { | ||
self[prop] = data[prop]; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
@@ -325,3 +337,3 @@ | ||
var hex = this.toHex(str); | ||
while (hex.length < pad * 2){ | ||
while (hex.length < pad * 2) { | ||
hex += '00'; | ||
@@ -336,3 +348,3 @@ } | ||
val = val.length > 2 ? val.substring(2) : '0'; | ||
return bignum(val, 16).toString(10); | ||
return bignum(val, 16).toString(10); | ||
}; | ||
@@ -339,0 +351,0 @@ |
{ | ||
"name": "ethereumjs-util", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "a collection of utility functions for Ethereum", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
434
15183
6