Skip to content

Commit

Permalink
Update LavaMoat policies and snap shasums
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Nov 29, 2023
1 parent 50df144 commit 5c32270
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 38 deletions.
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": "8uciDR11gmEm8QDs/sHYPeICmkpHOE4qVe+7zF0O7Ew=",
"shasum": "+aU0ZDyw/q4LLCDTrRWiVJAJBaRhZo4/nQY96bIwesw=",
"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": "7WXzhl1UIxj91n9Ptv/Rf+nbsYUg3XSOP14sSwEcI2k=",
"shasum": "PPXEiZMIbN808I+0bFiweqfGZ4v2QOE8HkMT1Kk6q2k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/cronjobs/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": "CxqAZ3b6NadkqDJJDZnKKhXS8ecdcV+qIf/dOmMHzbk=",
"shasum": "/fp1treUsx5dvYmsLPZidPiM1KAj86lY7akAKo3QdeM=",
"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": "BSNPuKt7cC0lEJaE8M6Y0/7tepS8HiC/+WpjZaOYwYM=",
"shasum": "YUV1vUoHg5JRPW7ZAEVyluDIuJTU/Yl23VwkVqXxKu0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/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.git"
},
"source": {
"shasum": "FL7kREmDDwiAIHskdA1lbCYQ4NHv8p4UHR/1Tm2mKKA=",
"shasum": "33NadYhy7vGLIdXWTzypXzP8wwbjrRhl2+CZcnnLjdE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-entropy/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": "FzwUtEms+bZSRoUP/48CxTVoSkoQCLG+86oWSUZfueA=",
"shasum": "6ieqFDjg0xEweuwpmz3/PKeO10dUNjrvIEimgUpNBEA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/home-page/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": "utXHOukgm1RlUl/sYsI2aNAgeVgEKL+310wUjEAMT9E=",
"shasum": "5K7IS6utL+vB7Z8N/+ZSi3k3ZE3akJxUNnOxa+8DCY8=",
"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": "dbIz1yLy7sqedSP6sb8cD5DJgDloM08zJERPa/e8N78=",
"shasum": "KXvL3yM3J10tD67tHJbhBesGt9xDCgPDaobR0IKR9VM=",
"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": "Ve8xul91MklBaGJpiaLvCB/qIHIXZEj251XP+pIOOUQ=",
"shasum": "K6iuC3+SdeS+VbvtO97KnMyBrYYt+1ye8RHsNDVFztY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/manage-state/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": "ClCqeb7sknLcct1+EMSjDpNqockG+NVmKnBmfXRXPpM=",
"shasum": "4BePPBbW32wn3vGDlyJpKPZqraRO/yy1jozDJ/qd53Q=",
"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": "cHCctempM7XrAWGixs2nkxELAqDEezV1/2JzBcVO4t0=",
"shasum": "iUVUwEFYztKjMAU71q0F6FA7L6Znhal5KXKL5/hPIXc=",
"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": "AVioFJ3NkshQl4HQhihpu+w+uq5I5QFn8uXZjgfFp8I=",
"shasum": "7X53hzeLexyHD7Xwnn3RS8kqHHw4/KKOUYolc26eQPE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,20 @@
"external:../snaps-sdk/src/internals/index.ts": true
}
},
"external:../snaps-sdk/src/images.ts": {
"globals": {
"fetch": true
},
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/ui/index.ts": true
}
},
"external:../snaps-sdk/src/index.ts": {
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/errors.ts": true,
"external:../snaps-sdk/src/images.ts": true,
"external:../snaps-sdk/src/internals/index.ts": true,
"external:../snaps-sdk/src/types/index.ts": true,
"external:../snaps-sdk/src/ui/index.ts": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,20 @@
"external:../snaps-sdk/src/internals/index.ts": true
}
},
"external:../snaps-sdk/src/images.ts": {
"globals": {
"fetch": true
},
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/ui/index.ts": true
}
},
"external:../snaps-sdk/src/index.ts": {
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/errors.ts": true,
"external:../snaps-sdk/src/images.ts": true,
"external:../snaps-sdk/src/internals/index.ts": true,
"external:../snaps-sdk/src/types/index.ts": true,
"external:../snaps-sdk/src/ui/index.ts": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,20 @@
"external:../snaps-sdk/src/internals/index.ts": true
}
},
"external:../snaps-sdk/src/images.ts": {
"globals": {
"fetch": true
},
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/ui/index.ts": true
}
},
"external:../snaps-sdk/src/index.ts": {
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/errors.ts": true,
"external:../snaps-sdk/src/images.ts": true,
"external:../snaps-sdk/src/internals/index.ts": true,
"external:../snaps-sdk/src/types/index.ts": true,
"external:../snaps-sdk/src/ui/index.ts": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@
"external:../snaps-sdk/src/internals/index.ts": true
}
},
"external:../snaps-sdk/src/images.ts": {
"globals": {
"fetch": true
},
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/ui/index.ts": true
}
},
"external:../snaps-sdk/src/index.ts": {
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/errors.ts": true,
"external:../snaps-sdk/src/images.ts": true,
"external:../snaps-sdk/src/internals/index.ts": true,
"external:../snaps-sdk/src/types/index.ts": true,
"external:../snaps-sdk/src/ui/index.ts": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,20 @@
"external:../snaps-sdk/src/internals/index.ts": true
}
},
"external:../snaps-sdk/src/images.ts": {
"globals": {
"fetch": true
},
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/ui/index.ts": true
}
},
"external:../snaps-sdk/src/index.ts": {
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/errors.ts": true,
"external:../snaps-sdk/src/images.ts": true,
"external:../snaps-sdk/src/internals/index.ts": true,
"external:../snaps-sdk/src/types/index.ts": true,
"external:../snaps-sdk/src/ui/index.ts": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@
"external:../snaps-sdk/src/internals/index.ts": true
}
},
"external:../snaps-sdk/src/images.ts": {
"globals": {
"fetch": true
},
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/ui/index.ts": true
}
},
"external:../snaps-sdk/src/index.ts": {
"packages": {
"@metamask/utils": true,
"external:../snaps-sdk/src/errors.ts": true,
"external:../snaps-sdk/src/images.ts": true,
"external:../snaps-sdk/src/internals/index.ts": true,
"external:../snaps-sdk/src/types/index.ts": true,
"external:../snaps-sdk/src/ui/index.ts": true
Expand Down
29 changes: 23 additions & 6 deletions packages/snaps-sdk/src/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,23 @@ describe('getImageData', () => {
'Failed to fetch image data from "https://example.com/image.gif": 404 Not Found',
);
});

it('throws if the Snap does not have the "endowment:network-access" permission', async () => {
const originalFetch = globalThis.fetch;

// @ts-expect-error - `fetch` is not optional.
globalThis.fetch = undefined;

await expect(getImageData('https://example.com/image.png')).rejects.toThrow(
'Failed to fetch image data from "https://example.com/image.png": Using this function requires the "endowment:network-access" permission.',
);

// eslint-disable-next-line require-atomic-updates
globalThis.fetch = originalFetch;
});
});

