Skip to content

Commit

Permalink
Merge branch 'main' into chore/refactor-txn-mgr
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 authored Dec 5, 2024
2 parents 6391e81 + c9e2c64 commit 873a1de
Show file tree
Hide file tree
Showing 22 changed files with 698 additions and 437 deletions.
34 changes: 34 additions & 0 deletions packages/starknet-snap/openrpc/starknet_snap_api_openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,40 @@
}
},
"errors": []
},
{
"name": "starkNet_getAddrFromStarkName",
"summary": "Get address from a stark name",
"paramStructure": "by-name",
"params": [
{
"name": "starkName",
"summary": "stark name of the user",
"description": "stark name of the user",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "chainId",
"summary": "Id of the target Starknet network",
"description": "Id of the target Starknet network (default to Starknet Goerli Testnet)",
"required": false,
"schema": {
"$ref": "#/components/schemas/CHAIN_ID"
}
}
],
"result": {
"name": "result",
"summary": "Address of the given stark name",
"description": "Address of the given stark name",
"schema": {
"$ref": "#/components/schemas/ADDRESS"
}
},
"errors": []
}
],
"components": {
Expand Down
35 changes: 0 additions & 35 deletions packages/starknet-snap/src/getTransactionStatus.ts

This file was deleted.

5 changes: 5 additions & 0 deletions packages/starknet-snap/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { onHomePage, onRpcRequest } from '.';
import * as createAccountApi from './createAccount';
import { HomePageController } from './on-home-page';
import * as keyPairUtils from './utils/keyPair';
import * as permissionUtil from './utils/permission';

jest.mock('./utils/logger');

Expand Down Expand Up @@ -41,7 +42,11 @@ describe('onRpcRequest', () => {
expect(createAccountSpy).toHaveBeenCalledTimes(1);
});

// It is a never case, as the permission of each method is checked in the `validateOrigin` function.
// But to increase the coverage, we keep this test case.
it('throws `MethodNotFoundError` if the request method not found', async () => {
jest.spyOn(permissionUtil, 'validateOrigin').mockReturnThis();

await expect(
onRpcRequest({
...createMockRequest(),
Expand Down
89 changes: 45 additions & 44 deletions packages/starknet-snap/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ import type {
import { MethodNotFoundError } from '@metamask/snaps-sdk';
import { Box, Link, Text } from '@metamask/snaps-sdk/jsx';

import { addNetwork } from './addNetwork';
import { Config } from './config';
import { createAccount } from './createAccount';
import { estimateAccDeployFee } from './estimateAccountDeployFee';
import { extractPublicKey } from './extractPublicKey';
import { getCurrentNetwork } from './getCurrentNetwork';
import { getErc20TokenBalance } from './getErc20TokenBalance';
import { getStarkName } from './getStarkName';
import { getStoredErc20Tokens } from './getStoredErc20Tokens';
import { getStoredNetworks } from './getStoredNetworks';
import { getStoredTransactions } from './getStoredTransactions';
import { getStoredUserAccounts } from './getStoredUserAccounts';
import { getTransactions } from './getTransactions';
import { getTransactionStatus } from './getTransactionStatus';
import { getValue } from './getValue';
import { homePageController } from './on-home-page';
import { recoverAccounts } from './recoverAccounts';
Expand All @@ -39,6 +35,8 @@ import type {
GetDeploymentDataParams,
DeclareContractParams,
WatchAssetParams,
GetAddrFromStarkNameParams,
GetTransactionStatusParams,
} from './rpcs';
import {
displayPrivateKey,
Expand All @@ -52,6 +50,8 @@ import {
switchNetwork,
getDeploymentData,
watchAsset,
getAddrFromStarkName,
getTransactionStatus,
} from './rpcs';
import { signDeployAccountTransaction } from './signDeployAccountTransaction';
import type {
Expand Down Expand Up @@ -80,6 +80,7 @@ import { UnknownError } from './utils/exceptions';
import { getAddressKeyDeriver } from './utils/keyPair';
import { acquireLock } from './utils/lock';
import { logger } from './utils/logger';
import { RpcMethod, validateOrigin } from './utils/permission';
import { toJson } from './utils/serializer';
import {
upsertErc20Token,
Expand All @@ -90,12 +91,17 @@ import {
declare const snap;
logger.logLevel = parseInt(Config.logLevel, 10);

export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
request,
}) => {
const requestParams = request?.params as unknown as ApiRequestParams;

logger.log(`${request.method}:\nrequestParams: ${toJson(requestParams)}`);

try {
validateOrigin(origin, request.method);

if (request.method === 'ping') {
logger.log('pong');
return 'pong';
Expand Down Expand Up @@ -141,13 +147,13 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
};

switch (request.method) {
case 'starkNet_createAccount':
case RpcMethod.CreateAccount:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await createAccount(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_createAccountLegacy':
case RpcMethod.DeployCario0Account:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await createAccount(
apiParams as unknown as ApiParamsWithKeyDeriver,
Expand All @@ -156,125 +162,120 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
CAIRO_VERSION_LEGACY,
);

case 'starkNet_getStoredUserAccounts':
case RpcMethod.ListAccounts:
return await getStoredUserAccounts(apiParams);

case 'starkNet_displayPrivateKey':
case RpcMethod.DisplayPrivateKey:
return await displayPrivateKey.execute(
apiParams.requestParams as unknown as DisplayPrivateKeyParams,
);

case 'starkNet_extractPublicKey':
case RpcMethod.ExtractPublicKey:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await extractPublicKey(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_signMessage':
case RpcMethod.SignMessage:
return await signMessage.execute(
apiParams.requestParams as unknown as SignMessageParams,
);

case 'starkNet_signTransaction':
case RpcMethod.SignTransaction:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await signTransaction.execute(
apiParams.requestParams as unknown as SignTransactionParams,
);

case 'starkNet_signDeclareTransaction':
case RpcMethod.SignDeclareTransaction:
return await signDeclareTransaction.execute(
apiParams.requestParams as unknown as SignDeclareTransactionParams,
);

case 'starkNet_signDeployAccountTransaction':
case RpcMethod.SignDeployAccountTransaction:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await signDeployAccountTransaction(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_verifySignedMessage':
case RpcMethod.VerifySignedMessage:
return await verifySignature.execute(
apiParams.requestParams as unknown as VerifySignatureParams,
);

case 'starkNet_getErc20TokenBalance':
case RpcMethod.GetErc20TokenBalance:
return await getErc20TokenBalance(apiParams);

case 'starkNet_getTransactionStatus':
return await getTransactionStatus(apiParams);
case RpcMethod.GetTransactionStatus:
return await getTransactionStatus.execute(
apiParams.requestParams as unknown as GetTransactionStatusParams,
);

case 'starkNet_getValue':
case RpcMethod.ReadContract:
return await getValue(apiParams);

case 'starkNet_estimateFee':
case RpcMethod.EstimateFee:
return await estimateFee.execute(
apiParams.requestParams as unknown as EstimateFeeParams,
);

case 'starkNet_estimateAccountDeployFee':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await estimateAccDeployFee(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_addErc20Token':
case RpcMethod.AddErc20Token:
return await watchAsset.execute(
apiParams.requestParams as unknown as WatchAssetParams,
);

case 'starkNet_getStoredErc20Tokens':
case RpcMethod.GetStoredErc20Tokens:
return await getStoredErc20Tokens(apiParams);

case 'starkNet_addNetwork':
return await addNetwork(apiParams);

case 'starkNet_switchNetwork':
case RpcMethod.SwitchNetwork:
return await switchNetwork.execute(
apiParams.requestParams as unknown as SwitchNetworkParams,
);

case 'starkNet_getCurrentNetwork':
case RpcMethod.GetCurrentNetwork:
return await getCurrentNetwork(apiParams);

case 'starkNet_getStoredNetworks':
case RpcMethod.GetStoredNetworks:
return await getStoredNetworks(apiParams);

case 'starkNet_getStoredTransactions':
return await getStoredTransactions(apiParams);

case 'starkNet_getTransactions':
case RpcMethod.GetTransactions:
return await getTransactions(apiParams);

case 'starkNet_recoverAccounts':
case RpcMethod.RecoverAccounts:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await recoverAccounts(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_executeTxn':
case RpcMethod.ExecuteTxn:
return await executeTxn.execute(
apiParams.requestParams as unknown as ExecuteTxnParams,
);

case 'starkNet_upgradeAccContract':
case RpcMethod.UpgradeAccContract:
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return upgradeAccContract(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_declareContract':
case RpcMethod.DeclareContract:
return await declareContract.execute(
apiParams.requestParams as unknown as DeclareContractParams,
);

case 'starkNet_getStarkName':
case RpcMethod.GetStarkName:
return await getStarkName(apiParams);

case 'starkNet_getDeploymentData':
case RpcMethod.GetDeploymentData:
return await getDeploymentData.execute(
apiParams.requestParams as unknown as GetDeploymentDataParams,
);

case RpcMethod.GetAddressByStarkName:
return await getAddrFromStarkName.execute(
apiParams.requestParams as unknown as GetAddrFromStarkNameParams,
);

default:
throw new MethodNotFoundError() as unknown as Error;
}
Expand Down
58 changes: 58 additions & 0 deletions packages/starknet-snap/src/rpcs/get-addr-from-starkname.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { constants } from 'starknet';

import { InvalidRequestParamsError } from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import {
getAddrFromStarkName,
type GetAddrFromStarkNameParams,
} from './get-addr-from-starkname';

jest.mock('../utils/snap');
jest.mock('../utils/logger');

const prepareMockGetAddrFromStarkName = ({
chainId,
starkName,
}: {
chainId: constants.StarknetChainId;
starkName: string;
}) => {
const request = {
chainId,
starkName,
} as unknown as GetAddrFromStarkNameParams;

const getAddrFromStarkNameSpy = jest.spyOn(
starknetUtils,
'getAddrFromStarkNameUtil',
);
getAddrFromStarkNameSpy.mockResolvedValue(
'0x01c744953f1d671673f46a9179a58a7e58d9299499b1e076cdb908e7abffe69f',
);

return {
request,
};
};

describe('getAddrFromStarkName', () => {
it('get address from stark name correctly', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const { request } = prepareMockGetAddrFromStarkName({
chainId,
starkName: 'testname.stark',
});

const result = await getAddrFromStarkName.execute(request);

expect(result).toBe(
'0x01c744953f1d671673f46a9179a58a7e58d9299499b1e076cdb908e7abffe69f',
);
});

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

0 comments on commit 873a1de

Please sign in to comment.