Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
fix: flask 10.17 snap breaking change fix (#206)
Browse files Browse the repository at this point in the history
* fix: flask 10.17 snap breaking change fix

* lint onRpcRequest fix

* manifest
  • Loading branch information
irubido authored Jul 21, 2022
1 parent ef53869 commit 5efed8a
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 253 deletions.
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"homepage": "https://github.com/chainsafe/filecoin-metamask-snap#readme",
"devDependencies": {
"@chainsafe/filsnap-types": "workspace:^",
"@metamask/snaps-cli": "^0.11.1",
"@metamask/snaps-cli": "^0.18.1",
"@types/bn.js": "^4.11.6",
"@types/chai": "^4.2.10",
"@types/mocha": "^9.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/Chainsafe/filsnap.git"
},
"source": {
"shasum": "yOfdreCEtwMdTmupfP+w9RyPdcOj6q1KgMfSwwd9jTg=",
"shasum": "Gh5Rkh+Tbw4uZDmH94HyCDl8difNBzji6B0/DEd3Mfg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
93 changes: 92 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,92 @@
import "./snap";
import { EmptyMetamaskState, Wallet } from "./interfaces";
import { getAddress } from "./rpc/getAddress";
import { exportPrivateKey } from "./rpc/exportPrivateKey";
import { getPublicKey } from "./rpc/getPublicKey";
import { getApi } from "./filecoin/api";
import { LotusRpcApi } from "./filecoin/types";
import { getBalance } from "./rpc/getBalance";
import { configure } from "./rpc/configure";
import { getMessages } from "./rpc/getMessages";
import { signMessage, signMessageRaw } from "./rpc/signMessage";
import { sendMessage } from "./rpc/sendMessage";
import { estimateMessageGas } from "./rpc/estimateMessageGas";

declare let wallet: Wallet;

const apiDependentMethods = [
"fil_getBalance",
"fil_signMessage",
"fil_sendMessage",
"fil_getGasForMessage",
"fil_configure",
];
// eslint-disable-next-line
module.exports.onRpcRequest = (async ({ origin, request }: { origin: string, request: any }) => {

const state = await wallet.request({
method: "snap_manageState",
params: ["get"],
});

if (!state) {
// initialize state if empty and set default config
await wallet.request({
method: "snap_manageState",
params: ["update", EmptyMetamaskState()],
});
}

let api: LotusRpcApi;
// initialize lotus RPC api if needed
// eslint-disable-next-line
if (apiDependentMethods.indexOf(request.method) >= 0) {
api = await getApi(wallet);
}
// eslint-disable-next-line
switch (request.method) {
case "fil_configure": {
const resp = await configure(
wallet,
// eslint-disable-next-line
request.params.configuration.network,
// eslint-disable-next-line
request.params.configuration
);
api = resp.api;
return resp.snapConfig;
}
case "fil_getAddress":
return await getAddress(wallet);
case "fil_getPublicKey":
return await getPublicKey(wallet);
case "fil_exportPrivateKey":
return exportPrivateKey(wallet);
case "fil_getBalance": {
const balance = await getBalance(wallet, api);
return balance;
}
case "fil_getMessages":
return getMessages(wallet);
case "fil_signMessage":
// eslint-disable-next-line
return await signMessage(wallet, api, request.params.message);
case "fil_signMessageRaw":
// eslint-disable-next-line
return await signMessageRaw(wallet, request.params.message);
case "fil_sendMessage":
// eslint-disable-next-line
return await sendMessage(wallet, api, request.params.signedMessage);
case "fil_getGasForMessage":
return await estimateMessageGas(
wallet,
api,
// eslint-disable-next-line
request.params.message,
// eslint-disable-next-line
request.params.maxFee
);
default:
throw new Error("Unsupported RPC method");
}
// eslint-disable-next-line
});
1 change: 0 additions & 1 deletion packages/snap/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ export const EmptyMetamaskState: () => MetamaskState = () => ({
});

export interface Wallet {
registerRpcMessageHandler: (fn: FMethodCallback) => unknown;
request(options: { method: string; params?: unknown[] }): unknown;
}
82 changes: 0 additions & 82 deletions packages/snap/src/snap.ts

This file was deleted.

Loading

0 comments on commit 5efed8a

Please sign in to comment.