Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new RPC starkNet_switchAccount #478

Merged
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
797861f
feat: add account service
stanleyyconsensys Jan 7, 2025
36358e1
chore: fix lint
stanleyyconsensys Jan 7, 2025
2dc68ae
chore: update account contract discovery logic
stanleyyconsensys Jan 8, 2025
17b25fc
fix: code comment
stanleyyconsensys Jan 8, 2025
cc1efa4
chore: add discovery logic description
stanleyyconsensys Jan 8, 2025
a31b9c0
fix: lint
stanleyyconsensys Jan 8, 2025
fa288a4
feat: add account service factory
stanleyyconsensys Jan 9, 2025
f216dfb
fix: rename deployPayload
stanleyyconsensys Jan 9, 2025
40d3113
Merge branch 'feat/add-account-service-factory' into refactor/adopt-a…
stanleyyconsensys Jan 9, 2025
871156b
chore: adopt account discovery in RPCs
stanleyyconsensys Jan 9, 2025
21952f3
chore: update execute txn test
stanleyyconsensys Jan 9, 2025
4ec0c4f
fix: execute test
stanleyyconsensys Jan 9, 2025
883a3e6
fix: account discovery bug
stanleyyconsensys Jan 9, 2025
c92750a
fix: discovery logic
stanleyyconsensys Jan 9, 2025
5bc7498
feat: add `AddAccount` RPC
stanleyyconsensys Jan 10, 2025
0ebb0fe
feat: add max account create limit
stanleyyconsensys Jan 10, 2025
d2274af
fix: add `isMaxAccountLimitExceeded` unit test
stanleyyconsensys Jan 10, 2025
2cb0633
fix: account deploy require result
stanleyyconsensys Jan 13, 2025
f579332
Merge branch 'refactor/adopt-account-discovery' into feat/add-account…
stanleyyconsensys Jan 13, 2025
29396ff
feat: add get current account RPC
stanleyyconsensys Jan 13, 2025
c6e4eaf
feat: add list accounts rpc
stanleyyconsensys Jan 13, 2025
391d197
fix: lint
stanleyyconsensys Jan 13, 2025
80af8c4
feat: add swtich account rpc
stanleyyconsensys Jan 13, 2025
a6e56b2
fix: add some detail comment on contract discovery
stanleyyconsensys Jan 14, 2025
cf01c6c
fix: comments on test title
stanleyyconsensys Jan 14, 2025
006284b
Merge branch 'feat/enable-multiple-accounts' into refactor/adopt-acco…
khanti42 Jan 14, 2025
2f37260
Merge branch 'refactor/adopt-account-discovery' into feat/add-account…
khanti42 Jan 14, 2025
0c69311
Merge branch 'feat/add-account-rpc' into feat/get-current-account-rpc
khanti42 Jan 14, 2025
4890410
Merge branch 'feat/get-current-account-rpc' into feat/list-accounts-rpc
khanti42 Jan 14, 2025
a7e5b2d
Merge branch 'feat/list-accounts-rpc' into feat/switch-account-rpc
khanti42 Jan 14, 2025
c1ecca1
Merge branch 'feat/enable-multiple-accounts' into feat/switch-account…
stanleyyconsensys Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/starknet-snap/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export type SnapConfig = {
txnsInLastNumOfDays: number;
};
};
account: {
maxAccountToCreate: number;
};
};

export enum DataClient {
Expand All @@ -61,6 +64,10 @@ export const Config: SnapConfig = {
},
},

account: {
maxAccountToCreate: 2,
},

// eslint-disable-next-line no-restricted-globals
rpcApiKey: process.env.DIN_API_KEY ?? '',

Expand Down
34 changes: 0 additions & 34 deletions packages/starknet-snap/src/getStoredUserAccounts.ts

This file was deleted.

