MessageHashUtils: Add toDataWithIntendedValidatorHash(address, bytes32) (#5081)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
This commit is contained in:
StackOverflowExcept1on
2025-03-01 03:00:43 +07:00
committed by GitHub
parent a9b1f58b00
commit a4b0d89900
4 changed files with 87 additions and 5 deletions

View File

@ -19,14 +19,16 @@ describe('MessageHashUtils', function () {
const message = ethers.randomBytes(32);
const expectedHash = ethers.hashMessage(message);
expect(await this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message)).to.equal(expectedHash);
await expect(this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message)).to.eventually.equal(
expectedHash,
);
});
it('prefixes dynamic length data correctly', async function () {
const message = ethers.randomBytes(128);
const expectedHash = ethers.hashMessage(message);
expect(await this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message)).to.equal(expectedHash);
await expect(this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message)).to.eventually.equal(expectedHash);
});
it('version match for bytes32', async function () {
@ -39,7 +41,20 @@ describe('MessageHashUtils', function () {
});
describe('toDataWithIntendedValidatorHash', function () {
it('returns the digest correctly', async function () {
it('returns the digest of `bytes32 messageHash` correctly', async function () {
const verifier = ethers.Wallet.createRandom().address;
const message = ethers.randomBytes(32);
const expectedHash = ethers.solidityPackedKeccak256(
['string', 'address', 'bytes32'],
['\x19\x00', verifier, message],
);
await expect(
this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes32)')(verifier, message),
).to.eventually.equal(expectedHash);
});
it('returns the digest of `bytes memory message` correctly', async function () {
const verifier = ethers.Wallet.createRandom().address;
const message = ethers.randomBytes(128);
const expectedHash = ethers.solidityPackedKeccak256(
@ -47,7 +62,21 @@ describe('MessageHashUtils', function () {
['\x19\x00', verifier, message],
);
expect(await this.mock.$toDataWithIntendedValidatorHash(verifier, message)).to.equal(expectedHash);
await expect(
this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes)')(verifier, message),
).to.eventually.equal(expectedHash);
});
it('version match for bytes32', async function () {
const verifier = ethers.Wallet.createRandom().address;
const message = ethers.randomBytes(32);
const fixed = await this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes)')(verifier, message);
const dynamic = await this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes32)')(
verifier,
message,
);
expect(fixed).to.equal(dynamic);
});
});
@ -62,7 +91,7 @@ describe('MessageHashUtils', function () {
const structhash = ethers.randomBytes(32);
const expectedHash = hashTypedData(domain, structhash);
expect(await this.mock.$toTypedDataHash(domainSeparator(domain), structhash)).to.equal(expectedHash);
await expect(this.mock.$toTypedDataHash(domainSeparator(domain), structhash)).to.eventually.equal(expectedHash);
});
});
});