diff --git a/package.json b/package.json index ace80317ef9..a02882c1570 100644 --- a/package.json +++ b/package.json @@ -169,7 +169,6 @@ "ethereumjs-wallet": "1.0.1", "events": "3.3.0", "fast-text-encoding": "1.0.4", - "flatted": "3.2.7", "global": "4.4.0", "grapheme-splitter": "1.0.4", "graphql-request": "5.0.0", diff --git a/src/screens/Diagnostics/helpers/createAndShareStateDumpFile.ts b/src/screens/Diagnostics/helpers/createAndShareStateDumpFile.ts index 1d6f1d57d00..60abfb63ea0 100644 --- a/src/screens/Diagnostics/helpers/createAndShareStateDumpFile.ts +++ b/src/screens/Diagnostics/helpers/createAndShareStateDumpFile.ts @@ -4,11 +4,56 @@ import { APP_STATE_DUMP_FILE_NAME } from '@/screens/Diagnostics/constants'; import RNShare from 'react-native-share'; import { IS_ANDROID } from '@/env'; import { logger, RainbowError } from '@/logger'; -import { stringify } from 'flatted'; +import { getAllActiveSessions } from '@/walletConnect'; + +// function partially developed by ChatGPT that helps remove and trace cyclic references in javascript objects +function cyclicReplacer() { + const seenObjects = new Map(); // Tracks objects and their paths + + function replacer(key: string | undefined, value: any, path: string[] = []): any { + if (typeof value === 'object' && value !== null) { + // Determine the new path + const newPath = key !== undefined ? path.concat(key) : path; + + if (seenObjects.has(value)) { + // Construct a string representation of the path to the cyclic reference + const keys = seenObjects.get(value)?.filter(key => !!key); + let path = keys?.shift(); + keys?.forEach(key => { + if (key) { + if (!isNaN(Number(key))) { + path += `[${key}]`; + } else { + path += `.${key}`; + } + } + }); + return `Cyclic reference to ${path}`; + } + + seenObjects.set(value, newPath); + + // Recursively handle nested objects and arrays + const valueCopy: { [key: string]: any } = Array.isArray(value) ? [] : {}; + for (const k of Object.keys(value)) { + valueCopy[k] = replacer(k, value[k], newPath); + } + return valueCopy; + } + return value; + } + + return (key: string, value: any): any => replacer(key, value); +} export async function createAndShareStateDumpFile() { - const appState = store.getState(); - const stringifiedState = stringify(appState); + const reduxState = store.getState(); + const walletConnectV2Sessions = await getAllActiveSessions(); + const state = { + reduxState, + walletConnectV2: { sessions: walletConnectV2Sessions }, + }; + const stringifiedState = JSON.stringify(state, cyclicReplacer()); const documentsFilePath = `${RNFS.DocumentDirectoryPath}/${APP_STATE_DUMP_FILE_NAME}`; try { diff --git a/yarn.lock b/yarn.lock index 481a41bd7e1..fba5cec2a53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10547,11 +10547,6 @@ flat@^5.0.2: resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.2.tgz#8ca6fe332069ffa9d324c327198c598259ceb241" integrity sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ== -flatted@3.2.7: - version "3.2.7" - resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" - integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== - flatted@^3.2.9: version "3.3.1" resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.1.tgz#21db470729a6734d4997002f439cb308987f567a"