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

Add onNameLookup export #1394

Merged
merged 61 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
bf3c3cf
added new handler type and updated enums for the new export handler
hmalik88 May 9, 2023
8e8524d
updated utils for name lookup
hmalik88 May 9, 2023
eb74ebb
updated onNameLookupResponse type
hmalik88 May 9, 2023
85c5543
removed onNameLookup caveat
hmalik88 May 9, 2023
518efc0
added name-lookup endowment and updated tests
hmalik88 May 9, 2023
89fcab7
updated base snap executor to account for new export
hmalik88 May 9, 2023
7b7e1a2
fix coverage
hmalik88 May 9, 2023
9d0e0b6
another fix
hmalik88 May 9, 2023
6d28835
update permissions struct
hmalik88 May 9, 2023
135732f
fix jsdoc
hmalik88 May 10, 2023
ba5aea3
update jsdoc
hmalik88 May 10, 2023
2af6085
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Jun 9, 2023
01f8ad3
updated tests and brought branch up to date
hmalik88 Jun 9, 2023
a199562
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Jun 14, 2023
6e873dc
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Jun 15, 2023
92968e5
made changes per comments
hmalik88 Jun 15, 2023
a61d7b7
fixes
hmalik88 Jun 15, 2023
8fe0d66
fix base snap executor test
hmalik88 Jun 16, 2023
b124891
update
hmalik88 Jun 30, 2023
56a27dd
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Jun 30, 2023
c04ae3d
fix handler type
hmalik88 Jun 30, 2023
a698eae
update shasums
hmalik88 Jun 30, 2023
9335c83
fix test and update coverage
hmalik88 Jun 30, 2023
6ef9ea5
fix coverage again
hmalik88 Jun 30, 2023
dbfdf5c
clear cache
hmalik88 Jun 30, 2023
458c2a5
update types
hmalik88 Jul 31, 2023
d67c258
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Sep 1, 2023
afb1e8d
fix coverage
hmalik88 Sep 1, 2023
87c89d6
add name lookup to exports
hmalik88 Sep 1, 2023
7763b15
update coverage and shasums
hmalik88 Sep 1, 2023
a9cb007
update ee coverage
hmalik88 Sep 1, 2023
44f3a15
fix snapshot
hmalik88 Sep 1, 2023
d4e3719
fix test
hmalik88 Sep 1, 2023
4a737c1
fix coverage
hmalik88 Sep 2, 2023
2213cd0
fix struct and add test
hmalik88 Sep 2, 2023
5e6a96c
update ee coverage
hmalik88 Sep 2, 2023
f128f80
actually fix type
hmalik88 Sep 3, 2023
921fe28
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Sep 7, 2023
e2974f1
addressed PR comments
hmalik88 Sep 7, 2023
d122d61
fix coverage
hmalik88 Sep 7, 2023
a9700de
fix test description
hmalik88 Sep 8, 2023
d8c056d
added chainIds caveat
hmalik88 Sep 8, 2023
2f7e5fa
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Sep 8, 2023
76f110c
update shasums
hmalik88 Sep 8, 2023
7912dd0
update snap-utils coverage
hmalik88 Sep 8, 2023
af9d171
fix tests
hmalik88 Sep 8, 2023
65cc62d
test fixes
hmalik88 Sep 8, 2023
606db17
fix condition (again)
hmalik88 Sep 8, 2023
3c047cc
update coverage
hmalik88 Sep 9, 2023
889f60c
fix branches coverage
hmalik88 Sep 9, 2023
04e1095
remove cast
hmalik88 Sep 9, 2023
b421771
moved test
hmalik88 Sep 9, 2023
3e5015f
fix test and update coverage
hmalik88 Sep 9, 2023
b3db22b
added new type and updated casting
hmalik88 Sep 10, 2023
b34d612
added extra validation and added more tests
hmalik88 Sep 12, 2023
64334a7
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Sep 12, 2023
941d0c6
update shasums
hmalik88 Sep 12, 2023
252891c
actually fix shasums
hmalik88 Sep 12, 2023
d7f40aa
fix coverage
hmalik88 Sep 12, 2023
9b73c24
Merge branch 'main' into hm/cypherpunk-address-resolution
hmalik88 Sep 12, 2023
5920bd3
updated shasums (again)
hmalik88 Sep 12, 2023
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
2 changes: 1 addition & 1 deletion packages/examples/packages/bip32/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "KH8aF/EMcR04f03R1UGKdifPNw7fpyZE6FkWx3CCXok=",
"shasum": "FzEv7I4+3tYNzM6dcEEaoq3J3aH6l8WCo0bFp56hI3k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip44/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "vHWASh9ACAU5KJrBTL9W5h+E4gBz1qicoLPaUPgyn1M=",
"shasum": "jX8PaE9Oi1FWnJsW2Dm71crLo2uBEksCNeYPphmoy0Y=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "oUlB7WJmzj4xkUy5zipc3a3XRuWX0y4Sb2RFe+W9Wl0=",
"shasum": "7ryoRHayMjd7Ws8C+806kGR3OMNQFryMvIXPp6cDRw8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "35pPRJItVu1++OdXtdFJSpQnikDwiGceqhLQstCcjDg=",
"shasum": "txKgxLDTD0dBYL6WQgZxUqwHTcQcUUNNPU9/oWVK4Kg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-locale/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "Hr7FyKdx/cCL589IHA9ktIb2x3ZuJnur0Ou3uHdz/BU=",
"shasum": "0T4dTLDl+8OxbagVuXQ+hgE81KlmdxpZYWSShfw6+Hc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "8j4f5pa80kudzKuhSZv4vjwACbnvSNMkL7E3JHht2Rc=",
"shasum": "AVvItsFeFROPIhuZLuxiNc6egWLhbhJQR9//daxg8qQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "d8UDQBUPEjjFcO6L2RmRSoMS5pFyvWn1XPhhlJws074=",
"shasum": "Gw+1DNkjLVuQfJbrVA2sG+lIgek5oEuk0jRdsd44Ky8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "M6vtG8D2FjhY/E7fNT7qHeRT5I7tg1Rs6KKT3z1+8cM=",
"shasum": "cZdsqMkePYvSOaFyIkpn3WchgN4Dk/tDvVEHRyjdHeQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 88.7,
"functions": 95,
"lines": 96.7,
"statements": 96.35
"branches": 89,
"functions": 95.13,
"lines": 96.77,
"statements": 96.44
}
Original file line number Diff line number Diff line change
Expand Up @@ -2529,7 +2529,7 @@ describe('SnapController', () => {
[MOCK_SNAP_ID]: {},
}),
).rejects.toThrow(
'A snap must request at least one of the following permissions: endowment:rpc, endowment:transaction-insight, endowment:cronjob, endowment:lifecycle-hooks.',
'A snap must request at least one of the following permissions: endowment:rpc, endowment:transaction-insight, endowment:cronjob, endowment:name-lookup, endowment:lifecycle-hooks.',
);

