Skip to content

Commit db66280

Browse files
ernestognwarr00
andauthored
Await .eventually test matchers (#54)
Co-authored-by: Arr00 <[email protected]>
1 parent 1fd395e commit db66280

File tree

6 files changed

+68
-69
lines changed

6 files changed

+68
-69
lines changed

test/account/Account.behavior.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function shouldBehaveLikeAccountCore() {
1515
describe('entryPoint', function () {
1616
it('should return the canonical entrypoint', async function () {
1717
await this.mock.deploy();
18-
expect(this.mock.entryPoint()).to.eventually.equal(entrypoint);
18+
await expect(this.mock.entryPoint()).to.eventually.equal(entrypoint);
1919
});
2020
});
2121

@@ -106,7 +106,7 @@ function shouldBehaveLikeAccountHolder() {
106106
it('receives ERC1155 tokens from a single ID', async function () {
107107
await this.token.connect(this.other).safeTransferFrom(this.other, this.mock, ids[0], values[0], data);
108108

109-
expect(
109+
await expect(
110110
this.token.balanceOfBatch(
111111
ids.map(() => this.mock),
112112
ids,
@@ -115,15 +115,15 @@ function shouldBehaveLikeAccountHolder() {
115115
});
116116

117117
it('receives ERC1155 tokens from a multiple IDs', async function () {
118-
expect(
118+
await expect(
119119
this.token.balanceOfBatch(
120120
ids.map(() => this.mock),
121121
ids,
122122
),
123123
).to.eventually.deep.equal(ids.map(() => 0n));
124124

125125
await this.token.connect(this.other).safeBatchTransferFrom(this.other, this.mock, ids, values, data);
126-
expect(
126+
await expect(
127127
this.token.balanceOfBatch(
128128
ids.map(() => this.mock),
129129
ids,
@@ -143,7 +143,7 @@ function shouldBehaveLikeAccountHolder() {
143143
it('receives an ERC721 token', async function () {
144144
await this.token.connect(this.other).safeTransferFrom(this.other, this.mock, tokenId);
145145

146-
expect(this.token.ownerOf(tokenId)).to.eventually.equal(this.mock);
146+
await expect(this.token.ownerOf(tokenId)).to.eventually.equal(this.mock);
147147
});
148148
});
149149
});
@@ -156,7 +156,7 @@ function shouldBehaveLikeAccountERC7821({ deployable = true } = {}) {
156156
await setBalance(this.mock.target, ethers.parseEther('1'));
157157

158158
// account is not initially deployed
159-
expect(ethers.provider.getCode(this.mock)).to.eventually.equal('0x');
159+
await expect(ethers.provider.getCode(this.mock)).to.eventually.equal('0x');
160160

161161
this.encodeUserOpCalldata = (...calls) =>
162162
this.mock.interface.encodeFunctionData('execute', [
@@ -195,13 +195,14 @@ function shouldBehaveLikeAccountERC7821({ deployable = true } = {}) {
195195
.then(op => op.addInitCode())
196196
.then(op => this.signUserOp(op));
197197

198-
expect(this.mock.getNonce()).to.eventually.equal(0);
198+
// Can't call the account to get its nonce before it's deployed
199+
await expect(entrypoint.getNonce(this.mock.target, 0)).to.eventually.equal(0);
199200
await expect(entrypoint.handleOps([operation.packed], this.beneficiary))
200201
.to.emit(entrypoint, 'AccountDeployed')
201202
.withArgs(operation.hash(), this.mock, this.factory, ethers.ZeroAddress)
202203
.to.emit(this.target, 'MockFunctionCalledExtra')
203204
.withArgs(this.mock, 17);
204-
expect(this.mock.getNonce()).to.eventually.equal(1);
205+
await expect(this.mock.getNonce()).to.eventually.equal(1);
205206
});
206207

207208
it('should revert if the signature is invalid', async function () {
@@ -238,24 +239,24 @@ function shouldBehaveLikeAccountERC7821({ deployable = true } = {}) {
238239
})
239240
.then(op => this.signUserOp(op));
240241

241-
expect(this.mock.getNonce()).to.eventually.equal(0);
242+
await expect(this.mock.getNonce()).to.eventually.equal(0);
242243
await expect(entrypoint.handleOps([operation.packed], this.beneficiary))
243244
.to.emit(this.target, 'MockFunctionCalledExtra')
244245
.withArgs(this.mock, 42);
245-
expect(this.mock.getNonce()).to.eventually.equal(1);
246+
await expect(this.mock.getNonce()).to.eventually.equal(1);
246247
});
247248

248249
it('should support sending eth to an EOA', async function () {
249250
const operation = await this.mock
250251
.createUserOp({ callData: this.encodeUserOpCalldata({ target: this.other, value }) })
251252
.then(op => this.signUserOp(op));
252253

253-
expect(this.mock.getNonce()).to.eventually.equal(0);
254+
await expect(this.mock.getNonce()).to.eventually.equal(0);
254255
await expect(entrypoint.handleOps([operation.packed], this.beneficiary)).to.changeEtherBalance(
255256
this.other,
256257
value,
257258
);
258-
expect(this.mock.getNonce()).to.eventually.equal(1);
259+
await expect(this.mock.getNonce()).to.eventually.equal(1);
259260
});
260261

261262
it('should support batch execution', async function () {
@@ -275,11 +276,11 @@ function shouldBehaveLikeAccountERC7821({ deployable = true } = {}) {
275276
})
276277
.then(op => this.signUserOp(op));
277278

278-
expect(this.mock.getNonce()).to.eventually.equal(0);
279+
await expect(this.mock.getNonce()).to.eventually.equal(0);
279280
const tx = entrypoint.handleOps([operation.packed], this.beneficiary);
280281
await expect(tx).to.changeEtherBalances([this.other, this.target], [value1, value2]);
281282
await expect(tx).to.emit(this.target, 'MockFunctionCalledExtra').withArgs(this.mock, value2);
282-
expect(this.mock.getNonce()).to.eventually.equal(1);
283+
await expect(this.mock.getNonce()).to.eventually.equal(1);
283284
});
284285
});
285286
});

test/crosschain/axelar/AxelarGateway.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ describe('AxelarGateway', function () {
3232
});
3333

3434
it('initial setup', async function () {
35-
expect(this.srcGateway.localGateway()).to.eventually.equal(this.axelar);
36-
expect(this.srcGateway.getEquivalentChain(this.CAIP2)).to.eventually.equal('local');
37-
expect(this.srcGateway.getRemoteGateway(this.CAIP2)).to.eventually.equal(getAddress(this.dstGateway));
35+
await expect(this.srcGateway.localGateway()).to.eventually.equal(this.axelar);
36+
await expect(this.srcGateway.getEquivalentChain(this.CAIP2)).to.eventually.equal('local');
37+
await expect(this.srcGateway.getRemoteGateway(this.CAIP2)).to.eventually.equal(getAddress(this.dstGateway));
3838

39-
expect(this.dstGateway.localGateway()).to.eventually.equal(this.axelar);
40-
expect(this.dstGateway.getEquivalentChain(this.CAIP2)).to.eventually.equal('local');
41-
expect(this.dstGateway.getRemoteGateway(this.CAIP2)).to.eventually.equal(getAddress(this.srcGateway));
39+
await expect(this.dstGateway.localGateway()).to.eventually.equal(this.axelar);
40+
await expect(this.dstGateway.getEquivalentChain(this.CAIP2)).to.eventually.equal('local');
41+
await expect(this.dstGateway.getRemoteGateway(this.CAIP2)).to.eventually.equal(getAddress(this.srcGateway));
4242
});
4343

4444
it('workflow', async function () {

test/helpers/erc7739.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class TypedDataSignHelper {
7777
ethers.concat([
7878
signature,
7979
ethers.TypedDataEncoder.hashDomain(domain), // appDomainSeparator
80-
ethers.TypedDataEncoder.hashStruct(this.contentsTypeName, this.allTypes, message.contents), // contentsHash
80+
this.hashStruct(this.contentsTypeName, message.contents), // contentsHash
8181
ethers.toUtf8Bytes(this.contentDescr),
8282
ethers.toBeHex(this.contentDescr.length, 2),
8383
]),

test/utils/cryptography/ERC7739Signer.behavior.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ function shouldBehaveLikeERC7739Signer() {
1919
const hash = PersonalSignHelper.hash(text);
2020
const signature = await PersonalSignHelper.sign(this.signTypedData, text, this.domain);
2121

22-
expect(this.mock.isValidSignature(hash, signature)).to.eventually.equal(MAGIC_VALUE);
22+
await expect(this.mock.isValidSignature(hash, signature)).to.eventually.equal(MAGIC_VALUE);
2323
});
2424

2525
it('returns false for an invalid personal signature', async function () {
2626
const hash = PersonalSignHelper.hash('Message the app expects');
2727
const signature = await PersonalSignHelper.sign(this.signTypedData, 'Message signed is different', this.domain);
2828

29-
expect(this.mock.isValidSignature(hash, signature)).to.eventually.not.equal(MAGIC_VALUE);
29+
await expect(this.mock.isValidSignature(hash, signature)).to.eventually.not.equal(MAGIC_VALUE);
3030
});
3131
});
3232

@@ -56,7 +56,7 @@ function shouldBehaveLikeERC7739Signer() {
5656
const hash = ethers.TypedDataEncoder.hash(this.appDomain, { Permit }, message.contents);
5757
const signature = await TypedDataSignHelper.sign(this.signTypedData, this.appDomain, { Permit }, message);
5858

59-
expect(this.mock.isValidSignature(hash, signature)).to.eventually.equal(MAGIC_VALUE);
59+
await expect(this.mock.isValidSignature(hash, signature)).to.eventually.equal(MAGIC_VALUE);
6060
});
6161

6262
it('returns true for valid typed data signature (nested types)', async function () {
@@ -72,7 +72,7 @@ function shouldBehaveLikeERC7739Signer() {
7272
const hash = TypedDataSignHelper.hash(this.appDomain, contentsTypes, message.contents);
7373
const signature = await TypedDataSignHelper.sign(this.signTypedData, this.appDomain, contentsTypes, message);
7474

75-
expect(this.mock.isValidSignature(hash, signature)).to.eventually.equal(MAGIC_VALUE);
75+
await expect(this.mock.isValidSignature(hash, signature)).to.eventually.equal(MAGIC_VALUE);
7676
});
7777

7878
it('returns false for an invalid typed data signature', async function () {
@@ -89,14 +89,13 @@ function shouldBehaveLikeERC7739Signer() {
8989
const hash = ethers.TypedDataEncoder.hash(this.appDomain, { Permit }, appContents);
9090
const signature = await TypedDataSignHelper.sign(this.signTypedData, this.appDomain, { Permit }, message);
9191

92-
expect(this.mock.isValidSignature(hash, signature)).to.eventually.not.equal(MAGIC_VALUE);
92+
await expect(this.mock.isValidSignature(hash, signature)).to.eventually.not.equal(MAGIC_VALUE);
9393
});
9494
});
9595

96-
it('support detection', function () {
97-
expect(
98-
this.mock.isValidSignature('0x7739773977397739773977397739773977397739773977397739773977397739', ''),
99-
).to.eventually.equal('0x77390001');
96+
it('support detection', async function () {
97+
const hash = '0x7739773977397739773977397739773977397739773977397739773977397739';
98+
await expect(this.mock.isValidSignature(hash, '0x')).to.eventually.equal('0x77390001');
10099
});
101100
});
102101
}

test/utils/cryptography/ERC7739Signer.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { ethers } = require('hardhat');
22
const { shouldBehaveLikeERC7739Signer } = require('./ERC7739Signer.behavior');
3-
const { NonNativeSigner, P256SigningKey, RSASigningKey } = require('../../helpers/signers');
3+
const { NonNativeSigner, P256SigningKey, RSASHA256SigningKey } = require('../../helpers/signers');
44

55
describe('ERC7739Signer', function () {
66
describe('for an ECDSA signer', function () {
@@ -26,7 +26,7 @@ describe('ERC7739Signer', function () {
2626

2727
describe('for an RSA signer', function () {
2828
before(async function () {
29-
this.signer = new NonNativeSigner(RSASigningKey.random());
29+
this.signer = new NonNativeSigner(RSASHA256SigningKey.random());
3030
this.mock = await ethers.deployContract('ERC7739SignerRSAMock', [
3131
this.signer.signingKey.publicKey.e,
3232
this.signer.signingKey.publicKey.n,

test/utils/cryptography/ERC7739Utils.test.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@ const { expect } = require('chai');
22
const { ethers } = require('hardhat');
33
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
44

5-
const { Permit, domainType } = require('@openzeppelin/contracts/test/helpers/eip712');
5+
const { Permit } = require('@openzeppelin/contracts/test/helpers/eip712');
66
const { PersonalSignHelper, TypedDataSignHelper } = require('../../helpers/erc7739');
77

88
// Helper for ERC20Permit applications
99
const helper = TypedDataSignHelper.from({ Permit });
1010

11-
function domainComponentsType(domain) {
12-
return ethers.TypedDataEncoder.from({ EIP712Domain: domainType(domain) })
13-
.encodeType('EIP712Domain')
14-
.replace(/EIP712Domain\((.*)\)/, (_, s) => s);
15-
}
16-
17-
function domainComponentsBytes(domain) {
18-
return ethers.hexlify(
19-
ethers
20-
.getBytes(ethers.TypedDataEncoder.from({ EIP712Domain: domainType(domain) }).encodeData('EIP712Domain', domain))
21-
.slice(32),
22-
);
23-
}
24-
2511
const fixture = async () => {
2612
const mock = await ethers.deployContract('$ERC7739Utils');
2713
const domain = {
@@ -65,9 +51,9 @@ describe('ERC7739Utils', function () {
6551
ethers.toBeHex(contentDescr.length, 2),
6652
]);
6753

68-
expect(this.mock.$encodeTypedDataSig(signature, appSeparator, contentsHash, contentDescr)).to.eventually.equal(
69-
encoded,
70-
);
54+
await expect(
55+
this.mock.$encodeTypedDataSig(signature, appSeparator, contentsHash, contentDescr),
56+
).to.eventually.equal(encoded);
7157
});
7258
});
7359

@@ -85,7 +71,7 @@ describe('ERC7739Utils', function () {
8571
ethers.toBeHex(contentDescr.length, 2),
8672
]);
8773

88-
expect(this.mock.$decodeTypedDataSig(encoded)).to.eventually.deep.equal([
74+
await expect(this.mock.$decodeTypedDataSig(encoded)).to.eventually.deep.equal([
8975
ethers.hexlify(signature),
9076
appSeparator,
9177
contentsHash,
@@ -95,7 +81,7 @@ describe('ERC7739Utils', function () {
9581

9682
it('returns default empty values if the signature is too short', async function () {
9783
const encoded = ethers.randomBytes(65); // DOMAIN_SEPARATOR (32 bytes) + CONTENTS (32 bytes) + CONTENTS_TYPE_LENGTH (2 bytes) - 1
98-
expect(this.mock.$decodeTypedDataSig(encoded)).to.eventually.deep.equal([
84+
await expect(this.mock.$decodeTypedDataSig(encoded)).to.eventually.deep.equal([
9985
'0x',
10086
ethers.ZeroHash,
10187
ethers.ZeroHash,
@@ -105,7 +91,7 @@ describe('ERC7739Utils', function () {
10591

10692
it('returns default empty values if the length is invalid', async function () {
10793
const encoded = ethers.concat([ethers.randomBytes(64), '0x3f']); // Can't be less than 64 bytes
108-
expect(this.mock.$decodeTypedDataSig(encoded)).to.eventually.deep.equal([
94+
await expect(this.mock.$decodeTypedDataSig(encoded)).to.eventually.deep.equal([
10995
'0x',
11096
ethers.ZeroHash,
11197
ethers.ZeroHash,
@@ -118,7 +104,7 @@ describe('ERC7739Utils', function () {
118104
it('should produce a personal signature EIP-712 nested type', async function () {
119105
const text = 'Hello, world!';
120106

121-
expect(this.mock.$personalSignStructHash(ethers.hashMessage(text))).to.eventually.equal(
107+
await expect(this.mock.$personalSignStructHash(ethers.hashMessage(text))).to.eventually.equal(
122108
ethers.TypedDataEncoder.hashStruct('PersonalSign', PersonalSignHelper.types, PersonalSignHelper.prepare(text)),
123109
);
124110
});
@@ -131,22 +117,36 @@ describe('ERC7739Utils', function () {
131117
const contentsHash = helper.hashStruct('Permit', message.contents);
132118
const hash = helper.hashStruct('TypedDataSign', message);
133119

134-
expect(
120+
const domainBytes = ethers.AbiCoder.defaultAbiCoder().encode(
121+
['bytes32', 'bytes32', 'uint256', 'address', 'bytes32'],
122+
[
123+
ethers.id(this.domain.name),
124+
ethers.id(this.domain.version),
125+
this.domain.chainId,
126+
this.domain.verifyingContract,
127+
ethers.ZeroHash,
128+
],
129+
);
130+
131+
await expect(
135132
this.mock.$typedDataSignStructHash(
136-
helper.contentDescr,
133+
helper.contentsTypeName,
134+
ethers.Typed.string(helper.contentDescr),
137135
contentsHash,
138-
domainComponentsType(this.domain),
139-
domainComponentsBytes(this.domain),
136+
domainBytes,
140137
),
141138
).to.eventually.equal(hash);
139+
await expect(
140+
this.mock.$typedDataSignStructHash(helper.contentDescr, contentsHash, domainBytes),
141+
).to.eventually.equal(hash);
142142
});
143143
});
144144

145145
describe('typedDataSignTypehash', function () {
146146
it('should match', async function () {
147147
const typedDataSignType = ethers.TypedDataEncoder.from(helper.allTypes).encodeType('TypedDataSign');
148148

149-
expect(
149+
await expect(
150150
this.mock.$typedDataSignTypehash(
151151
helper.contentsTypeName,
152152
typedDataSignType.slice(typedDataSignType.indexOf(')') + 1),
@@ -156,7 +156,6 @@ describe('ERC7739Utils', function () {
156156
});
157157

158158
describe('decodeContentsDescr', function () {
159-
const forbiddenFirstChars = 'abcdefghijklmnopqrstuvwxyz(';
160159
const forbiddenChars = ', )\x00';
161160

162161
for (const { descr, contentDescr, contentTypeName, contentType } of [].concat(
@@ -181,16 +180,16 @@ describe('ERC7739Utils', function () {
181180
contentDescr: 'SomeType',
182181
contentTypeName: null,
183182
},
184-
forbiddenFirstChars.split('').map(char => ({
185-
descr: `should return nothing if starts with [${char}] (implicit)`,
186-
contentDescr: `${char}SomeType()`,
183+
{
184+
descr: 'should return nothing if starts with [(] (implicit)',
185+
contentDescr: '(SomeType(address foo,uint256 bar)',
187186
contentTypeName: null,
188-
})),
189-
forbiddenFirstChars.split('').map(char => ({
190-
descr: `should return nothing if starts with [${char}] (explicit)`,
191-
contentDescr: `${char}SomeType()${char}SomeType`,
187+
},
188+
{
189+
descr: 'should return nothing if starts with [(] (explicit)',
190+
contentDescr: '(SomeType(address foo,uint256 bar)(SomeType',
192191
contentTypeName: null,
193-
})),
192+
},
194193
forbiddenChars.split('').map(char => ({
195194
descr: `should return nothing if contains [${char}] (implicit)`,
196195
contentDescr: `SomeType${char}(address foo,uint256 bar)`,
@@ -203,7 +202,7 @@ describe('ERC7739Utils', function () {
203202
})),
204203
)) {
205204
it(descr, async function () {
206-
expect(this.mock.$decodeContentsDescr(contentDescr)).to.eventually.deep.equal([
205+
await expect(this.mock.$decodeContentsDescr(contentDescr)).to.eventually.deep.equal([
207206
contentTypeName ?? '',
208207
contentTypeName ? contentType ?? contentDescr : '',
209208
]);

0 commit comments

Comments
 (0)