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

Commit

Permalink
fix: replace custom types with metamask types (#214)
Browse files Browse the repository at this point in the history
* replace custom types with metamask types

* fix sematic PR ci
  • Loading branch information
mpetrunic committed Jul 28, 2022
1 parent 9f6e1a6 commit 5fd1801
Show file tree
Hide file tree
Showing 34 changed files with 331 additions and 236 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ jobs:
types: |
fix
feat
chore
validateSingleCommit: true #single commit can ovveride squash merge commit message
chore
3 changes: 2 additions & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"manifest": "mm-snap manifest",
"lint": "yarn run lint:types && yarn run lint:style",
"lint:types": "tsc --noEmit --pretty",
"lint:style": "eslint --color '{src, @types}/**/*.{js,ts,tsx}'",
"lint:style": "eslint --color '{src, test, @types}/**/*.{js,ts,tsx}'",
"lint:style:fix": "yarn run lint --fix",
"start": "yarn run build && yarn run serve",
"prebuild": "rm -rf build && rm -rf dist",
Expand Down Expand Up @@ -48,6 +48,7 @@
"homepage": "https://github.com/chainsafe/filecoin-metamask-snap#readme",
"devDependencies": {
"@chainsafe/filsnap-types": "workspace:^",
"@metamask/providers": "^9.0.0",
"@metamask/snaps-cli": "^0.18.1",
"@types/bn.js": "^4.11.6",
"@types/chai": "^4.2.10",
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": "46bjmrAu8YQbX6LXa90Hutv4vc32XLY08rdhvO22a9Q=",
"shasum": "HFK3+OvYHpbPxh1VHoDDJNZltHSzYXLTbGmDLD6wcrA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
7 changes: 5 additions & 2 deletions packages/snap/src/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SnapConfig } from "@chainsafe/filsnap-types";
import { MetamaskState, Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { MetamaskState } from "../interfaces";
import {
defaultConfiguration,
filecoinMainnetConfiguration,
Expand All @@ -19,7 +20,9 @@ export function getDefaultConfiguration(networkName?: string): SnapConfig {
}
}

export async function getConfiguration(wallet: Wallet): Promise<SnapConfig> {
export async function getConfiguration(
wallet: SnapProvider
): Promise<SnapConfig> {
const state = (await wallet.request({
method: "snap_manageState",
params: ["get"],
Expand Down
5 changes: 3 additions & 2 deletions packages/snap/src/filecoin/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
getBIP44AddressKeyDeriver,
JsonBIP44CoinTypeNode,
} from "@metamask/key-tree";
import { SnapProvider } from "@metamask/snap-types";
import { getMetamaskVersion, isNewerVersion } from "../util/version";
import { MetamaskState, Wallet } from "../interfaces";
import { MetamaskState } from "../interfaces";

/**
* Return derived KeyPair from seed.
* @param wallet
*/
export async function getKeyPair(wallet: Wallet): Promise<KeyPair> {
export async function getKeyPair(wallet: SnapProvider): Promise<KeyPair> {
const snapState = (await wallet.request({
method: "snap_manageState",
params: ["get"],
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/filecoin/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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 { getConfiguration } from "../configuration";
import { Wallet } from "../interfaces";
import { LotusRpcApi } from "./types";

export async function getApi(wallet: Wallet): Promise<LotusRpcApi> {
export async function getApi(wallet: SnapProvider): Promise<LotusRpcApi> {
const configuration = await getConfiguration(wallet);
return getApiFromConfig(configuration);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/snap/src/filecoin/message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MessageStatus } from "@chainsafe/filsnap-types";
import { MetamaskState, Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { MetamaskState } from "../interfaces";

export async function updateMessageInState(
wallet: Wallet,
wallet: SnapProvider,
message: MessageStatus
): Promise<void> {
const state = (await wallet.request({
Expand Down
4 changes: 1 addition & 3 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnRpcRequestHandler } from "@metamask/snap-types";
import { EmptyMetamaskState, Wallet } from "./interfaces";
import { EmptyMetamaskState } from "./interfaces";
import { getAddress } from "./rpc/getAddress";
import { exportPrivateKey } from "./rpc/exportPrivateKey";
import { getPublicKey } from "./rpc/getPublicKey";
Expand All @@ -18,8 +18,6 @@ import {
isValidSignRequest,
} from "./util/params";

declare let wallet: Wallet;

const apiDependentMethods = [
"fil_getBalance",
"fil_signMessage",
Expand Down
4 changes: 0 additions & 4 deletions packages/snap/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ export type MetamaskState = {
export const EmptyMetamaskState: () => MetamaskState = () => ({
filecoin: { config: defaultConfiguration, messages: [] },
});

export interface Wallet {
request(options: { method: string; params?: unknown[] }): unknown;
}
11 changes: 6 additions & 5 deletions packages/snap/src/rpc/configure.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import deepmerge from "deepmerge";
import { SnapConfig } from "@chainsafe/filsnap-types";
import { MetamaskState, Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import deepmerge from "deepmerge";
import { getDefaultConfiguration } from "../configuration";
import { LotusRpcApi } from "../filecoin/types";
import { getApiFromConfig } from "../filecoin/api";
import { LotusRpcApi } from "../filecoin/types";
import { MetamaskState } from "../interfaces";

export interface ConfigureResponse {
api: LotusRpcApi;
snapConfig: SnapConfig;
}

export async function configure(
wallet: Wallet,
wallet: SnapProvider,
networkName: string,
overrides?: unknown
): Promise<ConfigureResponse> {
Expand Down Expand Up @@ -44,7 +45,7 @@ export async function configure(
params: ["get"],
})) as MetamaskState;
state.filecoin.config = configuration;
wallet.request({
await wallet.request({
method: "snap_manageState",
params: ["update", state],
});
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/rpc/estimateMessageGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
MessageRequest,
} from "@chainsafe/filsnap-types";
import { FilecoinNumber } from "@glif/filecoin-number/dist";
import { Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { getKeyPair } from "../filecoin/account";
import { LotusRpcApi } from "../filecoin/types";

export async function estimateMessageGas(
wallet: Wallet,
wallet: SnapProvider,
api: LotusRpcApi,
messageRequest: MessageRequest,
maxFee?: string
Expand Down
8 changes: 5 additions & 3 deletions packages/snap/src/rpc/exportPrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Wallet } from "../interfaces";
import { showConfirmationDialog } from "../util/confirmation";
import { SnapProvider } from "@metamask/snap-types";
import { getKeyPair } from "../filecoin/account";
import { showConfirmationDialog } from "../util/confirmation";

export async function exportPrivateKey(wallet: Wallet): Promise<string | null> {
export async function exportPrivateKey(
wallet: SnapProvider
): Promise<string | null> {
// ask for confirmation
const confirmation = await showConfirmationDialog(wallet, {
prompt: "Do you want to export your private key?",
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/rpc/getAddress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { getKeyPair } from "../filecoin/account";

export async function getAddress(wallet: Wallet): Promise<string> {
export async function getAddress(wallet: SnapProvider): Promise<string> {
const keyPair = await getKeyPair(wallet);
return keyPair.address;
}
4 changes: 2 additions & 2 deletions packages/snap/src/rpc/getBalance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FilecoinNumber } from "@glif/filecoin-number/dist";
import { Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { getKeyPair } from "../filecoin/account";
import { LotusRpcApi } from "../filecoin/types";

export async function getBalance(
wallet: Wallet,
wallet: SnapProvider,
api: LotusRpcApi,
address?: string
): Promise<string> {
Expand Down
7 changes: 5 additions & 2 deletions packages/snap/src/rpc/getMessages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { MessageStatus } from "@chainsafe/filsnap-types";
import { MetamaskState, Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { MetamaskState } from "../interfaces";

export async function getMessages(wallet: Wallet): Promise<MessageStatus[]> {
export async function getMessages(
wallet: SnapProvider
): Promise<MessageStatus[]> {
const state = (await wallet.request({
method: "snap_manageState",
params: ["get"],
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/rpc/getPublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";
import { getKeyPair } from "../filecoin/account";

export async function getPublicKey(wallet: Wallet): Promise<string> {
export async function getPublicKey(wallet: SnapProvider): Promise<string> {
const keyPair = await getKeyPair(wallet);
return keyPair.publicKey;
}
6 changes: 3 additions & 3 deletions packages/snap/src/rpc/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MessageStatus, SignedMessage } from "@chainsafe/filsnap-types";
import { Wallet } from "../interfaces";
import { LotusRpcApi } from "../filecoin/types";
import { SnapProvider } from "@metamask/snap-types";
import { updateMessageInState } from "../filecoin/message";
import { LotusRpcApi } from "../filecoin/types";

export async function sendMessage(
wallet: Wallet,
wallet: SnapProvider,
api: LotusRpcApi,
signedMessage: SignedMessage
): Promise<MessageStatus> {
Expand Down
20 changes: 10 additions & 10 deletions packages/snap/src/rpc/signMessage.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {
MessageRequest,
SignMessageResponse,
SignRawMessageResponse,
} from "@chainsafe/filsnap-types";
import { FilecoinNumber } from "@glif/filecoin-number/dist";
import { SnapProvider } from "@metamask/snap-types";
import {
Message,
SignedMessage,
transactionSign,
transactionSignRaw,
} from "@zondax/filecoin-signing-tools/js";
import { FilecoinNumber } from "@glif/filecoin-number/dist";
import {
MessageRequest,
SignMessageResponse,
SignRawMessageResponse,
} from "@chainsafe/filsnap-types";
import { Wallet } from "../interfaces";
import { getKeyPair } from "../filecoin/account";
import { showConfirmationDialog } from "../util/confirmation";
import { LotusRpcApi } from "../filecoin/types";
import { showConfirmationDialog } from "../util/confirmation";
import { messageCreator } from "../util/messageCreator";

export async function signMessage(
wallet: Wallet,
wallet: SnapProvider,
api: LotusRpcApi,
messageRequest: MessageRequest
): Promise<SignMessageResponse> {
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function signMessage(
}

export async function signMessageRaw(
wallet: Wallet,
wallet: SnapProvider,
rawMessage: string
): Promise<SignRawMessageResponse> {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/util/confirmation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";

type ConfirmationDialogContent = {
prompt: string;
Expand All @@ -7,7 +7,7 @@ type ConfirmationDialogContent = {
};

export async function showConfirmationDialog(
wallet: Wallet,
wallet: SnapProvider,
message: ConfirmationDialogContent
): Promise<boolean> {
return (await wallet.request({
Expand Down
6 changes: 4 additions & 2 deletions packages/snap/src/util/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Wallet } from "../interfaces";
import { SnapProvider } from "@metamask/snap-types";

export const getMetamaskVersion = async (wallet: Wallet): Promise<string> =>
export const getMetamaskVersion = async (
wallet: SnapProvider
): Promise<string> =>
(await wallet.request({
method: "web3_clientVersion",
params: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/test/unit/configuration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import sinonChai from "sinon-chai";
chai.use(sinonChai);

// eslint-disable-next-line @typescript-eslint/no-empty-function
describe('Test configuration functions', function() {});
describe("Test configuration functions", function () {});
Loading

0 comments on commit 5fd1801

Please sign in to comment.