Skip to content

Commit

Permalink
chore(Error): add error wrapper for wallet rpc error code (#374)
Browse files Browse the repository at this point in the history
* chore: add error wrapper

* fix: update estimate fee error test

* chore: packages/starknet-snap/src/index.ts

Co-authored-by: khanti42 <[email protected]>

* chore: update var name

---------

Co-authored-by: khanti42 <[email protected]>
  • Loading branch information
stanleyyconsensys and khanti42 authored Oct 9, 2024
1 parent 40dad6e commit e61eb8b
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 95 deletions.
11 changes: 4 additions & 7 deletions packages/starknet-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import type {
OnInstallHandler,
OnUpdateHandler,
} from '@metamask/snaps-sdk';
import {
panel,
text,
SnapError,
MethodNotFoundError,
} from '@metamask/snaps-sdk';
import { panel, text, MethodNotFoundError } from '@metamask/snaps-sdk';

import { addErc20Token } from './addErc20Token';
import { addNetwork } from './addNetwork';
Expand Down Expand Up @@ -67,6 +62,7 @@ import {
STARKNET_SEPOLIA_TESTNET_NETWORK,
STARKNET_TESTNET_NETWORK,
} from './utils/constants';
import { UnknownError } from './utils/exceptions';
import { getAddressKeyDeriver } from './utils/keyPair';
import { acquireLock } from './utils/lock';
import { logger } from './utils/logger';
Expand Down Expand Up @@ -287,7 +283,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
let snapError = error;

if (!isSnapRpcError(error)) {
snapError = new SnapError('Unable to execute the rpc request');
// To ensure the error meets both the SnapError format and WalletRpc format.
snapError = new UnknownError('Unable to execute the rpc request');
}
logger.error(
`onRpcRequest error: ${JSON.stringify(snapError.toJSON(), null, 2)}`,
Expand Down
16 changes: 8 additions & 8 deletions packages/starknet-snap/src/rpcs/displayPrivateKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
InvalidParamsError,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import { constants } from 'starknet';

import type { SnapState } from '../types/snapState';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import {
UserRejectedOpError,
InvalidRequestParamsError,
} from '../utils/exceptions';
import {
mockAccount,
prepareAlertDialog,
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('displayPrivateKey', () => {
]);
});

it('throws `UserRejectedRequestError` if user denies the operation', async () => {
it('throws `UserRejectedOpError` if user denies the operation', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const account = await mockAccount(chainId);
prepareMockAccount(account, state);
Expand All @@ -89,7 +89,7 @@ describe('displayPrivateKey', () => {
const request = createRequestParam(chainId, account.address);

await expect(displayPrivateKey.execute(request)).rejects.toThrow(
UserRejectedRequestError,
UserRejectedOpError,
);
});

Expand All @@ -108,11 +108,11 @@ describe('displayPrivateKey', () => {
},
},
])(
'throws `InvalidParamsError` when $case',
'throws `InvalidRequestParamsError` when $case',
async ({ request }: { request: unknown }) => {
await expect(
displayPrivateKey.execute(request as DisplayPrivateKeyParams),
).rejects.toThrow(InvalidParamsError);
).rejects.toThrow(InvalidRequestParamsError);
},
);
});
5 changes: 3 additions & 2 deletions packages/starknet-snap/src/rpcs/displayPrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyable, text, UserRejectedRequestError } from '@metamask/snaps-sdk';
import { copyable, text } from '@metamask/snaps-sdk';
import { type Infer, object, literal, assign } from 'superstruct';

