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 6e3d10d commit c21c178
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
11 changes: 6 additions & 5 deletions packages/connect/src/data/coinInfo.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/data/CoinInfo.js

import { deepClone } from '@trezor/utils';
import { ERRORS } from '../constants';
import { toHardened, fromHardened } from '../utils/pathUtils';
import type {
Expand All @@ -14,7 +15,7 @@ const ethereumNetworks: EthereumNetworkInfo[] = [];
const miscNetworks: MiscNetworkInfo[] = [];

export const getBitcoinNetwork = (pathOrName: number[] | string) => {
const networks: BitcoinNetworkInfo[] = structuredClone(bitcoinNetworks);
const networks: BitcoinNetworkInfo[] = deepClone(bitcoinNetworks);
if (typeof pathOrName === 'string') {
const name = pathOrName.toLowerCase();
return networks.find(
Expand All @@ -29,7 +30,7 @@ export const getBitcoinNetwork = (pathOrName: number[] | string) => {
};

export const getEthereumNetwork = (pathOrName: number[] | string) => {
const networks: EthereumNetworkInfo[] = structuredClone(ethereumNetworks);
const networks: EthereumNetworkInfo[] = deepClone(ethereumNetworks);
if (typeof pathOrName === 'string') {
const name = pathOrName.toLowerCase();
return networks.find(
Expand All @@ -41,7 +42,7 @@ export const getEthereumNetwork = (pathOrName: number[] | string) => {
};

export const getMiscNetwork = (pathOrName: number[] | string) => {
const networks: MiscNetworkInfo[] = structuredClone(miscNetworks);
const networks: MiscNetworkInfo[] = deepClone(miscNetworks);
if (typeof pathOrName === 'string') {
const name = pathOrName.toLowerCase();
return networks.find(
Expand Down Expand Up @@ -84,7 +85,7 @@ export const getBech32Network = (coin: BitcoinNetworkInfo) => {

// fix coinInfo network values from path (segwit/legacy)
export const fixCoinInfoNetwork = (ci: BitcoinNetworkInfo, path: number[]) => {
const coinInfo: BitcoinNetworkInfo = structuredClone(ci);
const coinInfo: BitcoinNetworkInfo = deepClone(ci);
if (path[0] === toHardened(84)) {
const bech32Network = getBech32Network(coinInfo);
if (bech32Network) {
Expand Down Expand Up @@ -120,7 +121,7 @@ const detectBtcVersion = (data: { subversion?: string }) => {

// TODO: https://github.com/trezor/trezor-suite/issues/4886
export const getCoinInfoByHash = (hash: string, networkInfo: any) => {
const networks: BitcoinNetworkInfo[] = structuredClone(bitcoinNetworks);
const networks: BitcoinNetworkInfo[] = deepClone(bitcoinNetworks);
const result = networks.find(
info => hash.toLowerCase() === info.hashGenesisBlock.toLowerCase(),
);
Expand Down
4 changes: 3 additions & 1 deletion packages/suite/src/actions/wallet/sendFormActions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import TrezorConnect, { PROTO } from '@trezor/connect';
import BigNumber from 'bignumber.js';
import { deepClone } from '@trezor/utils';

import * as accountActions from '@wallet-actions/accountActions';
import * as blockchainActions from '@wallet-actions/blockchainActions';
import * as transactionActions from '@wallet-actions/transactionActions';
Expand Down Expand Up @@ -133,7 +135,7 @@ export const convertDrafts = () => (dispatch: Dispatch, getState: GetState) => {
const conversionToUse =
areSatsSelected && areSatsSupported ? amountToSatoshi : formatAmount;

const updatedDraft: FormState = structuredClone(draft);
const updatedDraft: FormState = deepClone(draft);
const decimals = getAccountDecimals(relatedAccount.symbol)!;

updatedDraft.outputs.forEach(output => {
Expand Down
15 changes: 15 additions & 0 deletions packages/utils/src/deepClone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const deepClone = <T>(value: T): T => {
if (value === undefined) {
return value;
}

let clone;

if (typeof structuredClone === 'function') {
clone = structuredClone(value);
} else {
clone = JSON.parse(JSON.stringify(value));
}

return clone;
};
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './throwError';
export * from './isHex';
export * from './resolveStaticPath';
export * from './createTimeoutPromise';
export * from './deepClone';
24 changes: 24 additions & 0 deletions packages/utils/tests/deepClone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { deepClone } from '../src/deepClone';

describe('deepClone', () => {
it('deepClone works', () => {
const original = {
a: 1,
b: '2',
c: {
a: 1,
b: '2',
c: {
a: [1, 2, 3],
b: '2',
},
},
};

const copy = deepClone(original);
const shallowCopy = { ...original };

expect(copy.c.c.a === original.c.c.a).toBeFalsy();
expect(shallowCopy.c.c.a === original.c.c.a).toBeTruthy();
});
});

0 comments on commit c21c178

Please sign in to comment.