Skip to content

Commit

Permalink
BREAKING: Remove wallet global in favor of snaps and ethereum (#…
Browse files Browse the repository at this point in the history
…939)

* Initial implementation of new global APIs

* Fix a bunch of tests and simplify

* More tests

* Add EIP1193 permission

* Fix tests

* Use new APIs in examples

* Fix tests following rebase

* Update coverage

* Fix some PR comments

* Abstract createService function

* Remove leftover comment

* Refactor based on PR comments

* Rename snap global to snaps

Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
  • Loading branch information
FrederikBolding and Mrtenz committed Nov 15, 2022
1 parent cd30f4f commit 841ae5b
Show file tree
Hide file tree
Showing 44 changed files with 788 additions and 776 deletions.
3 changes: 2 additions & 1 deletion packages/examples/.eslintrc.js
Expand Up @@ -9,7 +9,8 @@ module.exports = {
browser: true,
},
globals: {
wallet: true,
ethereum: true,
snaps: true,
},
rules: {
'no-alert': 'off',
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/bls-signer/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps-monorepo.git"
},
"source": {
"shasum": "V9PaaBMIyXuDzYQfUWBZjNStPgSv1MBp6g9En7iV6x4=",
"shasum": "miCH7djJ/dGwPije8HLyMJsmXkmjD1E5XJ4ZWIRJt48=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/examples/bls-signer/src/index.js
Expand Up @@ -32,7 +32,7 @@ module.exports.onRpcRequest = async ({ request }) => {
throw rpcErrors.eth.unauthorized();
}

const PRIVATE_KEY = await wallet.request({
const PRIVATE_KEY = await snaps.request({
method: 'snap_getEntropy',
params: {
version: 1,
Expand All @@ -53,7 +53,7 @@ module.exports.onRpcRequest = async ({ request }) => {
* @returns {Promise<Uint8Array>} The BLS12-381 public key.
*/
async function getPubKey() {
const PRIV_KEY = await wallet.request({
const PRIV_KEY = await snaps.request({
method: 'snap_getAppKey',
});
return bls.getPublicKey(PRIV_KEY);
Expand All @@ -70,7 +70,7 @@ async function getPubKey() {
* and `false` otherwise.
*/
async function promptUser(header, message) {
const response = await wallet.request({
const response = await snaps.request({
method: 'snap_confirm',
params: [{ prompt: header, textAreaContent: message }],
});
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/browserify/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps-monorepo.git"
},
"source": {
"shasum": "C25lIsH+SbFGCJM9i1lGLdPYq7YsUX0UHr/tZFMfsHI=",
"shasum": "8+W9D201oiHmmRETMd+n4W7Qj8Q+NicMhFutRy7Wgm4=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/browserify/src/snap.ts
Expand Up @@ -14,7 +14,7 @@ import { OnRpcRequestHandler } from '@metamask/snaps-types';
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
switch (request.method) {
case 'inApp':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
6 changes: 4 additions & 2 deletions packages/examples/examples/ethers-js/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps-monorepo.git"
},
"source": {
"shasum": "zLzPFhtq7ZvlAkDtR+hmKm0fS/qh51QTE1eyAqpsuYQ=",
"shasum": "VOyBi0w/CPD9GUbWssCYdqrJtaPG0Lnu6GhT2ZN1eiI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -16,6 +16,8 @@
}
}
},
"initialPermissions": {},
"initialPermissions": {
"endowment:ethereum-provider": {}
},
"manifestVersion": "0.1"
}
8 changes: 2 additions & 6 deletions packages/examples/examples/ethers-js/src/index.js
Expand Up @@ -5,11 +5,7 @@

const ethers = require('ethers');

/*
* The `wallet` API is a superset of the standard provider,
* and can be used to initialize an ethers.js provider like this:
*/
const provider = new ethers.providers.Web3Provider(wallet);
const provider = new ethers.providers.Web3Provider(ethereum);

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand All @@ -22,7 +18,7 @@ const provider = new ethers.providers.Web3Provider(wallet);
*/
module.exports.onRpcRequest = async ({ request }) => {
console.log('received request', request);
const privKey = await wallet.request({
const privKey = await snaps.request({
method: 'snap_getAppKey',
});
console.log(`privKey is ${privKey}`);
Expand Down
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snap-template.git"
},
"source": {
"shasum": "7FEkvygLr4v+1VdoUCTGPLz4BNWRZe5q4F46tHbEWMA=",
"shasum": "ShjN76CTBN8ByMf+CZ9A81rHcKg3VcpU/aDsC8HjFls=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/notifications/src/index.js
Expand Up @@ -13,7 +13,7 @@
module.exports.onRpcRequest = async ({ origin, request }) => {
switch (request.method) {
case 'inApp':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -23,7 +23,7 @@ module.exports.onRpcRequest = async ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/rollup/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps-monorepo.git"
},
"source": {
"shasum": "KBCbe8PgRkiublGQuK8NPeUex737E8Px3fSrSdnYuIg=",
"shasum": "lXWNW+zE94++8/TeAZ1stUKzoRe1ohLqJY4dUiS54QA=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/rollup/src/snap.ts
Expand Up @@ -14,7 +14,7 @@ import { OnRpcRequestHandler } from '@metamask/snaps-types';
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
switch (request.method) {
case 'inApp':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/typescript/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snap-template.git"
},
"source": {
"shasum": "yUHyjRd6jE4MaIr2yc9eqRV2ndwBQAiYHlOpPQITY4c=",
"shasum": "s67hGX4eR3eMxlZIPN3C/M19HkeubAWpVID4HQvH2Ww=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/typescript/src/index.ts
Expand Up @@ -15,7 +15,7 @@ import { getMessage } from './message';
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
switch (request.method) {
case 'hello':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/webpack/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps-monorepo.git"
},
"source": {
"shasum": "ZWFcrhWfJfQko9o1PLh+IdOh+GbmDVpA5Ndhq8rhLJM=",
"shasum": "IWOYjh6N3N8KUj08/0bjGsRHJQSNuJlMvt/3/cCNPBk=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/webpack/src/snap.ts
Expand Up @@ -14,7 +14,7 @@ import { OnRpcRequestHandler } from '@metamask/snaps-types';
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
switch (request.method) {
case 'inApp':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/multichain-provider/package.json
Expand Up @@ -25,8 +25,8 @@
"publish:package": "../../scripts/publish-package.sh"
},
"dependencies": {
"@metamask/providers": "^10.2.0",
"@metamask/safe-event-emitter": "^2.0.0",
"@metamask/snaps-types": "^0.23.0",
"@metamask/snaps-utils": "^0.23.0",
"@metamask/utils": "^3.3.1",
"nanoid": "^3.1.31"
Expand Down
4 changes: 2 additions & 2 deletions packages/multichain-provider/src/MultiChainProvider.test.ts
@@ -1,4 +1,4 @@
import { SnapProvider } from '@metamask/snaps-types';
import { MetaMaskInpageProvider } from '@metamask/providers';
import { NamespaceId, RequestNamespace } from '@metamask/snaps-utils';
import {
getRequestNamespace,
Expand All @@ -14,7 +14,7 @@ Object.assign(globalThis, {
});

// Used for mocking the provider's return values.
declare const ethereum: SnapProvider;
declare const ethereum: MetaMaskInpageProvider;

/**
* Get a new `MultiChainProvider` instance, that is connected to the given
Expand Down
4 changes: 2 additions & 2 deletions packages/multichain-provider/src/MultiChainProvider.ts
Expand Up @@ -12,7 +12,7 @@ import {
Session,
} from '@metamask/snaps-utils';
import { JsonRpcRequest, Json } from '@metamask/utils';
import type { SnapProvider } from '@metamask/snaps-types';
import { MetaMaskInpageProvider } from '@metamask/providers';
import { nanoid } from 'nanoid';
import { Provider } from './Provider';

Expand All @@ -21,7 +21,7 @@ declare global {
// here.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
ethereum: SnapProvider;
ethereum: MetaMaskInpageProvider;
}
}

Expand Down
@@ -1,16 +1,13 @@
import { ControllerMessenger } from '@metamask/controllers';
import { HandlerType } from '@metamask/snaps-utils';
import {
ErrorMessageEvent,
ExecutionServiceMessenger,
} from './ExecutionService';
import { createService } from '../test-utils';
import { ExecutionServiceArgs } from './AbstractExecutionService';
import { NodeThreadExecutionService } from './node';

class MockExecutionService extends NodeThreadExecutionService {
constructor(messenger: ExecutionServiceMessenger) {
constructor({ messenger, setupSnapProvider }: ExecutionServiceArgs) {
super({
messenger,
setupSnapProvider: () => undefined,
setupSnapProvider,
});
}

Expand All @@ -27,19 +24,7 @@ describe('AbstractExecutionService', () => {
it('logs error for unrecognized notifications', async () => {
const consoleErrorSpy = jest.spyOn(console, 'error');

const controllerMessenger = new ControllerMessenger<
never,
ErrorMessageEvent
>();
const service = new MockExecutionService(
controllerMessenger.getRestricted<
'ExecutionService',
never,
ErrorMessageEvent['type']
>({
name: 'ExecutionService',
}),
);
const { service } = createService(MockExecutionService);

await service.executeSnap({
snapId: 'TestSnap',
Expand All @@ -66,19 +51,7 @@ describe('AbstractExecutionService', () => {
it('logs error for malformed UnhandledError notification', async () => {
const consoleErrorSpy = jest.spyOn(console, 'error');

const controllerMessenger = new ControllerMessenger<
never,
ErrorMessageEvent
>();
const service = new MockExecutionService(
controllerMessenger.getRestricted<
'ExecutionService',
never,
ErrorMessageEvent['type']
>({
name: 'ExecutionService',
}),
);
const { service } = createService(MockExecutionService);

await service.executeSnap({
snapId: 'TestSnap',
Expand Down Expand Up @@ -112,20 +85,7 @@ describe('AbstractExecutionService', () => {
});

it('throws an error if RPC request handler is unavailable', async () => {
const controllerMessenger = new ControllerMessenger<
never,
ErrorMessageEvent
>();
const service = new MockExecutionService(
controllerMessenger.getRestricted<
'ExecutionService',
never,
ErrorMessageEvent['type']
>({
name: 'ExecutionService',
}),
);

const { service } = createService(MockExecutionService);
const snapId = 'TestSnap';
await expect(
service.handleRpcRequest(snapId, {
Expand Down

0 comments on commit 841ae5b

Please sign in to comment.