28 changes: 26 additions & 2 deletions packages/starknet-snap/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { getErc20TokenBalance } from './getErc20TokenBalance';
import { getStarkName } from './getStarkName';
import { getStoredErc20Tokens } from './getStoredErc20Tokens';
import { getStoredNetworks } from './getStoredNetworks';
import { getStoredUserAccounts } from './getStoredUserAccounts';
import { getValue } from './getValue';
import { homePageController } from './on-home-page';
import { recoverAccounts } from './recoverAccounts';
Expand All @@ -37,6 +36,10 @@ import type {
GetAddrFromStarkNameParams,
GetTransactionStatusParams,
ListTransactionsParams,
AddAccountParams,
GetCurrentAccountParams,
ListAccountsParams,
SwitchAccountParams,
} from './rpcs';
import {
displayPrivateKey,
Expand All @@ -53,6 +56,10 @@ import {
getAddrFromStarkName,
getTransactionStatus,
listTransactions,
addAccount,
getCurrentAccount,
listAccounts,
switchAccount,
} from './rpcs';
import { signDeployAccountTransaction } from './signDeployAccountTransaction';
import type {
Expand Down Expand Up @@ -164,7 +171,9 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
);

case RpcMethod.ListAccounts:
return await getStoredUserAccounts(apiParams);
return await listAccounts.execute(
requestParams as unknown as ListAccountsParams,
);

case RpcMethod.DisplayPrivateKey:
return await displayPrivateKey.execute(
Expand Down Expand Up @@ -279,6 +288,21 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
apiParams.requestParams as unknown as GetAddrFromStarkNameParams,
);

case RpcMethod.AddAccount:
return await addAccount.execute(
requestParams as unknown as AddAccountParams,
);

case RpcMethod.GetCurrentAccount:
return await getCurrentAccount.execute(
requestParams as unknown as GetCurrentAccountParams,
);

case RpcMethod.SwitchAccount:
return await switchAccount.execute(
requestParams as unknown as SwitchAccountParams,
);

default:
throw new MethodNotFoundError() as unknown as Error;
}
Expand Down
95 changes: 65 additions & 30 deletions packages/starknet-snap/src/rpcs/__tests__/helper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { BigNumber } from 'ethers';
import type { constants } from 'starknet';

import type { StarknetAccount } from '../../__tests__/helper';
import { generateAccounts, generateRandomValue } from '../../__tests__/helper';
import { FeeTokenUnit } from '../../types/snapApi';
import type { SnapState } from '../../types/snapState';
import type { Network } from '../../types/snapState';
import * as snapUiUtils from '../../ui/utils';
import { getExplorerUrl, shortenAddress, toJson } from '../../utils';
import { mockEstimateFeeBulkResponse } from '../../utils/__tests__/helper';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../../utils/constants';
import * as snapHelper from '../../utils/snap';
import * as snapUtils from '../../utils/snapUtils';
import * as starknetUtils from '../../utils/starknetUtils';
import { AccountService, CairoAccountContract } from '../../wallet/account';
import { createAccountObject } from '../../wallet/account/__test__/helper';
import type { Account } from '../../wallet/account/account';

