Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve keyring endowment error messaging #884

Merged
merged 5 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/controllers/src/snaps/endowments/keyring.test.ts
Expand Up @@ -181,7 +181,9 @@ describe('keyringCaveatSpecifications', () => {
namespaces: undefined,
},
}),
).toThrow('Expected a valid namespaces object.');
).toThrow(
'Invalid namespaces object: Expected an object, but received: undefined.',
);
});
});
});
7 changes: 4 additions & 3 deletions packages/controllers/src/snaps/endowments/keyring.ts
@@ -1,7 +1,7 @@
import {
isNamespacesObject,
Namespaces,
SnapCaveatType,
validateNamespacesObject,
} from '@metamask/snap-utils';
import {
Caveat,
Expand Down Expand Up @@ -92,9 +92,10 @@ function validateCaveatNamespace(caveat: Caveat<string, any>): void {
});
}

if (!isNamespacesObject(value.namespaces)) {
const [error] = validateNamespacesObject(value.namespaces);
if (error) {
throw ethErrors.rpc.invalidParams({
message: 'Expected a valid namespaces object.',
message: `Invalid namespaces object: ${error.message}.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superstruct validates errors lazily, if you want, we could show all of them. error.failures() returns a list of all of them all

});
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/jest.config.js
Expand Up @@ -16,9 +16,9 @@ module.exports = {
coverageThreshold: {
global: {
branches: 86.05,
functions: 97.14,
lines: 96.79,
statements: 96.86,
functions: 97.16,
lines: 96.8,
statements: 96.87,
},
},
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
Expand Down
40 changes: 40 additions & 0 deletions packages/utils/src/namespace.test.ts
@@ -1,3 +1,4 @@
import { StructError } from 'superstruct';
import {
getChain,
getNamespace,
Expand All @@ -18,6 +19,7 @@ import {
isNamespacesObject,
parseAccountId,
parseChainId,
validateNamespacesObject,
} from './namespace';

describe('parseChainId', () => {
Expand Down Expand Up @@ -645,3 +647,41 @@ describe('isNamespacesObject', () => {
expect(isNamespacesObject(object)).toBe(false);
});
});

describe('validateNamespacesObject', () => {
it.each([
{},
{ eip155: getNamespace() },
{ bip122: getNamespace() },
{ eip155: getNamespace(), bip122: getNamespace() },
])('returns the object for a valid namespaces object', (object) => {
expect(validateNamespacesObject(object)).toStrictEqual([undefined, object]);
});

it.each([
true,
false,
null,
undefined,
1,
'foo',
{ eip155: {} },
{ eip155: [], bip122: [] },
{ eip155: true, bip122: true },
{ eip155: false, bip122: false },
{ eip155: null, bip122: null },
{ eip155: undefined, bip122: undefined },
{ eip155: 1, bip122: 1 },
{ eip155: 'foo', bip122: 'foo' },
{ eip155: { methods: [] }, bip122: { methods: [] } },
{ eip155: { chains: ['foo'] }, bip122: { chains: ['foo'] } },
{ a: getNamespace() },
{ eip155: getNamespace(), a: getNamespace() },
{ foobarbaz: getNamespace() },
])('returns an error for an invalid namespaces object', (object) => {
expect(validateNamespacesObject(object)).toStrictEqual([
expect.any(StructError),
undefined,
]);
});
});
18 changes: 18 additions & 0 deletions packages/utils/src/namespace.ts
Expand Up @@ -12,8 +12,10 @@ import {
assign,
partial,
pick,
validate,
} from 'superstruct';
import { JsonRpcRequestStruct } from '@metamask/utils';
import { StructError } from 'superstruct/lib/error';
import { assertStruct } from './assert';

export const CHAIN_ID_REGEX =
Expand Down Expand Up @@ -276,3 +278,19 @@ export function isNamespace(value: unknown): value is Namespace {
export function isNamespacesObject(value: unknown): value is Namespaces {
return is(value, NamespacesStruct);
Mrtenz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Check if a value is an object containing {@link Namespaces}s. This behaves
* the same as {@link isNamespacesObject}, but rather than returning a boolean,
* it returns a tuple containing an error message if the value is not valid, or
* the validated value if it is.
*
* @param value - The value to validate.
* @returns A tuple containing an error message if the value is not valid, or
* the validated value if it is.
*/
export function validateNamespacesObject(
value: unknown,
): [StructError, undefined] | [undefined, Namespaces] {
return validate(value, NamespacesStruct);
}