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

Commit

Permalink
fix: flask1.25 (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
irubido authored Mar 3, 2023
1 parent e8dadc9 commit 155e857
Show file tree
Hide file tree
Showing 32 changed files with 1,205 additions and 997 deletions.
12 changes: 4 additions & 8 deletions packages/adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ export async function enableFilecoinSnap(
if (!isInstalled) {
// // enable snap
await window.ethereum.request({
method: "wallet_enable",
params: [
{
[`wallet_snap_${snapId}`]: {
...snapInstallationParams,
},
},
],
method: "wallet_requestSnaps",
params: {
[snapId]: { ...snapInstallationParams },
},
});
}

Expand Down
7 changes: 5 additions & 2 deletions packages/adapter/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ async function sendSnapMethod<T>(
snapId: string
): Promise<T> {
return await window.ethereum.request({
method: snapId,
params: [request],
method: "wallet_invokeSnap",
params: {
request,
snapId,
},
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class MetamaskFilecoinSnap {

public constructor(snapOrigin: string) {
this.snapOrigin = snapOrigin;
this.snapId = `wallet_snap_${this.snapOrigin}`;
this.snapId = this.snapOrigin;
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare global {
isUnlocked: Promise<boolean>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
request: <T>(
request: SnapRpcMethodRequest | { method: string; params?: any[] }
request: SnapRpcMethodRequest | { method: string; params?: any }
) => Promise<T>;
on: (eventName: unknown, callback: unknown) => unknown;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/example/src/services/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare global {
ethereum: {
isMetaMask: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
send: <T>(request: SnapRpcMethodRequest | {method: string; params?: any[]}) => Promise<T>;
send: <T>(request: SnapRpcMethodRequest | {method: string; params?: any}) => Promise<T>;
on: (eventName: unknown, callback: unknown) => unknown;
// requestIndex: () => Promise<{getSnapApi: (origin: string) => Promise<FilecoinApi>}>;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
"@filecoin-shipyard/lotus-client-rpc": "^0.2.0",
"@filecoin-shipyard/lotus-client-schema": "^0.0.14",
"@glif/filecoin-number": "^2.0.0-beta.0",
"@metamask/key-tree": "^4.0.0",
"@metamask/snap-types": "^0.18.1",
"@metamask/key-tree": "^6.0.0",
"@metamask/snaps-types": "^0.26.2",
"@zondax/filecoin-signing-tools": "^0.20.0",
"bn.js": "^5.1.2",
"deepmerge": "^4.2.2"
Expand Down
6 changes: 5 additions & 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": "5nNXg78JtceAEoasI2SC65qcbnDBXtR/cAR4IfHIYYw=",
"shasum": "8Vx7jaoJt4mO71SjaNMmmlINtDrBOS6mVxskBxMw4sg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -18,6 +18,10 @@
},
"initialPermissions": {
"endowment:network-access": {},
"endowment:rpc": {
"dapps": true,
"snaps": true
},
"snap_confirm": {},
"snap_getBip44Entropy": [
{
Expand Down
8 changes: 4 additions & 4 deletions packages/snap/src/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SnapConfig } from "@chainsafe/filsnap-types";
import { SnapProvider } from "@metamask/snap-types";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import { MetamaskState } from "../interfaces";
import {
defaultConfiguration,
Expand All @@ -21,11 +21,11 @@ export function getDefaultConfiguration(networkName?: string): SnapConfig {
}

export async function getConfiguration(
wallet: SnapProvider
snap: SnapsGlobalObject
): Promise<SnapConfig> {
const state = (await wallet.request({
const state = (await snap.request({
method: "snap_manageState",
params: ["get"],
params: { operation: "get" },
})) as MetamaskState;
if (!state || !state.filecoin.config) {
return defaultConfiguration;
Expand Down
38 changes: 16 additions & 22 deletions packages/snap/src/filecoin/account.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
import { Buffer } from "buffer";
import { keyRecover } from "@zondax/filecoin-signing-tools/js";
import { KeyPair } from "@chainsafe/filsnap-types";
import {
getBIP44AddressKeyDeriver,
JsonBIP44CoinTypeNode,
} from "@metamask/key-tree";
import { SnapProvider } from "@metamask/snap-types";
import { getMetamaskVersion, isNewerVersion } from "../util/version";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import { MetamaskState } from "../interfaces";

/**
* Return derived KeyPair from seed.
* @param wallet
* @param snap
*/
export async function getKeyPair(wallet: SnapProvider): Promise<KeyPair> {
const snapState = (await wallet.request({
export async function getKeyPair(snap: SnapsGlobalObject): Promise<KeyPair> {
const snapState = (await snap.request({
method: "snap_manageState",
params: ["get"],
params: { operation: "get" },
})) as MetamaskState;
const { derivationPath } = snapState.filecoin.config;
const [, , coinType, account, change, addressIndex] =
derivationPath.split("/");
const bip44Code = coinType.replace("'", "");
const isFilecoinMainnet = bip44Code === "461";

let bip44Node: JsonBIP44CoinTypeNode;
const currentVersion = await getMetamaskVersion(wallet);
if (isNewerVersion("MetaMask/v10.18.99-flask.0", currentVersion))
bip44Node = (await wallet.request({
method: "snap_getBip44Entropy",
params: {
coinType: Number(bip44Code),
},
})) as JsonBIP44CoinTypeNode;
else
bip44Node = (await wallet.request({
method: `snap_getBip44Entropy_${bip44Code}`,
params: [],
})) as JsonBIP44CoinTypeNode;
const bip44Node = (await snap.request({
method: "snap_getBip44Entropy",
params: {
coinType: Number(bip44Code),
},
})) as JsonBIP44CoinTypeNode;

const addressKeyDeriver = await getBIP44AddressKeyDeriver(bip44Node, {
account: parseInt(account),
change: parseInt(change),
});
const extendedPrivateKey = await addressKeyDeriver(Number(addressIndex));
const privateKey = extendedPrivateKey.privateKeyBuffer.slice(0, 32);

const extendedKey = keyRecover(privateKey, !isFilecoinMainnet);
const privateKey = extendedPrivateKey.privateKeyBytes;
const privateKeyBuffer = Buffer.from(privateKey).slice(0, 32);

const extendedKey = keyRecover(privateKeyBuffer, !isFilecoinMainnet);

return {
address: extendedKey.address,
Expand Down
6 changes: 3 additions & 3 deletions packages/snap/src/filecoin/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
} from "@filecoin-shipyard/lotus-client-provider-nodejs";
import { testnet } from "@filecoin-shipyard/lotus-client-schema";
import { SnapConfig } from "@chainsafe/filsnap-types";
import { SnapProvider } from "@metamask/snap-types";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import { getConfiguration } from "../configuration";
import { LotusRpcApi } from "./types";

export async function getApi(wallet: SnapProvider): Promise<LotusRpcApi> {
const configuration = await getConfiguration(wallet);
export async function getApi(snap: SnapsGlobalObject): Promise<LotusRpcApi> {
const configuration = await getConfiguration(snap);
return getApiFromConfig(configuration);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/snap/src/filecoin/message.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { MessageStatus } from "@chainsafe/filsnap-types";
import { SnapProvider } from "@metamask/snap-types";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import { MetamaskState } from "../interfaces";

export async function updateMessageInState(
wallet: SnapProvider,
snap: SnapsGlobalObject,
message: MessageStatus
): Promise<void> {
const state = (await wallet.request({
const state = (await snap.request({
method: "snap_manageState",
params: ["get"],
params: { operation: "get" },
})) as MetamaskState;
const index = state.filecoin.messages.findIndex(
(msg) => msg.cid === message.cid
Expand All @@ -18,8 +18,8 @@ export async function updateMessageInState(
} else {
state.filecoin.messages.push(message);
}
await wallet.request({
await snap.request({
method: "snap_manageState",
params: ["update", state],
params: { newState: state, operation: "update" },
});
}
32 changes: 16 additions & 16 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OnRpcRequestHandler } from "@metamask/snap-types";
import { OnRpcRequestHandler } from "@metamask/snaps-types";
import { EmptyMetamaskState } from "./interfaces";
import { getAddress } from "./rpc/getAddress";
import { exportPrivateKey } from "./rpc/exportPrivateKey";
Expand Down Expand Up @@ -27,66 +27,66 @@ const apiDependentMethods = [
];

export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
const state = await wallet.request({
const state = await snap.request({
method: "snap_manageState",
params: ["get"],
params: { operation: "get" },
});

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

let api: LotusRpcApi;
// initialize lotus RPC api if needed
if (apiDependentMethods.indexOf(request.method) >= 0) {
api = await getApi(wallet);
api = await getApi(snap);
}
switch (request.method) {
case "fil_configure": {
isValidConfigureRequest(request.params);
const resp = await configure(
wallet,
snap,
request.params.configuration.network,
request.params.configuration
);
api = resp.api;
return resp.snapConfig;
}
case "fil_getAddress":
return await getAddress(wallet);
return await getAddress(snap);
case "fil_getPublicKey":
return await getPublicKey(wallet);
return await getPublicKey(snap);
case "fil_exportPrivateKey":
return exportPrivateKey(wallet);
return exportPrivateKey(snap);
case "fil_getBalance": {
const balance = await getBalance(wallet, api);
const balance = await getBalance(snap, api);
return balance;
}
case "fil_getMessages":
return getMessages(wallet);
return getMessages(snap);
case "fil_signMessage":
isValidSignRequest(request.params);
return await signMessage(wallet, api, request.params.message);
return await signMessage(snap, api, request.params.message);
case "fil_signMessageRaw":
if (
"message" in request.params &&
typeof request.params.message == "string"
) {
return await signMessageRaw(wallet, request.params.message);
return await signMessageRaw(snap, request.params.message);
} else {
throw new Error("Invalid raw message signing request");
}
case "fil_sendMessage":
isValidSendRequest(request.params);
return await sendMessage(wallet, api, request.params.signedMessage);
return await sendMessage(snap, api, request.params.signedMessage);
case "fil_getGasForMessage":
isValidEstimateGasRequest(request.params);
return await estimateMessageGas(
wallet,
snap,
api,
request.params.message,
request.params.maxFee
Expand Down
12 changes: 6 additions & 6 deletions packages/snap/src/rpc/configure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SnapConfig } from "@chainsafe/filsnap-types";
import { SnapProvider } from "@metamask/snap-types";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import deepmerge from "deepmerge";
import { getDefaultConfiguration } from "../configuration";
import { getApiFromConfig } from "../filecoin/api";
Expand All @@ -12,7 +12,7 @@ export interface ConfigureResponse {
}

export async function configure(
wallet: SnapProvider,
snap: SnapsGlobalObject,
networkName: string,
overrides?: unknown
): Promise<ConfigureResponse> {
Expand Down Expand Up @@ -40,14 +40,14 @@ export async function configure(
"Mismatch between configured network and network provided by RPC"
);
}
const state = (await wallet.request({
const state = (await snap.request({
method: "snap_manageState",
params: ["get"],
params: { operation: "get" },
})) as MetamaskState;
state.filecoin.config = configuration;
await wallet.request({
await snap.request({
method: "snap_manageState",
params: ["update", state],
params: { newState: state, operation: "update" },
});
return { api: api, snapConfig: configuration };
}
6 changes: 3 additions & 3 deletions packages/snap/src/rpc/estimateMessageGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import {
MessageRequest,
} from "@chainsafe/filsnap-types";
import { FilecoinNumber } from "@glif/filecoin-number/dist";
import { SnapProvider } from "@metamask/snap-types";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import { getKeyPair } from "../filecoin/account";
import { LotusRpcApi } from "../filecoin/types";

export async function estimateMessageGas(
wallet: SnapProvider,
snap: SnapsGlobalObject,
api: LotusRpcApi,
messageRequest: MessageRequest,
maxFee?: string
): Promise<MessageGasEstimate> {
const keypair = await getKeyPair(wallet);
const keypair = await getKeyPair(snap);
const message: Message = {
...messageRequest,
from: keypair.address,
Expand Down
10 changes: 5 additions & 5 deletions packages/snap/src/rpc/exportPrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { SnapProvider } from "@metamask/snap-types";
import { SnapsGlobalObject } from "@metamask/snaps-types";
import { getKeyPair } from "../filecoin/account";
import { showConfirmationDialog } from "../util/confirmation";

export async function exportPrivateKey(
wallet: SnapProvider
snap: SnapsGlobalObject
): Promise<string | null> {
// ask for confirmation
const confirmation = await showConfirmationDialog(wallet, {
const confirmation = await showConfirmationDialog(snap, {
prompt: "Do you want to export your private key?",
});
// return private key if user confirmed action
// return private key if user confirmed actions
if (confirmation) {
const keypair = await getKeyPair(wallet);
const keypair = await getKeyPair(snap);
return keypair.privateKey;
}
return null;
Expand Down
Loading

0 comments on commit 155e857

Please sign in to comment.