Bouncer contract implementation (#883)
* feat: implement bouncer contracts * fix: rename Bouncer to SignatureBouncer
This commit is contained in:
119
test/access/SignatureBouncer.test.js
Normal file
119
test/access/SignatureBouncer.test.js
Normal file
@ -0,0 +1,119 @@
|
||||
|
||||
import assertRevert from '../helpers/assertRevert';
|
||||
import { signHex } from '../helpers/sign';
|
||||
|
||||
const Bouncer = artifacts.require('SignatureBouncerMock');
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-as-promised'))
|
||||
.should();
|
||||
|
||||
export const getSigner = (contract, signer, data = '') => (addr) => {
|
||||
// via: https://github.com/OpenZeppelin/zeppelin-solidity/pull/812/files
|
||||
const message = contract.address.substr(2) + addr.substr(2) + data;
|
||||
// ^ substr to remove `0x` because in solidity the address is a set of byes, not a string `0xabcd`
|
||||
return signHex(signer, message);
|
||||
};
|
||||
|
||||
contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBouncer]) => {
|
||||
before(async function () {
|
||||
this.bouncer = await Bouncer.new({ from: owner });
|
||||
this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
|
||||
this.genSig = getSigner(this.bouncer, bouncerAddress);
|
||||
});
|
||||
|
||||
it('should have a default owner of self', async function () {
|
||||
const theOwner = await this.bouncer.owner();
|
||||
theOwner.should.eq(owner);
|
||||
});
|
||||
|
||||
it('should allow owner to add a bouncer', async function () {
|
||||
await this.bouncer.addBouncer(bouncerAddress, { from: owner });
|
||||
const hasRole = await this.bouncer.hasRole(bouncerAddress, this.roleBouncer);
|
||||
hasRole.should.eq(true);
|
||||
});
|
||||
|
||||
it('should not allow anyone to add a bouncer', async function () {
|
||||
await assertRevert(
|
||||
this.bouncer.addBouncer(bouncerAddress, { from: anyone })
|
||||
);
|
||||
});
|
||||
|
||||
context('modifiers', () => {
|
||||
it('should allow valid signature for sender', async function () {
|
||||
await this.bouncer.onlyWithValidSignature(
|
||||
this.genSig(authorizedUser),
|
||||
{ from: authorizedUser }
|
||||
);
|
||||
});
|
||||
it('should not allow invalid signature for sender', async function () {
|
||||
await assertRevert(
|
||||
this.bouncer.onlyWithValidSignature(
|
||||
'abcd',
|
||||
{ from: authorizedUser }
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('signatures', () => {
|
||||
it('should accept valid message for valid user', async function () {
|
||||
const isValid = await this.bouncer.checkValidSignature(
|
||||
authorizedUser,
|
||||
this.genSig(authorizedUser)
|
||||
);
|
||||
isValid.should.eq(true);
|
||||
});
|
||||
it('should not accept invalid message for valid user', async function () {
|
||||
const isValid = await this.bouncer.checkValidSignature(
|
||||
authorizedUser,
|
||||
this.genSig(anyone)
|
||||
);
|
||||
isValid.should.eq(false);
|
||||
});
|
||||
it('should not accept invalid message for invalid user', async function () {
|
||||
const isValid = await this.bouncer.checkValidSignature(
|
||||
anyone,
|
||||
'abcd'
|
||||
);
|
||||
isValid.should.eq(false);
|
||||
});
|
||||
it('should not accept valid message for invalid user', async function () {
|
||||
const isValid = await this.bouncer.checkValidSignature(
|
||||
anyone,
|
||||
this.genSig(authorizedUser)
|
||||
);
|
||||
isValid.should.eq(false);
|
||||
});
|
||||
});
|
||||
|
||||
context('management', () => {
|
||||
it('should not allow anyone to add bouncers', async function () {
|
||||
await assertRevert(
|
||||
this.bouncer.addBouncer(newBouncer, { from: anyone })
|
||||
);
|
||||
});
|
||||
|
||||
it('should be able to add bouncers', async function () {
|
||||
await this.bouncer.addBouncer(newBouncer, { from: owner })
|
||||
.should.be.fulfilled;
|
||||
});
|
||||
|
||||
it('should not allow adding invalid address', async function () {
|
||||
await assertRevert(
|
||||
this.bouncer.addBouncer('0x0', { from: owner })
|
||||
);
|
||||
});
|
||||
|
||||
it('should not allow anyone to remove bouncer', async function () {
|
||||
await assertRevert(
|
||||
this.bouncer.removeBouncer(newBouncer, { from: anyone })
|
||||
);
|
||||
});
|
||||
|
||||
it('should be able to remove bouncers', async function () {
|
||||
await this.bouncer.removeBouncer(newBouncer, { from: owner })
|
||||
.should.be.fulfilled;
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,8 +0,0 @@
|
||||
import utils from 'ethereumjs-util';
|
||||
|
||||
// Hash and add same prefix to the hash that ganache use.
|
||||
module.exports = function (message) {
|
||||
const messageHex = Buffer.from(utils.sha3(message).toString('hex'), 'hex');
|
||||
const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + messageHex.length.toString());
|
||||
return utils.bufferToHex(utils.sha3(Buffer.concat([prefix, messageHex])));
|
||||
};
|
||||
22
test/helpers/sign.js
Normal file
22
test/helpers/sign.js
Normal file
@ -0,0 +1,22 @@
|
||||
import utils from 'ethereumjs-util';
|
||||
|
||||
/**
|
||||
* Hash and add same prefix to the hash that ganache use.
|
||||
* @param {string} message the plaintext/ascii/original message
|
||||
* @return {string} the hash of the message, prefixed, and then hashed again
|
||||
*/
|
||||
export const hashMessage = (message) => {
|
||||
const messageHex = Buffer.from(utils.sha3(message).toString('hex'), 'hex');
|
||||
const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + messageHex.length.toString());
|
||||
return utils.bufferToHex(utils.sha3(Buffer.concat([prefix, messageHex])));
|
||||
};
|
||||
|
||||
// signs message using web3 (auto-applies prefix)
|
||||
export const signMessage = (signer, message = '', options = {}) => {
|
||||
return web3.eth.sign(signer, web3.sha3(message, options));
|
||||
};
|
||||
|
||||
// signs hex string using web3 (auto-applies prefix)
|
||||
export const signHex = (signer, message = '') => {
|
||||
return signMessage(signer, message, { encoding: 'hex' });
|
||||
};
|
||||
@ -1,6 +1,13 @@
|
||||
var ECRecoveryMock = artifacts.require('ECRecoveryMock');
|
||||
|
||||
var hashMessage = require('../helpers/hashMessage.js');
|
||||
import {
|
||||
hashMessage,
|
||||
signMessage,
|
||||
} from '../helpers/sign';
|
||||
const ECRecoveryMock = artifacts.require('ECRecoveryMock');
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-as-promised'))
|
||||
.should();
|
||||
|
||||
contract('ECRecovery', function (accounts) {
|
||||
let ecrecovery;
|
||||
@ -16,8 +23,8 @@ contract('ECRecovery', function (accounts) {
|
||||
let message = web3.sha3(TEST_MESSAGE);
|
||||
// eslint-disable-next-line max-len
|
||||
let signature = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be89200';
|
||||
await ecrecovery.recover(message, signature);
|
||||
assert.equal(signer, await ecrecovery.addrRecovered());
|
||||
const addrRecovered = await ecrecovery.recover(message, signature);
|
||||
addrRecovered.should.eq(signer);
|
||||
});
|
||||
|
||||
it('recover v1', async function () {
|
||||
@ -26,34 +33,45 @@ contract('ECRecovery', function (accounts) {
|
||||
let message = web3.sha3(TEST_MESSAGE);
|
||||
// eslint-disable-next-line max-len
|
||||
let signature = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e001';
|
||||
await ecrecovery.recover(message, signature);
|
||||
assert.equal(signer, await ecrecovery.addrRecovered());
|
||||
const addrRecovered = await ecrecovery.recover(message, signature);
|
||||
addrRecovered.should.eq(signer);
|
||||
});
|
||||
|
||||
it('recover using web3.eth.sign()', async function () {
|
||||
// Create the signature using account[0]
|
||||
const signature = web3.eth.sign(accounts[0], web3.sha3(TEST_MESSAGE));
|
||||
const signature = signMessage(accounts[0], TEST_MESSAGE);
|
||||
|
||||
// Recover the signer address from the generated message and signature.
|
||||
await ecrecovery.recover(hashMessage(TEST_MESSAGE), signature);
|
||||
assert.equal(accounts[0], await ecrecovery.addrRecovered());
|
||||
const addrRecovered = await ecrecovery.recover(
|
||||
hashMessage(TEST_MESSAGE),
|
||||
signature
|
||||
);
|
||||
addrRecovered.should.eq(accounts[0]);
|
||||
});
|
||||
|
||||
it('recover using web3.eth.sign() should return wrong signer', async function () {
|
||||
// Create the signature using account[0]
|
||||
const signature = web3.eth.sign(accounts[0], web3.sha3(TEST_MESSAGE));
|
||||
const signature = signMessage(accounts[0], TEST_MESSAGE);
|
||||
|
||||
// Recover the signer address from the generated message and wrong signature.
|
||||
await ecrecovery.recover(hashMessage('Test'), signature);
|
||||
assert.notEqual(accounts[0], await ecrecovery.addrRecovered());
|
||||
const addrRecovered = await ecrecovery.recover(hashMessage('Test'), signature);
|
||||
assert.notEqual(accounts[0], addrRecovered);
|
||||
});
|
||||
|
||||
it('recover should fail when a wrong hash is sent', async function () {
|
||||
// Create the signature using account[0]
|
||||
let signature = web3.eth.sign(accounts[0], web3.sha3(TEST_MESSAGE));
|
||||
let signature = signMessage(accounts[0], TEST_MESSAGE);
|
||||
|
||||
// Recover the signer address from the generated message and wrong signature.
|
||||
await ecrecovery.recover(hashMessage(TEST_MESSAGE).substring(2), signature);
|
||||
assert.equal('0x0000000000000000000000000000000000000000', await ecrecovery.addrRecovered());
|
||||
const addrRecovered = await ecrecovery.recover(hashMessage(TEST_MESSAGE).substring(2), signature);
|
||||
addrRecovered.should.eq('0x0000000000000000000000000000000000000000');
|
||||
});
|
||||
|
||||
context('toEthSignedMessage', () => {
|
||||
it('should prefix hashes correctly', async function () {
|
||||
const hashedMessage = web3.sha3(TEST_MESSAGE);
|
||||
const ethMessage = await ecrecovery.toEthSignedMessageHash(hashedMessage);
|
||||
ethMessage.should.eq(hashMessage(TEST_MESSAGE));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user