Skip to content

Commit

Permalink
Refactor: Extract Utils From Snap (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
poi-son-ivy authored Feb 23, 2024
1 parent 44ba23a commit e75f278
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/hashgraph/hedera-metamask-snaps.git"
},
"source": {
"shasum": "kA8SzTzH49ZRjOlMP9LpXm8EsIOG8fg3Gl67f6LhB0I=",
"shasum": "czc6KHQMgLwju9hzMn/WG1RUh+ORbHoXV3Ig0eDum3I=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { MirrorTokenInfo, TxReceipt } from '../../types/hedera';
import {
ApproveAllowanceAssetDetail,
Expand Down Expand Up @@ -147,9 +147,11 @@ export async function approveAllowance(

const dialogParamsForApproveAllowance: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParamsForApproveAllowance);
const confirmed = await SnapUtils.snapDialog(
dialogParamsForApproveAllowance,
);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { providerErrors } from '@metamask/rpc-errors';
import { heading, text } from '@metamask/snaps-ui';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { updateSnapState } from '../../snap/state';
import { Account, AccountInfo } from '../../types/account';
import { AccountBalance, TxReceipt } from '../../types/hedera';
Expand Down Expand Up @@ -67,9 +67,9 @@ export async function deleteAccount(
];
const dialogParamsForDeleteAccount: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParamsForDeleteAccount);
const confirmed = await SnapUtils.snapDialog(dialogParamsForDeleteAccount);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { MirrorTokenInfo, TxReceipt } from '../../types/hedera';
import { DeleteAllowanceRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
Expand Down Expand Up @@ -97,9 +97,11 @@ export async function deleteAllowance(

const dialogParamsForDeleteAllowance: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParamsForDeleteAllowance);
const confirmed = await SnapUtils.snapDialog(
dialogParamsForDeleteAllowance,
);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { updateSnapState } from '../../snap/state';
import { AccountInfo } from '../../types/account';
import { GetAccountInfoRequestParams, ServiceFee } from '../../types/params';
Expand Down Expand Up @@ -140,9 +140,11 @@ export async function getAccountInfo(

const dialogParamsForHederaAccountId: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParamsForHederaAccountId);
const confirmed = await SnapUtils.snapDialog(
dialogParamsForHederaAccountId,
);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { updateSnapState } from '../../snap/state';
import { TxReceipt } from '../../types/hedera';
import { StakeHbarRequestParams } from '../../types/params';
Expand Down Expand Up @@ -127,9 +127,9 @@ export async function stakeHbar(

const dialogParamsForStakeHbar: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParamsForStakeHbar);
const confirmed = await SnapUtils.snapDialog(dialogParamsForStakeHbar);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { TxReceipt } from '../../types/hedera';
import { AssociateTokensRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
Expand Down Expand Up @@ -106,9 +106,9 @@ export async function associateTokens(

const dialogParams: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParams);
const confirmed = await SnapUtils.snapDialog(dialogParams);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { TxReceipt } from '../../types/hedera';
import { CreateTokenRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
Expand Down Expand Up @@ -178,9 +178,9 @@ export async function createToken(

const dialogParams: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParams);
const confirmed = await SnapUtils.snapDialog(dialogParams);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { heading, text } from '@metamask/snaps-ui';
import { Wallet, ethers } from 'ethers';
import { Wallet as HederaWallet } from '../../domain/wallet/abstract';
import { PrivateKeySoftwareWallet } from '../../domain/wallet/software-private-key';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { SignMessageRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { CryptoUtils } from '../../utils/CryptoUtils';
Expand Down Expand Up @@ -58,9 +58,9 @@ export async function signMessage(
];
const dialogParamsForSignMessage: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParamsForSignMessage);
const confirmed = await SnapUtils.snapDialog(dialogParamsForSignMessage);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { generateCommonPanel, snapDialog } from '../../snap/dialog';
import { SnapUtils } from '../../utils/SnapUtils';
import { AccountInfo } from '../../types/account';
import { SimpleTransfer, TxReceipt } from '../../types/hedera';
import {
Expand Down Expand Up @@ -234,9 +234,9 @@ export async function transferCrypto(

const dialogParams: SnapDialogParams = {
type: 'confirmation',
content: await generateCommonPanel(origin, panelToShow),
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await snapDialog(dialogParams);
const confirmed = await SnapUtils.snapDialog(dialogParams);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
Expand Down
26 changes: 15 additions & 11 deletions packages/hedera-wallet-snap/packages/snap/src/snap/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import { hederaNetworks } from '../types/constants';
import { SimpleHederaClient } from '../types/hedera';
import { KeyStore, SnapDialogParams, WalletSnapState } from '../types/state';
import { CryptoUtils } from '../utils/CryptoUtils';
import { generateCommonPanel, snapDialog } from './dialog';
import { validHederaNetwork } from './network';
import { SnapUtils } from '../utils/SnapUtils';
import { HederaUtils } from '../utils/HederaUtils';
import {
getHederaAccountIdIfExists,
initAccountState,
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function setCurrentAccount(
): Promise<void> {
try {
const { network = 'mainnet' } = (params ?? {}) as NetworkParams;
if (!validHederaNetwork(network)) {
if (!HederaUtils.validHederaNetwork(network)) {
console.error(
`Invalid Hedera network '${network}'. Valid networks are '${hederaNetworks.join(
', ',
Expand Down Expand Up @@ -244,15 +244,17 @@ async function connectEVMAccount(
if (_.isEmpty(connectedAddress)) {
const dialogParamsForPrivateKey: SnapDialogParams = {
type: 'prompt',
content: await generateCommonPanel(origin, [
content: await SnapUtils.generateCommonPanel(origin, [
heading('Connect to EVM Account'),
text('Enter private key for the following account'),
divider(),
text(`EVM Address: ${evmAddress}`),
]),
placeholder: '2386d1d21644dc65d...',
};
const privateKey = (await snapDialog(dialogParamsForPrivateKey)) as string;
const privateKey = (await SnapUtils.snapDialog(
dialogParamsForPrivateKey,
)) as string;

try {
const hederaService = new HederaServiceImpl(network, mirrorNodeUrl);
Expand Down Expand Up @@ -337,14 +339,14 @@ async function connectEVMAccount(
} else {
const dialogParamsForHederaAccountId: SnapDialogParams = {
type: 'alert',
content: await generateCommonPanel(origin, [
content: await SnapUtils.generateCommonPanel(origin, [
heading('Hedera Account Status'),
text(
`The private key you passed is not associated with the Hedera account '${evmAddress}' on '${network}' that uses the elliptic curve '${curve}'`,
),
]),
};
await snapDialog(dialogParamsForHederaAccountId);
await SnapUtils.snapDialog(dialogParamsForHederaAccountId);

console.error(
`The private key you passed is not associated with the Hedera account '${result.address}' on '${network}' that uses the elliptic curve '${curve}'`,
Expand Down Expand Up @@ -415,15 +417,17 @@ async function connectHederaAccount(
if (_.isEmpty(connectedAddress)) {
const dialogParamsForPrivateKey: SnapDialogParams = {
type: 'prompt',
content: await generateCommonPanel(origin, [
content: await SnapUtils.generateCommonPanel(origin, [
heading('Connect to Hedera Account'),
text('Enter private key for the following account'),
divider(),
text(`Account Id: ${accountId}`),
]),
placeholder: '2386d1d21644dc65d...',
};
const privateKey = (await snapDialog(dialogParamsForPrivateKey)) as string;
const privateKey = (await SnapUtils.snapDialog(
dialogParamsForPrivateKey,
)) as string;

try {
const hederaService = new HederaServiceImpl(network, mirrorNodeUrl);
Expand Down Expand Up @@ -496,14 +500,14 @@ async function connectHederaAccount(
} else {
const dialogParamsForHederaAccountId: SnapDialogParams = {
type: 'alert',
content: await generateCommonPanel(origin, [
content: await SnapUtils.generateCommonPanel(origin, [
heading('Hedera Account Status'),
text(
`The private key you passed is not associated with the Hedera account '${accountId}' on '${network}' that uses the elliptic curve '${curve}'`,
),
]),
};
await snapDialog(dialogParamsForHederaAccountId);
await SnapUtils.snapDialog(dialogParamsForHederaAccountId);

console.error(
`The private key you passed is not associated with the Hedera account '${accountId}' on '${network}' that uses the elliptic curve '${curve}'`,
Expand Down
61 changes: 1 addition & 60 deletions packages/hedera-wallet-snap/packages/snap/src/snap/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
*
*/

import { divider, heading, panel, Panel, text } from '@metamask/snaps-ui';

import { SnapDialogParams, WalletSnapState } from '../types/state';
import { WalletSnapState } from '../types/state';
import { updateSnapState } from './state';

/**
Expand All @@ -32,60 +30,3 @@ export async function updatePopups(state: WalletSnapState) {
state.snapConfig.dApp.disablePopups = !state.snapConfig.dApp.disablePopups;
await updateSnapState(state);
}

/**
* Function that opens snap dialog.
*
* @param params - Snap dialog params.
*/
export async function snapDialog(
params: SnapDialogParams,
): Promise<string | boolean | null> {
return (await snap.request({
method: 'snap_dialog',
params,
})) as boolean;
}

/**
* Function to generate snap dialog panel.
*
* @param origin - The origin of where the call is being made from.
* @param prompt - Prompt text of the metamask dialog box(eg. 'Are you sure you want to send VCs to the dApp?').
*/
export async function generateCommonPanel(
origin: string,
prompt: any[],
): Promise<Panel> {
const panelToShow = [text(`Origin: ${origin}`), divider(), ...prompt];
return panel(panelToShow);
}

/**
* Request Hedera Account Id.
*
* @param origin - Source.
* @param publicKey - Public key.
* @param address - EVM address.
*/
export async function requestHederaAccountId(
origin: string,
publicKey: string,
address: string,
): Promise<string> {
const dialogParamsForHederaAccountId: SnapDialogParams = {
type: 'prompt',
content: await generateCommonPanel(origin, [
heading('Connect to Hedera Account'),
text(
`Enter your hedera account Id associated with the following account`,
),
divider(),
text(`Public Key: ${publicKey}`),
text(`EVM Address: ${address}`),
divider(),
]),
placeholder: '0.0.3658062',
};
return (await snapDialog(dialogParamsForHederaAccountId)) as string;
}
5 changes: 0 additions & 5 deletions packages/hedera-wallet-snap/packages/snap/src/snap/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import { MetaMaskInpageProvider } from '@metamask/providers';

import _ from 'lodash';
import { hederaNetworks, isIn } from '../types/constants';
import { WalletSnapState } from '../types/state';
import { HederaUtils } from '../utils/HederaUtils';
import { updateSnapState } from './state';
Expand All @@ -39,10 +38,6 @@ export async function getCurrentNetwork(
})) as string;
}

export const validHederaNetwork = (network: string) => {
return isIn(hederaNetworks, network);
};

/**
* Function that gets the mirror node url from snap state or whatever was passed in
* by the user.
Expand Down
Loading

0 comments on commit e75f278

Please sign in to comment.