diff --git a/packages/adapter/src/extension/index.ts b/packages/adapter/src/extension/index.ts index 2b807cd2..ba3b200f 100644 --- a/packages/adapter/src/extension/index.ts +++ b/packages/adapter/src/extension/index.ts @@ -21,9 +21,10 @@ interface IInjectPolkadotSnap extends IEnablePolkadotSnapParams { injectedSnapId?: string; } -function transformAccounts(accounts: string[]): InjectedAccount[] { +function transformAccounts(accounts: string[], config?: SnapConfig): InjectedAccount[] { return accounts.map((address, i) => ({ address, + genesisHash: config?.genesisHash, name: `Polkadot Snap #${i}`, type: 'ed25519' })); @@ -46,7 +47,7 @@ function injectPolkadotSnap({ accounts: { get: async (): Promise => { const response = await snap.getAddress(); - return transformAccounts([response]); + return transformAccounts([response], config); }, // Currently there is only available only one account, in that case this method will never return anything // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/packages/example/src/components/CustomNetworkConfig/CustomNetworkConfig.tsx b/packages/example/src/components/CustomNetworkConfig/CustomNetworkConfig.tsx index 6524b604..609a66d8 100644 --- a/packages/example/src/components/CustomNetworkConfig/CustomNetworkConfig.tsx +++ b/packages/example/src/components/CustomNetworkConfig/CustomNetworkConfig.tsx @@ -4,6 +4,7 @@ import { useState } from 'react'; export type CustomNetworkConfigInput = { networkName?: string; + genesisHash?: `0x${string}`; rpcUrl?: string; addressPrefix?: number; unitDecimals?: number; @@ -53,6 +54,10 @@ export const CustonNetworkConfig: FC = ({ onSubmit }) => { Network name + + Network genesis hash + + RPC url diff --git a/packages/example/src/containers/Dashboard/Dashboard.tsx b/packages/example/src/containers/Dashboard/Dashboard.tsx index 1fcc7329..d9b9d21a 100644 --- a/packages/example/src/containers/Dashboard/Dashboard.tsx +++ b/packages/example/src/containers/Dashboard/Dashboard.tsx @@ -11,7 +11,12 @@ import { Select, Typography } from '@material-ui/core'; -import type { BlockInfo, SnapNetworks, Transaction } from '@chainsafe/metamask-polkadot-types'; +import type { + BlockInfo, + SnapNetworks, + Transaction, + WellKnownSnapNetworks +} from '@chainsafe/metamask-polkadot-types'; import type { MetamaskSnapApi } from '@chainsafe/metamask-polkadot-adapter/src/types'; import { Transfer } from '../../components/Transfer/Transfer'; import { SignMessage } from '../../components/SignMessage/SignMessage'; @@ -52,7 +57,7 @@ export const Dashboard = (): React.JSX.Element => { return; } else setCustomNetworkInputs(false); - const networkName = event.target.value as SnapNetworks; + const networkName = event.target.value as WellKnownSnapNetworks; if (networkName === network) return; if (!api) return; await api.setConfiguration({ networkName: networkName }); @@ -60,11 +65,12 @@ export const Dashboard = (): React.JSX.Element => { }; const onCustomNetworkConnect = async (submitData: CustomNetworkConfigInput): Promise => { - const { networkName, rpcUrl, addressPrefix } = submitData; + const { networkName, genesisHash, rpcUrl, addressPrefix } = submitData; - if (!api || !networkName || !rpcUrl || !addressPrefix) return; + if (!api || !networkName || !genesisHash || !rpcUrl || !addressPrefix) return; const configuration = { - networkName: networkName, + networkName, + genesisHash, wsRpcUrl: rpcUrl, addressPrefix: addressPrefix, unit: { diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index d06d5c46..63051aef 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "git+https://github.com/chainsafe/metamask-snap-polkadot.git" }, "source": { - "shasum": "o2JYBxUWDxVyQEX+K7O58mlhetpKywdog+dHPGXj98U=", + "shasum": "Sx91E/1tHSq5I0NqCXv1u+P93YJh2dvNzf89X+SaIz8=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/configuration/predefined.ts b/packages/snap/src/configuration/predefined.ts index 71dd2fe7..f3f08792 100644 --- a/packages/snap/src/configuration/predefined.ts +++ b/packages/snap/src/configuration/predefined.ts @@ -3,6 +3,7 @@ import type { SnapConfig } from '@chainsafe/metamask-polkadot-types'; export const kusamaConfiguration: SnapConfig = { addressPrefix: 2, networkName: 'kusama', + genesisHash: '0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe', unit: { decimals: 12, image: 'https://svgshare.com/i/L3o.svg', @@ -14,6 +15,7 @@ export const kusamaConfiguration: SnapConfig = { export const westendConfiguration: SnapConfig = { addressPrefix: 42, networkName: 'westend', + genesisHash: '0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e', unit: { decimals: 12, image: 'https://svgshare.com/i/L2d.svg', @@ -25,6 +27,7 @@ export const westendConfiguration: SnapConfig = { export const polkadotConfiguration: SnapConfig = { addressPrefix: 0, networkName: 'polkadot', + genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', unit: { decimals: 12, image: 'https://polkadot.js.org/apps/static/polkadot-circle.1eea41b2..svg', diff --git a/packages/snap/test/unit/rpc/configure.test.ts b/packages/snap/test/unit/rpc/configure.test.ts index 27a5c532..81c32654 100644 --- a/packages/snap/test/unit/rpc/configure.test.ts +++ b/packages/snap/test/unit/rpc/configure.test.ts @@ -38,6 +38,7 @@ describe('Test rpc handler function: configure', function () { const customConfiguration: SnapConfig = { addressPrefix: 1, networkName: 'westend', + genesisHash: '0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e', unit: { customViewUrl: 'custom-view-url', decimals: 1, image: 'image', symbol: 'TST' }, wsRpcUrl: 'ws-rpc-url' }; diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index d1dd8fa4..7d758c6c 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -16,7 +16,7 @@ export interface ExportAccountRequest { method: 'exportAccount'; params: { jsonPassphrase?: string; - } + }; } export interface GetTransactionsRequest { @@ -132,14 +132,18 @@ export interface UnitConfiguration { customViewUrl?: string; } -export type SnapNetworks = 'polkadot' | 'kusama' | 'westend' | string; +export type WellKnownSnapNetworks = 'polkadot' | 'kusama' | 'westend'; + +export type SnapNetworks = WellKnownSnapNetworks | string; -export interface SnapConfig { - networkName: SnapNetworks; +export type SnapConfig = { wsRpcUrl?: string; addressPrefix?: number; unit?: UnitConfiguration; -} +} & ( + | { networkName: WellKnownSnapNetworks; genesisHash?: `0x${string}` } + | { networkName: SnapNetworks; genesisHash: `0x${string}` } +); // Polkadot types