From 6e3fdfbd46f90f76dc1fbd615d70af08d5431341 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Mon, 17 Jul 2023 19:04:21 -0230 Subject: [PATCH] Fix `@metamask/eth-sig-util` types The types for the package `@metamask/eth-sig-util` were being overwritten by a local type declaration. That has been removed, and all type errors have been resolved. All of the type errors were related to `normalize` returning `undefined` if given a nullish input. We never pass it a nullish input, but TypeScript wasn't able to infer that, so that issue was addressed with type casts. This isn't ideal, but it's temporary. Once we finish migrating all Keyrings to the new Keyring interface, we won't need to normalize addresses anymore at all. --- src/KeyringController.ts | 16 ++++++++++++---- src/types/@metamask/eth-sig-util.d.ts | 2 -- 2 files changed, 12 insertions(+), 6 deletions(-) delete mode 100644 src/types/@metamask/eth-sig-util.d.ts diff --git a/src/KeyringController.ts b/src/KeyringController.ts index 7962f02e..195b045b 100644 --- a/src/KeyringController.ts +++ b/src/KeyringController.ts @@ -334,7 +334,9 @@ class KeyringController extends EventEmitter { return res.concat(arr); }, []); - return addresses.map(normalizeToHex); + // Cast to `Hex[]` here is safe here because `addresses` has no nullish + // values, and `normalizeToHex` returns `Hex` unless given a nullish value + return addresses.map(normalizeToHex) as Hex[]; } /** @@ -515,7 +517,9 @@ class KeyringController extends EventEmitter { }, opts: Record = { version: 'V1' }, ): Promise { - const address = normalizeToHex(msgParams.from); + // Cast to `Hex` here is safe here because `msgParams.from` is not nullish. + // `normalizeToHex` returns `Hex` unless given a nullish value. + const address = normalizeToHex(msgParams.from) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.signTypedData) { throw new Error(KeyringControllerError.UnsupportedSignTypedMessage); @@ -697,7 +701,9 @@ class KeyringController extends EventEmitter { * @returns The keyring of the account, if it exists. */ async getKeyringForAccount(address: string): Promise> { - const hexed = normalizeToHex(address); + // Cast to `Hex` here is safe here because `address` is not nullish. + // `normalizeToHex` returns `Hex` unless given a nullish value. + const hexed = normalizeToHex(address) as Hex; const candidates = await Promise.all( this.keyrings.map(async (keyring) => { @@ -1086,7 +1092,9 @@ async function displayForKeyring( return { type: keyring.type, - accounts: accounts.map(normalizeToHex), + // Cast to `Hex[]` here is safe here because `addresses` has no nullish + // values, and `normalizeToHex` returns `Hex` unless given a nullish value + accounts: accounts.map(normalizeToHex) as Hex[], }; } diff --git a/src/types/@metamask/eth-sig-util.d.ts b/src/types/@metamask/eth-sig-util.d.ts deleted file mode 100644 index a3d38c39..00000000 --- a/src/types/@metamask/eth-sig-util.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -// eslint-disable-next-line import/unambiguous -declare module '@metamask/eth-sig-util';