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

Use error wrappers in all examples #2178

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ jobs:
- run: yarn --immutable --immutable-cache
- name: Install Google Chrome
run: yarn install-chrome
- run: yarn workspace @metamask/snaps-sdk run build
if: ${{ matrix.package-name == '@metamask/snaps-cli' }}
- run: yarn workspace ${{ matrix.package-name }} run test:ci
- name: Get coverage folder
id: get-coverage-folder
Expand Down Expand Up @@ -316,4 +318,5 @@ jobs:
node-version-file: '.nvmrc'
cache: yarn
- run: yarn --immutable
- run: yarn workspace @metamask/snaps-sdk run build
- run: yarn workspace @metamask/snaps-cli run test
1 change: 0 additions & 1 deletion packages/examples/packages/bip32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
},
"dependencies": {
"@metamask/key-tree": "^9.0.0",
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^",
"@metamask/utils": "^8.3.0",
"@noble/ed25519": "^1.6.0",
Expand Down
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": "6g87K+NnEehFnghqk0s8wWv6NnGkeaAAOq+eemGJndQ=",
"shasum": "8ab4U4VgNt1CNwLUmnANXyySVWy6MST2ywkXbK1/q54=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
17 changes: 7 additions & 10 deletions packages/examples/packages/bip32/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import {
DialogType,
panel,
text,
heading,
copyable,
InvalidParamsError,
UserRejectedRequestError,
MethodNotFoundError,
} from '@metamask/snaps-sdk';
import {
add0x,
Expand Down Expand Up @@ -46,9 +48,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
const { message, curve, ...params } = request.params as SignMessageParams;

if (!message || typeof message !== 'string') {
throw rpcErrors.invalidParams({
message: `Invalid signature data: "${message}".`,
});
throw new InvalidParamsError(`Invalid signature data: "${message}".`);
}

const node = await getPrivateNode({ ...params, curve });
Expand All @@ -71,7 +71,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});

if (!approved) {
throw providerErrors.userRejectedRequest();
throw new UserRejectedRequestError();
}

if (curve === 'ed25519') {
Expand All @@ -95,10 +95,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
throw new Error(`Unsupported curve: ${String(curve)}.`);
}

default: {
throw rpcErrors.methodNotFound({
data: { method: request.method },
});
}
default:
throw new MethodNotFoundError({ method: request.method });
}
};
1 change: 0 additions & 1 deletion packages/examples/packages/bip44/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
},
"dependencies": {
"@metamask/key-tree": "^9.0.0",
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^",
"@metamask/utils": "^8.3.0",
"@noble/bls12-381": "^1.2.0"
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": "h9Unyy6nfIB+2Dri5Hn6fNMJEtEu6e4lmoBRk7SqGqY=",
"shasum": "hhQQ0zW2jmTR2pzXD3qr+q2WlhxzHZdjvhV7RmCCJ+k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
9 changes: 4 additions & 5 deletions packages/examples/packages/bip44/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { rpcErrors, providerErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import {
DialogType,
panel,
text,
heading,
copyable,
MethodNotFoundError,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import { bytesToHex, stringToBytes } from '@metamask/utils';
import { getPublicKey, sign } from '@noble/bls12-381';
Expand Down Expand Up @@ -58,16 +59,14 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});

if (!approved) {
throw providerErrors.userRejectedRequest();
throw new UserRejectedRequestError();
}

const newLocal = await sign(stringToBytes(message), privateKey);
return bytesToHex(newLocal);
}

default:
throw rpcErrors.methodNotFound({
data: { method: request.method },
});
throw new MethodNotFoundError({ method: request.method });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^"
},
"devDependencies": {
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": "GDfl/OJepeU/Ms3q5ojSU1lRIu7mj01gV1uC2lctYG0=",
"shasum": "jsLU4LfTnxcd6HxBnO0We83mboe2DGUVugwjyjMr/S4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/examples/packages/browserify-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import {
MethodNotFoundError,
type OnRpcRequestHandler,
} from '@metamask/snaps-sdk';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand All @@ -21,9 +23,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return 'Hello from Browserify!';

