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

Implement 0x00 version of EIP-191 in ECDSA Library #4063

Merged
merged 7 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 .changeset/small-cars-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': patch
---

Implement 0x00 version of EIP-191 in ECDSA Library
YamenMerhi marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions contracts/utils/cryptography/ECDSA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,14 @@ library ECDSA {
data := keccak256(ptr, 0x42)
}
}

/**
* @dev Returns an Ethereum Signed Data with intended validator, created from a
* `validator` and `data` according to the version 0 of EIP-191.
*
* See {recover}.
*/
function toDataWithIntendedValidatorHash(address validator, bytes memory data) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x00", validator, data));
}
}
16 changes: 16 additions & 0 deletions test/helpers/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ function toEthSignedMessageHash(messageHex) {
return web3.utils.sha3(Buffer.concat([prefix, messageBuffer]));
}

/**
* Create a signed data with intended validator according to the version 0 of EIP-191
* @param validatorAddress The address of the validator
* @param dataHex The data to be concatened with the prefix and signed
YamenMerhi marked this conversation as resolved.
Show resolved Hide resolved
*/
function toDataWithIntendedValidatorHash(validatorAddress, dataHex) {
const validatorBuffer = Buffer.from(web3.utils.hexToBytes(validatorAddress));
const dataBuffer = Buffer.from(web3.utils.hexToBytes(dataHex));
const preambleBuffer = Buffer.from('\x19');
const versionBuffer = Buffer.from('\x00');
const ethMessage = Buffer.concat([preambleBuffer, versionBuffer, validatorBuffer, dataBuffer]);

return web3.utils.sha3(ethMessage);
}

/**
* Create a signer between a contract and a signer for a voucher of method, args, and redeemer
* Note that `method` is the web3 method, not the truffle-contract method
Expand Down Expand Up @@ -43,5 +58,6 @@ const getSignFor =

module.exports = {
toEthSignedMessageHash,
toDataWithIntendedValidatorHash,
getSignFor,
};
11 changes: 10 additions & 1 deletion test/utils/cryptography/ECDSA.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expectRevert } = require('@openzeppelin/test-helpers');
const { toEthSignedMessageHash } = require('../../helpers/sign');
const { toEthSignedMessageHash, toDataWithIntendedValidatorHash } = require('../../helpers/sign');

const { expect } = require('chai');

Expand All @@ -8,6 +8,7 @@ const ECDSA = artifacts.require('$ECDSA');
const TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
const WRONG_MESSAGE = web3.utils.sha3('Nope');
const NON_HASH_MESSAGE = '0x' + Buffer.from('abcd').toString('hex');
const RANDOM_ADDRESS = '0xA9d8C83D404d3397aDE08321E7551c8B36dbF4Ab';
Amxx marked this conversation as resolved.
Show resolved Hide resolved

function to2098Format(signature) {
const long = web3.utils.hexToBytes(signature);
Expand Down Expand Up @@ -248,4 +249,12 @@ contract('ECDSA', function (accounts) {
);
});
});

context('toDataWithIntendedValidatorHash', function () {
it('returns the hash correctly', async function () {
expect(
await this.ecdsa.methods['$toDataWithIntendedValidatorHash(address,bytes)'](RANDOM_ADDRESS, NON_HASH_MESSAGE),
).to.equal(toDataWithIntendedValidatorHash(RANDOM_ADDRESS, NON_HASH_MESSAGE));
});
});
});