import {
Expand All @@ -8,6 +8,7 @@ import {
alertDialog,
BaseRequestStruct,
} from '../utils';
import { UserRejectedOpError } from '../utils/exceptions';

export const DisplayPrivateKeyRequestStruct = assign(
object({
Expand Down Expand Up @@ -58,7 +59,7 @@ export class DisplayPrivateKeyRpc extends AccountRpcController<
const confirmComponents = [text('Do you want to export your private key?')];

if (!(await confirmDialog(confirmComponents))) {
throw new UserRejectedRequestError() as unknown as Error;
throw new UserRejectedOpError() as unknown as Error;
}

const alertComponents = [
Expand Down
6 changes: 3 additions & 3 deletions packages/starknet-snap/src/rpcs/estimateFee.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { InvalidParamsError } from '@metamask/snaps-sdk';
import type { Invocations } from 'starknet';
import { constants, TransactionType } from 'starknet';
import type { Infer } from 'superstruct';

import { getEstimateFees } from '../__tests__/helper';
import { FeeTokenUnit } from '../types/snapApi';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import { InvalidRequestParamsError } from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import type { TxVersionStruct } from '../utils/superstruct';
import { mockAccount, prepareMockAccount } from './__tests__/helper';
Expand Down Expand Up @@ -101,9 +101,9 @@ describe('estimateFee', () => {
});
});

it('throws `InvalidParamsError` when request parameter is not correct', async () => {
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => {
await expect(
estimateFee.execute({} as unknown as EstimateFeeParams),
).rejects.toThrow(InvalidParamsError);
).rejects.toThrow(InvalidRequestParamsError);
});
});
16 changes: 8 additions & 8 deletions packages/starknet-snap/src/rpcs/executeTxn.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
InvalidParamsError,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import type { UniversalDetails, Call, InvokeFunctionResponse } from 'starknet';
import { constants } from 'starknet';

import callsExamples from '../__tests__/fixture/callsExamples.json'; // Assuming you have a similar fixture
import { getEstimateFees } from '../__tests__/helper';
import type { FeeTokenUnit } from '../types/snapApi';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import {
UserRejectedOpError,
InvalidRequestParamsError,
} from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import { executeTxn as executeTxnUtil } from '../utils/starknetUtils';
import {
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('ExecuteTxn', () => {
},
);

it('throws UserRejectedRequestError if user cancels execution', async () => {
it('throws UserRejectedOpError if user cancels execution', async () => {
callsExample = callsExamples[1];
const { request, confirmDialogSpy } = await prepareMockExecuteTxn(
callsExample.hash,
Expand All @@ -190,7 +190,7 @@ describe('ExecuteTxn', () => {
confirmDialogSpy.mockResolvedValue(false);

await expect(executeTxn.execute(request)).rejects.toThrow(
UserRejectedRequestError,
UserRejectedOpError,
);
});

Expand All @@ -209,9 +209,9 @@ describe('ExecuteTxn', () => {
await expect(executeTxn.execute(request)).rejects.toThrow(Error);
});

it('throws `InvalidParamsError` when request parameter is not correct', async () => {
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => {
await expect(
executeTxn.execute({} as unknown as ExecuteTxnParams),
).rejects.toThrow(InvalidParamsError);
).rejects.toThrow(InvalidRequestParamsError);
});
});
11 changes: 3 additions & 8 deletions packages/starknet-snap/src/rpcs/executeTxn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { Component, Json } from '@metamask/snaps-sdk';
import {
heading,
row,
UserRejectedRequestError,
text,
divider,
} from '@metamask/snaps-sdk';
import { heading, row, text, divider } from '@metamask/snaps-sdk';
import convert from 'ethereum-unit-converter';
import type { Call, Calldata } from 'starknet';
import { constants, TransactionStatus, TransactionType } from 'starknet';
Expand All @@ -27,6 +21,7 @@ import {
CallsStruct,
mapDeprecatedParams,
} from '../utils';
import { UserRejectedOpError } from '../utils/exceptions';
import { logger } from '../utils/logger';
import {
createAccount,
Expand Down Expand Up @@ -140,7 +135,7 @@ export class ExecuteTxnRpc extends AccountRpcController<
version,
))
) {
throw new UserRejectedRequestError() as unknown as Error;
throw new UserRejectedOpError() as unknown as Error;
}

if (!accountDeployed) {
Expand Down
16 changes: 8 additions & 8 deletions packages/starknet-snap/src/rpcs/sign-declare-transaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
InvalidParamsError,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import type { DeclareSignerDetails } from 'starknet';
import { constants } from 'starknet';

import type { SnapState } from '../types/snapState';
import { toJson } from '../utils';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import {
UserRejectedOpError,
InvalidRequestParamsError,
} from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import {
mockAccount,
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('signDeclareTransaction', () => {
]);
});

it('throws `UserRejectedRequestError` if user denied the operation', async () => {
it('throws `UserRejectedOpError` if user denied the operation', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const account = await mockAccount(chainId);

Expand All @@ -120,15 +120,15 @@ describe('signDeclareTransaction', () => {
const request = createRequest(chainId, account.address);

await expect(signDeclareTransaction.execute(request)).rejects.toThrow(
UserRejectedRequestError,
UserRejectedOpError,
);
});

it('throws `InvalidParamsError` when request parameter is not correct', async () => {
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => {
await expect(
signDeclareTransaction.execute(
{} as unknown as SignDeclareTransactionParams,
),
).rejects.toThrow(InvalidParamsError);
).rejects.toThrow(InvalidRequestParamsError);
});
});
10 changes: 3 additions & 7 deletions packages/starknet-snap/src/rpcs/sign-declare-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { Component } from '@metamask/snaps-sdk';
import {
heading,
row,
text,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import { heading, row, text } from '@metamask/snaps-sdk';
import type { DeclareSignerDetails } from 'starknet';
import type { Infer } from 'superstruct';
import { array, object, string, assign } from 'superstruct';
Expand All @@ -18,6 +13,7 @@ import {
DeclareSignDetailsStruct,
mapDeprecatedParams,
} from '../utils';
import { UserRejectedOpError } from '../utils/exceptions';
import { signDeclareTransaction as signDeclareTransactionUtil } from '../utils/starknetUtils';

export const SignDeclareTransactionRequestStruct = assign(
Expand Down Expand Up @@ -87,7 +83,7 @@ export class SignDeclareTransactionRpc extends AccountRpcController<
): Promise<SignDeclareTransactionResponse> {
const { details } = params;
if (!(await this.getSignDeclareTransactionConsensus(details))) {
throw new UserRejectedRequestError() as unknown as Error;
throw new UserRejectedOpError() as unknown as Error;
}

return (await signDeclareTransactionUtil(
Expand Down
16 changes: 8 additions & 8 deletions packages/starknet-snap/src/rpcs/signMessage.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
InvalidParamsError,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import { constants } from 'starknet';

import typedDataExample from '../__tests__/fixture/typedDataExample.json';
import type { SnapState } from '../types/snapState';
import { toJson } from '../utils';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import {
UserRejectedOpError,
InvalidRequestParamsError,
} from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import {
mockAccount,
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('signMessage', () => {
]);
});

it('throws `UserRejectedRequestError` if user denied the operation', async () => {
it('throws `UserRejectedOpError` if user denied the operation', async () => {
const account = await mockAccount(constants.StarknetChainId.SN_SEPOLIA);

prepareMockAccount(account, state);
Expand All @@ -105,13 +105,13 @@ describe('signMessage', () => {
};

await expect(signMessage.execute(request)).rejects.toThrow(
UserRejectedRequestError,
UserRejectedOpError,
);
});

it('throws `InvalidParamsError` when request parameter is not correct', async () => {
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => {
await expect(
signMessage.execute({} as unknown as SignMessageParams),
).rejects.toThrow(InvalidParamsError);
).rejects.toThrow(InvalidRequestParamsError);
});
});
10 changes: 3 additions & 7 deletions packages/starknet-snap/src/rpcs/signMessage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { Component } from '@metamask/snaps-sdk';
import {
heading,
row,
text,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import { heading, row, text } from '@metamask/snaps-sdk';
import type { Infer } from 'superstruct';
import { array, object, string, assign } from 'superstruct';

Expand All @@ -18,6 +13,7 @@ import {
AccountRpcController,
mapDeprecatedParams,
} from '../utils';
import { UserRejectedOpError } from '../utils/exceptions';
import { signMessage as signMessageUtil } from '../utils/starknetUtils';

export const SignMessageRequestStruct = assign(
Expand Down Expand Up @@ -84,7 +80,7 @@ export class SignMessageRpc extends AccountRpcController<
enableAuthorize &&
!(await this.getSignMessageConsensus(typedDataMessage, address))
) {
throw new UserRejectedRequestError() as unknown as Error;
throw new UserRejectedOpError() as unknown as Error;
}

return await signMessageUtil(
Expand Down
16 changes: 8 additions & 8 deletions packages/starknet-snap/src/rpcs/signTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
InvalidParamsError,
UserRejectedRequestError,
} from '@metamask/snaps-sdk';
import type { InvocationsSignerDetails } from 'starknet';
import { constants } from 'starknet';

import transactionExample from '../__tests__/fixture/transactionExample.json'; // Assuming you have a similar fixture
import type { SnapState } from '../types/snapState';
import { toJson } from '../utils';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import {
UserRejectedOpError,
InvalidRequestParamsError,
} from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import {
mockAccount,
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('signTransaction', () => {
expect(confirmDialogSpy).not.toHaveBeenCalled();
});

it('throws `UserRejectedRequestError` if user denied the operation', async () => {
it('throws `UserRejectedOpError` if user denied the operation', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const account = await mockAccount(chainId);
prepareMockAccount(account, state);
Expand All @@ -127,13 +127,13 @@ describe('signTransaction', () => {
const request = createRequestParam(chainId, account.address, true);

await expect(signTransaction.execute(request)).rejects.toThrow(
UserRejectedRequestError,
UserRejectedOpError,
);
});

it('throws `InvalidParamsError` when request parameter is not correct', async () => {
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => {
await expect(
signTransaction.execute({} as unknown as SignTransactionParams),
).rejects.toThrow(InvalidParamsError);
).rejects.toThrow(InvalidRequestParamsError);
});
});
Loading

0 comments on commit e61eb8b

Please sign in to comment.