diff --git a/CHANGELOG.md b/CHANGELOG.md index ed45e16..9a58c54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/README.md b/README.md index 3d2512e..7388d2b 100644 --- a/README.md +++ b/README.md @@ -127,8 +127,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. --- diff --git a/src/makeInterfaceId.js b/src/makeInterfaceId.js index c1a78b1..7f82638 100644 --- a/src/makeInterfaceId.js +++ b/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') @@ -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, +}; diff --git a/test/src/makeInterfaceId.test.js b/test/src/makeInterfaceId.test.js index 8e9e1be..c29fdb6 100644 --- a/test/src/makeInterfaceId.test.js +++ b/test/src/makeInterfaceId.test.js @@ -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' + ); + }); }); });