From 7633f1effa35a9c237c7a25090c8785e7b83545b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Thu, 30 May 2024 15:41:32 +0300 Subject: [PATCH] Sort connected accounts on local storage --- src/PeraWalletConnect.ts | 319 ++++-------------- src/modal/peraWalletConnectModalUtils.ts | 6 +- src/util/connect/connectFlow.ts | 11 +- src/util/connect/connectFlowModels.ts | 9 - src/util/connect/connectFlowReducers.ts | 169 +--------- src/util/peraWalletConstants.ts | 6 +- src/util/peraWalletTypes.ts | 1 + src/util/sign/signTransactionFlow.ts | 9 +- src/util/sign/signTransactionFlowReducers.ts | 4 +- src/util/storage/storageUtils.ts | 9 +- src/util/wallet-connect/walletConnectUtils.ts | 24 ++ 11 files changed, 118 insertions(+), 449 deletions(-) create mode 100644 src/util/wallet-connect/walletConnectUtils.ts diff --git a/src/PeraWalletConnect.ts b/src/PeraWalletConnect.ts index 640dff2..f9e4fe0 100644 --- a/src/PeraWalletConnect.ts +++ b/src/PeraWalletConnect.ts @@ -12,8 +12,7 @@ import { PERA_WALLET_REDIRECT_MODAL_ID, openPeraWalletSignTxnToast, PERA_WALLET_SIGN_TXN_TOAST_ID, - PERA_WALLET_IFRAME_ID, - PERA_WALLET_MODAL_CLASSNAME, + PERA_WALLET_MODAL_CLASSNAME } from "./modal/peraWalletConnectModalUtils"; import { getWalletDetailsFromStorage, @@ -24,23 +23,20 @@ import { resetWalletDetailsFromStorage } from "./util/storage/storageUtils"; import {getPeraConnectConfig} from "./util/api/peraWalletConnectApi"; -import { - PeraWalletTransaction, - SignerTransaction, -} from "./util/model/peraWalletModels"; +import {PeraWalletTransaction, SignerTransaction} from "./util/model/peraWalletModels"; import { base64ToUint8Array, composeTransaction, formatJsonRpcRequest } from "./util/transaction/transactionUtils"; -import {detectBrowser, isMobile} from "./util/device/deviceUtils"; +import {isMobile} from "./util/device/deviceUtils"; import {AlgorandChainIDs, PeraWalletNetwork} from "./util/peraWalletTypes"; import {runWebSignTransactionFlow} from "./util/sign/signTransactionFlow"; import {PERA_WALLET_LOCAL_STORAGE_KEYS} from "./util/storage/storageConstants"; import {getPeraWebWalletURL} from "./util/peraWalletConstants"; import appTellerManager, {PeraTeller} from "./util/network/teller/appTellerManager"; import {getMetaInfo, waitForTabOpening} from "./util/dom/domUtils"; -import {generateEmbeddedWalletURL} from "./util/peraWalletUtils"; +import {formatWalletConnectSessionResponse} from "./util/wallet-connect/walletConnectUtils"; interface PeraWalletConnectOptions { projectId?: string; @@ -108,143 +104,11 @@ class PeraWalletConnect { webWalletURL: string, chainId: number | undefined ) { - const browser = detectBrowser(); const webWalletURLs = getPeraWebWalletURL(webWalletURL); - const peraWalletIframe = document.createElement("iframe"); - - function onReceiveMessage(event: MessageEvent>) { - if (resolve && event.data.message.type === "CONNECT_CALLBACK") { - const accounts = event.data.message.data.addresses; - - saveWalletDetailsToStorage(accounts, "pera-wallet-web"); - - resolve(accounts); - - onClose(); - - document.getElementById(PERA_WALLET_IFRAME_ID)?.remove(); - } else if (event.data.message.type === "CONNECT_NETWORK_MISMATCH") { - reject( - new PeraWalletConnectError( - { - type: "CONNECT_NETWORK_MISMATCH", - detail: event.data.message.error - }, - event.data.message.error || - `Your wallet is connected to a different network to this dApp. Update your wallet to the correct network (MainNet or TestNet) to continue.` - ) - ); - - onClose(); - - document.getElementById(PERA_WALLET_IFRAME_ID)?.remove(); - } else if ( - ["CREATE_PASSCODE_EMBEDDED", "SELECT_ACCOUNT_EMBEDDED"].includes( - event.data.message.type - ) - ) { - if (event.data.message.type === "CREATE_PASSCODE_EMBEDDED") { - const newPeraWalletTab = window.open(webWalletURLs.CONNECT, "_blank"); - - if (newPeraWalletTab && newPeraWalletTab.opener) { - appTellerManager.sendMessage({ - message: { - type: "CONNECT", - data: { - ...getMetaInfo(), - chainId - } - }, - - origin: webWalletURLs.CONNECT, - targetWindow: newPeraWalletTab - }); - } - - const checkTabIsAliveInterval = setInterval(() => { - if (newPeraWalletTab?.closed === true) { - reject( - new PeraWalletConnectError( - { - type: "CONNECT_CANCELLED" - }, - "Connect is cancelled by user" - ) - ); - - onClose(); - clearInterval(checkTabIsAliveInterval); - } - - // eslint-disable-next-line no-magic-numbers - }, 2000); - - appTellerManager.setupListener({ - onReceiveMessage: (newTabEvent: MessageEvent>) => { - if (resolve && newTabEvent.data.message.type === "CONNECT_CALLBACK") { - const accounts = newTabEvent.data.message.data.addresses; - - saveWalletDetailsToStorage(accounts, "pera-wallet-web"); - - resolve(accounts); - - onClose(); - - newPeraWalletTab?.close(); - } - } - }); - } else if (event.data.message.type === "SELECT_ACCOUNT_EMBEDDED") { - const peraWalletConnectModalWrapper = document.getElementById( - PERA_WALLET_CONNECT_MODAL_ID - ); - - const peraWalletConnectModal = peraWalletConnectModalWrapper - ?.querySelector("pera-wallet-connect-modal") - ?.shadowRoot?.querySelector(`.${PERA_WALLET_MODAL_CLASSNAME}`); - - const peraWalletConnectModalDesktopMode = peraWalletConnectModal - ?.querySelector("pera-wallet-modal-desktop-mode") - ?.shadowRoot?.querySelector(".pera-wallet-connect-modal-desktop-mode"); - - if (peraWalletConnectModal && peraWalletConnectModalDesktopMode) { - peraWalletConnectModal.classList.add( - `${PERA_WALLET_MODAL_CLASSNAME}--select-account` - ); - peraWalletConnectModal.classList.remove( - `${PERA_WALLET_MODAL_CLASSNAME}--create-passcode` - ); - peraWalletConnectModalDesktopMode.classList.add( - `pera-wallet-connect-modal-desktop-mode--select-account` - ); - peraWalletConnectModalDesktopMode.classList.remove( - `pera-wallet-connect-modal-desktop-mode--create-passcode` - ); - } - - appTellerManager.sendMessage({ - message: { - type: "SELECT_ACCOUNT_EMBEDDED_CALLBACK" - }, - origin: webWalletURLs.CONNECT, - targetWindow: peraWalletIframe.contentWindow! - }); - } - } - } - - function onWebWalletConnect(peraWalletIframeWrapper: Element) { - if (browser === "Chrome") { - peraWalletIframe.setAttribute("id", PERA_WALLET_IFRAME_ID); - peraWalletIframe.setAttribute( - "src", - generateEmbeddedWalletURL(webWalletURLs.CONNECT) - ); - - peraWalletIframeWrapper.appendChild(peraWalletIframe); - - if (peraWalletIframe.contentWindow) { + function onWebWalletConnect() { + waitForTabOpening(webWalletURLs.CONNECT).then((newPeraWalletTab) => { + if (newPeraWalletTab && newPeraWalletTab.opener) { appTellerManager.sendMessage({ message: { type: "CONNECT", @@ -255,82 +119,60 @@ class PeraWalletConnect { }, origin: webWalletURLs.CONNECT, - targetWindow: peraWalletIframe.contentWindow, + targetWindow: newPeraWalletTab, timeout: 5000 }); } - appTellerManager.setupListener({ - onReceiveMessage - }); - } else { - waitForTabOpening(webWalletURLs.CONNECT).then((newPeraWalletTab) => { - if (newPeraWalletTab && newPeraWalletTab.opener) { - appTellerManager.sendMessage({ - message: { - type: "CONNECT", - data: { - ...getMetaInfo(), - chainId - } - }, + const checkTabIsAliveInterval = setInterval(() => { + if (newPeraWalletTab?.closed === true) { + reject( + new PeraWalletConnectError( + { + type: "CONNECT_CANCELLED" + }, + "Connect is cancelled by user" + ) + ); - origin: webWalletURLs.CONNECT, - targetWindow: newPeraWalletTab, - timeout: 5000 - }); + clearInterval(checkTabIsAliveInterval); + onClose(); } - const checkTabIsAliveInterval = setInterval(() => { - if (newPeraWalletTab?.closed === true) { + // eslint-disable-next-line no-magic-numbers + }, 2000); + + appTellerManager.setupListener({ + onReceiveMessage: (event: MessageEvent>) => { + if (resolve && event.data.message.type === "CONNECT_CALLBACK") { + const accounts = event.data.message.data.addresses; + + saveWalletDetailsToStorage(accounts, "pera-wallet-web"); + + resolve(accounts); + + onClose(); + + newPeraWalletTab?.close(); + } else if (event.data.message.type === "CONNECT_NETWORK_MISMATCH") { reject( new PeraWalletConnectError( { - type: "CONNECT_CANCELLED" + type: "CONNECT_NETWORK_MISMATCH", + detail: event.data.message.error }, - "Connect is cancelled by user" + event.data.message.error || + `Your wallet is connected to a different network to this dApp. Update your wallet to the correct network (MainNet or TestNet) to continue.` ) ); - clearInterval(checkTabIsAliveInterval); onClose(); - } - - // eslint-disable-next-line no-magic-numbers - }, 2000); - - appTellerManager.setupListener({ - onReceiveMessage: (event: MessageEvent>) => { - if (resolve && event.data.message.type === "CONNECT_CALLBACK") { - const accounts = event.data.message.data.addresses; - - saveWalletDetailsToStorage(accounts, "pera-wallet-web"); - resolve(accounts); - - onClose(); - - newPeraWalletTab?.close(); - } else if (event.data.message.type === "CONNECT_NETWORK_MISMATCH") { - reject( - new PeraWalletConnectError( - { - type: "CONNECT_NETWORK_MISMATCH", - detail: event.data.message.error - }, - event.data.message.error || - `Your wallet is connected to a different network to this dApp. Update your wallet to the correct network (MainNet or TestNet) to continue.` - ) - ); - - onClose(); - - newPeraWalletTab?.close(); - } + newPeraWalletTab?.close(); } - }); + } }); - } + }); } function onClose() { @@ -430,29 +272,17 @@ class PeraWalletConnect { this.session = await approval(); - const {namespaces} = this.session; - - const accounts = Object.values(namespaces) - .map((namespace) => namespace.accounts) - .flat(); - - const [namespace, reference, _address] = accounts[0].split(":"); - - const accountsArray: string[] = accounts.map((account) => { - const [_namespace, _reference, address] = account.split(":"); - - return address; - }); - - const accountsSet = [...new Set(accountsArray)]; + const {namespace, reference, accounts} = formatWalletConnectSessionResponse( + this.session + ); saveWalletDetailsToStorage( - accountsSet || [], + accounts || [], "pera-wallet", `${namespace}:${reference}` ); - resolve(accountsSet); + resolve(accounts); } catch (error: any) { console.log(error); @@ -476,7 +306,14 @@ class PeraWalletConnect { try { const walletDetails = getWalletDetailsFromStorage(); - if (!walletDetails?.chainId) { + if (!walletDetails) { + resolve([]); + + return; + } + + // Do not reconnect if the last session was connected with Wallet Connect v1 + if (!walletDetails?.version) { await resetWalletDetailsFromStorage(); reject( @@ -485,14 +322,9 @@ class PeraWalletConnect { type: "SESSION_RECONNECT", detail: "Failed to reconnect session. Wallet Connect version mismatch." }, - "Failed to reconnect session" - )); - } - - if (!walletDetails) { - resolve([]); - - return; + "Failed to reconnect session. Please try to connect again." + ) + ); } // ================================================= // @@ -565,19 +397,12 @@ class PeraWalletConnect { this.session = session; - const {namespaces: updatedNamespaces} = this.session; - const accounts = Object.values(updatedNamespaces) - .map((namespace) => namespace.accounts) - .flat(); - const accountsArray: string[] = accounts.map((account) => { - const [_namespace, _reference, address] = account.split(":"); - - return address; - }); - const [namespace, reference, _address] = accounts[0].split(":"); + const {namespace, reference, accounts} = formatWalletConnectSessionResponse( + this.session + ); saveWalletDetailsToStorage( - accountsArray || [], + accounts || [], "pera-wallet", `${namespace}:${reference}` ); @@ -592,7 +417,7 @@ class PeraWalletConnect { } if (typeof this.client?.session === "undefined") { - reject(new Error("WalletConnect is not initialized")); + reject(new Error("WalletConnect is not initialized 2")); } try { @@ -698,10 +523,9 @@ class PeraWalletConnect { signer: string; chainId: AlgorandChainIDs; }) { - const formattedSignTxnRequest = formatJsonRpcRequest( - "algo_signData", - [{data, signer, chainID: chainId, message}] - ); + const formattedSignTxnRequest = formatJsonRpcRequest("algo_signData", [ + {data, signer, chainID: chainId, message} + ]); try { try { @@ -774,7 +598,7 @@ class PeraWalletConnect { // ================================================= // } - signData(data: Uint8Array, signer: string, message: string,): Promise { + signData(data: Uint8Array, signer: string, message: string): Promise { // eslint-disable-next-line no-magic-numbers const chainId = (this.chainId || 4160) as AlgorandChainIDs; @@ -793,7 +617,12 @@ class PeraWalletConnect { } // Pera Mobile Wallet flow - return this.signDataWithMobile({data: Buffer.from(data).toString('base64'), message, signer, chainId}); + return this.signDataWithMobile({ + data: Buffer.from(data).toString("base64"), + message, + signer, + chainId + }); } } diff --git a/src/modal/peraWalletConnectModalUtils.ts b/src/modal/peraWalletConnectModalUtils.ts index ed50b0f..3ca708a 100644 --- a/src/modal/peraWalletConnectModalUtils.ts +++ b/src/modal/peraWalletConnectModalUtils.ts @@ -126,9 +126,9 @@ function openPeraWalletSignTxnModal({isCompactMode}: {isCompactMode?: boolean}) return signTxnModal ? waitForElementCreatedAtShadowDOM( - signTxnModal, - "pera-wallet-sign-txn-modal__body__content" - ) + signTxnModal, + "pera-wallet-sign-txn-modal__body__content" + ) : Promise.reject(); } diff --git a/src/util/connect/connectFlow.ts b/src/util/connect/connectFlow.ts index f859cea..9202325 100644 --- a/src/util/connect/connectFlow.ts +++ b/src/util/connect/connectFlow.ts @@ -1,18 +1,13 @@ import { PERA_WALLET_CONNECT_MODAL_ID, - removeModalWrapperFromDOM, + removeModalWrapperFromDOM } from "../../modal/peraWalletConnectModalUtils"; import PeraWalletConnectError from "../PeraWalletConnectError"; -import { - getMetaInfo, - waitForTabOpening -} from "../dom/domUtils"; +import {getMetaInfo, waitForTabOpening} from "../dom/domUtils"; import appTellerManager, {PeraTeller} from "../network/teller/appTellerManager"; import {getPeraWebWalletURL} from "../peraWalletConstants"; import {RunWebConnectFlowTypes} from "./connectFlowModels"; -import { - newTabConnectFlowTellerReducer -} from "./connectFlowReducers"; +import {newTabConnectFlowTellerReducer} from "./connectFlowReducers"; function runWebConnectFlow({ webWalletURL, diff --git a/src/util/connect/connectFlowModels.ts b/src/util/connect/connectFlowModels.ts index 6e5bdae..26f006e 100644 --- a/src/util/connect/connectFlowModels.ts +++ b/src/util/connect/connectFlowModels.ts @@ -13,14 +13,6 @@ interface RunWebConnectFlowTypes extends ConnectFlowPromise { isCompactMode?: boolean; } -interface EmbeddedConnectFlowTellerReducerParams extends ConnectFlowPromise { - event: MessageEvent>; - peraWalletIframe: HTMLIFrameElement; - chainId: AlgorandChainIDs | undefined; - isIframeInitializedChecker: NodeJS.Timer; - webWalletURLs: PeraWebWalletURLs; -} - interface NewTabConnectFlowTellerReducerParams extends ConnectFlowPromise { event: MessageEvent>; newPeraWalletTab: Window | null; @@ -29,6 +21,5 @@ interface NewTabConnectFlowTellerReducerParams extends ConnectFlowPromise { export type { ConnectFlowPromise, RunWebConnectFlowTypes, - EmbeddedConnectFlowTellerReducerParams, NewTabConnectFlowTellerReducerParams }; diff --git a/src/util/connect/connectFlowReducers.ts b/src/util/connect/connectFlowReducers.ts index c80f773..b284255 100644 --- a/src/util/connect/connectFlowReducers.ts +++ b/src/util/connect/connectFlowReducers.ts @@ -1,175 +1,10 @@ import { - PERA_WALLET_IFRAME_ID, PERA_WALLET_CONNECT_MODAL_ID, - PERA_WALLET_MODAL_CLASSNAME, removeModalWrapperFromDOM } from "../../modal/peraWalletConnectModalUtils"; import PeraWalletConnectError from "../PeraWalletConnectError"; -import {getMetaInfo, waitForTabOpening} from "../dom/domUtils"; -import appTellerManager, {PeraTeller} from "../network/teller/appTellerManager"; import {saveWalletDetailsToStorage} from "../storage/storageUtils"; -import { - EmbeddedConnectFlowTellerReducerParams, - NewTabConnectFlowTellerReducerParams -} from "./connectFlowModels"; - -// =========== Embedded Connect Flow =========== -function embeddedConnectFlowTellerReducer({ - event, - peraWalletIframe, - chainId, - isIframeInitializedChecker, - webWalletURLs, - resolve, - reject -}: EmbeddedConnectFlowTellerReducerParams) { - switch (event.data.message.type) { - case "IFRAME_INITIALIZED_RECEIVED": - clearInterval(isIframeInitializedChecker); - - appTellerManager.sendMessage({ - message: { - type: "CONNECT", - data: { - ...getMetaInfo(), - chainId - } - }, - - origin: webWalletURLs.CONNECT, - targetWindow: peraWalletIframe.contentWindow! - }); - break; - - case "CONNECT_CALLBACK": { - const accounts = event.data.message.data.addresses; - - saveWalletDetailsToStorage(accounts, "pera-wallet-web"); - - resolve(accounts); - - removeModalWrapperFromDOM(PERA_WALLET_CONNECT_MODAL_ID); - - document.getElementById(PERA_WALLET_IFRAME_ID)?.remove(); - - break; - } - - case "CONNECT_NETWORK_MISMATCH": - reject( - new PeraWalletConnectError( - { - type: "CONNECT_NETWORK_MISMATCH", - detail: event.data.message.error - }, - event.data.message.error || - `Your wallet is connected to a different network to this dApp. Update your wallet to the correct network (MainNet or TestNet) to continue.` - ) - ); - - removeModalWrapperFromDOM(PERA_WALLET_CONNECT_MODAL_ID); - - document.getElementById(PERA_WALLET_IFRAME_ID)?.remove(); - break; - - case "CREATE_PASSCODE_EMBEDDED": { - waitForTabOpening(webWalletURLs.CONNECT).then((newPeraWalletTab) => { - if (newPeraWalletTab) { - appTellerManager.sendMessage({ - message: { - type: "CONNECT", - data: { - ...getMetaInfo(), - chainId - } - }, - - origin: webWalletURLs.CONNECT, - targetWindow: newPeraWalletTab - }); - } - - const checkTabIsAliveInterval = setInterval(() => { - if (newPeraWalletTab?.closed === true) { - reject( - new PeraWalletConnectError( - { - type: "CONNECT_CANCELLED" - }, - "Connect is cancelled by user" - ) - ); - - removeModalWrapperFromDOM(PERA_WALLET_CONNECT_MODAL_ID); - clearInterval(checkTabIsAliveInterval); - } - - // eslint-disable-next-line no-magic-numbers - }, 2000); - - appTellerManager.setupListener({ - onReceiveMessage: (newTabEvent: MessageEvent>) => { - if (resolve && newTabEvent.data.message.type === "CONNECT_CALLBACK") { - const accounts = newTabEvent.data.message.data.addresses; - - saveWalletDetailsToStorage(accounts, "pera-wallet-web"); - - resolve(accounts); - - removeModalWrapperFromDOM(PERA_WALLET_CONNECT_MODAL_ID); - - newPeraWalletTab?.close(); - } - } - }); - }); - - break; - } - - case "SELECT_ACCOUNT_EMBEDDED": { - const peraWalletConnectModalWrapper = document.getElementById( - PERA_WALLET_CONNECT_MODAL_ID - ); - - const peraWalletConnectModal = peraWalletConnectModalWrapper - ?.querySelector("pera-wallet-connect-modal") - ?.shadowRoot?.querySelector(`.${PERA_WALLET_MODAL_CLASSNAME}`); - - const peraWalletConnectModalDesktopMode = peraWalletConnectModal - ?.querySelector("pera-wallet-modal-desktop-mode") - ?.shadowRoot?.querySelector(".pera-wallet-connect-modal-desktop-mode"); - - if (peraWalletConnectModal && peraWalletConnectModalDesktopMode) { - peraWalletConnectModal.classList.add( - `${PERA_WALLET_MODAL_CLASSNAME}--select-account` - ); - peraWalletConnectModal.classList.remove( - `${PERA_WALLET_MODAL_CLASSNAME}--create-passcode` - ); - peraWalletConnectModalDesktopMode.classList.add( - `pera-wallet-connect-modal-desktop-mode--select-account` - ); - peraWalletConnectModalDesktopMode.classList.remove( - `pera-wallet-connect-modal-desktop-mode--create-passcode` - ); - } - - appTellerManager.sendMessage({ - message: { - type: "SELECT_ACCOUNT_EMBEDDED_CALLBACK" - }, - origin: webWalletURLs.CONNECT, - targetWindow: peraWalletIframe.contentWindow! - }); - - break; - } - - default: - break; - } -} +import {NewTabConnectFlowTellerReducerParams} from "./connectFlowModels"; // =========== New Tab Connect Flow =========== function newTabConnectFlowTellerReducer({ @@ -206,4 +41,4 @@ function newTabConnectFlowTellerReducer({ } } -export {embeddedConnectFlowTellerReducer, newTabConnectFlowTellerReducer}; +export {newTabConnectFlowTellerReducer}; diff --git a/src/util/peraWalletConstants.ts b/src/util/peraWalletConstants.ts index 79176c5..9780ffd 100644 --- a/src/util/peraWalletConstants.ts +++ b/src/util/peraWalletConstants.ts @@ -17,8 +17,4 @@ function getPeraWebWalletURL(webWalletURL: string): PeraWebWalletURLs { }; } -export { - PERA_WALLET_APP_DEEP_LINK, - getPeraWebWalletURL, - PERA_DOWNLOAD_URL -}; +export {PERA_WALLET_APP_DEEP_LINK, getPeraWebWalletURL, PERA_DOWNLOAD_URL}; diff --git a/src/util/peraWalletTypes.ts b/src/util/peraWalletTypes.ts index bba3467..f3dedb6 100644 --- a/src/util/peraWalletTypes.ts +++ b/src/util/peraWalletTypes.ts @@ -10,6 +10,7 @@ interface PeraWalletDetails { type: PeraWalletType; accounts: string[]; selectedAccount: string; + version: string; chainId?: string; } diff --git a/src/util/sign/signTransactionFlow.ts b/src/util/sign/signTransactionFlow.ts index 9bbe592..5be13dc 100644 --- a/src/util/sign/signTransactionFlow.ts +++ b/src/util/sign/signTransactionFlow.ts @@ -1,14 +1,10 @@ import PeraWalletConnectError from "../PeraWalletConnectError"; -import { - waitForTabOpening -} from "../dom/domUtils"; +import {waitForTabOpening} from "../dom/domUtils"; import {PeraWalletArbitraryData, PeraWalletTransaction} from "../model/peraWalletModels"; import appTellerManager, {PeraTeller} from "../network/teller/appTellerManager"; import {getPeraWebWalletURL} from "../peraWalletConstants"; import {RunSignTransactionFlowParams} from "./signTransactionFlowModels"; -import { - newTabSignTransactionFlowTellerReducer -} from "./signTransactionFlowReducers"; +import {newTabSignTransactionFlowTellerReducer} from "./signTransactionFlowReducers"; function runWebSignTransactionFlow({ method, @@ -23,7 +19,6 @@ function runWebSignTransactionFlow({ const webWalletURLs = getPeraWebWalletURL(webWalletURL); runNewTabSignFlow(); - // =========== New Tab Sign Flow =========== async function runNewTabSignFlow() { try { diff --git a/src/util/sign/signTransactionFlowReducers.ts b/src/util/sign/signTransactionFlowReducers.ts index 568f176..5e6d0c5 100644 --- a/src/util/sign/signTransactionFlowReducers.ts +++ b/src/util/sign/signTransactionFlowReducers.ts @@ -1,9 +1,7 @@ import PeraWalletConnectError from "../PeraWalletConnectError"; import {resetWalletDetailsFromStorage} from "../storage/storageUtils"; import {base64ToUint8Array} from "../transaction/transactionUtils"; -import { - NewTabSignTransactionFlowTellerReducerParams -} from "./signTransactionFlowModels"; +import {NewTabSignTransactionFlowTellerReducerParams} from "./signTransactionFlowModels"; // =========== New Tab Sign Flow =========== function newTabSignTransactionFlowTellerReducer({ diff --git a/src/util/storage/storageUtils.ts b/src/util/storage/storageUtils.ts index cb7352b..497db9a 100644 --- a/src/util/storage/storageUtils.ts +++ b/src/util/storage/storageUtils.ts @@ -1,6 +1,10 @@ // eslint-disable-next-line import/no-unresolved -import {PeraWalletDetails, PeraWalletNetwork, PeraWalletPlatformType} from "../peraWalletTypes"; +import { + PeraWalletDetails, + PeraWalletNetwork, + PeraWalletPlatformType +} from "../peraWalletTypes"; import {PERA_WALLET_LOCAL_STORAGE_KEYS} from "./storageConstants"; function getLocalStorage() { @@ -18,7 +22,8 @@ function saveWalletDetailsToStorage( type: type || "pera-wallet", accounts, selectedAccount: accounts[0], - chainId + chainId, + version: "2.0" }) ); } diff --git a/src/util/wallet-connect/walletConnectUtils.ts b/src/util/wallet-connect/walletConnectUtils.ts new file mode 100644 index 0000000..cc4aa18 --- /dev/null +++ b/src/util/wallet-connect/walletConnectUtils.ts @@ -0,0 +1,24 @@ +import {SessionTypes} from "@walletconnect/types"; + +function formatWalletConnectSessionResponse(session: SessionTypes.Struct) { + const {namespaces} = session; + const accounts = Object.values(namespaces) + .map((namespace) => namespace.accounts) + .flat(); + const [namespace, reference, _address] = accounts[0].split(":"); + const accountsArray: string[] = accounts.map((account) => { + const [_namespace, _reference, address] = account.split(":"); + + return address; + }); + // Make sure the accounts are sorted and unique + const accountsSet = [...new Set(accountsArray)].sort(); + + return { + reference, + namespace, + accounts: accountsSet + }; +} + +export {formatWalletConnectSessionResponse};