describe('getImage', () => {
describe('getImageComponent', () => {
beforeEach(() => {
fetchMock.resetMocks();
});
Expand All @@ -71,16 +85,20 @@ describe('getImage', () => {
},
});

const result = await getImageComponent('https://example.com/image.png');
const result = await getImageComponent('https://example.com/image.png', {
width: 100,
height: 100,
});

expect(result).toMatchInlineSnapshot(`
{
"type": "image",
"value": "<svg xmlns="http://www.w3.org/2000/svg"><image href="data:image/png;base64,aW1hZ2UgZGF0YQ==" /></svg>",
"value": "<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><image width="100" height="100" href="data:image/png;base64,aW1hZ2UgZGF0YQ==" /></svg>",
}
`);
});

it('returns the image data as an image component with width and height', async () => {
it('returns the image data as an image component with only a width', async () => {
fetchMock.mockResponse('image data', {
headers: {
'Content-Type': 'image/png',
Expand All @@ -89,13 +107,12 @@ describe('getImage', () => {

const result = await getImageComponent('https://example.com/image.png', {
width: 100,
height: 100,
});

expect(result).toMatchInlineSnapshot(`
{
"type": "image",
"value": "<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><image href="data:image/png;base64,aW1hZ2UgZGF0YQ==" /></svg>",
"value": "<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><image width="100" height="100" href="data:image/png;base64,aW1hZ2UgZGF0YQ==" /></svg>",
}
`);
});
Expand Down
34 changes: 14 additions & 20 deletions packages/snaps-sdk/src/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { image } from './ui';
* @returns A promise that resolves to the image data as a blob.
*/
async function getRawImageData(url: string, options?: RequestInit) {
if (typeof fetch !== 'function') {
throw new Error(
`Failed to fetch image data from "${url}": Using this function requires the "endowment:network-access" permission.`,
);
}

return fetch(url, options).then(async (response) => {
if (!response.ok) {
throw new Error(
Expand Down Expand Up @@ -60,15 +66,14 @@ export async function getImageData(url: string, options?: RequestInit) {
/**
* Options for getting an SVG image element from a URL.
*
* @property width - The width of the image. If this is not provided, the image
* will be rendered at its original width.
* @property width - The width of the image.
* @property height - The height of the image. If this is not provided, the
* image will be rendered at its original height.
* width will be used as the height.
* @property request - The options to use when fetching the image data. This is
* passed directly to `fetch`.
*/
export type ImageOptions = {
width?: number;
width: number;
height?: number;
request?: RequestInit;
};
Expand All @@ -94,32 +99,21 @@ export type ImageOptions = {
* });
* @param url - The URL to get the image data from.
* @param options - The options to use when fetching and rendering the image.
* @param options.width - The width of the image. If this is not provided, the
* image will be rendered at its original width.
* @param options.width - The width of the image.
* @param options.height - The height of the image. If this is not provided, the
* image will be rendered at its original height.
* width will be used as the height.
* @param options.request - The options to use when fetching the image data.
* This is passed directly to `fetch`.
* @returns A promise that resolves to the image data as an image component.
*/
export async function getImageComponent(
url: string,
{ width, height, request }: ImageOptions = {},
{ width, height = width, request }: ImageOptions,
) {
const imageData = await getImageData(url, request);

let size = '';
if (width) {
assert(width > 0, 'Expected width to be greater than 0.');
size += `width="${width}" `;
}

if (height) {
assert(height > 0, 'Expected height to be greater than 0.');
size += `height="${height}"`;
}
const size = `width="${width}" height="${height}"`;

return image(
`<svg ${size.trim()} xmlns="http://www.w3.org/2000/svg"><image href="${imageData}" /></svg>`,
`<svg ${size.trim()} xmlns="http://www.w3.org/2000/svg"><image ${size.trim()} href="${imageData}" /></svg>`,
);
}

0 comments on commit 5c32270

Please sign in to comment.