Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

fix: throw correct error on empty or invalid address #238

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@ethereumjs/tx": "^4.1.1",
"@ethereumjs/tx": "^4.2.0",
"@metamask/browser-passworder": "^4.1.0",
"@metamask/eth-hd-keyring": "^6.0.0",
"@metamask/eth-sig-util": "^6.0.0",
"@metamask/eth-simple-keyring": "^5.0.0",
"@metamask/utils": "^6.1.0",
"@metamask/utils": "^6.2.0",
"obs-store": "^4.0.3"
},
"devDependencies": {
Expand Down
18 changes: 16 additions & 2 deletions src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,22 @@ describe('KeyringController', () => {
);
});

it('throws error when address is invalid', async () => {
await expect(
keyringController.getKeyringForAccount('0x04'),
).rejects.toThrow(
new Error(
`${KeyringControllerError.NoKeyring}. Error info: The address passed in is invalid/empty`,
),
);
});

it('throws error when there are no keyrings', async () => {
keyringController.keyrings = [];
await expect(
keyringController.getKeyringForAccount('0x04'),
keyringController.getKeyringForAccount(
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
),
).rejects.toThrow(
new Error(
`${KeyringControllerError.NoKeyring}. Error info: There are no keyrings`,
Expand All @@ -745,7 +757,9 @@ describe('KeyringController', () => {
];

await expect(
keyringController.getKeyringForAccount('0x04'),
keyringController.getKeyringForAccount(
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
),
).rejects.toThrow(
new Error(
`${KeyringControllerError.NoKeyring}. Error info: There are keyrings, but none match the address`,
Expand Down
4 changes: 2 additions & 2 deletions src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as encryptorUtils from '@metamask/browser-passworder';
import HDKeyring from '@metamask/eth-hd-keyring';
import { normalize as normalizeToHex } from '@metamask/eth-sig-util';
import SimpleKeyring from '@metamask/eth-simple-keyring';
import { remove0x } from '@metamask/utils';
import { remove0x, isValidHexAddress } from '@metamask/utils';
import type {
Hex,
Json,
Expand Down Expand Up @@ -716,7 +716,7 @@ class KeyringController extends EventEmitter {

// Adding more info to the error
let errorInfo = '';
if (!address) {
if (!isValidHexAddress(hexed)) {
errorInfo = 'The address passed in is invalid/empty';
} else if (!candidates.length) {
errorInfo = 'There are no keyrings';
Expand Down
129 changes: 48 additions & 81 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -419,33 +419,6 @@ __metadata:
languageName: node
linkType: hard

"@chainsafe/as-sha256@npm:^0.4.1":
version: 0.4.1
resolution: "@chainsafe/as-sha256@npm:0.4.1"
checksum: 6d86975e648ecdafd366802278ac15b392b252e967f3681412ec48b5a3518b936cc5e977517499882b084991446d25787d98f8f585891943688cc81549a44e9a
languageName: node
linkType: hard

"@chainsafe/persistent-merkle-tree@npm:^0.6.1":
version: 0.6.1
resolution: "@chainsafe/persistent-merkle-tree@npm:0.6.1"
dependencies:
"@chainsafe/as-sha256": ^0.4.1
"@noble/hashes": ^1.3.0
checksum: 74614b8d40970dc930d5bf741619498b0bbbde5ff24ce45fce6ad122143aa77bf57249a28175b1b972cf56bff57d529a4258b7222ab4e60c1261119b5986c51b
languageName: node
linkType: hard

"@chainsafe/ssz@npm:^0.11.1":
version: 0.11.1
resolution: "@chainsafe/ssz@npm:0.11.1"
dependencies:
"@chainsafe/as-sha256": ^0.4.1
"@chainsafe/persistent-merkle-tree": ^0.6.1
checksum: e3c2928f9ab4a0544e645f0302b9535046d1e6e1d4b3bd1c3dd6bc8e6302fddad6036d65e7900d1446f285f496051da05fa14c1bde590b511d03033907175c8f
languageName: node
linkType: hard

"@cspotcode/source-map-support@npm:^0.8.0":
version: 0.8.1
resolution: "@cspotcode/source-map-support@npm:0.8.1"
Expand Down Expand Up @@ -508,13 +481,13 @@ __metadata:
languageName: node
linkType: hard

"@ethereumjs/common@npm:^3.1.2":
version: 3.1.2
resolution: "@ethereumjs/common@npm:3.1.2"
"@ethereumjs/common@npm:^3.2.0":
version: 3.2.0
resolution: "@ethereumjs/common@npm:3.2.0"
dependencies:
"@ethereumjs/util": ^8.0.6
"@ethereumjs/util": ^8.1.0
crc-32: ^1.2.0
checksum: e80a8bc86476f1ce878bacb1915d91681671bb5303291cdcece26e456ac13a6158f0f59625cb02a1cfbdd7c9a7dc8b175f8d8f0fee596b3eb9dfb965465ad43d
checksum: cb9cc11f5c868cb577ba611cebf55046e509218bbb89b47ccce010776dafe8256d70f8f43fab238aec74cf71f62601cd5842bc03a83261200802de365732a14b
languageName: node
linkType: hard

Expand All @@ -527,33 +500,26 @@ __metadata:
languageName: node
linkType: hard

"@ethereumjs/tx@npm:^4.1.1, @ethereumjs/tx@npm:^4.1.2":
version: 4.1.2
resolution: "@ethereumjs/tx@npm:4.1.2"
"@ethereumjs/tx@npm:^4.1.2, @ethereumjs/tx@npm:^4.2.0":
version: 4.2.0
resolution: "@ethereumjs/tx@npm:4.2.0"
dependencies:
"@chainsafe/ssz": ^0.11.1
"@ethereumjs/common": ^3.1.2
"@ethereumjs/common": ^3.2.0
"@ethereumjs/rlp": ^4.0.1
"@ethereumjs/util": ^8.0.6
"@ethereumjs/util": ^8.1.0
ethereum-cryptography: ^2.0.0
peerDependencies:
c-kzg: ^1.0.8
peerDependenciesMeta:
c-kzg:
optional: true
checksum: ad2fb692c3746cd5935b01c98b6b54046ae2a1fccff57ad2209e10446f3b279a204d7477accf05b27078445b14379314077769662142ac07117c45a5a1ea427f
checksum: 87a3f5f2452cfbf6712f8847525a80c213210ed453c211c793c5df801fe35ecef28bae17fadd222fcbdd94277478a47e52d2b916a90a6b30cda21f1e0cdaee42
languageName: node
linkType: hard

"@ethereumjs/util@npm:^8.0.0, @ethereumjs/util@npm:^8.0.2, @ethereumjs/util@npm:^8.0.6":
version: 8.0.6
resolution: "@ethereumjs/util@npm:8.0.6"
"@ethereumjs/util@npm:^8.0.0, @ethereumjs/util@npm:^8.0.2, @ethereumjs/util@npm:^8.0.6, @ethereumjs/util@npm:^8.1.0":
version: 8.1.0
resolution: "@ethereumjs/util@npm:8.1.0"
dependencies:
"@chainsafe/ssz": ^0.11.1
"@ethereumjs/rlp": ^4.0.1
ethereum-cryptography: ^2.0.0
micro-ftch: ^0.3.1
checksum: 034e06cddec27417318434a1a7cd7a9dc0f0b447c1f54423c515d8809c9697386eee6429d0a1c13517a85c696e6fdba570b243d882e65764c274859606027015
checksum: 9ae5dee8f12b0faf81cd83f06a41560e79b0ba96a48262771d897a510ecae605eb6d84f687da001ab8ccffd50f612ae50f988ef76e6312c752897f462f3ac08d
languageName: node
linkType: hard

Expand Down Expand Up @@ -1062,7 +1028,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@metamask/eth-keyring-controller@workspace:."
dependencies:
"@ethereumjs/tx": ^4.1.1
"@ethereumjs/tx": ^4.2.0
"@lavamoat/allow-scripts": ^2.3.1
"@lavamoat/preinstall-always-fail": ^1.0.0
"@metamask/auto-changelog": ^3.0.0
Expand All @@ -1074,7 +1040,7 @@ __metadata:
"@metamask/eth-hd-keyring": ^6.0.0
"@metamask/eth-sig-util": ^6.0.0
"@metamask/eth-simple-keyring": ^5.0.0
"@metamask/utils": ^6.1.0
"@metamask/utils": ^6.2.0
"@types/jest": ^29.4.0
"@types/sinon": ^10.0.13
"@typescript-eslint/eslint-plugin": ^5.55.0
Expand Down Expand Up @@ -1152,25 +1118,26 @@ __metadata:
languageName: node
linkType: hard

"@metamask/utils@npm:^6.1.0":
version: 6.1.0
resolution: "@metamask/utils@npm:6.1.0"
"@metamask/utils@npm:^6.2.0":
version: 6.2.0
resolution: "@metamask/utils@npm:6.2.0"
dependencies:
"@ethereumjs/tx": ^4.1.2
"@noble/hashes": ^1.3.1
"@types/debug": ^4.1.7
debug: ^4.3.4
semver: ^7.3.8
superstruct: ^1.0.3
checksum: d4eac3ce3c08674b8e9ef838d1661a5025690c6f266c26ebdb8e8d0da11fce786e54c326b5d9c6d33b262f37e7057e31d6545a3715613bd0a5bfa10e7755643a
checksum: 0bc675358ecc09b3bc04da613d73666295d7afa51ff6b8554801585966900b24b8545bd93b8b2e9a17db867ebe421fe884baf3558ec4ca3199fa65504f677c1b
languageName: node
linkType: hard

"@noble/curves@npm:1.0.0, @noble/curves@npm:~1.0.0":
version: 1.0.0
resolution: "@noble/curves@npm:1.0.0"
"@noble/curves@npm:1.1.0, @noble/curves@npm:~1.1.0":
version: 1.1.0
resolution: "@noble/curves@npm:1.1.0"
dependencies:
"@noble/hashes": 1.3.0
checksum: 6bcef44d626c640dc8961819d68dd67dffb907e3b973b7c27efe0ecdd9a5c6ce62c7b9e3dfc930c66605dced7f1ec0514d191c09a2ce98d6d52b66e3315ffa79
"@noble/hashes": 1.3.1
checksum: 2658cdd3f84f71079b4e3516c47559d22cf4b55c23ac8ee9d2b1f8e5b72916d9689e59820e0f9d9cb4a46a8423af5b56dc6bb7782405c88be06a015180508db5
languageName: node
linkType: hard

Expand All @@ -1181,10 +1148,10 @@ __metadata:
languageName: node
linkType: hard

"@noble/hashes@npm:1.3.0, @noble/hashes@npm:^1.3.0, @noble/hashes@npm:~1.3.0":
version: 1.3.0
resolution: "@noble/hashes@npm:1.3.0"
checksum: d7ddb6d7c60f1ce1f87facbbef5b724cdea536fc9e7f59ae96e0fc9de96c8f1a2ae2bdedbce10f7dcc621338dfef8533daa73c873f2b5c87fa1a4e05a95c2e2e
"@noble/hashes@npm:1.3.1, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:~1.3.0, @noble/hashes@npm:~1.3.1":
version: 1.3.1
resolution: "@noble/hashes@npm:1.3.1"
checksum: 7fdefc0f7a0c1ec27acc6ff88841793e3f93ec4ce6b8a6a12bfc0dd70ae6b7c4c82fe305fdfeda1735d5ad4a9eebe761e6693b3d355689c559e91242f4bc95b1
languageName: node
linkType: hard

Expand Down Expand Up @@ -1310,14 +1277,14 @@ __metadata:
languageName: node
linkType: hard

"@scure/bip32@npm:1.3.0":
version: 1.3.0
resolution: "@scure/bip32@npm:1.3.0"
"@scure/bip32@npm:1.3.1":
version: 1.3.1
resolution: "@scure/bip32@npm:1.3.1"
dependencies:
"@noble/curves": ~1.0.0
"@noble/hashes": ~1.3.0
"@noble/curves": ~1.1.0
"@noble/hashes": ~1.3.1
"@scure/base": ~1.1.0
checksum: 6eae997f9bdf41fe848134898960ac48e645fa10e63d579be965ca331afd0b7c1b8ebac170770d237ab4099dafc35e5a82995384510025ccf2abe669f85e8918
checksum: 394d65f77a40651eba21a5096da0f4233c3b50d422864751d373fcf142eeedb94a1149f9ab1dbb078086dab2d0bc27e2b1afec8321bf22d4403c7df2fea5bfe2
languageName: node
linkType: hard

Expand All @@ -1331,13 +1298,13 @@ __metadata:
languageName: node
linkType: hard

"@scure/bip39@npm:1.2.0":
version: 1.2.0
resolution: "@scure/bip39@npm:1.2.0"
"@scure/bip39@npm:1.2.1":
version: 1.2.1
resolution: "@scure/bip39@npm:1.2.1"
dependencies:
"@noble/hashes": ~1.3.0
"@scure/base": ~1.1.0
checksum: 980d761f53e63de04a9e4db840eb13bfb1bd1b664ecb04a71824c12c190f4972fd84146f3ed89b2a8e4c6bd2c17c15f8b592b7ac029e903323b0f9e2dae6916b
checksum: c5bd6f1328fdbeae2dcdd891825b1610225310e5e62a4942714db51066866e4f7bef242c7b06a1b9dcc8043a4a13412cf5c5df76d3b10aa9e36b82e9b6e3eeaa
languageName: node
linkType: hard

Expand Down Expand Up @@ -3436,14 +3403,14 @@ __metadata:
linkType: hard

"ethereum-cryptography@npm:^2.0.0":
version: 2.0.0
resolution: "ethereum-cryptography@npm:2.0.0"
version: 2.1.1
resolution: "ethereum-cryptography@npm:2.1.1"
dependencies:
"@noble/curves": 1.0.0
"@noble/hashes": 1.3.0
"@scure/bip32": 1.3.0
"@scure/bip39": 1.2.0
checksum: 958f8aab2d1b32aa759fb27a27877b3647410e8bb9aca7d65d1d477db4864cf7fc46b918eb52a1e246c25e98ee0a35a632c88b496aeaefa13469ee767a76c8db
"@noble/curves": 1.1.0
"@noble/hashes": 1.3.1
"@scure/bip32": 1.3.1
"@scure/bip39": 1.2.1
checksum: a6e5b41948b6da61640359f15a927c9c79e6fdf5a198eeb16fe9fccdfe87f0feb145e15a61a9b4760f1204e4590679a2a3c5d66ec77d8515b2f129fec502b8dd
languageName: node
linkType: hard

Expand Down