default: {
throw rpcErrors.methodNotFound({
data: { method: request.method },
});
throw new MethodNotFoundError({ method: request.method });
}
}
};
1 change: 0 additions & 1 deletion packages/examples/packages/browserify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/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.git"
},
"source": {
"shasum": "jjkP8lDWArcnVeNB1YyZuFf9JSLlGvbmsOtDWVMcURc=",
"shasum": "YdcJWyqICGZig9C76bmjFzXj228WsHpfyQbpKFstk18=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/examples/packages/browserify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import {
MethodNotFoundError,
type OnRpcRequestHandler,
} from '@metamask/snaps-sdk';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand All @@ -23,9 +25,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return 'Hello from the MetaMask Snaps CLI using Browserify!';

default: {
throw rpcErrors.methodNotFound({
data: { method: request.method },
});
throw new MethodNotFoundError({ method: request.method });
}
}
};
1 change: 0 additions & 1 deletion packages/examples/packages/client-status/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^"
},
"devDependencies": {
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": "vBpEZ72bofDjez8+bFPLKZu/bFDZD+2qp1M4awO+AsA=",
"shasum": "RdYraLfrwCrccsEbDUR9J7T8r+8+jAF9NtShsPkpqIY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
12 changes: 5 additions & 7 deletions packages/examples/packages/client-status/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import {
MethodNotFoundError,
type OnRpcRequestHandler,
} from '@metamask/snaps-sdk';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand All @@ -21,10 +23,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
}

default:
throw rpcErrors.methodNotFound({
data: {
method: request.method,
},
});
throw new MethodNotFoundError({ method: request.method });
}
};
1 change: 0 additions & 1 deletion packages/examples/packages/cronjobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^"
},
"devDependencies": {
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": "hXFQrGOJ+ScAdetboaVpVTv9jHS9ZbeGTSF6jQ8P8yM=",
"shasum": "cGr5IrUQn88gt4Et3hoG5p0hlcR1UwfsA3n6TJGh7kE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
9 changes: 2 additions & 7 deletions packages/examples/packages/cronjobs/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 { OnCronjobHandler } from '@metamask/snaps-sdk';
import { panel, text, heading } from '@metamask/snaps-sdk';
import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk';

/**
* Handle cronjob execution requests from MetaMask. This handler handles one
Expand Down Expand Up @@ -31,10 +30,6 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
},
});
default:
throw rpcErrors.methodNotFound({
data: {
method: request.method,
},
});
throw new MethodNotFoundError({ method: request.method });
}
};
1 change: 0 additions & 1 deletion packages/examples/packages/dialogs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^"
},
"devDependencies": {
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": "N2zhh1oUTEL5T4B+XYJz46f5ip9YEa9vTVnteSz8CC4=",
"shasum": "0EWOdxj1/tE41ruHFjRCOvD5GNYROKzwlrjya39HVVw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
15 changes: 8 additions & 7 deletions packages/examples/packages/dialogs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import { DialogType, panel, text, heading } from '@metamask/snaps-sdk';
import {
DialogType,
panel,
text,
heading,
MethodNotFoundError,
} from '@metamask/snaps-sdk';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand Down Expand Up @@ -71,10 +76,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});

default:
throw rpcErrors.methodNotFound({
data: {
method: request.method,
},
});
throw new MethodNotFoundError({ method: request.method });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^",
"@metamask/utils": "^8.3.0"
},
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": "7g/LtDd1foBy/f1eGIxdFI5jSzGidjTXewUiuvrlFdA=",
"shasum": "lC5/FHnxWiHbzFNBHxyDYyo0fAn/SfZhh7g8m0BpoeM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
12 changes: 5 additions & 7 deletions packages/examples/packages/ethereum-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import {
MethodNotFoundError,
type OnRpcRequestHandler,
} from '@metamask/snaps-sdk';
import type { Hex } from '@metamask/utils';
import { assert, stringToBytes, bytesToHex } from '@metamask/utils';

Expand Down Expand Up @@ -121,10 +123,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
}

default:
throw rpcErrors.methodNotFound({
data: {
method: request.method,
},
});
throw new MethodNotFoundError({ method: request.method });
}
};
1 change: 0 additions & 1 deletion packages/examples/packages/ethers-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^6.2.1",
"@metamask/snaps-sdk": "workspace:^",
"ethers": "^6.3.0"
},
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": "D/6w/eOgde9NmCfVoCnwLGDZSihAK7UNMHoj7llQKSU=",
"shasum": "5kmKjco5B1kA802FWjzh+W+h5f6nE/vDfVWZhd2e17U=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Loading
Loading