From 435932a760b95fd37bf07a3da46421a6cace0608 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 5 Jan 2016 20:40:36 +0000 Subject: [PATCH] Introduce isPrecompiled() --- README.md | 7 +++++++ index.js | 12 +++++++++++- test/index.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a5287c..4007586 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,13 @@ Generates an address of a newly created contract. Don't forget to increment the **Return:** `Buffer` +### `isPrecompiled(address)` +**Parameters** +- `address` - the address to check (`Buffer` or `String`) +Return true if the supplied address belongs to a precompiled account + +**Return:** `Boolean` + ### `sha3(a, bytes)` Returns a sha3 of `a` of the length of `bytes` **Parameters** diff --git a/index.js b/index.js index b530794..e5273d5 100644 --- a/index.js +++ b/index.js @@ -292,6 +292,16 @@ exports.generateAddress = function (from, nonce) { return hash.slice(12) } +/** + * Returns true if the supplied address belongs to a precompiled account + * @method isPrecompiled + * @param {Buffer|String} + */ +exports.isPrecompiled = function (address) { + var a = exports.unpad(address) + return a.length === 1 && a[0] > 0 && a[0] < 5 +} + // Returns a Boolean on whether or not the a sting starts with 0x exports.isHexPrefixed = function (str) { return str.slice(0, 2) === '0x' @@ -317,7 +327,7 @@ exports.addHexPrefix = function (str) { /** * defines properties on a `Object` * @method defineProperties - * @para[M`Êm {Object} self the `Object` to define properties on + * @param {Object} self the `Object` to define properties on * @param {Array} fields an array fields to define */ exports.defineProperties = function (self, fields, data) { diff --git a/test/index.js b/test/index.js index dc711d4..05b511f 100644 --- a/test/index.js +++ b/test/index.js @@ -204,3 +204,17 @@ describe('hex prefix', function () { assert.equal(ethUtils.addHexPrefix(string), '0x' + string) }) }) + +describe('isPrecompiled', function() { + it('should return true', function () { + assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000001'), true) + assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000002'), true) + assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000003'), true) + assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000004'), true) + }) + it('should return false', function () { + assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000000'), false) + assert.equal(ethUtils.isPrecompiled('0000000000000000000000000000000000000005'), false) + assert.equal(ethUtils.isPrecompiled('1000000000000000000000000000000000000000'), false) + }) +})