controller.destroy();
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-controllers/src/snaps/endowments/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export enum SnapEndowments {
EthereumProvider = 'endowment:ethereum-provider',
Rpc = 'endowment:rpc',
WebAssemblyAccess = 'endowment:webassembly',
NameLookup = 'endowment:name-lookup',
LifecycleHooks = 'endowment:lifecycle-hooks',
}
10 changes: 10 additions & 0 deletions packages/snaps-controllers/src/snaps/endowments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import {
import { ethereumProviderEndowmentBuilder } from './ethereum-provider';
import { lifecycleHooksEndowmentBuilder } from './lifecycle-hooks';
import { longRunningEndowmentBuilder } from './long-running';
import {
getNameLookupCaveatMapper,
nameLookupCaveatSpecifications,
nameLookupEndowmentBuilder,
} from './name-lookup';
import { networkAccessEndowmentBuilder } from './network-access';
import {
getRpcCaveatMapper,
Expand All @@ -33,13 +38,15 @@ export const endowmentPermissionBuilders = {
ethereumProviderEndowmentBuilder,
[rpcEndowmentBuilder.targetName]: rpcEndowmentBuilder,
[webAssemblyEndowmentBuilder.targetName]: webAssemblyEndowmentBuilder,
[nameLookupEndowmentBuilder.targetName]: nameLookupEndowmentBuilder,
[lifecycleHooksEndowmentBuilder.targetName]: lifecycleHooksEndowmentBuilder,
} as const;

export const endowmentCaveatSpecifications = {
...cronjobCaveatSpecifications,
...transactionInsightCaveatSpecifications,
...rpcCaveatSpecifications,
...nameLookupCaveatSpecifications,
};

export const endowmentCaveatMappers: Record<
Expand All @@ -50,16 +57,19 @@ export const endowmentCaveatMappers: Record<
[transactionInsightEndowmentBuilder.targetName]:
getTransactionInsightCaveatMapper,
[rpcEndowmentBuilder.targetName]: getRpcCaveatMapper,
[nameLookupEndowmentBuilder.targetName]: getNameLookupCaveatMapper,
};

export const handlerEndowments: Record<HandlerType, string> = {
[HandlerType.OnRpcRequest]: rpcEndowmentBuilder.targetName,
[HandlerType.OnTransaction]: transactionInsightEndowmentBuilder.targetName,
[HandlerType.OnCronjob]: cronjobEndowmentBuilder.targetName,
[HandlerType.OnNameLookup]: nameLookupEndowmentBuilder.targetName,
[HandlerType.OnInstall]: lifecycleHooksEndowmentBuilder.targetName,
[HandlerType.OnUpdate]: lifecycleHooksEndowmentBuilder.targetName,
};

export * from './enum';
export { getRpcCaveatOrigins } from './rpc';
export { getTransactionOriginCaveat } from './transaction-insight';
export { getChainIdsCaveat } from './name-lookup';
187 changes: 187 additions & 0 deletions packages/snaps-controllers/src/snaps/endowments/name-lookup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import type { PermissionConstraint } from '@metamask/permission-controller';
import { PermissionType, SubjectType } from '@metamask/permission-controller';
import { SnapCaveatType } from '@metamask/snaps-utils';

import { SnapEndowments } from '.';
import {
nameLookupEndowmentBuilder,
getChainIdsCaveat,
getNameLookupCaveatMapper,
nameLookupCaveatSpecifications,
} from './name-lookup';

describe('endowment:name-lookup', () => {
const specification = nameLookupEndowmentBuilder.specificationBuilder({});
it('builds the expected permission specification', () => {
expect(specification).toStrictEqual({
permissionType: PermissionType.Endowment,
targetName: SnapEndowments.NameLookup,
endowmentGetter: expect.any(Function),
allowedCaveats: [SnapCaveatType.ChainIds],
subjectTypes: [SubjectType.Snap],
validator: expect.any(Function),
});

expect(specification.endowmentGetter()).toBeUndefined();
});

describe('validator', () => {
it('disallows no caveats', () => {
expect(() =>
// @ts-expect-error Missing required permission types.
specification.validator({}),
).toThrow('Expected a single "chainIds" caveat.');
});

it('throws if the caveat is not a single "chainIds"', () => {
expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [{ type: 'foo', value: 'bar' }],
}),
).toThrow('Expected a single "chainIds" caveat.');

expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [
{ type: 'chainIds', value: ['foo'] },
{ type: 'chainIds', value: ['bar'] },
],
}),
).toThrow('Expected a single "chainIds" caveat.');
});
});
});

