Skip to content

Commit

Permalink
Refactor based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 15, 2022
1 parent 22498be commit 93771da
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/examples/examples/ethers-js/snap.manifest.json
Expand Up @@ -17,7 +17,7 @@
}
},
"initialPermissions": {
"endowment:eip1193": {}
"endowment:ethereum-provider": {}
},
"manifestVersion": "0.1"
}
8 changes: 4 additions & 4 deletions packages/snaps-controllers/src/snaps/SnapController.test.ts
Expand Up @@ -929,7 +929,7 @@ describe('SnapController', () => {
`;

const options = getSnapControllerWithEESOptions({
environmentEndowmentPermissions: [SnapEndowments.EIP1193],
environmentEndowmentPermissions: [SnapEndowments.EthereumProvider],
idleTimeCheckInterval: 30000,
maxIdleTime: 160000,
state: {
Expand All @@ -954,7 +954,7 @@ describe('SnapController', () => {
return true;
} else if (
method === 'PermissionController:getEndowments' &&
args[1] === SnapEndowments.EIP1193
args[1] === SnapEndowments.EthereumProvider
) {
return ['ethereum'];
}
Expand Down Expand Up @@ -1023,7 +1023,7 @@ describe('SnapController', () => {
`;

const options = getSnapControllerWithEESOptions({
environmentEndowmentPermissions: [SnapEndowments.EIP1193],
environmentEndowmentPermissions: [SnapEndowments.EthereumProvider],
idleTimeCheckInterval: 30000,
maxIdleTime: 160000,
state: {
Expand All @@ -1048,7 +1048,7 @@ describe('SnapController', () => {
return true;
} else if (
method === 'PermissionController:getEndowments' &&
args[1] === SnapEndowments.EIP1193
args[1] === SnapEndowments.EthereumProvider
) {
return ['ethereum'];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-controllers/src/snaps/endowments/enum.ts
Expand Up @@ -4,5 +4,5 @@ export enum SnapEndowments {
TransactionInsight = 'endowment:transaction-insight',
Keyring = 'endowment:keyring',
Cronjob = 'endowment:cronjob',
EIP1193 = 'endowment:eip1193',
EthereumProvider = 'endowment:ethereum-provider',
}
@@ -1,13 +1,15 @@
import { PermissionType } from '@metamask/controllers';
import { SnapEndowments } from './enum';
import { eip1193EndowmentBuilder } from './eip1193';
import { ethereumProviderEndowmentBuilder } from './ethereum-provider';

describe('endowment:eip1193', () => {
it('builds the expected permission specification', () => {
const specification = eip1193EndowmentBuilder.specificationBuilder({});
const specification = ethereumProviderEndowmentBuilder.specificationBuilder(
{},
);
expect(specification).toStrictEqual({
permissionType: PermissionType.Endowment,
targetKey: SnapEndowments.EIP1193,
targetKey: SnapEndowments.EthereumProvider,
endowmentGetter: expect.any(Function),
allowedCaveats: null,
});
Expand Down
Expand Up @@ -6,27 +6,29 @@ import {
} from '@metamask/controllers';
import { SnapEndowments } from './enum';

const permissionName = SnapEndowments.EIP1193;
const permissionName = SnapEndowments.EthereumProvider;

type EIP1193EndowmentSpecification = ValidPermissionSpecification<{
type EthereumProviderEndowmentSpecification = ValidPermissionSpecification<{
permissionType: PermissionType.Endowment;
targetKey: typeof permissionName;
endowmentGetter: (_options?: any) => ['ethereum'];
allowedCaveats: null;
}>;

/**
* `endowment:eip1193` returns the name of the ethereum global browser API.
* `endowment:ethereum-provider` returns the name of the ethereum global browser API.
* This is intended to populate the endowments of the
* SES Compartment in which a Snap executes.
*
* This populates the global scope with an EIP-1193 provider, which DOES NOT implement all legacy functionality exposed to dapps.
*
* @param _builderOptions - Optional specification builder options.
* @returns The specification for the network endowment.
*/
const specificationBuilder: PermissionSpecificationBuilder<
PermissionType.Endowment,
any,
EIP1193EndowmentSpecification
EthereumProviderEndowmentSpecification
> = (_builderOptions?: any) => {
return {
permissionType: PermissionType.Endowment,
Expand All @@ -38,7 +40,7 @@ const specificationBuilder: PermissionSpecificationBuilder<
};
};

export const eip1193EndowmentBuilder = Object.freeze({
export const ethereumProviderEndowmentBuilder = Object.freeze({
targetKey: permissionName,
specificationBuilder,
} as const);
5 changes: 3 additions & 2 deletions packages/snaps-controllers/src/snaps/endowments/index.ts
Expand Up @@ -13,7 +13,7 @@ import {
keyringCaveatSpecifications,
getKeyringCaveatMapper,
} from './keyring';
import { eip1193EndowmentBuilder } from './eip1193';
import { ethereumProviderEndowmentBuilder } from './ethereum-provider';

export const endowmentPermissionBuilders = {
[networkAccessEndowmentBuilder.targetKey]: networkAccessEndowmentBuilder,
Expand All @@ -22,7 +22,8 @@ export const endowmentPermissionBuilders = {
transactionInsightEndowmentBuilder,
[keyringEndowmentBuilder.targetKey]: keyringEndowmentBuilder,
[cronjobEndowmentBuilder.targetKey]: cronjobEndowmentBuilder,
[eip1193EndowmentBuilder.targetKey]: eip1193EndowmentBuilder,
[ethereumProviderEndowmentBuilder.targetKey]:
ethereumProviderEndowmentBuilder,
} as const;

export const endowmentCaveatSpecifications = {
Expand Down
Expand Up @@ -3,7 +3,7 @@
import { Duplex } from 'stream';
import { StreamProvider } from '@metamask/providers';
import { createIdRemapMiddleware } from 'json-rpc-engine';
import { SnapExports, SnapAPI } from '@metamask/snaps-types';
import { SnapExports, SnapsGlobalObject } from '@metamask/snaps-types';
import { errorCodes, ethErrors, serializeError } from 'eth-rpc-errors';
import {
isObject,
Expand Down Expand Up @@ -298,7 +298,7 @@ export class BaseSnapExecutor {

await provider.initialize();

const snap = this.createSnapAPI(provider);
const snap = this.createSnapGlobal(provider);
const ethereum = this.createEIP1193Provider(provider);
// We specifically use any type because the Snap can modify the object any way they want
const snapModule: any = { exports: {} };
Expand Down Expand Up @@ -377,7 +377,7 @@ export class BaseSnapExecutor {
* @param provider - A StreamProvider connected to MetaMask.
* @returns The snap provider object.
*/
private createSnapAPI(provider: StreamProvider): SnapAPI {
private createSnapGlobal(provider: StreamProvider): SnapsGlobalObject {
const originalRequest = provider.request;

const request = async (args: RequestArguments) => {
Expand All @@ -397,10 +397,10 @@ export class BaseSnapExecutor {
}

/**
* Instantiates an eip1193 provider object (i.e. `globalThis.ethereum`).
* Instantiates an EIP-1193 Ethereum provider object (i.e. `globalThis.ethereum`).
*
* @param provider - A StreamProvider connected to MetaMask.
* @returns The eip1193 provider object.
* @returns The EIP-1193 Ethereum provider object.
*/
private createEIP1193Provider(provider: StreamProvider): StreamProvider {
const originalRequest = provider.request;
Expand Down
@@ -1,4 +1,4 @@
import { SnapAPI } from '@metamask/snaps-types';
import { SnapsGlobalObject } from '@metamask/snaps-types';
import { hasProperty } from '@metamask/utils';
import { StreamProvider } from '@metamask/providers';
import { rootRealmGlobal } from '../globalObject';
Expand Down Expand Up @@ -49,7 +49,7 @@ const endowmentFactories = [timeout, interval, network, crypto, math].reduce(
* @returns An object containing the Snap's endowments.
*/
export function createEndowments(
snap: SnapAPI,
snap: SnapsGlobalObject,
ethereum: StreamProvider,
endowments: string[] = [],
): { endowments: Record<string, unknown>; teardown: () => Promise<void> } {
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-types/global.d.ts
@@ -1,8 +1,8 @@
import { MetaMaskInpageProvider } from '@metamask/providers';
import { SnapAPI } from './src';
import { SnapsGlobalObject } from './src';

// Types that should be available globally within a Snap
declare global {
const ethereum: MetaMaskInpageProvider;
const snap: SnapAPI;
const snap: SnapsGlobalObject;
}
2 changes: 1 addition & 1 deletion packages/snaps-types/src/types.d.ts
Expand Up @@ -22,7 +22,7 @@ export type OnCronjobHandler = (args: {
request: JsonRpcRequest<unknown[] | { [key: string]: unknown }>;
}) => Promise<unknown>;

export type SnapAPI = { request: StreamProvider['request'] };
export type SnapsGlobalObject = { request: StreamProvider['request'] };

export type Ethereum = StreamProvider;

Expand Down
6 changes: 3 additions & 3 deletions packages/snaps-utils/src/mock.ts
Expand Up @@ -6,7 +6,7 @@ const NETWORK_APIS = ['fetch', 'WebSocket'];

export const ALL_APIS: string[] = [...DEFAULT_ENDOWMENTS, ...NETWORK_APIS];

type MockSnapProvider = {
type MockSnapGlobal = {
request: () => Promise<any>;
};

Expand All @@ -19,7 +19,7 @@ type MockEthereumProvider = EventEmitter & {
*
* @returns A mocked snap provider.
*/
function getMockSnapAPI(): MockSnapProvider {
function getMockSnapGlobal(): MockSnapGlobal {
return { request: async () => true };
}

Expand Down Expand Up @@ -117,6 +117,6 @@ const generateMockEndowment = (key: string) => {
export const generateMockEndowments = () => {
return ALL_APIS.reduce<Record<string, any>>(
(acc, cur) => ({ ...acc, [cur]: generateMockEndowment(cur) }),
{ snap: getMockSnapAPI(), ethereum: getMockEthereumProvider() },
{ snap: getMockSnapGlobal(), ethereum: getMockEthereumProvider() },
);
};

0 comments on commit 93771da

Please sign in to comment.