@@ -139,6 +139,60 @@ describe('EIP712Decoder', function () {
139139 } ) ;
140140} ) ;
141141
142+ describe ( 'EIP-1271' , ( ) => {
143+ let contract , eip1271 , owner , other , accounts , signer , typedData , eip712Decoder , privateKey , wallet ;
144+
145+ beforeEach ( async ( ) => {
146+ // Compile the contract using Hardhat
147+ await hre . run ( 'compile' ) ;
148+
149+ // Set up a ganache provider with the generated Solidity code
150+ const ganacheProvider = ganache . provider ( { } )
151+ const provider = new ethers . providers . Web3Provider ( ganacheProvider ) ;
152+ const mnemonic = ganacheProvider . options . mnemonic ;
153+ wallet = ethers . Wallet . fromMnemonic ( mnemonic ) ;
154+ privateKey = wallet . privateKey ;
155+ accounts = await provider . listAccounts ( ) ;
156+ signer = provider . getSigner ( accounts [ 0 ] ) ;
157+
158+ // Load up the compiled contract artifact
159+ const EIP712Decoder = await hre . artifacts . readArtifact ( 'MockEIP712Decoder' ) ;
160+
161+ // Deploy the contract
162+ const EIP712DecoderFactory = new ethers . ContractFactory ( EIP712Decoder . abi , EIP712Decoder . bytecode , signer ) ;
163+ contract = await EIP712DecoderFactory . deploy ( [ 1 ] ) ;
164+ message . domain . verifyingContract = contract . address ;
165+ await contract . deployed ( ) ;
166+
167+ // Create the typed data for testing
168+ typedData = JSON . parse ( JSON . stringify ( message ) ) ;
169+ typedData . domain . verifyingContract = contract . address ;
170+
171+ // Deploy the EIP-1271 contract
172+ const EIP1271 = await hre . artifacts . readArtifact ( 'EIP1271' ) ;
173+ const EIP1271ContractFactory = new ethers . ContractFactory ( EIP1271 . abi , EIP1271 . bytecode , signer ) ;
174+ const EIP1271Contract = await EIP1271ContractFactory . deploy ( [ ] ) ;
175+ eip1271 = await EIP1271Contract . deployed ( ) ;
176+ await eip1271 . deployed ( ) ;
177+
178+ await eip1271 . addOwner ( wallet . address ) ;
179+ } ) ;
180+
181+ describe ( 'contract account signature recovery' , ( ) => {
182+ it ( 'should recover the correct signer' , async ( ) => {
183+ // Sign the typed data using eth-sig-util
184+ const signedPerson = signStruct ( privateKey ) ;
185+ signedPerson . signer = eip1271 . address ;
186+
187+ // Call the verifySigned method with the EIP-1271 contract address as the signer
188+ const result = await contract . verifySignedPerson ( signedPerson ) ;
189+
190+ // Verify that the returned address is equal to the EIP-1271 contract address
191+ expect ( result ) . to . equal ( eip1271 . address ) ;
192+ } ) ;
193+ } ) ;
194+ } ) ;
195+
142196function signStruct ( privateKey ) {
143197 const signature = sigUtil . signTypedData ( {
144198 privateKey : fromHexString ( privateKey . indexOf ( '0x' ) === 0 ? privateKey . substring ( 2 ) : privateKey ) ,
0 commit comments