@@ -2,28 +2,23 @@ const { ethers } = require('hardhat');
2
2
const { expect } = require ( 'chai' ) ;
3
3
const { loadFixture } = require ( '@nomicfoundation/hardhat-network-helpers' ) ;
4
4
5
- const { getDomain } = require ( '../../helpers/eip712' ) ;
5
+ const { getDomain, Permit } = require ( '../../helpers/eip712' ) ;
6
6
const { PersonalSignHelper, TypedDataSignHelper } = require ( '../../helpers/erc7739' ) ;
7
7
8
8
// Constant
9
9
const MAGIC_VALUE = '0x1626ba7e' ;
10
10
11
+ // SignedTypedData helpers for a ERC20Permit application.
12
+ const helper = TypedDataSignHelper . from ( { Permit } ) ;
13
+
11
14
// Fixture
12
15
async function fixture ( ) {
13
16
// Using getSigners fails, probably due to a bad implementation of signTypedData somewhere in hardhat
14
17
const eoa = await ethers . Wallet . createRandom ( ) ;
15
18
const mock = await ethers . deployContract ( '$ERC7739SignerMock' , [ eoa ] ) ;
16
19
const domain = await getDomain ( mock ) ;
17
20
18
- // Dummy app domain, different from the ERC7739Signer's domain
19
- const appDomain = {
20
- name : 'SomeApp' ,
21
- version : '1' ,
22
- chainId : domain . chainId ,
23
- verifyingContract : '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512' ,
24
- } ;
25
-
26
- return { eoa, mock, domain, appDomain } ;
21
+ return { eoa, mock, domain } ;
27
22
}
28
23
29
24
describe ( 'ERC7739Signer' , function ( ) {
@@ -32,44 +27,57 @@ describe('ERC7739Signer', function () {
32
27
} ) ;
33
28
34
29
describe ( 'isValidSignature' , function ( ) {
35
- it ( 'returns true for a valid personal signature' , async function ( ) {
36
- const text = 'Hello, world!' ;
30
+ describe ( 'PersonalSign' , async function ( ) {
31
+ it ( 'returns true for a valid personal signature' , async function ( ) {
32
+ const text = 'Hello, world!' ;
37
33
38
- const hash = PersonalSignHelper . hash ( text ) ;
39
- const signature = await PersonalSignHelper . sign ( this . eoa , text , this . domain ) ;
34
+ const hash = PersonalSignHelper . hash ( text ) ;
35
+ const signature = await PersonalSignHelper . sign ( this . eoa , text , this . domain ) ;
40
36
41
- expect ( await this . mock . isValidSignature ( hash , signature ) ) . to . equal ( MAGIC_VALUE ) ;
42
- } ) ;
37
+ expect ( await this . mock . isValidSignature ( hash , signature ) ) . to . equal ( MAGIC_VALUE ) ;
38
+ } ) ;
43
39
44
- it ( 'returns false for an invalid personal signature' , async function ( ) {
45
- const hash = PersonalSignHelper . hash ( 'Message the app expects' ) ;
46
- const signature = await PersonalSignHelper . sign ( this . eoa , 'Message signed is different' , this . domain ) ;
40
+ it ( 'returns false for an invalid personal signature' , async function ( ) {
41
+ const hash = PersonalSignHelper . hash ( 'Message the app expects' ) ;
42
+ const signature = await PersonalSignHelper . sign ( this . eoa , 'Message signed is different' , this . domain ) ;
47
43
48
- expect ( await this . mock . isValidSignature ( hash , signature ) ) . to . not . equal ( MAGIC_VALUE ) ;
44
+ expect ( await this . mock . isValidSignature ( hash , signature ) ) . to . not . equal ( MAGIC_VALUE ) ;
45
+ } ) ;
49
46
} ) ;
50
47
51
- it ( 'returns true for a valid typed data signature' , async function ( ) {
52
- const helper = TypedDataSignHelper . from ( 'SomeType' , { something : 'bytes32' } ) ;
53
-
54
- const appMessage = { something : ethers . randomBytes ( 32 ) } ;
55
- const message = TypedDataSignHelper . prepare ( appMessage , this . domain ) ;
56
-
57
- const hash = helper . hash ( appMessage , this . appDomain ) ;
58
- const signature = await helper . sign ( this . eoa , message , this . appDomain ) ;
59
-
60
- expect ( await this . mock . isValidSignature ( hash , signature ) ) . to . equal ( MAGIC_VALUE ) ;
61
- } ) ;
62
-
63
- it ( 'returns false for an invalid typed data signature' , async function ( ) {
64
- const helper = TypedDataSignHelper . from ( 'SomeType' , { something : 'bytes32' } ) ;
65
-
66
- const appMessage = { something : ethers . randomBytes ( 32 ) } ;
67
- const signedMessage = TypedDataSignHelper . prepare ( { something : ethers . randomBytes ( 32 ) } , this . domain ) ;
68
-
69
- const hash = helper . hash ( appMessage , this . appDomain ) ;
70
- const signature = await helper . sign ( this . eoa , signedMessage , this . appDomain ) ;
71
-
72
- expect ( await this . mock . isValidSignature ( hash , signature ) ) . to . not . equal ( MAGIC_VALUE ) ;
48
+ describe ( 'TypedDataSign' , async function ( ) {
49
+ beforeEach ( async function ( ) {
50
+ // Dummy app domain, different from the ERC7739Signer's domain
51
+ this . appDomain = {
52
+ name : 'SomeApp' ,
53
+ version : '1' ,
54
+ chainId : this . domain . chainId ,
55
+ verifyingContract : '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512' ,
56
+ } ;
57
+ this . appMessage = {
58
+ owner : '0x1ab5E417d9AF00f1ca9d159007e12c401337a4bb' ,
59
+ spender : '0xD68E96620804446c4B1faB3103A08C98d4A8F55f' ,
60
+ value : 1_000_000n ,
61
+ nonce : 0n ,
62
+ deadline : ethers . MaxUint256 ,
63
+ } ;
64
+ this . appHash = helper . hash ( this . appMessage , this . appDomain ) ;
65
+ } ) ;
66
+
67
+ it ( 'returns true for a valid typed data signature' , async function ( ) {
68
+ const message = TypedDataSignHelper . prepare ( this . appMessage , this . domain ) ;
69
+ const signature = await helper . sign ( this . eoa , message , this . appDomain ) ;
70
+
71
+ expect ( await this . mock . isValidSignature ( this . appHash , signature ) ) . to . equal ( MAGIC_VALUE ) ;
72
+ } ) ;
73
+
74
+ it ( 'returns false for an invalid typed data signature' , async function ( ) {
75
+ // signed message is for a lower value.
76
+ const message = TypedDataSignHelper . prepare ( { ...this . appMessage , value : 1n } , this . domain ) ;
77
+ const signature = await helper . sign ( this . eoa , message , this . appDomain ) ;
78
+
79
+ expect ( await this . mock . isValidSignature ( this . appHash , signature ) ) . to . not . equal ( MAGIC_VALUE ) ;
80
+ } ) ;
73
81
} ) ;
74
82
} ) ;
75
83
} ) ;
0 commit comments