Skip to content

Commit

Permalink
refactor(SNAP): rename file and clean up (#391)
Browse files Browse the repository at this point in the history
* chore: rename file

* chore: remove unused var and file

* fix: lint
  • Loading branch information
stanleyyconsensys authored Oct 18, 2024
1 parent 157b5ad commit 063b2a6
Show file tree
Hide file tree
Showing 20 changed files with 23 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
prepareMockAccount,
prepareConfirmDialog,
} from './__tests__/helper';
import { displayPrivateKey } from './displayPrivateKey';
import type { DisplayPrivateKeyParams } from './displayPrivateKey';
import { displayPrivateKey } from './display-private-key';
import type { DisplayPrivateKeyParams } from './display-private-key';

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { InvalidRequestParamsError } from '../utils/exceptions';
import * as starknetUtils from '../utils/starknetUtils';
import type { TxVersionStruct } from '../utils/superstruct';
import { mockAccount, prepareMockAccount } from './__tests__/helper';
import { estimateFee } from './estimateFee';
import type { EstimateFeeParams } from './estimateFee';
import { estimateFee } from './estimate-fee';
import type { EstimateFeeParams } from './estimate-fee';

jest.mock('../utils/snap');
jest.mock('../utils/logger');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
prepareConfirmDialog,
prepareMockAccount,
} from './__tests__/helper';
import type { ExecuteTxnParams } from './executeTxn';
import { executeTxn } from './executeTxn';
import type { ExecuteTxnParams } from './execute-txn';
import { executeTxn } from './execute-txn';

jest.mock('../utils/snap');
jest.mock('../utils/logger');
Expand Down
10 changes: 5 additions & 5 deletions packages/starknet-snap/src/rpcs/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './displayPrivateKey';
export * from './estimateFee';
export * from './executeTxn';
export * from './signMessage';
export * from './signTransaction';
export * from './display-private-key';
export * from './estimate-fee';
export * from './execute-txn';
export * from './sign-message';
export * from './sign-transaction';
export * from './sign-declare-transaction';
export * from './verify-signature';
export * from './switch-network';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
prepareMockAccount,
prepareConfirmDialog,
} from './__tests__/helper';
import { signMessage } from './signMessage';
import type { SignMessageParams } from './signMessage';
import { signMessage } from './sign-message';
import type { SignMessageParams } from './sign-message';

jest.mock('../utils/snap');
jest.mock('../utils/logger');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
prepareMockAccount,
prepareConfirmDialog,
} from './__tests__/helper';
import { signTransaction } from './signTransaction';
import type { SignTransactionParams } from './signTransaction';
import { signTransaction } from './sign-transaction';
import type { SignTransactionParams } from './sign-transaction';

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

Expand Down
35 changes: 0 additions & 35 deletions packages/starknet-snap/src/typedData/typedDataExample.json

This file was deleted.

64 changes: 2 additions & 62 deletions packages/starknet-snap/src/types/snapApi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import type { BIP44AddressKeyDeriver } from '@metamask/key-tree';
import type Mutex from 'async-mutex/lib/Mutex';
import type {
Abi,
Call,
InvocationsSignerDetails,
DeclareContractPayload,
InvocationsDetails,
Invocations,
EstimateFeeDetails,
DeployAccountSignerDetails,
DeclareSignerDetails,
typedData,
constants,
} from 'starknet';

