Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into added-network-clients…
Browse files Browse the repository at this point in the history
…-rebased

* origin/main:
  docs: update controller-utils/isValidHexAddress to match reality (#1308)
  keyring-controller: validate from-address in signTypedMessage (#1293)
  NetworkController: Fix chain IDs in tests (#1307)
  NetworkController: Normalize INFURA_NETWORKS array (#1306)
  • Loading branch information
Gudahtt committed May 3, 2023
2 parents 267eab4 + 9d44801 commit 32736e8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 58 deletions.
9 changes: 3 additions & 6 deletions packages/controller-utils/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,12 @@ export function toChecksumHexAddress(address: string) {
/**
* Validates that the input is a hex address. This utility method is a thin
* wrapper around ethereumjs-util.isValidAddress, with the exception that it
* does not throw an error when provided values that are not hex strings. In
* addition, and by default, this method will return true for hex strings that
* meet the length requirement of a hex address, but are not prefixed with `0x`
* Finally, if the mixedCaseUseChecksum flag is true and a mixed case string is
* provided this method will validate it has the proper checksum formatting.
* by default will return true for hex strings that meet the length requirement
* of a hex address, but are not prefixed with `0x`.
*
* @param possibleAddress - Input parameter to check against.
* @param options - The validation options.
* @param options.allowNonPrefixed - If true will first ensure '0x' is prepended to the string.
* @param options.allowNonPrefixed - If true will allow addresses without `0x` prefix.`
* @returns Whether or not the input is a valid hex address.
*/
export function isValidHexAddress(
Expand Down
12 changes: 11 additions & 1 deletion packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
stripHexPrefix,
getBinarySize,
} from 'ethereumjs-util';
import {
isValidHexAddress,
toChecksumHexAddress,
} from '@metamask/controller-utils';
import {
normalize as normalizeAddress,
signTypedData,
Expand All @@ -28,7 +32,6 @@ import {
PersonalMessageParams,
TypedMessageParams,
} from '@metamask/message-manager';
import { toChecksumHexAddress } from '@metamask/controller-utils';

/**
* Available keyring types
Expand Down Expand Up @@ -493,6 +496,11 @@ export class KeyringController extends BaseController<
) {
try {
const address = normalizeAddress(messageParams.from);
if (!address || !isValidHexAddress(address)) {
throw new Error(
`Missing or invalid address ${JSON.stringify(messageParams.from)}`,
);
}
const qrKeyring = await this.getOrAddQRKeyring();
const qrAccounts = await qrKeyring.getAccounts();
if (
Expand Down Expand Up @@ -751,6 +759,8 @@ export class KeyringController extends BaseController<
};
});
} catch (e) {
// TODO: Add test case for when keyring throws
/* istanbul ignore next */
throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
}
}
Expand Down
Loading

0 comments on commit 32736e8

Please sign in to comment.