Skip to content

Commit

Permalink
Use EIP-191 for message signing
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed Mar 28, 2024
1 parent 5706812 commit e04ea15
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
12 changes: 11 additions & 1 deletion contracts/reverseRegistrar/L2ReverseRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,18 @@ contract L2ReverseRegistrar is
uint256 inceptionDate,
bytes memory signature
) internal view returns (bool) {
// Follow ERC191 version 0 https://eips.ethereum.org/EIPS/eip-191
bytes32 message = keccak256(
abi.encodePacked(hash, addr, owner, inceptionDate, chainId)
abi.encodePacked(
bytes1(0x19),
bytes1(0),
address(this),
hash,
addr,
owner,
inceptionDate,
chainId
)
).toEthSignedMessageHash();
bytes32 node = _getNamehash(addr);

Expand Down
11 changes: 10 additions & 1 deletion contracts/reverseRegistrar/SignatureReverseResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,17 @@ contract SignatureReverseResolver is Ownable, ISignatureReverseResolver {
uint256 inceptionDate,
bytes memory signature
) internal view returns (bool) {
// Follow ERC191 version 0 https://eips.ethereum.org/EIPS/eip-191
bytes32 message = keccak256(
abi.encodePacked(hash, addr, inceptionDate, chainId)
abi.encodePacked(
bytes1(0x19),
bytes1(0),
address(this),
hash,
addr,
inceptionDate,
chainId
)
).toEthSignedMessageHash();
bytes32 node = _getNamehash(addr);

Expand Down
82 changes: 65 additions & 17 deletions test/reverseRegistrar/TestL2ReverseRegistrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const { namehash } = require('../test-utils/ens')

const keccak256 = ethers.utils.solidityKeccak256

const geterc191 = (address) => {
return ['0x19', '0x00', address]
}

const ERC191_TYPES = ['bytes1', 'bytes1', 'address']

describe('L2ReverseRegistrar', function () {
let L2ReverseRegistrar
let L2ReverseRegistrarWithAccount2
Expand Down Expand Up @@ -104,8 +110,9 @@ describe('L2ReverseRegistrar', function () {
signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrar.address),
keccak256(['bytes4', 'string'], [funcId, name]),
account,
inceptionDate,
Expand Down Expand Up @@ -149,8 +156,9 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4', 'string'], [funcId, name]),
account,
inceptionDate,
Expand Down Expand Up @@ -179,8 +187,9 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4', 'string'], [funcId, 'hello.eth']),
account,
inceptionDate,
Expand All @@ -204,8 +213,9 @@ describe('L2ReverseRegistrar', function () {
const signature2 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4', 'string'], [funcId, 'hello.eth']),
account,
inceptionDate2,
Expand Down Expand Up @@ -244,8 +254,16 @@ describe('L2ReverseRegistrar', function () {
signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'address', 'uint256', 'uint256'],
[
...ERC191_TYPES,
'bytes32',
'address',
'address',
'uint256',
'uint256',
],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4', 'string'], [funcId, name]),
MockOwnable.address,
MockSmartContractWallet.address,
Expand Down Expand Up @@ -311,8 +329,9 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
Expand Down Expand Up @@ -353,8 +372,13 @@ describe('L2ReverseRegistrar', function () {
['bytes32', 'address', 'uint256'],
[
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
[...ERC191_TYPES, 'bytes4', 'string', 'string'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
funcId,
'url',
'http://ens.domains',
],
),
account,
inceptionDate,
Expand Down Expand Up @@ -384,8 +408,9 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
Expand Down Expand Up @@ -416,8 +441,9 @@ describe('L2ReverseRegistrar', function () {
const signature2 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
Expand Down Expand Up @@ -455,8 +481,16 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'address', 'uint256', 'uint256'],
[
...ERC191_TYPES,
'bytes32',
'address',
'address',
'uint256',
'uint256',
],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
Expand Down Expand Up @@ -529,8 +563,9 @@ describe('L2ReverseRegistrar', function () {
const signature1 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(
['bytes4', 'string', 'string'],
[funcId1, 'url', 'http://ens.domains'],
Expand All @@ -546,8 +581,9 @@ describe('L2ReverseRegistrar', function () {
const signature2 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4', 'string'], [funcId2, 'hello.eth']),
account,
inceptionDate + 1,
Expand Down Expand Up @@ -612,8 +648,14 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[keccak256(['bytes4'], [funcId]), account, inceptionDate, chainId],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4'], [funcId]),
account,
inceptionDate,
chainId,
],
),
),
)
Expand Down Expand Up @@ -646,8 +688,14 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[keccak256(['bytes4'], [funcId]), account, inceptionDate, chainId],
[...ERC191_TYPES, 'bytes32', 'address', 'uint256', 'uint256'],
[
...geterc191(L2ReverseRegistrarWithAccount2.address),
keccak256(['bytes4'], [funcId]),
account,
inceptionDate,
chainId,
],
),
),
)
Expand Down

0 comments on commit e04ea15

Please sign in to comment.