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

Commit

Permalink
feat: get current configuration from snap
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jul 2, 2024
1 parent 259590d commit 050398f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/adapter/src/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ function injectPolkadotSnap({
await enablePolkadotSnap(config, snapOrigin, snapInstallationParams)
).getMetamaskSnapApi();

const snapConfiguration = await snap.getConfiguration();

return {
accounts: {
get: async (): Promise<InjectedAccount[]> => {
const response = await snap.getAddress();
return transformAccounts([response], config);
return transformAccounts([response], snapConfiguration);
},
// 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
Expand Down
4 changes: 4 additions & 0 deletions packages/adapter/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export async function exportAccount(
)) as string;
}

export async function getConfiguration(this: MetamaskPolkadotSnap): Promise<SnapConfig> {
return (await sendSnapMethod({ method: 'getConfiguration' }, this.snapId)) as SnapConfig;
}

export async function setConfiguration(
this: MetamaskPolkadotSnap,
config: SnapConfig
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter/src/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getAddress,
getAllTransactions,
getBalance,
getConfiguration,
getLatestBlock,
getPublicKey,
sendSignedData,
Expand Down Expand Up @@ -39,6 +40,7 @@ export class MetamaskPolkadotSnap {
getLatestBlock: getLatestBlock.bind(this),
getPublicKey: getPublicKey.bind(this),
send: sendSignedData.bind(this),
getConfiguration: getConfiguration.bind(this),
setConfiguration: setConfiguration.bind(this),
signPayloadJSON: signPayloadJSON.bind(this),
signPayloadRaw: signPayloadRaw.bind(this)
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface MetamaskSnapApi {

getLatestBlock(): Promise<BlockInfo>;

getConfiguration(): Promise<SnapConfig>;

setConfiguration(configuration: SnapConfig): Promise<void>;

getAllTransactions(): Promise<Transaction[]>;
Expand Down
4 changes: 2 additions & 2 deletions packages/example/src/containers/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
BlockInfo,
SnapNetworks,
Transaction,
WellKnownSnapNetworks
SupportedSnapNetworks
} from '@chainsafe/metamask-polkadot-types';
import type { MetamaskSnapApi } from '@chainsafe/metamask-polkadot-adapter/src/types';
import { Transfer } from '../../components/Transfer/Transfer';
Expand Down Expand Up @@ -57,7 +57,7 @@ export const Dashboard = (): React.JSX.Element => {
return;
} else setCustomNetworkInputs(false);

const networkName = event.target.value as WellKnownSnapNetworks;
const networkName = event.target.value as SupportedSnapNetworks;
if (networkName === network) return;
if (!api) return;
await api.setConfiguration({ networkName: networkName });
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": "Sx91E/1tHSq5I0NqCXv1u+P93YJh2dvNzf89X+SaIz8=",
"shasum": "Rox1dBkfYddPGejHC+G8uQ/MZeUf+T36mtTFB5qnTy8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
validSignPayloadRawSchema
} from './util/validation';
import { exportAccount } from './rpc/exportAccount';
import { getConfiguration } from './configuration';

const apiDependentMethods = [
'getBlock',
Expand Down Expand Up @@ -78,6 +79,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
case 'getBalance': {
return await getBalance(api);
}
case 'getConfiguration':
return await getConfiguration();
case 'configure': {
const state = (await snap.request({
method: 'snap_manageState',
Expand Down
11 changes: 8 additions & 3 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface GetBalanceRequest {
method: 'getBalance';
}

export interface GetConfigurationRequest {
method: 'getConfiguration';
}

export interface ConfigureSnapRequest {
method: 'configure';
params: {
Expand Down Expand Up @@ -87,6 +91,7 @@ export type MetamaskPolkadotRpcRequest =
| GetTransactionsRequest
| GetBlockRequest
| GetBalanceRequest
| GetConfigurationRequest
| ConfigureSnapRequest
| AddPolkadotAssetRequest
| GetChainHeadRequest
Expand Down Expand Up @@ -132,16 +137,16 @@ export interface UnitConfiguration {
customViewUrl?: string;
}

export type WellKnownSnapNetworks = 'polkadot' | 'kusama' | 'westend';
export type SupportedSnapNetworks = 'polkadot' | 'kusama' | 'westend';

export type SnapNetworks = WellKnownSnapNetworks | string;
export type SnapNetworks = SupportedSnapNetworks | string;

export type SnapConfig = {
wsRpcUrl?: string;
addressPrefix?: number;
unit?: UnitConfiguration;
} & (
| { networkName: WellKnownSnapNetworks; genesisHash?: `0x${string}` }
| { networkName: SupportedSnapNetworks; genesisHash?: `0x${string}` }
| { networkName: SnapNetworks; genesisHash: `0x${string}` }
);

Expand Down

0 comments on commit 050398f

Please sign in to comment.