Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup and refactor
  • Loading branch information
aflesher committed Jun 5, 2018
commit 006db160496e767df4813ec110f6ccbe5171ad0b
1 change: 1 addition & 0 deletions contracts/access/SignatureBouncer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contract SignatureBouncer is Ownable, RBAC {

string public constant ROLE_BOUNCER = "bouncer";
uint constant METHOD_ID_SIZE = 4;
// (signature length size) 32 bytes + (signature size 65 bytes padded) 96 bytes
uint constant SIG_SIZE = 128;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why SIG instead of SIGNATURE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just keeping this variable name consistent with our _sig params naming. SIGNATURE does make more sense though, I'll change it.


/**
Expand Down
12 changes: 1 addition & 11 deletions contracts/mocks/BouncerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract SignatureBouncerMock is SignatureBouncer {

}

function checkValidSignatureAndData(address _address, uint _val, bytes _sig)
function checkValidSignatureAndData(address _address, bytes _bytes, uint _val, bytes _sig)
public
view
returns (bool)
Expand All @@ -51,14 +51,4 @@ contract SignatureBouncerMock is SignatureBouncer {
{

}

function checkValidSignatureAndDataBytes(address _address, bytes _bytes, uint _val, bytes _sig)
public
view
returns (bool)
{
return isValidSignatureAndData(_address, _sig);
}

function msgData(address _address, bytes _bytes, uint _val, bytes _sig) public view returns(bytes) { return msg.data; }
}
54 changes: 25 additions & 29 deletions test/access/SignatureBouncer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBounc
this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
this.genSig = getSigner(this.bouncer, bouncerAddress);
this.uintValue = 23;
this.checkValidSignatureAndDataId = getMethodId('checkValidSignatureAndData', 'address', 'uint256', 'bytes');
this.checkValidSignatureAndMethodId = getMethodId('checkValidSignatureAndMethod', 'address', 'bytes');
this.uintValueData = stripAndPadHexValue(web3.toHex(this.uintValue), 32);
this.authorizedUserData = stripAndPadHexValue(authorizedUser, 32);
this.sigIntLocation = stripAndPadHexValue(web3.toHex(64), 32);
this.sigAddressIntLocation = stripAndPadHexValue(web3.toHex(96), 32);
this.bytesValue = web3.toHex('bytesValue');
this.validateSignatureAndDataMsgData = [
getMethodId('checkValidSignatureAndData', 'address', 'bytes', 'uint256', 'bytes'),
stripAndPadHexValue(authorizedUser, 32),
stripAndPadHexValue(web3.toHex(32 * 4), 32), // bytesValue location
this.uintValueData,
stripAndPadHexValue(web3.toHex(32 * 6), 32), // sig location
stripAndPadHexValue(web3.toHex(this.bytesValue.substr(2).length / 2), 32), // bytesValue size
stripAndPadHexValue(this.bytesValue, 32, false) // bytesValue
]
});

it('should have a default owner of self', async function () {
Expand Down Expand Up @@ -95,7 +102,11 @@ contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBounc
const sig = getSigner(
this.bouncer,
bouncerAddress,
`${getMethodId('onlyWithValidSignatureAndData', 'uint256', 'bytes')}${this.uintValueData}${this.sigIntLocation}`
[
getMethodId('onlyWithValidSignatureAndData', 'uint256', 'bytes'),
this.uintValueData,
stripAndPadHexValue(web3.toHex(64), 32)
].join('')
)(authorizedUser);
await this.bouncer.onlyWithValidSignatureAndData(
this.uintValue,
Expand Down Expand Up @@ -179,47 +190,30 @@ contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBounc
);
isValid.should.eq(false);
});
it('should accept valid method with valid params for valid user (address, uint, _sig)', async function () {
it('should accept valid method with valid params for valid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
`${this.checkValidSignatureAndDataId}${this.authorizedUserData}${this.uintValueData}${this.sigAddressIntLocation}`
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
authorizedUser,
this.uintValue,
sig
);
isValid.should.eq(true);
});
it('should accept valid method with valid params for valid user (address, bytes, uint, _sig)', async function () {
const bytesVal = web3.toHex('bytesValue');
const bytesLoc = stripAndPadHexValue(web3.toHex(32 * 4), 32);
const sigLoc = stripAndPadHexValue(web3.toHex(32 * 6), 32);
const bytesValSize = stripAndPadHexValue(web3.toHex(bytesVal.substr(2).length / 2), 32);
const bytesValData = stripAndPadHexValue(bytesVal, 32, false);
const methodId = getMethodId('checkValidSignatureAndDataBytes', 'address', 'bytes', 'uint256', 'bytes');
const sig = getSigner(
this.bouncer,
bouncerAddress,
`${methodId}${this.authorizedUserData}${bytesLoc}${this.uintValueData}${sigLoc}${bytesValSize}${bytesValData}`
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndDataBytes(
authorizedUser,
bytesVal,
this.bytesValue,
this.uintValue,
sig
);
isValid.should.eq(true);
});
it('should not accept an invalid method with valid params for valid user', async function () {
this.validateSignatureAndDataMsgData[0] = getMethodId('invalidMethod', 'address', 'bytes', 'uint256', 'bytes');
const sig = getSigner(
this.bouncer,
bouncerAddress,
`${getMethodId('invalidMethod', 'address', 'uint256', 'bytes')}${this.authorizedUserData}${this.uintValueData}${this.sigAddressIntLocation}`
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
authorizedUser,
this.bytesValue,
this.uintValue,
sig
);
Expand All @@ -229,10 +223,11 @@ contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBounc
const sig = getSigner(
this.bouncer,
bouncerAddress,
`${this.checkValidSignatureAndDataId}${this.authorizedUserData}${this.uintValueData}${this.sigAddressIntLocation}`
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
authorizedUser,
this.bytesValue,
500,
sig
);
Expand All @@ -242,10 +237,11 @@ contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBounc
const sig = getSigner(
this.bouncer,
bouncerAddress,
`${this.checkValidSignatureAndDataId}${this.authorizedUserData}${this.uintValueData}${this.sigAddressIntLocation}`
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
anyone,
this.bytesValue,
this.uintValue,
sig
);
Expand Down