diff --git a/packages/keyring-controller/src/KeyringController.test.ts b/packages/keyring-controller/src/KeyringController.test.ts index 24966a71cbf..444761a2a03 100644 --- a/packages/keyring-controller/src/KeyringController.test.ts +++ b/packages/keyring-controller/src/KeyringController.test.ts @@ -1719,6 +1719,25 @@ describe('KeyringController', () => { ); }); }); + + it.each([undefined, null])( + 'should throw error if contract address is %s', + async (invalidContractAddress) => { + await withController(async ({ controller, initialState }) => { + const account = initialState.keyrings[0].accounts[0]; + await expect( + controller.signEip7702Authorization({ + from: account, + chainId, + contractAddress: invalidContractAddress as unknown as string, + nonce, + }), + ).rejects.toThrow( + KeyringControllerError.MissingEip7702AuthorizationContractAddress, + ); + }); + }, + ); }); describe('when the keyring for the given address does not support signEip7702Authorization', () => { diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index 6c194c8ee61..22a0e5f7cb2 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -1217,9 +1217,15 @@ export class KeyringController extends BaseController< | Hex | undefined; + if (contractAddress === undefined) { + throw new Error( + KeyringControllerError.MissingEip7702AuthorizationContractAddress, + ); + } + return await keyring.signEip7702Authorization(from, [ chainId, - contractAddress as Hex, + contractAddress, nonce, ]); } diff --git a/packages/keyring-controller/src/constants.ts b/packages/keyring-controller/src/constants.ts index f7b81828221..abf89373050 100644 --- a/packages/keyring-controller/src/constants.ts +++ b/packages/keyring-controller/src/constants.ts @@ -25,6 +25,7 @@ export enum KeyringControllerError { UnsupportedPatchUserOperation = 'KeyringController - The keyring for the current address does not support the method patchUserOperation.', UnsupportedSignUserOperation = 'KeyringController - The keyring for the current address does not support the method signUserOperation.', UnsupportedVerifySeedPhrase = 'KeyringController - The keyring does not support the method verifySeedPhrase.', + MissingEip7702AuthorizationContractAddress = 'KeyringController - The EIP-7702 Authorization is invalid. No contract address provided.', NoAccountOnKeychain = "KeyringController - The keychain doesn't have accounts.", ControllerLocked = 'KeyringController - The operation cannot be completed while the controller is locked.', MissingCredentials = 'KeyringController - Cannot persist vault without password and encryption key',