Skip to content

Commit

Permalink
Introduce isPrecompiled()
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Jan 5, 2016
1 parent f9a3977 commit 435932a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 435932a

Please sign in to comment.