Skip to content

Commit

Permalink
fixup! feat(suite): use structuredClone for deep copying
Browse files Browse the repository at this point in the history
  • Loading branch information
dahaca committed Aug 24, 2022
1 parent c21c178 commit da91863
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
3 changes: 2 additions & 1 deletion packages/connect-explorer/src/components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
import { withRouter } from 'react-router-dom';
import InfinityMenu from 'react-infinity-menu';
import { createGlobalStyle } from 'styled-components';
import { deepClone } from '@trezor/utils';

import config from '../data/menu';

Expand Down Expand Up @@ -139,7 +140,7 @@ const findFiltered = (url: any, tree: any, node: any, key: any) => {

const getTree = (url: string) => {
// clone config
const tree = JSON.parse(JSON.stringify(config));
const tree = deepClone(config);
const filteredTree = tree.reduce((prev, curr, key) => {
if (key === undefined) return prev;
return findFiltered(url, prev, curr, key);
Expand Down
16 changes: 7 additions & 9 deletions packages/connect-explorer/src/reducers/methodReducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-eval */

import stringifyObject from 'stringify-object';
import { deepClone } from '@trezor/utils';

import {
TAB_CHANGE,
Expand Down Expand Up @@ -182,7 +183,7 @@ const findField = (state: MethodState, field: any) => {

const onFieldChange = (state: MethodState, _field: any, value: any) => {
const newState = {
...JSON.parse(JSON.stringify(state)),
...deepClone(state),
...state,
};
const field = findField(newState, _field);
Expand All @@ -206,10 +207,7 @@ const getMethodState = (url: string) => {
const method = config.find(m => m.url === url);
if (!method) return initialState;
// clone object
const state = {
...JSON.parse(JSON.stringify(method)),
// ...method,
};
const state = deepClone(method) as MethodState;

// set default values
state.fields = state.fields.map(f => setAffectedValues(state, prepareBundle(f)));
Expand All @@ -219,9 +217,9 @@ const getMethodState = (url: string) => {
};

const onAddBatch = (state: MethodState, _field: Field<any>, item: any) => {
const newState = JSON.parse(JSON.stringify(state));
const newState = deepClone(state) as any;
const field = newState.fields.find(f => f.name === _field.name);
field.items = [...field.items, item];
field.items = [...field?.items, item];
prepareBundle(field);

return updateParams(newState);
Expand All @@ -231,8 +229,8 @@ const onRemoveBatch = (state: MethodState, _field: any, _batch: any) => {
const field = state.fields.find(f => f.name === _field.name);
const items = field?.items?.filter(batch => batch !== _batch);

const newState = JSON.parse(JSON.stringify(state));
const newField = newState.fields.find(f => f.name === field?.name);
const newState = deepClone(state);
const newField = newState.fields.find(f => f.name === field?.name) as any;
newField.items = items;
prepareBundle(newField);

Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/api/common/paramsValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/core/methods/helpers/paramsValidator.js
import { deepClone } from '@trezor/utils';

import { ERRORS } from '../../constants';
import { fromHardened } from '../../utils/pathUtils';
Expand Down Expand Up @@ -102,7 +103,7 @@ export const getFirmwareRange = (
coinInfo: CoinInfo | null | undefined,
currentRange: FirmwareRange,
) => {
const current: FirmwareRange = JSON.parse(JSON.stringify(currentRange));
const current: FirmwareRange = deepClone(currentRange);
// set minimum required firmware from coins.json (coinInfo)
if (coinInfo) {
if (!coinInfo.support || typeof coinInfo.support.trezor1 !== 'string') {
Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/api/customMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/core/methods/CustomMessage.js

import { deepClone } from '@trezor/utils';
import { AbstractMethod } from '../core/AbstractMethod';
import { validateParams } from './common/paramsValidator';
import { ERRORS } from '../constants';
Expand All @@ -26,7 +27,7 @@ export default class CustomMessage extends AbstractMethod<'customMessage', Param

if (payload.messages) {
try {
JSON.parse(JSON.stringify(payload.messages));
deepClone(payload.messages);
} catch (error) {
throw ERRORS.TypedError(
'Method_InvalidParameter',
Expand Down
4 changes: 2 additions & 2 deletions packages/suite/src/actions/suite/metadataActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import TrezorConnect from '@trezor/connect';
import { analytics, EventType } from '@trezor/suite-analytics';

import { createDeferred } from '@trezor/utils';
import { createDeferred, deepClone } from '@trezor/utils';
import { METADATA } from '@suite-actions/constants';
import { Dispatch, GetState } from '@suite-types';
import {
Expand Down Expand Up @@ -518,7 +518,7 @@ export const addAccountMetadata =
const account = getState().wallet.accounts.find(a => a.key === payload.accountKey);
if (!account) return false;
// clone Account.metadata
const metadata = JSON.parse(JSON.stringify(account.metadata));
const metadata = deepClone(account.metadata);

if (payload.type === 'outputLabel') {
if (typeof payload.value !== 'string' || payload.value.length === 0) {
Expand Down
3 changes: 2 additions & 1 deletion packages/suite/src/actions/wallet/formDraftActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getFormDraftKey } from '@suite-common/wallet-utils';
import { FORM_DRAFT } from './constants';

import type { FormDraftKeyPrefix, FormDraft } from '@wallet-types/form';
import { deepClone } from '@trezor/utils';

export type FormDraftAction =
| {
Expand Down Expand Up @@ -36,7 +37,7 @@ export const getDraft =

if (draft) {
// draft is a read-only redux object. make a copy to be able to modify values
return JSON.parse(JSON.stringify(draft)) as T;
return deepClone(draft) as T;
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/actions/wallet/sendFormActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const getDraft = () => (_dispatch: Dispatch, getState: GetState) => {
const draft = send.drafts[selectedAccount.account.key];
if (draft) {
// draft is a read-only redux object. make a copy to be able to modify values
return JSON.parse(JSON.stringify(draft));
return deepClone(draft);
}
};

Expand Down
3 changes: 2 additions & 1 deletion packages/utxo-lib/tests/compose.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deepClone } from '@trezor/utils';
import { composeTx } from '../src';
import * as utils from '../src/compose/utils';
import { Permutation } from '../src/compose/permutation';
Expand Down Expand Up @@ -26,7 +27,7 @@ describe('composeTx', () => {
delete input.REV_hash;
});
const o = result.transaction.PERM_outputs;
const sorted = JSON.parse(JSON.stringify(o.sorted));
const sorted = deepClone(o.sorted);
sorted.forEach((ss: any) => {
const s = ss;
if (s.opReturnData != null) {
Expand Down

0 comments on commit da91863

Please sign in to comment.