Skip to content

Commit

Permalink
Support for ERC1820 interface ids (#21)
Browse files Browse the repository at this point in the history
* Add makeInterfaceId.ERC165.

* Add makeInterfaceId.ERC1820.

* Update description.

* Add changelog entry.

* Update CHANGELOG.md

Co-Authored-By: nventuro <nicolas.venturo@gmail.com>
  • Loading branch information
nventuro committed May 3, 2019
1 parent e325766 commit 3f5850f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,11 @@
## 0.4.0 (unreleased)
* Fix `shouldFail.reverting.withMessage` on non-Ganache chains. ([#25](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/25)
* `shouldFail.reverting.withMessage` fails if no error string is provided. ([#28](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/28)
* Rename `makeInterfaceId` to `makeInterfaceId.ERC165`, and add `makeInterfaceId.ERC1820`. ([#21](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/21)

#### How to upgrade from 0.3
- Change all occurences of `makeInterfaceId` to `makeInterfaceId.ERC165`.
- Some uses of `shouldFail.reverting.withMessage` may fail now. This means it was being used incorrectly and an error string to match against should be added. Alternatively, if the error message is unknown, use `shouldFail.reverting` instead.

## 0.3.2 (2019-04-10)
* Update ERC1820Registry address. ([#26](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/26))
Expand Down
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -144,8 +144,12 @@ Same as `inLogs`, but for events emitted in an arbitrary transaction (of hash `t
---


### makeInterfaceId (interfaces = [])
Calculates the [EIP 165](https://eips.ethereum.org/EIPS/eip-165) interface ID of a contract, given a series of function signatures.
### makeInterfaceId
#### ERC165 (interfaces = [])
Calculates the [ERC165](https://eips.ethereum.org/EIPS/eip-165) interface ID of a contract, given a series of function signatures.

#### ERC1820 (name)
Calculates the [ERC1820](https://eips.ethereum.org/EIPS/eip-1820) interface hash of a contract, given its name.

---

Expand Down
17 changes: 12 additions & 5 deletions src/makeInterfaceId.js
@@ -1,8 +1,8 @@
const INTERFACE_ID_LENGTH = 4;
function ERC165 (functionSignatures = []) {
const INTERFACE_ID_LENGTH = 4;

function makeInterfaceId (interfaces = []) {
const interfaceIdBuffer = interfaces
.map(methodSignature => web3.utils.soliditySha3(methodSignature)) // keccak256
const interfaceIdBuffer = functionSignatures
.map(signature => web3.utils.soliditySha3(signature)) // keccak256
.map(h =>
Buffer
.from(h.substring(2), 'hex')
Expand All @@ -18,4 +18,11 @@ function makeInterfaceId (interfaces = []) {
return `0x${interfaceIdBuffer.toString('hex')}`;
}

module.exports = makeInterfaceId;
function ERC1820 (interfaceName) {
return web3.utils.soliditySha3(interfaceName); // keccak256
}

module.exports = {
ERC165,
ERC1820,
};
28 changes: 19 additions & 9 deletions test/src/makeInterfaceId.test.js
Expand Up @@ -5,15 +5,25 @@ const makeInterfaceId = require('../../src/makeInterfaceId');
const OwnableInterfaceId = artifacts.require('OwnableInterfaceId');

describe('makeInterfaceId', function () {
it('calculates the EIP165 interface id from function signatures', async function () {
const calculator = await OwnableInterfaceId.new();
const ownableId = await calculator.getInterfaceId();
describe('ERC165', function () {
it('calculates the interface id from function signatures', async function () {
const calculator = await OwnableInterfaceId.new();
const ownableId = await calculator.getInterfaceId();

expect(makeInterfaceId([
'owner()',
'isOwner()',
'renounceOwnership()',
'transferOwnership(address)',
])).to.equal(ownableId);
expect(makeInterfaceId.ERC165([
'owner()',
'isOwner()',
'renounceOwnership()',
'transferOwnership(address)',
])).to.equal(ownableId);
});
});

describe('ERC1820', function () {
it('calculates the interface hash a from a contract name', async function () {
expect(makeInterfaceId.ERC1820('ERC777Token')).to.equal(
'0xac7fbab5f54a3ca8194167523c6753bfeb96a445279294b6125b68cce2177054'
);
});
});
});

0 comments on commit 3f5850f

Please sign in to comment.