Skip to content

Commit

Permalink
chore: refactor showAccountRequireUpgradeOrDeployModal (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyyconsensys authored Aug 12, 2024
1 parent ad12592 commit 45c127c
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 547 deletions.
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/declareContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
getDeclareSnapTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
declareContract as declareContractUtil,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -38,16 +37,7 @@ export async function declareContract(params: ApiParamsWithKeyDeriver) {
senderAddress,
);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
senderAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, senderAddress, publicKey);

const snapComponents = getDeclareSnapTxt(
senderAddress,
Expand Down
9 changes: 6 additions & 3 deletions packages/starknet-snap/src/estimateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import type {
import { ACCOUNT_CLASS_HASH } from './utils/constants';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getNetworkFromChainId } from './utils/snapUtils';
import {
validateAccountRequireUpgradeOrDeploy,
getNetworkFromChainId,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
validateAndParseAddress,
getKeysFromAddress,
getCallDataArray,
Expand Down Expand Up @@ -64,10 +66,11 @@ export async function estimateFee(params: ApiParamsWithKeyDeriver) {
const { privateKey: senderPrivateKey, publicKey } =
await getKeysFromAddress(keyDeriver, network, state, senderAddress);

await validateAccountRequireUpgradeOrDeploy(
await verifyIfAccountNeedUpgradeOrDeploy(
network,
senderAddress,
publicKey,
false,
);

const txnInvocation = {
Expand Down
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/executeTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getNetworkFromChainId,
getTxnSnapTxt,
addDialogTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
Expand All @@ -23,7 +23,6 @@ import {
estimateFeeBulk,
getAccContractAddressAndCallData,
addFeesFromAllTransactions,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -42,16 +41,7 @@ export async function executeTxn(params: ApiParamsWithKeyDeriver) {
addressIndex,
} = await getKeysFromAddress(keyDeriver, network, state, senderAddress);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
senderAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, senderAddress, publicKey);

const txnInvocationArray = Array.isArray(requestParamsObj.txnInvocation)
? requestParamsObj.txnInvocation
Expand Down
11 changes: 7 additions & 4 deletions packages/starknet-snap/src/extractPrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import type {
} from './types/snapApi';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getNetworkFromChainId } from './utils/snapUtils';
import {
getNetworkFromChainId,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
validateAndParseAddress,
getKeysFromAddress,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
*
* @param params
Expand Down Expand Up @@ -43,10 +44,12 @@ export async function extractPrivateKey(params: ApiParamsWithKeyDeriver) {
state,
userAddress,
);
await validateAccountRequireUpgradeOrDeploy(

await verifyIfAccountNeedUpgradeOrDeploy(
network,
userAddress,
publicKey,
false,
);

const response = await wallet.request({
Expand Down
11 changes: 8 additions & 3 deletions packages/starknet-snap/src/extractPublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import type {
} from './types/snapApi';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getAccount, getNetworkFromChainId } from './utils/snapUtils';
import {
getAccount,
getNetworkFromChainId,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
validateAndParseAddress,
validateAccountRequireUpgradeOrDeploy,
getKeysFromAddress,
} from './utils/starknetUtils';

Expand Down Expand Up @@ -48,10 +51,12 @@ export async function extractPublicKey(params: ApiParamsWithKeyDeriver) {
state,
userAddress,
);
await validateAccountRequireUpgradeOrDeploy(

await verifyIfAccountNeedUpgradeOrDeploy(
network,
userAddress,
publicKey,
false,
);

let userPublicKey;
Expand Down
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/signDeclareTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
getSignTxnTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
signDeclareTransaction as signDeclareTransactionUtil,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -38,16 +37,7 @@ export async function signDeclareTransaction(
signerAddress,
);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
signerAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, signerAddress, publicKey);

logger.log(
`signDeclareTransaction params: ${toJson(
Expand Down
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/signDeployAccountTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
getSignTxnTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
signDeployAccountTransaction as signDeployAccountTransactionUtil,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -38,16 +37,7 @@ export async function signDeployAccountTransaction(
signerAddress,
);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
signerAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, signerAddress, publicKey);

logger.log(
`signDeployAccountTransaction params: ${toJson(
Expand Down
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
addDialogTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
signMessage as signMessageUtil,
getKeysFromAddress,
validateAndParseAddress,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand Down Expand Up @@ -52,16 +51,7 @@ export async function signMessage(params: ApiParamsWithKeyDeriver) {
);
}

try {
await validateAccountRequireUpgradeOrDeploy(
network,
signerAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, signerAddress, publicKey);

const components = [];
addDialogTxt(components, 'Message', toJson(typedDataMessage));
Expand Down
19 changes: 3 additions & 16 deletions packages/starknet-snap/src/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
getSignTxnTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
signTransactions,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';
import { getKeysFromAddress, signTransactions } from './utils/starknetUtils';

/**
*
Expand All @@ -37,16 +33,7 @@ export async function signTransaction(
signerAddress,
);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
signerAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, signerAddress, publicKey);

logger.log(
`signTransaction params: ${toJson(requestParamsObj.transactions, 2)}}`,
Expand Down
2 changes: 2 additions & 0 deletions packages/starknet-snap/src/utils/__mocks__/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const getBip44Deriver = jest.fn();

export const confirmDialog = jest.fn();

export const alertDialog = jest.fn();

export const getStateData = jest.fn();

export const setStateData = jest.fn();
18 changes: 16 additions & 2 deletions packages/starknet-snap/src/utils/exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
export class UpgradeRequiredError extends Error {}
import { SnapError } from '@metamask/snaps-sdk';

export class DeployRequiredError extends Error {}
// Extend SnapError to allow error message visible to client
export class UpgradeRequiredError extends SnapError {
constructor(message?: string) {
super(message ?? 'Upgrade required');
}
}

export class DeployRequiredError extends SnapError {
constructor(message?: string) {
super(
message ??
'Cairo 0 contract address balance is not empty, deploy required',
);
}
}
24 changes: 24 additions & 0 deletions packages/starknet-snap/src/utils/snap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ describe('confirmDialog', () => {
});
});

describe('alertDialog', () => {
it('calls snap_dialog', async () => {
const spy = jest.spyOn(snapUtil.getProvider(), 'request');
const components: Component[] = [
heading('header'),
text('subHeader'),
divider(),
row('Label1', text('Value1')),
text('**Label2**:'),
row('SubLabel1', text('SubValue1')),
];

await snapUtil.alertDialog(components);

expect(spy).toHaveBeenCalledWith({
method: 'snap_dialog',
params: {
type: 'alert',
content: panel(components),
},
});
});
});

describe('getStateData', () => {
it('gets state data', async () => {
const spy = jest.spyOn(snapUtil.getProvider(), 'request');
Expand Down
22 changes: 20 additions & 2 deletions packages/starknet-snap/src/utils/snap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BIP44AddressKeyDeriver } from '@metamask/key-tree';
import { getBIP44AddressKeyDeriver } from '@metamask/key-tree';
import type { Component, DialogResult, Json } from '@metamask/snaps-sdk';
import { panel, type SnapsProvider } from '@metamask/snaps-sdk';
import { DialogType, panel, type SnapsProvider } from '@metamask/snaps-sdk';

declare const snap: SnapsProvider;

Expand Down Expand Up @@ -41,7 +41,25 @@ export async function confirmDialog(
return snap.request({
method: 'snap_dialog',
params: {
type: 'confirmation',
type: DialogType.Confirmation,
content: panel(components),
},
});
}

/**
* Displays a alert dialog with the specified components.
*
* @param components - An array of components to display in the dialog.
* @returns A Promise that resolves to the result of the dialog.
*/
export async function alertDialog(
components: Component[],
): Promise<DialogResult> {
return snap.request({
method: 'snap_dialog',
params: {
type: DialogType.Alert,
content: panel(components),
},
});
Expand Down
Loading

0 comments on commit 45c127c

Please sign in to comment.