Skip to content

Commit

Permalink
chore: maintain the naming convention for get single entity in all st…
Browse files Browse the repository at this point in the history
…ate manager (#331)

* chore: rename all state mgr find entity func name

* chore: move test utils to __tests__

* chore: fix sonarcloud comment
  • Loading branch information
stanleyyconsensys authored Aug 23, 2024
1 parent 3b0dbd6 commit b0d6151
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {
BIP44CoinTypeNode,
getBIP44AddressKeyDeriver,
} from '@metamask/key-tree';
import { generateMnemonic } from 'bip39';
import { getRandomValues } from 'crypto';
import type { constants } from 'starknet';
import {
ec,
constants,
CallData,
hash,
type Calldata,
Expand All @@ -10,18 +15,15 @@ import {
TransactionExecutionStatus,
TransactionType,
} from 'starknet';
import {
BIP44CoinTypeNode,
getBIP44AddressKeyDeriver,
} from '@metamask/key-tree';
import { AccContract, Transaction } from '../src/types/snapState';

import type { AccContract, Transaction } from '../types/snapState';
import {
ACCOUNT_CLASS_HASH,
ACCOUNT_CLASS_HASH_LEGACY,
PRELOADED_TOKENS,
PROXY_CONTRACT_HASH,
} from '../src/utils/constants';
import { grindKey } from '../src/utils/keyPair';
} from '../utils/constants';
import { grindKey } from '../utils/keyPair';

/* eslint-disable */
export type StarknetAccount = AccContract & {
Expand All @@ -30,6 +32,23 @@ export type StarknetAccount = AccContract & {

/* eslint-disable */

/**
* Using pseudorandom number generators (PRNGs) to generate a security-sensitive random value that recommended by sonarcloud.
* It has led in the past to the following vulnerabilities:
* - https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386
* - https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419
* - https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102
*
* @returns An random number.
*/
export function generateRandomValue() {
// max value of 32 bit signed integer
const maxU32 = 2 ** 32;
const u32Arr = new Uint32Array(1);
// by dividing the random value by maxU32, we get a decimal number between 0 and 1, which is the same as Math.random()
return getRandomValues(u32Arr)[0] / maxU32;
}

/**
* Method to generate Bip44 Entropy.
*
Expand Down Expand Up @@ -212,15 +231,23 @@ export function generateTransactions({

for (let i = 1; i <= createCnt; i++) {
const randomContractAddress =
contractAddresses[Math.floor(Math.random() * contractAddresses.length)];
contractAddresses[
Math.floor(generateRandomValue() * contractAddresses.length)
];
const randomTxnType =
filteredTxnTypes[Math.floor(Math.random() * filteredTxnTypes.length)];
filteredTxnTypes[
Math.floor(generateRandomValue() * filteredTxnTypes.length)
];
let randomFinalityStatus =
finalityStatuses[Math.floor(Math.random() * finalityStatuses.length)];
finalityStatuses[
Math.floor(generateRandomValue() * finalityStatuses.length)
];
let randomExecutionStatus =
executionStatuses[Math.floor(Math.random() * executionStatuses.length)];
executionStatuses[
Math.floor(generateRandomValue() * executionStatuses.length)
];
let randomContractFuncName = ['transfer', 'upgrade'][
Math.floor(Math.random() * 2)
Math.floor(generateRandomValue() * 2)
];
accumulatedTimestamp += i * 100;
accumulatedTxnHash += BigInt(i * 100);
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { constants } from 'starknet';

import { onRpcRequest, onHomePage } from '.';
import { manageStateSpy } from '../test/snap-provider.mock';
import { generateAccounts, type StarknetAccount } from '../test/utils';
import { generateAccounts, type StarknetAccount } from './__tests__/helper';
import * as createAccountApi from './createAccount';
import type { SnapState } from './types/snapState';
import {
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/src/rpcs/__tests__/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { constants } from 'starknet';

import type { StarknetAccount } from '../../../test/utils';
import { generateAccounts } from '../../../test/utils';
import type { StarknetAccount } from '../../__tests__/helper';
import { generateAccounts } from '../../__tests__/helper';
import type { SnapState } from '../../types/snapState';
import * as snapHelper from '../../utils/snap';
import * as snapUtils from '../../utils/snapUtils';
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/state/__tests__/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { constants } from 'starknet';

import { generateAccounts, type StarknetAccount } from '../../../test/utils';
import { generateAccounts, type StarknetAccount } from '../../__tests__/helper';
import type { Erc20Token, Network, Transaction } from '../../types/snapState';
import * as snapHelper from '../../utils/snap';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './account-state-manager';

describe('AccountStateManager', () => {
describe('findAccount', () => {
describe('getAccount', () => {
it('returns the account', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const accountsInTestnet = await mockAcccounts(chainId);
Expand All @@ -20,7 +20,7 @@ describe('AccountStateManager', () => {
});

const stateManager = new AccountStateManager();
const result = await stateManager.findAccount({
const result = await stateManager.getAccount({
address: accountsInTestnet[0].address,
chainId,
});
Expand All @@ -36,7 +36,7 @@ describe('AccountStateManager', () => {
});

const stateManager = new AccountStateManager();
const result = await stateManager.findAccount({
const result = await stateManager.getAccount({
address: accountNotExist.address,
chainId,
});
Expand All @@ -52,7 +52,7 @@ describe('AccountStateManager', () => {
});

const stateManager = new AccountStateManager();
const result = await stateManager.findAccount({
const result = await stateManager.getAccount({
address: accounts[0].address,
chainId: constants.StarknetChainId.SN_MAIN,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/starknet-snap/src/state/account-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class AccountStateManager extends StateManager<AccContract> {
* @param [state] - The optional SnapState object.
* @returns A Promise that resolves with the matching AccContract object if found, or null if not found.
*/
async findAccount(
async getAccount(
{
address,
chainId,
Expand All @@ -68,7 +68,7 @@ export class AccountStateManager extends StateManager<AccContract> {
async updateAccount(data: AccContract): Promise<void> {
try {
await this.update(async (state: SnapState) => {
const accountInState = await this.findAccount(
const accountInState = await this.getAccount(
{
address: data.address,
chainId: data.chainId,
Expand Down Expand Up @@ -98,7 +98,7 @@ export class AccountStateManager extends StateManager<AccContract> {
async addAccount(data: AccContract): Promise<void> {
try {
await this.update(async (state: SnapState) => {
const accountInState = await this.findAccount(
const accountInState = await this.getAccount(
{
address: data.address,
chainId: data.chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { NetworkStateManager, ChainIdFilter } from './network-state-manager';
import { StateManagerError } from './state-manager';

describe('NetworkStateManager', () => {
describe('findNetwork', () => {
describe('getNetwork', () => {
it('returns the network', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
await mockState({
networks: [STARKNET_MAINNET_NETWORK, STARKNET_SEPOLIA_TESTNET_NETWORK],
});

const stateManager = new NetworkStateManager();
const result = await stateManager.findNetwork({
const result = await stateManager.getNetwork({
chainId,
});

Expand All @@ -32,7 +32,7 @@ describe('NetworkStateManager', () => {
});

const stateManager = new NetworkStateManager();
const result = await stateManager.findNetwork({
const result = await stateManager.getNetwork({
chainId,
});

Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/src/state/network-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class NetworkStateManager extends StateManager<Network> {
* @param [state] - The optional SnapState object.
* @returns A Promise that resolves with the Network object if found, or null if not found.
*/
async findNetwork(
async getNetwork(
{
chainId,
}: {
Expand All @@ -88,7 +88,7 @@ export class NetworkStateManager extends StateManager<Network> {
async updateNetwork(data: Network): Promise<void> {
try {
await this.update(async (state: SnapState) => {
const dataInState = await this.findNetwork(
const dataInState = await this.getNetwork(
{
chainId: data.chainId,
},
Expand Down
8 changes: 4 additions & 4 deletions packages/starknet-snap/src/state/token-state-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { StateManagerError } from './state-manager';
import { TokenStateManager, ChainIdFilter } from './token-state-manager';

describe('TokenStateManager', () => {
describe('findToken', () => {
describe('getToken', () => {
it('returns the token', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
await mockState({
tokens: [ETHER_MAINNET, ETHER_SEPOLIA_TESTNET],
});

const stateManager = new TokenStateManager();
const result = await stateManager.findToken({
const result = await stateManager.getToken({
chainId,
address: ETHER_SEPOLIA_TESTNET.address,
});
Expand All @@ -35,7 +35,7 @@ describe('TokenStateManager', () => {
});

const stateManager = new TokenStateManager();
const result = await stateManager.findToken({
const result = await stateManager.getToken({
chainId,
address: ETHER_SEPOLIA_TESTNET.address,
});
Expand All @@ -50,7 +50,7 @@ describe('TokenStateManager', () => {
});

const stateManager = new TokenStateManager();
const result = await stateManager.findToken({
const result = await stateManager.getToken({
chainId,
address: ETHER_SEPOLIA_TESTNET.address,
});
Expand Down
10 changes: 5 additions & 5 deletions packages/starknet-snap/src/state/token-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class TokenStateManager extends StateManager<Erc20Token> {
* @param [state] - The optional SnapState object.
* @returns A Promise that resolves with the Erc20Token object if found, or null if not found.
*/
async findToken(
async getToken(
{
address,
chainId,
Expand Down Expand Up @@ -116,7 +116,7 @@ export class TokenStateManager extends StateManager<Erc20Token> {
async updateToken(data: Erc20Token): Promise<void> {
try {
await this.update(async (state: SnapState) => {
const dataInState = await this.findToken(
const dataInState = await this.getToken(
{
address: data.address,
chainId: data.chainId,
Expand Down Expand Up @@ -145,7 +145,7 @@ export class TokenStateManager extends StateManager<Erc20Token> {
async addToken(data: Erc20Token): Promise<void> {
try {
await this.update(async (state: SnapState) => {
const dataInState = await this.findToken(
const dataInState = await this.getToken(
{
address: data.address,
chainId: data.chainId,
Expand Down Expand Up @@ -183,7 +183,7 @@ export class TokenStateManager extends StateManager<Erc20Token> {
if (!address) {
return null;
}
return await this.findToken(
return await this.getToken(
{
address,
chainId,
Expand Down Expand Up @@ -212,7 +212,7 @@ export class TokenStateManager extends StateManager<Erc20Token> {
if (!address) {
return null;
}
return await this.findToken(
return await this.getToken(
{
address,
chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
TransactionExecutionStatus,
} from 'starknet';

import { generateTransactions } from '../../test/utils';
import { generateTransactions } from '../__tests__/helper';
import { PRELOADED_TOKENS } from '../utils/constants';
import { mockAcccounts, mockState } from './__tests__/helper';
import { StateManagerError } from './state-manager';
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/src/utils/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { constants } from 'starknet';
import { object, string } from 'superstruct';
import type { Struct, Infer } from 'superstruct';

import type { StarknetAccount } from '../../test/utils';
import { generateAccounts } from '../../test/utils';
import type { StarknetAccount } from '../__tests__/helper';
import { generateAccounts } from '../__tests__/helper';
import type { SnapState } from '../types/snapState';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from './constants';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/utils/snapUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { constants } from 'starknet';

import { generateAccounts } from '../../test/utils';
import { generateAccounts } from '../__tests__/helper';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from './constants';
import { DeployRequiredError, UpgradeRequiredError } from './exceptions';
import * as snapHelper from './snap';
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/test/snap-provider.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BIP44CoinTypeNode } from '@metamask/key-tree';
import { generateMnemonic } from 'bip39';
import { generateBip44Entropy } from './utils';
import { generateBip44Entropy } from '../src/__tests__/helper';

export type SnapProvider = {
registerRpcMessageHandler: (fn) => unknown;
Expand Down

0 comments on commit b0d6151

Please sign in to comment.