From 774e20667c48e04e339ce5e944a0a62c81afcbed Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Tue, 21 Feb 2023 20:42:39 +0200 Subject: [PATCH 1/7] Add `toDataWithIntendedValidatorHash` function in ECDSA --- contracts/utils/cryptography/ECDSA.sol | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/contracts/utils/cryptography/ECDSA.sol b/contracts/utils/cryptography/ECDSA.sol index a499b546b91..3cd58ce569c 100644 --- a/contracts/utils/cryptography/ECDSA.sol +++ b/contracts/utils/cryptography/ECDSA.sol @@ -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)); + } } From 8cd850b73bcca2f25c941bca53d4ba23620000d1 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Tue, 21 Feb 2023 20:42:48 +0200 Subject: [PATCH 2/7] Add tests to `toDataWithIntendedValidatorHash` --- test/helpers/sign.js | 16 ++++++++++++++++ test/utils/cryptography/ECDSA.test.js | 11 ++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/helpers/sign.js b/test/helpers/sign.js index 417ef591dbb..a2e09664e2d 100644 --- a/test/helpers/sign.js +++ b/test/helpers/sign.js @@ -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 + */ +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 @@ -43,5 +58,6 @@ const getSignFor = module.exports = { toEthSignedMessageHash, + toDataWithIntendedValidatorHash, getSignFor, }; diff --git a/test/utils/cryptography/ECDSA.test.js b/test/utils/cryptography/ECDSA.test.js index 3b19cde60dd..425949da04f 100644 --- a/test/utils/cryptography/ECDSA.test.js +++ b/test/utils/cryptography/ECDSA.test.js @@ -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'); @@ -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'; function to2098Format(signature) { const long = web3.utils.hexToBytes(signature); @@ -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)); + }); + }); }); From ae52cd3221a55f4312ca044369b6a55f010a4318 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Tue, 21 Feb 2023 20:45:04 +0200 Subject: [PATCH 3/7] Add what changed to the changelog --- .changeset/small-cars-appear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/small-cars-appear.md diff --git a/.changeset/small-cars-appear.md b/.changeset/small-cars-appear.md new file mode 100644 index 00000000000..c49f9d8a8ad --- /dev/null +++ b/.changeset/small-cars-appear.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': patch +--- + +Implement 0x00 version of EIP-191 in ECDSA Library From 08d4767b6ae86a4bcef7a662d5347a6b3b114ca9 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Wed, 22 Feb 2023 11:06:06 +0200 Subject: [PATCH 4/7] Change changelog message --- .changeset/small-cars-appear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/small-cars-appear.md b/.changeset/small-cars-appear.md index c49f9d8a8ad..0263bcd1854 100644 --- a/.changeset/small-cars-appear.md +++ b/.changeset/small-cars-appear.md @@ -2,4 +2,4 @@ 'openzeppelin-solidity': patch --- -Implement 0x00 version of EIP-191 in ECDSA Library +`ECDSA`: Add a function `toDataWithIntendedValidatorHash` that encodes data with version 0x00 following EIP-191. From 9b390f6a9a1209efdddc090149260211b7e2d4d7 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Wed, 22 Feb 2023 11:07:01 +0200 Subject: [PATCH 5/7] Fix wording in sign.js helper --- test/helpers/sign.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers/sign.js b/test/helpers/sign.js index a2e09664e2d..d537116bbb8 100644 --- a/test/helpers/sign.js +++ b/test/helpers/sign.js @@ -7,7 +7,7 @@ function toEthSignedMessageHash(messageHex) { /** * 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 + * @param dataHex The data to be concatenated with the prefix and signed */ function toDataWithIntendedValidatorHash(validatorAddress, dataHex) { const validatorBuffer = Buffer.from(web3.utils.hexToBytes(validatorAddress)); From 375a26b12db86999d47a5202996174fda866e166 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Wed, 22 Feb 2023 11:07:48 +0200 Subject: [PATCH 6/7] Randomize the address in ECDSA test --- test/utils/cryptography/ECDSA.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils/cryptography/ECDSA.test.js b/test/utils/cryptography/ECDSA.test.js index 425949da04f..ae737086b12 100644 --- a/test/utils/cryptography/ECDSA.test.js +++ b/test/utils/cryptography/ECDSA.test.js @@ -8,7 +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'; +const RANDOM_ADDRESS = web3.utils.toChecksumAddress(web3.utils.randomHex(20)); function to2098Format(signature) { const long = web3.utils.hexToBytes(signature); From 16d7e39fb84ec25ff380e2aa1f04fc7a04adfcf6 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Wed, 22 Feb 2023 11:36:23 +0200 Subject: [PATCH 7/7] Run linter --- contracts/utils/cryptography/ECDSA.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/utils/cryptography/ECDSA.sol b/contracts/utils/cryptography/ECDSA.sol index 3cd58ce569c..77279eb4f18 100644 --- a/contracts/utils/cryptography/ECDSA.sol +++ b/contracts/utils/cryptography/ECDSA.sol @@ -206,7 +206,7 @@ library ECDSA { } /** - * @dev Returns an Ethereum Signed Data with intended validator, created from a + * @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}.