Expand All @@ -31,27 +26,21 @@ export type ApiParamsWithKeyDeriver = ApiParams & {
export type ApiRequestParams =
| CreateAccountRequestParams
| GetStoredUserAccountsRequestParams
| ExtractPrivateKeyRequestParams
| ExtractPublicKeyRequestParams
| SignMessageRequestParams
| VerifySignedMessageRequestParams
| GetErc20TokenBalanceRequestParams
| GetTransactionStatusRequestParams
| SendTransactionRequestParams
| GetValueRequestParams
| EstimateFeeRequestParams
| EstimateAccountDeployFeeRequestParams
| AddErc20TokenRequestParams
| GetStoredErc20TokensRequestParams
| AddNetworkRequestParams
| GetStoredNetworksRequestParams
| GetStoredTransactionsRequestParams
| GetTransactionsRequestParams
| RecoverAccountsRequestParams
| ExecuteTxnRequestParams
| EstimateFeesRequestParams
| DeclareContractRequestParams
| SignTransactionRequestParams;
| DeclareContractRequestParams;

export type BaseRequestParams = {
chainId?: string;
Expand All @@ -74,26 +63,10 @@ export type GetStoredErc20TokensRequestParams = BaseRequestParams;

export type GetStoredNetworksRequestParams = Omit<BaseRequestParams, 'chainId'>;

export type ExtractPrivateKeyRequestParams = {
userAddress: string;
} & BaseRequestParams;

export type ExtractPublicKeyRequestParams = {
userAddress: string;
} & BaseRequestParams;

export type SignMessageRequestParams = {
typedDataMessage: typeof typedData.TypedData;
} & Authorizable &
SignRequestParams &
BaseRequestParams;

export type VerifySignedMessageRequestParams = {
signerAddress: string;
signature: string;
typedDataMessage?: string;
} & BaseRequestParams;

export type GetErc20TokenBalanceRequestParams = {
tokenAddress: string;
userAddress: string;
Expand Down Expand Up @@ -134,13 +107,6 @@ export type EstimateAccountDeployFeeRequestParams = {
addressIndex?: string | number;
} & BaseRequestParams;

export type AddErc20TokenRequestParams = {
tokenAddress: string;
tokenName: string;
tokenSymbol: string;
tokenDecimals?: string | number;
} & BaseRequestParams;

export type AddNetworkRequestParams = {
networkName: string;
networkChainId: string;
Expand Down Expand Up @@ -172,13 +138,6 @@ export type RecoverAccountsRequestParams = {
maxMissed?: string | number;
} & BaseRequestParams;

export type ExecuteTxnRequestParams = {
senderAddress: string;
txnInvocation: Call | Call[];
abis?: Abi[];
invocationsDetails?: InvocationsDetails;
} & BaseRequestParams;

export type EstimateFeesRequestParams = {
senderAddress: string;
invocations: Invocations;
Expand All @@ -205,30 +164,12 @@ export type SignRequestParams = {
signerAddress: string;
};

export type SignTransactionRequestParams = {
transactions: Call[];
transactionsDetail: InvocationsSignerDetails;
} & Authorizable &
SignRequestParams &
BaseRequestParams;

export type SignDeployAccountTransactionRequestParams = {
transaction: DeployAccountSignerDetails;
} & Authorizable &
SignRequestParams &
BaseRequestParams;

export type SignDeclareTransactionRequestParams = {
transaction: DeclareSignerDetails;
} & Authorizable &
SignRequestParams &
BaseRequestParams;

export type SwitchNetworkRequestParams = {
chainId: string;
} & Authorizable &
BaseRequestParams;

export type GetStarkNameRequestParam = {
userAddress: string;
} & BaseRequestParams;
Expand All @@ -242,5 +183,4 @@ export enum FeeTokenUnit {
ETH = 'wei',
STRK = 'fri',
}

/* eslint-disable */
/* eslint-enable */
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mapDeprecatedParams } from './formatterUtils';
import { mapDeprecatedParams } from './formatter-utils';

describe('mapDeprecatedParams', () => {
it('maps deprecated parameters to their new equivalents', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from './snap';
export * from './serializer';
export * from './superstruct';
export * from './logger';
export * from './formatterUtils';
export * from './formatter-utils';
export * from './snap-state';
export * from './url';
export * from './string';
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import {
BlockIdentifierEnum,
} from './constants';
import { DeployRequiredError, UpgradeRequiredError } from './exceptions';
import { hexToString } from './formatterUtils';
import { hexToString } from './formatter-utils';
import { getAddressKey } from './keyPair';
import { logger } from './logger';
import { toJson } from './serializer';
Expand Down
52 changes: 1 addition & 51 deletions packages/starknet-snap/src/utils/superstruct.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { constants, TransactionType } from 'starknet';
import { StructError, assert, object, number, string } from 'superstruct';
import { StructError, assert } from 'superstruct';

import contractExample from '../__tests__/fixture/contract-example.json';
import transactionExample from '../__tests__/fixture/transactionExample.json';
Expand All @@ -16,7 +16,6 @@ import {
BaseRequestStruct,
CairoVersionStruct,
CallDataStruct,
createStructWithAdditionalProperties,
DeclareSignDetailsStruct,
EDataModeStruct,
NumberStringStruct,
Expand Down Expand Up @@ -387,55 +386,6 @@ describe('DeclareSignDetailsStruct', () => {
});
});

describe('createStructWithAdditionalProperties', () => {
const predefinedProperties = object({
name: string(),
age: number(),
});

const additionalPropertyTypes = string(); // Additional properties should be strings
const ExtendedPropStruct = createStructWithAdditionalProperties(
predefinedProperties,
additionalPropertyTypes,
);
it('should validate predefined properties correctly', () => {
const validData = {
name: 'John',
age: 30,
};
const [error, result] = ExtendedPropStruct.validate(validData);

expect(error).toBeUndefined();
expect(result).toStrictEqual(validData);
});

it('should validate additional properties correctly', () => {
const validDataWithExtra = {
name: 'John',
age: 30,
nickname: 'Johnny',
};

const [error, result] = ExtendedPropStruct.validate(validDataWithExtra);

expect(error).toBeUndefined();
expect(result).toStrictEqual(validDataWithExtra);
});

it('should fail validation if additional properties are of the wrong type', () => {
const invalidData = {
name: 'John',
age: 30,
nickname: 12345, // Invalid type for additional property
};

const [error] = ExtendedPropStruct.validate(invalidData);

expect(error).toBeDefined();
expect(error?.message).toContain('Expected a string, but received');
});
});

describe('InvocationsStruct', () => {
it.each([
{
Expand Down
Loading

0 comments on commit 063b2a6

Please sign in to comment.