/**
*
Expand All @@ -24,36 +26,69 @@ export async function mockAccount(chainId: constants.StarknetChainId | string) {
/**
*
* @param account
* @param state
* @param account.accountObj
* @param account.network
* @param account.requireUpgrade
* @param account.requireDeploy
* @param account.isDeployed
*/
export function prepareMockAccount(account: StarknetAccount, state: SnapState) {
const getStateDataSpy = jest.spyOn(snapHelper, 'getStateData');
const verifyIfAccountNeedUpgradeOrDeploySpy = jest.spyOn(
snapUtils,
'verifyIfAccountNeedUpgradeOrDeploy',
export async function setupAccountController({
accountObj,
network = STARKNET_SEPOLIA_TESTNET_NETWORK,
requireUpgrade = false,
requireDeploy = false,
isDeployed = false,
}: {
accountObj?: Account;
network?: Network;
requireUpgrade?: boolean;
requireDeploy?: boolean;
isDeployed?: boolean;
}) {
const account = accountObj ?? (await createAccountObject(network)).accountObj;

// Mock the `accountContract` properties in the `Account` object
const isRequireUpgradeSpy = jest.spyOn(
CairoAccountContract.prototype,
'isRequireUpgrade',
);
const isRequireDeploySpy = jest.spyOn(
CairoAccountContract.prototype,
'isRequireDeploy',
);
const getKeysFromAddressSpy = jest.spyOn(starknetUtils, 'getKeysFromAddress');
const isDeploySpy = jest.spyOn(CairoAccountContract.prototype, 'isDeployed');

getKeysFromAddressSpy.mockResolvedValue({
privateKey: account.privateKey,
publicKey: account.publicKey,
addressIndex: account.addressIndex,
derivationPath: account.derivationPath as unknown as any,
});
isRequireUpgradeSpy.mockResolvedValue(requireUpgrade);
isRequireDeploySpy.mockResolvedValue(requireDeploy);
isDeploySpy.mockResolvedValue(isDeployed);

const deriveAccountByAddressSpy = jest.spyOn(
AccountService.prototype,
'deriveAccountByAddress',
);

const deriveAccountByIndexSpy = jest.spyOn(
AccountService.prototype,
'deriveAccountByIndex',
);

verifyIfAccountNeedUpgradeOrDeploySpy.mockReturnThis();
getStateDataSpy.mockResolvedValue(state);
deriveAccountByAddressSpy.mockResolvedValue(account);
deriveAccountByIndexSpy.mockResolvedValue(account);

return {
getKeysFromAddressSpy,
verifyIfAccountNeedUpgradeOrDeploySpy,
deriveAccountByAddressSpy,
deriveAccountByIndexSpy,
isRequireDeploySpy,
isRequireUpgradeSpy,
isDeploySpy,
account,
};
}

/**
*
*/
export function prepareConfirmDialog() {
export function mockConfirmDialog() {
const confirmDialogSpy = jest.spyOn(snapHelper, 'confirmDialog');
confirmDialogSpy.mockResolvedValue(true);
return {
Expand All @@ -64,7 +99,7 @@ export function prepareConfirmDialog() {
/**
*
*/
export function prepareRenderWatchAssetUI() {
export function mockRenderWatchAssetUI() {
const confirmDialogSpy = jest.spyOn(snapUiUtils, 'renderWatchAssetUI');
confirmDialogSpy.mockResolvedValue(true);
return {
Expand All @@ -75,7 +110,7 @@ export function prepareRenderWatchAssetUI() {
/**
*
*/
export function prepareRenderSwitchNetworkUI() {
export function mockRenderSwitchNetworkUI() {
const confirmDialogSpy = jest.spyOn(snapUiUtils, 'renderSwitchNetworkUI');
confirmDialogSpy.mockResolvedValue(true);
return {
Expand All @@ -86,7 +121,7 @@ export function prepareRenderSwitchNetworkUI() {
/**
*
*/
export function prepareRenderSignMessageUI() {
export function mockRenderSignMessageUI() {
const confirmDialogSpy = jest.spyOn(snapUiUtils, 'renderSignMessageUI');
confirmDialogSpy.mockResolvedValue(true);
return {
Expand All @@ -97,7 +132,7 @@ export function prepareRenderSignMessageUI() {
/**
*
*/
export function prepareRenderSignTransactionUI() {
export function mockRenderSignTransactionUI() {
const confirmDialogSpy = jest.spyOn(snapUiUtils, 'renderSignTransactionUI');
confirmDialogSpy.mockResolvedValue(true);
return {
Expand All @@ -108,7 +143,7 @@ export function prepareRenderSignTransactionUI() {
/**
*
*/
export function prepareRenderSignDeclareTransactionUI() {
export function mockRenderSignDeclareTransactionUI() {
const confirmDialogSpy = jest.spyOn(
snapUiUtils,
'renderSignDeclareTransactionUI',
Expand All @@ -122,7 +157,7 @@ export function prepareRenderSignDeclareTransactionUI() {
/**
*
*/
export function prepareRenderDisplayPrivateKeyConfirmUI() {
export function mockRenderDisplayPrivateKeyConfirmUI() {
const confirmDialogSpy = jest.spyOn(
snapUiUtils,
'renderDisplayPrivateKeyConfirmUI',
Expand All @@ -136,7 +171,7 @@ export function prepareRenderDisplayPrivateKeyConfirmUI() {
/**
*
*/
export function prepareRenderDisplayPrivateKeyAlertUI() {
export function mockRenderDisplayPrivateKeyAlertUI() {
const alertDialogSpy = jest.spyOn(
snapUiUtils,
'renderDisplayPrivateKeyAlertUI',
Expand All @@ -150,7 +185,7 @@ export function prepareRenderDisplayPrivateKeyAlertUI() {
*
* @param result
*/
export function prepareConfirmDialogInteractiveUI(result = true) {
export function mockConfirmDialogInteractiveUI(result = true) {
const confirmDialogSpy = jest.spyOn(
snapHelper,
'createInteractiveConfirmDialog',
Expand All @@ -164,7 +199,7 @@ export function prepareConfirmDialogInteractiveUI(result = true) {
/**
*
*/
export function prepareAlertDialog() {
export function mockAlertDialog() {
const alertDialogSpy = jest.spyOn(snapHelper, 'alertDialog');
alertDialogSpy.mockResolvedValue(true);
return {
Expand Down
Loading
Loading