ethereumjs-util
Advanced tools
+37
-25
@@ -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 @@ |
+1
-1
| { | ||
| "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", |
Sorry, the diff of this file is not supported yet
434
2.12%15183
-2.7%6
-14.29%