Skip to content

Commit 8cc5322

Browse files
authored
refactor(keyring-controller): ensure authorization contract address is provided (#5353)
#5301 introduced a new method for signing EIP-7702 Authorizations. This change adds validation to ensure that `contractAddress` is specified, rather than type asserting the `contractAddress` to `Hex`.
1 parent 620b633 commit 8cc5322

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,25 @@ describe('KeyringController', () => {
17191719
);
17201720
});
17211721
});
1722+
1723+
it.each([undefined, null])(
1724+
'should throw error if contract address is %s',
1725+
async (invalidContractAddress) => {
1726+
await withController(async ({ controller, initialState }) => {
1727+
const account = initialState.keyrings[0].accounts[0];
1728+
await expect(
1729+
controller.signEip7702Authorization({
1730+
from: account,
1731+
chainId,
1732+
contractAddress: invalidContractAddress as unknown as string,
1733+
nonce,
1734+
}),
1735+
).rejects.toThrow(
1736+
KeyringControllerError.MissingEip7702AuthorizationContractAddress,
1737+
);
1738+
});
1739+
},
1740+
);
17221741
});
17231742

17241743
describe('when the keyring for the given address does not support signEip7702Authorization', () => {

packages/keyring-controller/src/KeyringController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,15 @@ export class KeyringController extends BaseController<
12171217
| Hex
12181218
| undefined;
12191219

1220+
if (contractAddress === undefined) {
1221+
throw new Error(
1222+
KeyringControllerError.MissingEip7702AuthorizationContractAddress,
1223+
);
1224+
}
1225+
12201226
return await keyring.signEip7702Authorization(from, [
12211227
chainId,
1222-
contractAddress as Hex,
1228+
contractAddress,
12231229
nonce,
12241230
]);
12251231
}

packages/keyring-controller/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export enum KeyringControllerError {
2525
UnsupportedPatchUserOperation = 'KeyringController - The keyring for the current address does not support the method patchUserOperation.',
2626
UnsupportedSignUserOperation = 'KeyringController - The keyring for the current address does not support the method signUserOperation.',
2727
UnsupportedVerifySeedPhrase = 'KeyringController - The keyring does not support the method verifySeedPhrase.',
28+
MissingEip7702AuthorizationContractAddress = 'KeyringController - The EIP-7702 Authorization is invalid. No contract address provided.',
2829
NoAccountOnKeychain = "KeyringController - The keychain doesn't have accounts.",
2930
ControllerLocked = 'KeyringController - The operation cannot be completed while the controller is locked.',
3031
MissingCredentials = 'KeyringController - Cannot persist vault without password and encryption key',

0 commit comments

Comments
 (0)