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

BREAKING: Remove wallet global in favor of snaps and ethereum #939

Merged
merged 13 commits into from
Nov 15, 2022
3 changes: 2 additions & 1 deletion packages/examples/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = {
browser: true,
},
globals: {
wallet: true,
ethereum: true,
snap: true,
},
rules: {
'no-alert': 'off',
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/bls-signer/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-monorepo.git"
},
"source": {
"shasum": "V9PaaBMIyXuDzYQfUWBZjNStPgSv1MBp6g9En7iV6x4=",
"shasum": "ixIA4a4Yuk4obx/C6ZwX8wKeYh9pFZTNueB60O7l+rg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/examples/bls-signer/src/index.js
Original file line number Diff line number Diff line change
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 snap.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 snap.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 snap.request({
method: 'snap_confirm',
params: [{ prompt: header, textAreaContent: message }],
});
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/browserify/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-monorepo.git"
},
"source": {
"shasum": "C25lIsH+SbFGCJM9i1lGLdPYq7YsUX0UHr/tZFMfsHI=",
"shasum": "NOiOxPyR8Iu9y+I/6ziTImUC223pg/bLd6z9hRVrPtc=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/browserify/src/snap.ts
Original file line number Diff line number Diff line change
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 snap.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snap.request({
method: 'snap_notify',
params: [
{
Expand Down
6 changes: 4 additions & 2 deletions packages/examples/examples/ethers-js/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-monorepo.git"
},
"source": {
"shasum": "zLzPFhtq7ZvlAkDtR+hmKm0fS/qh51QTE1eyAqpsuYQ=",
"shasum": "ZKh05KNz06P5GlNCYOuSUe3QQX6OuhSbMjH7RyCQmHo=",
"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
Original file line number Diff line number Diff line change
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 snap.request({
method: 'snap_getAppKey',
});
console.log(`privKey is ${privKey}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snap-template.git"
},
"source": {
"shasum": "7FEkvygLr4v+1VdoUCTGPLz4BNWRZe5q4F46tHbEWMA=",
"shasum": "4aAg0VXdfByp6BWb6DuQo5Ui8rq+dx03/zuVgxp+KG4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/notifications/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
module.exports.onRpcRequest = async ({ origin, request }) => {
switch (request.method) {
case 'inApp':
return wallet.request({
return snap.request({
method: 'snap_notify',
params: [
{
Expand All @@ -23,7 +23,7 @@ module.exports.onRpcRequest = async ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snap.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/rollup/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-monorepo.git"
},
"source": {
"shasum": "KBCbe8PgRkiublGQuK8NPeUex737E8Px3fSrSdnYuIg=",
"shasum": "LLph+wooA1BT1BgEem4w2xzfocSKhSOcuQOyHnSNM/4=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/rollup/src/snap.ts
Original file line number Diff line number Diff line change
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 snap.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snap.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/typescript/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snap-template.git"
},
"source": {
"shasum": "yUHyjRd6jE4MaIr2yc9eqRV2ndwBQAiYHlOpPQITY4c=",
"shasum": "PFi6gpY6a3nVdQwkAvujHDe0hnFmdUKjS8bKMlM/qKs=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
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 snap.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/webpack/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-monorepo.git"
},
"source": {
"shasum": "ZWFcrhWfJfQko9o1PLh+IdOh+GbmDVpA5Ndhq8rhLJM=",
"shasum": "LMGtgd574bqP5aJQSJXJwgeOzB0sBaeJ6c8t9fkIdqM=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/examples/webpack/src/snap.ts
Original file line number Diff line number Diff line change
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 snap.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return wallet.request({
return snap.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/multichain-provider/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Loading