Migrate utils-cryptography to ethers (#4749)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,55 +1,68 @@
|
||||
require('@openzeppelin/test-helpers');
|
||||
const { toEthSignedMessageHash, toDataWithIntendedValidatorHash } = require('../../helpers/sign');
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { domainSeparator, hashTypedData } = require('../../helpers/eip712');
|
||||
|
||||
const { expect } = require('chai');
|
||||
async function fixture() {
|
||||
const mock = await ethers.deployContract('$MessageHashUtils');
|
||||
return { mock };
|
||||
}
|
||||
|
||||
const MessageHashUtils = artifacts.require('$MessageHashUtils');
|
||||
|
||||
contract('MessageHashUtils', function () {
|
||||
describe('MessageHashUtils', function () {
|
||||
beforeEach(async function () {
|
||||
this.messageHashUtils = await MessageHashUtils.new();
|
||||
|
||||
this.message = '0x' + Buffer.from('abcd').toString('hex');
|
||||
this.messageHash = web3.utils.sha3(this.message);
|
||||
this.verifyingAddress = web3.utils.toChecksumAddress(web3.utils.randomHex(20));
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
context('toEthSignedMessageHash', function () {
|
||||
describe('toEthSignedMessageHash', function () {
|
||||
it('prefixes bytes32 data correctly', async function () {
|
||||
expect(await this.messageHashUtils.methods['$toEthSignedMessageHash(bytes32)'](this.messageHash)).to.equal(
|
||||
toEthSignedMessageHash(this.messageHash),
|
||||
);
|
||||
const message = ethers.randomBytes(32);
|
||||
const expectedHash = ethers.hashMessage(message);
|
||||
|
||||
expect(await this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message)).to.equal(expectedHash);
|
||||
});
|
||||
|
||||
it('prefixes dynamic length data correctly', async function () {
|
||||
expect(await this.messageHashUtils.methods['$toEthSignedMessageHash(bytes)'](this.message)).to.equal(
|
||||
toEthSignedMessageHash(this.message),
|
||||
);
|
||||
const message = ethers.randomBytes(128);
|
||||
const expectedHash = ethers.hashMessage(message);
|
||||
|
||||
expect(await this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message)).to.equal(expectedHash);
|
||||
});
|
||||
|
||||
it('version match for bytes32', async function () {
|
||||
const message = ethers.randomBytes(32);
|
||||
const fixed = await this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message);
|
||||
const dynamic = await this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message);
|
||||
|
||||
expect(fixed).to.equal(dynamic);
|
||||
});
|
||||
});
|
||||
|
||||
context('toDataWithIntendedValidatorHash', function () {
|
||||
describe('toDataWithIntendedValidatorHash', function () {
|
||||
it('returns the digest correctly', async function () {
|
||||
expect(
|
||||
await this.messageHashUtils.$toDataWithIntendedValidatorHash(this.verifyingAddress, this.message),
|
||||
).to.equal(toDataWithIntendedValidatorHash(this.verifyingAddress, this.message));
|
||||
const verifier = ethers.Wallet.createRandom().address;
|
||||
const message = ethers.randomBytes(128);
|
||||
const expectedHash = ethers.solidityPackedKeccak256(
|
||||
['string', 'address', 'bytes'],
|
||||
['\x19\x00', verifier, message],
|
||||
);
|
||||
|
||||
expect(await this.mock.$toDataWithIntendedValidatorHash(verifier, message)).to.equal(expectedHash);
|
||||
});
|
||||
});
|
||||
|
||||
context('toTypedDataHash', function () {
|
||||
describe('toTypedDataHash', function () {
|
||||
it('returns the digest correctly', async function () {
|
||||
const domain = {
|
||||
name: 'Test',
|
||||
version: 1,
|
||||
chainId: 1,
|
||||
verifyingContract: this.verifyingAddress,
|
||||
version: '1',
|
||||
chainId: 1n,
|
||||
verifyingContract: ethers.Wallet.createRandom().address,
|
||||
};
|
||||
const structhash = web3.utils.randomHex(32);
|
||||
const expectedDomainSeparator = await domainSeparator(domain);
|
||||
expect(await this.messageHashUtils.$toTypedDataHash(expectedDomainSeparator, structhash)).to.equal(
|
||||
hashTypedData(domain, structhash),
|
||||
);
|
||||
const structhash = ethers.randomBytes(32);
|
||||
const expectedHash = hashTypedData(domain, structhash);
|
||||
|
||||
expect(await this.mock.$toTypedDataHash(domainSeparator(domain), structhash)).to.equal(expectedHash);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user