Skip to content

Commit

Permalink
Validate snap parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolad committed Dec 5, 2023
1 parent ecc22a3 commit 8ffee46
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
6 changes: 5 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
// the return is a plain hex string
// https://docs.metamask.io/snaps/reference/rpc-api/#returns-5
case 'getPublicKey': {

Check failure on line 32 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎······const·{·path,·compressed·}·=·request.params·as·GetBip32PublicKeyParams;⏎` with `const·{·path,·compressed·}·=·request.params·as·GetBip32PublicKeyParams;`
const { path, compressed } = request.params as GetBip32PublicKeyParams;


// eslint-disable-next-line @typescript-eslint/await-thenable
const approved = await snap.request({
method: 'snap_dialog',
Expand All @@ -44,6 +46,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
]),
},
});

Check failure on line 49 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `······⏎`

if (!approved) {
throw providerErrors.userRejectedRequest();
Expand All @@ -63,6 +66,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
case 'signTransaction': {
const { transaction, path } = request.params as SignTransactionParams;

Check failure on line 68 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎······`

try {
const call = wasm.serializeCall(transaction.message, transaction.nonce);
const entropy = await snap.request({
Expand All @@ -78,6 +82,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
const node = await SLIP10Node.fromJSON(entropy);
assert(node.privateKey);

Check failure on line 83 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`


// eslint-disable-next-line @typescript-eslint/await-thenable
const approved = await snap.request({
method: 'snap_dialog',
Expand Down Expand Up @@ -113,7 +118,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
const txHex = bytesToHex(tx);

wasm.dealloc();

return txHex;
} catch (er) {
wasm.dealloc();
Expand Down
79 changes: 44 additions & 35 deletions packages/snap/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
import { Bytes } from '@metamask/utils';

Check failure on line 1 in packages/snap/src/types.ts

View workflow job for this annotation

GitHub Actions / Lint

'Bytes' is defined but never used
import { Bip32PathStruct } from '@metamask/snaps-utils/*';

Check failure on line 2 in packages/snap/src/types.ts

View workflow job for this annotation

GitHub Actions / Lint

`@metamask/snaps-utils/*` import should occur before import of `@metamask/utils`
import { Infer, boolean, enums, object, optional, type, string, number, array } from 'superstruct';

Check failure on line 3 in packages/snap/src/types.ts

View workflow job for this annotation

GitHub Actions / Lint

'superstruct' should be listed in the project's dependencies. Run 'npm i -S superstruct' to add it

Check failure on line 3 in packages/snap/src/types.ts

View workflow job for this annotation

GitHub Actions / Lint

Import "Infer" is only used as types

Check failure on line 3 in packages/snap/src/types.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `·Infer,·boolean,·enums,·object,·optional,·type,·string,·number,·array·}·from·'superstruct';⏎` with `⏎··Infer,⏎··boolean,⏎··enums,⏎··object,⏎··optional,⏎··type,⏎··string,⏎··number,⏎··array,⏎}·from·'superstruct';`

Check failure on line 3 in packages/snap/src/types.ts

View workflow job for this annotation

GitHub Actions / Lint

'enums' is defined but never used


/**
* The parameters for calling the `getPublicKey` JSON-RPC method.
*
* Note: For simplicity, these are not validated by the snap. In production, you
* should validate that the request object matches this type before using it.
* `type` is used instead of `object` to allow unknown properties.
*/
export type GetBip32PublicKeyParams = {
export const GetBip32PublicKeyParamsStruct = type({
/**
* The BIP-32 path to the account.
*/
path: ['m', ...(`${number}` | `${number}'`)[]];

path: Bip32PathStruct,
/**
* Whether to return the public key in compressed form.
*/
compressed?: boolean | undefined;
compressed: optional(boolean()),
});

/**
* Miscellaneous parameters, which are passed to `snap_getBip32PublicKey`.
*/
[key: string]: unknown;
};

/**
* The transaction object to be submitted by the UI so the signature can be generated.
* The parameters for calling the `getPublicKey` JSON-RPC method.
*
* Note: For simplicity, these are not validated by the snap. In production, you
* should validate that the request object matches this type before using it.
* Unknown properties are ignored and passed to `snap_getBip32PublicKey`.
*/
export type Transaction = {
/**
* The JSON transaction to sign.
*/
message: string;
export type GetBip32PublicKeyParams = Infer<
typeof GetBip32PublicKeyParamsStruct
>;

/**
* The nonce for the transaction signature.

/**
* The transaction object to be submitted by the UI so the signature can be generated.
*
* Note: For simplicity, these are not validated by the snap. In production, you
* should validate that the request object matches this type before using it.
*/
export const TransactionStruct = object({
/**
* The JSON transaction to sign.
*/
nonce: number;
};
message: string(),
/**
* The nonce for the transaction signature.
*/
nonce: number(),
})

/**
* The parameters for calling the `signTransaction` JSON-RPC method.
*
* Note: For simplicity, these are not validated by the snap. In production, you
* should validate that the request object matches this type before using it.
*/
export type SignTransactionParams = {
/**
*
* Note: For simplicity, these are not validated by the snap. In production, you
* should validate that the request object matches this type before using it.
*/
export const SignTransactionStruct = object({
/**
* The JSON transaction to sign.
*/
transaction: Transaction;
transaction: TransactionStruct,

/**
/**
* The BIP-32 path to the account.
*/
path: string[];
};
path: array(string()),
})

export type SignTransactionParams = Infer<typeof SignTransactionStruct>;


/**
* The expected WASM interface from the imported module.
Expand Down

0 comments on commit 8ffee46

Please sign in to comment.