Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
feat: implemented polkadot and westmint network (#155)
Browse files Browse the repository at this point in the history
* implemented `polkadot` and `westmint` network

* fix comments

* fix typo

* fix tests
  • Loading branch information
BeroBurny authored Oct 5, 2022
1 parent 5b82e91 commit 78cb267
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 26 deletions.
12 changes: 6 additions & 6 deletions packages/example/src/containers/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Account } from "../../components/Account/Account";
import { MetaMaskConnector } from "../MetaMaskConnector/MetaMaskConnector";
import { MetaMaskContext } from "../../context/metamask";
import { LatestBlock } from "../../components/LatestBlock/LatestBlock";
import { BlockInfo, Transaction } from "@chainsafe/metamask-polkadot-types";
import { BlockInfo, SnapNetworks, Transaction } from "@chainsafe/metamask-polkadot-types";
import { MetamaskSnapApi } from "@chainsafe/metamask-polkadot-adapter/build/types";

export const Dashboard = () => {
Expand All @@ -29,7 +29,7 @@ export const Dashboard = () => {
const [publicKey, setPublicKey] = useState("");
const [latestBlock, setLatestBlock] = useState<BlockInfo>({ hash: "", number: "" });
const [transactions, setTransactions] = useState<Transaction[]>([]);
const [network, setNetwork] = useState<"kusama" | "westend">("westend");
const [network, setNetwork] = useState<SnapNetworks>("westend");

const [api, setApi] = useState<MetamaskSnapApi | null>(null);

Expand All @@ -38,9 +38,8 @@ export const Dashboard = () => {
setTransactions((await api.getAllTransactions()));
}, [setTransactions]);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleNetworkChange = async (event: React.ChangeEvent<{ value: any }>) => {
const networkName = event.target.value as "kusama" | "westend";
const handleNetworkChange = async (event: React.ChangeEvent<{ value: unknown }>) => {
const networkName = event.target.value as SnapNetworks;
if (networkName === network) return;
if (!api) return;
await api.setConfiguration({ networkName: networkName });
Expand Down Expand Up @@ -98,8 +97,9 @@ export const Dashboard = () => {
defaultValue={"westend"}
onChange={handleNetworkChange}
>
<MenuItem value={"kusama"}>Kusama</MenuItem>
<MenuItem value={"westend"}>Westend</MenuItem>
<MenuItem value={"kusama"}>Kusama</MenuItem>
<MenuItem value={"polkadot"}>Polkadot</MenuItem>
</Select>
</Box>
<Grid container spacing={3} alignItems={"stretch"}>
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": "git+https://github.com/chainsafe/metamask-snap-polkadot.git"
},
"source": {
"shasum": "Vz+dLpnwiRlDThyNAF1rPHPRpJ0EL+aj6O2kFufWWKI=",
"shasum": "s16b9O989/SN2xnI0vsEqwYQw6zqIn0AoS1P64COrVc=",
"location": {
"npm": {
"filePath": "./dist/bundle.js",
Expand Down
10 changes: 9 additions & 1 deletion packages/snap/src/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import {MetamaskState, Wallet} from "../interfaces";
import {defaultConfiguration, kusamaConfiguration, westendConfiguration} from "./predefined";
import {
defaultConfiguration,
kusamaConfiguration,
polkadotConfiguration,
westendConfiguration,
} from "./predefined";
import {SnapConfig} from "@chainsafe/metamask-polkadot-types";

export function getDefaultConfiguration(networkName: string): SnapConfig {
switch (networkName) {
case "polkadot":
console.log("Polkadot configuration selected");
return polkadotConfiguration;
case "kusama":
console.log("Kusama configuration selected");
return kusamaConfiguration;
Expand Down
11 changes: 11 additions & 0 deletions packages/snap/src/configuration/predefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ export const westendConfiguration: SnapConfig = {
wsRpcUrl: "wss://westend-rpc.polkadot.io/",
};

export const polkadotConfiguration: SnapConfig = {
addressPrefix: 0,
networkName: "polkadot",
unit: {
decimals: 12,
image: "https://polkadot.js.org/apps/static/polkadot-circle.1eea41b2..svg",
symbol: "DOT",
},
wsRpcUrl: "wss://rpc.polkadot.io/",
};

export const defaultConfiguration: SnapConfig = westendConfiguration;
18 changes: 13 additions & 5 deletions packages/snap/src/polkadot/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@ import { Keyring } from '@polkadot/keyring';
import { stringToU8a } from "@polkadot/util";
import { getConfiguration } from "../configuration";
import { JsonBIP44CoinTypeNode } from "@metamask/key-tree";

const kusamaCoinType = 434;
// const polkadotCoinType = 354;
import { SnapNetworks } from "@chainsafe/metamask-polkadot-types";

/**
* Returns KeyringPair if one is saved in wallet state, creates new one otherwise
* @param wallet
*/
export async function getKeyPair(wallet: Wallet): Promise<KeyringPair> {
const config = await getConfiguration(wallet);

const bip44Node = (await wallet.request({
method: `snap_getBip44Entropy_${kusamaCoinType}`,
method: `snap_getBip44Entropy_${getCoinTypeByNetwork(config.networkName)}`,
params: [],
})) as JsonBIP44CoinTypeNode;

// generate keys
const seed = bip44Node.privateKey.slice(0, 32);
const config = await getConfiguration(wallet);
const ss58Format = config.addressPrefix;
const keyring = new Keyring({ ss58Format });

return keyring.addFromSeed(stringToU8a(seed));
}

const getCoinTypeByNetwork = (network: SnapNetworks): number => {
switch (network) {
case "kusama":
case "westend":
return 434;
case "polkadot":
return 354;
}
};
2 changes: 1 addition & 1 deletion packages/snap/test/unit/asset/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Test asset functions ', function() {
100,
"test-address",
{ // configuration
networkName: "test-network",
networkName: "westend",
unit: {
assetId: POLKADOT_SNAP_ASSET_IDENTIFIER,
customViewUrl: "custom-view",
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/test/unit/configuration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sinonChai from "sinon-chai";
import {getConfiguration, getDefaultConfiguration} from "../../../src/configuration";
import {defaultConfiguration, kusamaConfiguration, westendConfiguration} from "../../../src/configuration/predefined";
import {WalletMock} from "../wallet.mock.test";
import {EmptyMetamaskState, Wallet} from "../../../src/interfaces";
import {EmptyMetamaskState} from "../../../src/interfaces";
import {SnapConfig} from "@chainsafe/metamask-polkadot-types";

chai.use(sinonChai);
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('Test configuration functions', function() {
});

it('should return configuration saved in state"', async function () {
const customConfiguration: SnapConfig = {addressPrefix: 5, networkName: "test-network", wsRpcUrl: "url"};
const customConfiguration: SnapConfig = {addressPrefix: 5, networkName: "westend", wsRpcUrl: "url"};
walletStub.request.returns({polkadot: {config: customConfiguration}});
const configuration = await getConfiguration(walletStub);
expect(configuration).to.be.deep.eq(customConfiguration);
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/test/unit/polkadot/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('Test account function: getKeyPair', function() {
});

it('should return keypair', async function() {
walletStub.request.onFirstCall().returns({privateKey: testAppKey});
walletStub.request.onSecondCall().returns({polkadot: {configuration: westendConfiguration}});
walletStub.request.onFirstCall().returns({polkadot: {configuration: westendConfiguration}});
walletStub.request.onSecondCall().returns({privateKey: testAppKey});
const result = await getKeyPair(walletStub);
expect(result.address).to.be.eq(testAddress);
expect(result.publicKey).to.be.deep.eq(hexToU8a(testPublicKey));
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/test/unit/rpc/configure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Test rpc handler function: configure', function() {
// stubs
const customConfiguration: SnapConfig = {
addressPrefix: 1,
networkName: "test-network",
networkName: "westend",
unit: {customViewUrl: "custom-view-url", decimals: 1, image: "image", symbol: "TST"},
wsRpcUrl: "ws-rpc-url",

Expand Down
4 changes: 2 additions & 2 deletions packages/snap/test/unit/rpc/getAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('Test rpc handler function: getAddress', function() {
});

it('should return valid address with westend configuration', async function () {
walletStub.request.onFirstCall().returns({ privateKey: testAppKey});
walletStub.request.onSecondCall().returns({polkadot: {configuration: westendConfiguration}});
walletStub.request.onFirstCall().returns({polkadot: {configuration: westendConfiguration}});
walletStub.request.onSecondCall().returns({ privateKey: testAppKey});
const result = await getAddress(walletStub);
expect(result).to.be.eq("5DW5CXHWbM13Az7aetLQVUEviNq8WeXFQanHNPVMmzyRYKvX");
});
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/test/unit/rpc/getPublicKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('Test rpc handler function: getPublicKey', function() {
});

it('should return pk', async function () {
walletStub.request.onFirstCall().returns({privateKey: testAppKey});
walletStub.request.onSecondCall().returns(EmptyMetamaskState());
walletStub.request.onFirstCall().returns(EmptyMetamaskState());
walletStub.request.onSecondCall().returns({privateKey: testAppKey});
const result = await getPublicKey(walletStub);
expect(result).to.be.eq(testPublicKey);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/test/unit/rpc/substrate/getBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('Test rpc handler function: getBalance', function() {

it('should return balance on saved keyring in state', async function () {
// wallet stub
walletStub.request.onFirstCall().returns({privateKey: testAppKey});
walletStub.request.onSecondCall().returns(EmptyMetamaskState());
walletStub.request.onFirstCall().returns(EmptyMetamaskState());
walletStub.request.onSecondCall().returns({privateKey: testAppKey});
// api stub
const apiStub = {query: {system: {account: sinon.stub()}}};
apiStub.query.system.account.returns({data: {free: '0'}} as unknown as AccountInfo);
Expand Down
4 changes: 3 additions & 1 deletion packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ export interface UnitConfiguration {
customViewUrl?: string;
}

export type SnapNetworks = "polkadot" | "kusama" | "westend";

export interface SnapConfig {
networkName: string;
networkName: SnapNetworks;
wsRpcUrl?: string;
addressPrefix?: number;
unit?: UnitConfiguration;
Expand Down

0 comments on commit 78cb267

Please sign in to comment.