describe('getChainIdsCaveat', () => {
it('returns the value from a name-lookup permission', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: [
{
type: SnapCaveatType.ChainIds,
value: ['eip155:1'],
},
],
};
expect(getChainIdsCaveat(permission)).toStrictEqual(['eip155:1']);
});

it('returns null if the input is undefined', () => {
expect(getChainIdsCaveat(undefined)).toBeNull();
});

it('returns null if the permission does not have caveats', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: null,
};

expect(getChainIdsCaveat(permission)).toBeNull();
});

it('throws if the permission does not have exactly one caveat', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: [
{
type: SnapCaveatType.ChainIds,
value: ['eip155:1'],
},
{
type: SnapCaveatType.ChainIds,
value: ['eip155:2'],
},
],
};

expect(() => getChainIdsCaveat(permission)).toThrow('Assertion failed');
});

it('throws if the first caveat is not a "chainIds" caveat', () => {
const permission: PermissionConstraint = {
date: 0,
parentCapability: 'foo',
invoker: 'bar',
id: 'baz',
caveats: [
{
type: SnapCaveatType.PermittedCoinTypes,
value: 'foo',
},
],
};

expect(() => getChainIdsCaveat(permission)).toThrow('Assertion failed');
});
});

describe('getNameLookupCaveatMapper', () => {
it('maps input to a caveat', () => {
expect(getNameLookupCaveatMapper(['eip155:1'])).toStrictEqual({
caveats: [
{
type: 'chainIds',
value: ['eip155:1'],
},
],
});
});

it('does not include caveat if input is empty array', () => {
expect(getNameLookupCaveatMapper([])).toStrictEqual({
caveats: null,
});
});
});

describe('nameLookupCaveatSpecifications', () => {
describe('validator', () => {
it('throws if the caveat is missing a value key or is not an object', () => {
expect(() =>
nameLookupCaveatSpecifications[SnapCaveatType.ChainIds].validator?.(
// @ts-expect-error Missing value type.
{
type: SnapCaveatType.ChainIds,
},
),
).toThrow('Expected a plain object.');
});

it.each([
{
type: SnapCaveatType.ChainIds,
value: ['eip155'],
},
{
type: SnapCaveatType.ChainIds,
value: undefined,
},
])('throws if the caveat values are invalid types', (val) => {
expect(() =>
nameLookupCaveatSpecifications[SnapCaveatType.ChainIds].validator?.(
val,
),
).toThrow('Expected caveat value to have type "string array"');
});

it('will not throw with a valid caveat value', () => {
expect(() =>
nameLookupCaveatSpecifications[SnapCaveatType.ChainIds].validator?.({
type: SnapCaveatType.ChainIds,
value: ['eip155:1'],
}),
).not.toThrow();
});
});
});
Loading