Skip to content

Commit

Permalink
Rename snap global to snaps
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 15, 2022
1 parent 93771da commit 443444c
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/examples/.eslintrc.js
Expand Up @@ -10,7 +10,7 @@ module.exports = {
},
globals: {
ethereum: true,
snap: 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": "ixIA4a4Yuk4obx/C6ZwX8wKeYh9pFZTNueB60O7l+rg=",
"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 snap.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 snap.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 snap.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": "NOiOxPyR8Iu9y+I/6ziTImUC223pg/bLd6z9hRVrPtc=",
"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 snap.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return snap.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/ethers-js/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps-monorepo.git"
},
"source": {
"shasum": "ZKh05KNz06P5GlNCYOuSUe3QQX6OuhSbMjH7RyCQmHo=",
"shasum": "VOyBi0w/CPD9GUbWssCYdqrJtaPG0Lnu6GhT2ZN1eiI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/examples/ethers-js/src/index.js
Expand Up @@ -18,7 +18,7 @@ const provider = new ethers.providers.Web3Provider(ethereum);
*/
module.exports.onRpcRequest = async ({ request }) => {
console.log('received request', request);
const privKey = await snap.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": "4aAg0VXdfByp6BWb6DuQo5Ui8rq+dx03/zuVgxp+KG4=",
"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 snap.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -23,7 +23,7 @@ module.exports.onRpcRequest = async ({ origin, request }) => {
],
});
case 'native':
return snap.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": "LLph+wooA1BT1BgEem4w2xzfocSKhSOcuQOyHnSNM/4=",
"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 snap.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return snap.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": "PFi6gpY6a3nVdQwkAvujHDe0hnFmdUKjS8bKMlM/qKs=",
"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 snap.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": "LMGtgd574bqP5aJQSJXJwgeOzB0sBaeJ6c8t9fkIdqM=",
"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 snap.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand All @@ -24,7 +24,7 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
],
});
case 'native':
return snap.request({
return snaps.request({
method: 'snap_notify',
params: [
{
Expand Down
Expand Up @@ -533,7 +533,7 @@ describe('BaseSnapExecutor', () => {

it('reports when outbound requests are made using snap API', async () => {
const CODE = `
module.exports.onRpcRequest = () => snap.request({ method: 'wallet_getPermissions', params: [] });
module.exports.onRpcRequest = () => snaps.request({ method: 'wallet_getPermissions', params: [] });
`;
const executor = new TestSnapExecutor();

Expand Down Expand Up @@ -637,7 +637,7 @@ describe('BaseSnapExecutor', () => {

it('only allows certain methods in snap API', async () => {
const CODE = `
module.exports.onRpcRequest = () => snap.request({ method: 'eth_blockNumber', params: [] });
module.exports.onRpcRequest = () => snaps.request({ method: 'eth_blockNumber', params: [] });
`;
const executor = new TestSnapExecutor();

Expand Down
@@ -1,7 +1,7 @@
import fetchMock from 'jest-fetch-mock';
import { createEndowments, isConstructor } from '.';

const mockSnapAPI = { foo: Symbol('bar') };
const mockSnapsAPI = { foo: Symbol('bar') };
const mockEthereum = { foo: Symbol('bar') };

describe('Endowment utils', () => {
Expand All @@ -18,30 +18,30 @@ describe('Endowment utils', () => {

it('handles no endowments', () => {
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
);

expect(
createEndowments(mockSnapAPI as any, mockEthereum as any),
createEndowments(mockSnapsAPI as any, mockEthereum as any),
).toStrictEqual({
endowments: {
snap: mockSnapAPI,
snaps: mockSnapsAPI,
},
teardown: expect.any(Function),
});
expect(endowments.snap).toBe(mockSnapAPI);
expect(endowments.snaps).toBe(mockSnapsAPI);
});

it('handles unattenuated endowments', () => {
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['Uint8Array', 'Date'],
);

expect(endowments).toStrictEqual({
snap: mockSnapAPI,
snaps: mockSnapsAPI,
Uint8Array,
Date,
});
Expand All @@ -55,7 +55,7 @@ describe('Endowment utils', () => {
};
Object.assign(globalThis, { mockEndowment });
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['Date', 'mockEndowment'],
);
Expand All @@ -65,41 +65,41 @@ describe('Endowment utils', () => {

it('handles factory endowments', () => {
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['setTimeout'],
);

expect(endowments).toStrictEqual({
snap: mockSnapAPI,
snaps: mockSnapsAPI,
setTimeout: expect.any(Function),
});
expect(endowments.setTimeout).not.toBe(setTimeout);
});

it('handles some endowments from the same factory', () => {
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['setTimeout'],
);

expect(endowments).toMatchObject({
snap: mockSnapAPI,
snaps: mockSnapsAPI,
setTimeout: expect.any(Function),
});
expect(endowments.setTimeout).not.toBe(setTimeout);
});

it('handles all endowments from the same factory', () => {
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['setTimeout', 'clearTimeout'],
);

expect(endowments).toMatchObject({
snap: mockSnapAPI,
snaps: mockSnapsAPI,
setTimeout: expect.any(Function),
clearTimeout: expect.any(Function),
});
Expand All @@ -108,7 +108,7 @@ describe('Endowment utils', () => {

it('handles multiple endowments, factory and non-factory', () => {
const { endowments } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
[
'console',
Expand All @@ -121,7 +121,7 @@ describe('Endowment utils', () => {
);

expect(endowments).toMatchObject({
snap: mockSnapAPI,
snaps: mockSnapsAPI,
console,
Uint8Array,
Math: expect.any(Object),
Expand All @@ -130,7 +130,7 @@ describe('Endowment utils', () => {
WebAssembly,
});

expect(endowments.snap).toBe(mockSnapAPI);
expect(endowments.snaps).toBe(mockSnapsAPI);
expect(endowments.console).toBe(console);
expect(endowments.Uint8Array).toBe(Uint8Array);
expect(endowments.WebAssembly).toBe(WebAssembly);
Expand All @@ -142,13 +142,13 @@ describe('Endowment utils', () => {

it('throws for unknown endowments', () => {
expect(() =>
createEndowments(mockSnapAPI as any, mockEthereum as any, ['foo']),
createEndowments(mockSnapsAPI as any, mockEthereum as any, ['foo']),
).toThrow('Unknown endowment: "foo"');
});

it('teardown calls all teardown functions', () => {
const { endowments, teardown } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'],
);
Expand All @@ -175,7 +175,7 @@ describe('Endowment utils', () => {
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);
expect(clearIntervalSpy).toHaveBeenCalledTimes(1);
expect(endowments).toMatchObject({
snap: mockSnapAPI,
snaps: mockSnapsAPI,
setTimeout: expect.any(Function),
clearTimeout: expect.any(Function),
setInterval: expect.any(Function),
Expand All @@ -185,7 +185,7 @@ describe('Endowment utils', () => {

it('teardown can be called multiple times', async () => {
const { endowments, teardown } = createEndowments(
mockSnapAPI as any,
mockSnapsAPI as any,
mockEthereum as any,
['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'],
);
Expand Down
Expand Up @@ -43,13 +43,13 @@ const endowmentFactories = [timeout, interval, network, crypto, math].reduce(
* such attenuated / modified endowments. Otherwise, the value that's on the
* root realm global will be used.
*
* @param snap - The Snap's API object.
* @param snaps - The Snaps global API object.
* @param ethereum - The Snap's EIP-1193 provider object.
* @param endowments - The list of endowments to provide to the snap.
* @returns An object containing the Snap's endowments.
*/
export function createEndowments(
snap: SnapsGlobalObject,
snaps: SnapsGlobalObject,
ethereum: StreamProvider,
endowments: string[] = [],
): { endowments: Record<string, unknown>; teardown: () => Promise<void> } {
Expand Down Expand Up @@ -101,7 +101,7 @@ export function createEndowments(
return { allEndowments, teardowns };
},
{
allEndowments: { snap } as Record<string, unknown>,
allEndowments: { snaps } as Record<string, unknown>,
teardowns: [] as (() => void)[],
},
);
Expand Down

0 comments on commit 443444c

Please sign in to comment.