Skip to content

Commit

Permalink
Support utf8 encoding for snap_getFile (#1858)
Browse files Browse the repository at this point in the history
Support `utf8` encoding for `snap_getFile`. This is useful for loading
JSON or other text files.

---------

Co-authored-by: Maarten Zuidhoorn <[email protected]>
  • Loading branch information
FrederikBolding and Mrtenz authored Oct 17, 2023
1 parent 7ba2ae5 commit 3882d29
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 21 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": "EdwnuHKiqsIz4f+l6nKY0wZaq8bni4a5YEs4p6375/c=",
"shasum": "AevK0LdcWtG890SssMLHsK8vI1NFlaZqjLrHiOAinX8=",
"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": "MVUJZIRIG1QLd6ZhFftNn5pFGTzvnd2D39K9UABBfhU=",
"shasum": "13EWl1zMYnrGxMsJEK0JBvcUL2d8FoKlrB/9ugIg9g8=",
"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": "co2jtMEwsAvgoeQJjq51RbqWbFramRLf/eVToZvetEo=",
"shasum": "OGGoO4sXe19namsdmALoI5D5Gh9YgWtfZf4XujuggKQ=",
"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": "7t1AQR2GJYVsKreUSkmEDUx2xHZNHkyHZ6IKexPyhWA=",
"shasum": "a5CI0z65D1RZM88OQ+5xOVCZ7RFjhr6pG6DgwBooD0Q=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions packages/examples/packages/get-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ JSON-RPC methods:

- `getFile`: Returns a static JSON file.
- `getFileInBase64`: Returns the same static JSON file in Base64.
- `getFileInHex`: Returns the same static JSON file in hexadecimal.

For more information, you can refer to
[the end-to-end tests](./src/index.test.ts).
3 changes: 1 addition & 2 deletions packages/examples/packages/get-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/snaps-types": "workspace:^",
"@metamask/utils": "^8.1.0"
"@metamask/snaps-types": "workspace:^"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-file/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": "IsVfJYwK/RvI9RZKZyuhYfNCE+eVG2rL6/Hyjjt9hJw=",
"shasum": "KeR2SaLL7hPmuTOW4n1Tz0VhgMEYqUTmjIMJHjdxBC8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
16 changes: 16 additions & 0 deletions packages/examples/packages/get-file/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,20 @@ describe('onRpcRequest', () => {
await close();
});
});

describe('getFileInHex', () => {
it('returns the file in hex', async () => {
const { request, close } = await installSnap();

const response = request({
method: 'getFileInHex',
});

expect(await response).toRespondWith(
'0x7b0a202022666f6f223a2022626172220a7d0a',
);

await close();
});
});
});
17 changes: 12 additions & 5 deletions packages/examples/packages/get-file/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-types';
import { bytesToString, hexToBytes } from '@metamask/utils';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand All @@ -10,6 +9,8 @@ import { bytesToString, hexToBytes } from '@metamask/utils';
* `snap_getFile` JSON-RPC method to get this file and parses it before returning.
* - `getFileInBase64`: Returns a statically defined JSON file in Base64.
* This uses the `snap_getFile` JSON-RPC method to get this file returns it directly.
* - `getFileInHex`: Returns a statically defined JSON file in hexadecimal.
* This uses the `snap_getFile` JSON-RPC method to get this file returns it directly.
*
* @param params - The request parameters.
* @param params.request - The JSON-RPC request object.
Expand All @@ -21,12 +22,11 @@ import { bytesToString, hexToBytes } from '@metamask/utils';
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
case 'getFile': {
const fileInHexadecimal = await snap.request({
const fileInPlaintext = await snap.request({
method: 'snap_getFile',
params: { path: './src/foo.json', encoding: 'hex' },
params: { path: './src/foo.json', encoding: 'utf8' },
});
const bytes = hexToBytes(fileInHexadecimal);
return JSON.parse(bytesToString(bytes));
return JSON.parse(fileInPlaintext);
}

case 'getFileInBase64': {
Expand All @@ -37,6 +37,13 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});
}

case 'getFileInHex': {
return await snap.request({
method: 'snap_getFile',
params: { path: './src/foo.json', encoding: 'hex' },
});
}

default:
throw rpcErrors.methodNotFound({
data: { method: request.method },
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-locale/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": "jKYiuKBqNaeeWsUi/TQp6OBh6UveiWiBb3NrL78L4V8=",
"shasum": "WHjV/BMEqShmPsz94ITTRZKqC80X1Iergou1FxRsBQI=",
"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": "dDhGT/5cpuAv+fsSIinTSg0vo+r1l4Xb4WfypWEjyyM=",
"shasum": "ruYznsJRKMvZ2+hquwW9rvBqfVP8jPi0LbsDaeZFwaw=",
"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": "TO6zLIG+k6OIa6iJ951A0R4kmQ28kT4A67Jaa87ElLY=",
"shasum": "jWOnAWbwW9vVmugnyT3Xax2tmkBR6MMOAuf0Gb4EVq4=",
"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": "1Hf1QF/WH3S6kjX7B0VDO0D8sj8WqcfvsK7NoeNrrFc=",
"shasum": "iI/GkZp/Ji9godApV9NAXyRzffAy9dDCr++2Ry3xrLk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions packages/rpc-methods/src/permitted/getFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const GetFileArgsStruct = object({
union([
enumValue(AuxiliaryFileEncoding.Base64),
enumValue(AuxiliaryFileEncoding.Hex),
enumValue(AuxiliaryFileEncoding.Utf8),
]),
),
});
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 95.5,
"branches": 95.52,
"functions": 100,
"lines": 98.72,
"statements": 95.81
"statements": 95.82
}
6 changes: 6 additions & 0 deletions packages/snaps-utils/src/auxiliary-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ describe('encodeAuxiliaryFile', () => {
bytesToHex(bytes),
);
});

it('returns plaintext when requested', () => {
const bytes = stringToBytes('foo');
const value = base64.encode(bytes);
expect(encodeAuxiliaryFile(value, AuxiliaryFileEncoding.Utf8)).toBe('foo');
});
});
8 changes: 6 additions & 2 deletions packages/snaps-utils/src/auxiliary-files.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { bytesToHex } from '@metamask/utils';
import { bytesToHex, bytesToString } from '@metamask/utils';
import { base64 } from '@scure/base';

export enum AuxiliaryFileEncoding {
Base64 = 'base64',
Hex = 'hex',
Utf8 = 'utf8',
}

/**
Expand All @@ -23,7 +24,10 @@ export function encodeAuxiliaryFile(
}

// TODO: Use @metamask/utils for this
// For now, the requested encoding here will always be hex.
const decoded = base64.decode(value);
if (encoding === AuxiliaryFileEncoding.Utf8) {
return bytesToString(decoded);
}

return bytesToHex(decoded);
}
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4444,7 +4444,6 @@ __metadata:
"@metamask/snaps-cli": "workspace:^"
"@metamask/snaps-jest": "workspace:^"
"@metamask/snaps-types": "workspace:^"
"@metamask/utils": ^8.1.0
"@swc/core": 1.3.78
"@swc/jest": ^0.2.26
"@typescript-eslint/eslint-plugin": ^5.42.1
Expand Down

0 comments on commit 3882d29

Please sign in to comment.