|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { ethers } = require('hardhat'); |
| 3 | +const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); |
| 4 | + |
| 5 | +const TEST_MESSAGE = ethers.id('OpenZeppelin'); |
| 6 | +const TEST_MESSAGE_HASH = ethers.hashMessage(TEST_MESSAGE); |
| 7 | + |
| 8 | +const WRONG_MESSAGE = ethers.id('Nope'); |
| 9 | +const WRONG_MESSAGE_HASH = ethers.hashMessage(WRONG_MESSAGE); |
| 10 | + |
| 11 | +async function fixture() { |
| 12 | + const [, signer, other] = await ethers.getSigners(); |
| 13 | + const mock = await ethers.deployContract('$ERC7913Utils'); |
| 14 | + |
| 15 | + // Deploy a mock ERC-1271 wallet |
| 16 | + const wallet = await ethers.deployContract('ERC1271WalletMock', [signer]); |
| 17 | + |
| 18 | + // Deploy a mock ERC-7913 verifier |
| 19 | + const verifier = await ethers.deployContract('ERC7913VerifierMock'); |
| 20 | + |
| 21 | + // Create test keys |
| 22 | + const validKey = ethers.toUtf8Bytes('valid_key'); |
| 23 | + const invalidKey = ethers.randomBytes(32); |
| 24 | + |
| 25 | + // Create signer bytes (verifier address + key) |
| 26 | + const validSignerBytes = ethers.concat([verifier.target, validKey]); |
| 27 | + const invalidKeySignerBytes = ethers.concat([verifier.target, invalidKey]); |
| 28 | + |
| 29 | + // Create test signatures |
| 30 | + const validSignature = ethers.toUtf8Bytes('valid_signature'); |
| 31 | + const invalidSignature = ethers.randomBytes(65); |
| 32 | + |
| 33 | + // Get EOA signature from the signer |
| 34 | + const eoaSignature = await signer.signMessage(TEST_MESSAGE); |
| 35 | + |
| 36 | + return { |
| 37 | + signer, |
| 38 | + other, |
| 39 | + mock, |
| 40 | + wallet, |
| 41 | + verifier, |
| 42 | + validKey, |
| 43 | + invalidKey, |
| 44 | + validSignerBytes, |
| 45 | + invalidKeySignerBytes, |
| 46 | + validSignature, |
| 47 | + invalidSignature, |
| 48 | + eoaSignature, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +describe('ERC7913Utils', function () { |
| 53 | + beforeEach(async function () { |
| 54 | + Object.assign(this, await loadFixture(fixture)); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('isValidSignatureNow', function () { |
| 58 | + describe('with EOA signer', function () { |
| 59 | + it('with matching signer and signature', async function () { |
| 60 | + const eoaSigner = ethers.zeroPadValue(this.signer.address, 20); |
| 61 | + await expect(this.mock.$isValidSignatureNow(eoaSigner, TEST_MESSAGE_HASH, this.eoaSignature)).to.eventually.be |
| 62 | + .true; |
| 63 | + }); |
| 64 | + |
| 65 | + it('with invalid signer', async function () { |
| 66 | + const eoaSigner = ethers.zeroPadValue(this.other.address, 20); |
| 67 | + await expect(this.mock.$isValidSignatureNow(eoaSigner, TEST_MESSAGE_HASH, this.eoaSignature)).to.eventually.be |
| 68 | + .false; |
| 69 | + }); |
| 70 | + |
| 71 | + it('with invalid signature', async function () { |
| 72 | + const eoaSigner = ethers.zeroPadValue(this.signer.address, 20); |
| 73 | + await expect(this.mock.$isValidSignatureNow(eoaSigner, WRONG_MESSAGE_HASH, this.eoaSignature)).to.eventually.be |
| 74 | + .false; |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('with ERC-1271 wallet', function () { |
| 79 | + it('with matching signer and signature', async function () { |
| 80 | + const walletSigner = ethers.zeroPadValue(this.wallet.target, 20); |
| 81 | + await expect(this.mock.$isValidSignatureNow(walletSigner, TEST_MESSAGE_HASH, this.eoaSignature)).to.eventually |
| 82 | + .be.true; |
| 83 | + }); |
| 84 | + |
| 85 | + it('with invalid signer', async function () { |
| 86 | + const walletSigner = ethers.zeroPadValue(this.mock.target, 20); |
| 87 | + await expect(this.mock.$isValidSignatureNow(walletSigner, TEST_MESSAGE_HASH, this.eoaSignature)).to.eventually |
| 88 | + .be.false; |
| 89 | + }); |
| 90 | + |
| 91 | + it('with invalid signature', async function () { |
| 92 | + const walletSigner = ethers.zeroPadValue(this.wallet.target, 20); |
| 93 | + await expect(this.mock.$isValidSignatureNow(walletSigner, WRONG_MESSAGE_HASH, this.eoaSignature)).to.eventually |
| 94 | + .be.false; |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('with ERC-7913 verifier', function () { |
| 99 | + it('with matching signer and signature', async function () { |
| 100 | + await expect(this.mock.$isValidSignatureNow(this.validSignerBytes, TEST_MESSAGE_HASH, this.validSignature)).to |
| 101 | + .eventually.be.true; |
| 102 | + }); |
| 103 | + |
| 104 | + it('with invalid verifier', async function () { |
| 105 | + const invalidVerifierSigner = ethers.concat([this.mock.target, this.validKey]); |
| 106 | + await expect(this.mock.$isValidSignatureNow(invalidVerifierSigner, TEST_MESSAGE_HASH, this.validSignature)).to |
| 107 | + .eventually.be.false; |
| 108 | + }); |
| 109 | + |
| 110 | + it('with invalid key', async function () { |
| 111 | + await expect(this.mock.$isValidSignatureNow(this.invalidKeySignerBytes, TEST_MESSAGE_HASH, this.validSignature)) |
| 112 | + .to.eventually.be.false; |
| 113 | + }); |
| 114 | + |
| 115 | + it('with invalid signature', async function () { |
| 116 | + await expect(this.mock.$isValidSignatureNow(this.validSignerBytes, TEST_MESSAGE_HASH, this.invalidSignature)).to |
| 117 | + .eventually.be.false; |
| 118 | + }); |
| 119 | + |
| 120 | + it('with signer too short', async function () { |
| 121 | + const shortSigner = ethers.randomBytes(19); |
| 122 | + await expect(this.mock.$isValidSignatureNow(shortSigner, TEST_MESSAGE_HASH, this.validSignature)).to.eventually |
| 123 | + .be.false; |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments