Skip to content

Commit

Permalink
fix: 7862 invalid address error (#7881)
Browse files Browse the repository at this point in the history
## **Description**


This PR was created to fix the issue [#7862], following code change has
been done:
1. Change the `getKeyringByAddress()` method to return undefined when
address is not valid hex address (e.g. ENS domain name)
2. Fix the existing unit tests to reflect above changes in
`address/index.test.ts` file
3. add extra unit tests to make sure function will return undefined when
address is not valid hex address (e.g. ENS domain)


## **Related issues**

Fixes: [#7862]

## **Manual testing steps**

Please refer to following videos for detail how to test.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**


https://github.com/MetaMask/metamask-mobile/assets/7315988/2c61515b-df26-460f-b2da-93fe6902e004


### **After**



https://github.com/MetaMask/metamask-mobile/assets/7315988/f8cf64bb-e323-452f-99be-0c3ffc99592f



- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've clearly explained what problem this PR is solving and how it
is solved.
- [x] I've linked related issues
- [x] I've included manual testing steps
- [x] I've included screenshots/recordings if applicable
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [x] I’ve properly set the pull request status:
  - [x] In case it's not yet "ready for review", I've set it to "draft".
- [x] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: sethkfman <10342624+sethkfman@users.noreply.github.com>
Co-authored-by: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent 4e01d6e commit f63c1dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/util/address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ export function isQRHardwareAccount(address) {
* get address's kerying
*
* @param {String} address - String corresponding to an address
* @returns {Keyring} - Returns address's account keyriong
* @returns {Keyring | undefined} - Returns the keyring of the provided address if keyring found, otherwise returns undefined
*/
export function getKeyringByAddress(address) {
if (!isValidHexAddress(address)) {
throw new Error(`Invalid address: ${address}`);
return undefined;
}
const { KeyringController } = Engine.context;
const { keyrings } = KeyringController.state;
Expand Down
9 changes: 5 additions & 4 deletions app/util/address/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,11 @@ describe('isQRHardwareAccount', () => {
});
});
describe('getKeyringByAddress', () => {
it('should throw an error if argument address is undefined', () => {
expect(() => getKeyringByAddress(undefined as any)).toThrow(
'Invalid address: undefined',
);
it('should return undefined if argument address is undefined', () => {
expect(getKeyringByAddress(undefined as any)).toBeUndefined();
});
it('should return undefined if argument address is not hex address', () => {
expect(getKeyringByAddress('ens.eth')).toBeUndefined();
});
it('should return address if found', () => {
expect(
Expand Down

0 comments on commit f63c1dd

Please sign in to comment.