Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ERC1820 interface ids #21

Merged
merged 6 commits into from May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
nventuro marked this conversation as resolved.
Show resolved Hide resolved

#### 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 @@ -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.

---

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'
);
});
});
});