From c7a40ae1e3f7bf8852622d8072b4a73f80c6e66c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Wed, 1 May 2024 16:31:16 +0300 Subject: [PATCH] Make `projectId` optional --- src/PeraWalletConnect.ts | 68 ++++--------------- src/util/connect/connectFlow.ts | 65 ------------------ src/util/sign/signTransactionFlow.ts | 99 +--------------------------- 3 files changed, 12 insertions(+), 220 deletions(-) diff --git a/src/PeraWalletConnect.ts b/src/PeraWalletConnect.ts index 0a5f5bd..614fdc8 100644 --- a/src/PeraWalletConnect.ts +++ b/src/PeraWalletConnect.ts @@ -25,7 +25,6 @@ import { } from "./util/storage/storageUtils"; import {getPeraConnectConfig} from "./util/api/peraWalletConnectApi"; import { - PeraWalletArbitraryData, PeraWalletTransaction, SignerTransaction, } from "./util/model/peraWalletModels"; @@ -44,7 +43,7 @@ import {getMetaInfo, waitForTabOpening} from "./util/dom/domUtils"; import {generateEmbeddedWalletURL} from "./util/peraWalletUtils"; interface PeraWalletConnectOptions { - projectId: string; + projectId?: string; deep_link?: string; app_meta?: AppMeta; network?: PeraWalletNetwork; @@ -55,10 +54,10 @@ interface PeraWalletConnectOptions { class PeraWalletConnect { client: Client | null; - projectId: string; shouldShowSignTxnToast: boolean; network = getNetworkFromStorage(); session: SessionTypes.Struct | null; + projectId?: string; chainId?: number; compactMode?: boolean; @@ -86,7 +85,9 @@ class PeraWalletConnect { options?.network || "mainnet" ); - this.projectId = options.projectId; + if (options.projectId) { + this.projectId = options.projectId; + } this.shouldShowSignTxnToast = typeof options?.shouldShowSignTxnToast === "undefined" @@ -634,8 +635,6 @@ class PeraWalletConnect { chainId: walletDetails.chainId }); - console.log(response); - // We send the full txn group to the mobile wallet. // Therefore, we first filter out txns that were not signed by the wallet. // These are received as `null`. @@ -690,22 +689,19 @@ class PeraWalletConnect { private async signDataWithMobile({ data, + message, signer, chainId }: { // Converted Uin8Array data to base64 - data: {data: string; message: string;}[]; + data: string; + message: string; signer: string; chainId: AlgorandChainIDs; }) { const formattedSignTxnRequest = formatJsonRpcRequest( "algo_signData", - data.map((item) => ({ - ...item, - - signer, - chainId - })) + [{data, signer, chainID: chainId, message}] ); try { @@ -743,31 +739,6 @@ class PeraWalletConnect { } } - private signDataWithWeb({ - data, - signer, - chainId, - webWalletURL - }: { - data: PeraWalletArbitraryData[]; - signer: string; - chainId: AlgorandChainIDs; - webWalletURL: string; - }): Promise { - return new Promise((resolve, reject) => - runWebSignTransactionFlow({ - method: "SIGN_DATA", - signTxnRequestParams: data, - signer, - chainId, - webWalletURL, - // isCompactMode: this.compactMode, - resolve, - reject - }) - ); - } - async signTransaction( txGroups: SignerTransaction[][], signerAddress?: string @@ -804,7 +775,7 @@ class PeraWalletConnect { // ================================================= // } - async signData(data: PeraWalletArbitraryData[], signer: string): Promise { + signData(data: Uint8Array, signer: string, message: string,): Promise { // eslint-disable-next-line no-magic-numbers const chainId = (this.chainId || 4160) as AlgorandChainIDs; @@ -822,25 +793,8 @@ class PeraWalletConnect { } } - // Pera Wallet Web flow - if (this.platform === "web") { - const {webWalletURL} = await getPeraConnectConfig(); - - return this.signDataWithWeb({ - data, - signer, - chainId: (this.chainId || "4160") as AlgorandChainIDs, - webWalletURL - }); - } - - const b64encodedData = data.map((item) => ({ - ...item, - data: Buffer.from(item.data).toString('base64') - })); - // Pera Mobile Wallet flow - return this.signDataWithMobile({data: b64encodedData, signer, chainId}); + return this.signDataWithMobile({data: Buffer.from(data).toString('base64'), message, signer, chainId}); } } diff --git a/src/util/connect/connectFlow.ts b/src/util/connect/connectFlow.ts index 0cb454c..f859cea 100644 --- a/src/util/connect/connectFlow.ts +++ b/src/util/connect/connectFlow.ts @@ -1,94 +1,29 @@ import { - // PERA_WALLET_IFRAME_ID, PERA_WALLET_CONNECT_MODAL_ID, removeModalWrapperFromDOM, - // getHeaderCloseButton } from "../../modal/peraWalletConnectModalUtils"; import PeraWalletConnectError from "../PeraWalletConnectError"; -// import {peraWalletFlowType} from "../device/deviceUtils"; import { - // WAIT_FOR_TAB_MAX_TRY_COUNT, - // WAIT_FOR_TAB_TRY_INTERVAL, getMetaInfo, waitForTabOpening } from "../dom/domUtils"; import appTellerManager, {PeraTeller} from "../network/teller/appTellerManager"; import {getPeraWebWalletURL} from "../peraWalletConstants"; -// import {generateEmbeddedWalletURL} from "../peraWalletUtils"; import {RunWebConnectFlowTypes} from "./connectFlowModels"; import { - // embeddedConnectFlowTellerReducer, newTabConnectFlowTellerReducer } from "./connectFlowReducers"; function runWebConnectFlow({ webWalletURL, chainId, - // isCompactMode, resolve, reject }: RunWebConnectFlowTypes) { const webWalletURLs = getPeraWebWalletURL(webWalletURL); - // if (peraWalletFlowType() === "EMBEDDED") { - // return runEmbeddedWebConnectFlow; - // } - return runNewTabWebConnectFlow; - // =========== Embedded Connect Flow =========== - // function runEmbeddedWebConnectFlow(peraWalletIframeWrapper: Element) { - // const peraWalletIframe = document.createElement("iframe"); - - // peraWalletIframe.setAttribute("id", PERA_WALLET_IFRAME_ID); - // peraWalletIframe.setAttribute( - // "src", - // generateEmbeddedWalletURL(webWalletURLs.CONNECT, isCompactMode) - // ); - - // peraWalletIframeWrapper.appendChild(peraWalletIframe); - - // if (peraWalletIframe.contentWindow) { - // let count = 0; - - // const isIframeInitializedChecker = setInterval(() => { - // count += 1; - - // if (count === WAIT_FOR_TAB_MAX_TRY_COUNT) { - // clearInterval(isIframeInitializedChecker); - - // return; - // } - - // appTellerManager.sendMessage({ - // message: { - // type: "IFRAME_INITIALIZED" - // }, - - // origin: webWalletURLs.CONNECT, - // targetWindow: peraWalletIframe.contentWindow! - // }); - - // getHeaderCloseButton("connect")?.addEventListener("click", () => { - // clearInterval(isIframeInitializedChecker); - // }); - // }, WAIT_FOR_TAB_TRY_INTERVAL); - - // appTellerManager.setupListener({ - // onReceiveMessage: (event: MessageEvent>) => - // embeddedConnectFlowTellerReducer({ - // event, - // peraWalletIframe, - // chainId, - // isIframeInitializedChecker, - // webWalletURLs, - // resolve, - // reject - // }) - // }); - // } - // } - // =========== New Tab Connect Flow =========== async function runNewTabWebConnectFlow() { try { diff --git a/src/util/sign/signTransactionFlow.ts b/src/util/sign/signTransactionFlow.ts index 34255f9..9bbe592 100644 --- a/src/util/sign/signTransactionFlow.ts +++ b/src/util/sign/signTransactionFlow.ts @@ -1,24 +1,12 @@ -// import { -// openPeraWalletSignTxnModal, -// PERA_WALLET_IFRAME_ID, -// setupPeraWalletConnectModalCloseListener, -// PERA_WALLET_SIGN_TXN_MODAL_ID, -// getHeaderCloseButton -// } from "../../modal/peraWalletConnectModalUtils"; import PeraWalletConnectError from "../PeraWalletConnectError"; -// import {peraWalletFlowType} from "../device/deviceUtils"; import { - // WAIT_FOR_TAB_MAX_TRY_COUNT, - // WAIT_FOR_TAB_TRY_INTERVAL, waitForTabOpening } from "../dom/domUtils"; import {PeraWalletArbitraryData, PeraWalletTransaction} from "../model/peraWalletModels"; import appTellerManager, {PeraTeller} from "../network/teller/appTellerManager"; import {getPeraWebWalletURL} from "../peraWalletConstants"; -// import {generateEmbeddedWalletURL} from "../peraWalletUtils"; import {RunSignTransactionFlowParams} from "./signTransactionFlowModels"; import { - // embeddedSignTransactionFlowTellerReducer, newTabSignTransactionFlowTellerReducer } from "./signTransactionFlowReducers"; @@ -35,92 +23,7 @@ function runWebSignTransactionFlow({ const webWalletURLs = getPeraWebWalletURL(webWalletURL); runNewTabSignFlow(); - // switch (peraWalletFlowType()) { - // case "EMBEDDED": - // runEmbeddedSignTransactionFlow(); - // break; - - // case "NEW_TAB": - // break; - - // default: - // break; - // } - - // =========== Embedded Sign Flow =========== - // async function runEmbeddedSignTransactionFlow() { - // const peraWalletSignTxnModalIFrameWrapper = await openPeraWalletSignTxnModal({ - // isCompactMode - // }); - - // const peraWalletIframe = document.createElement("iframe"); - // const peraWalletIframeSrc = generateEmbeddedWalletURL( - // webWalletURLs.TRANSACTION_SIGN, - // isCompactMode - // ); - // const peraWalletIframeAllow = `hid ${peraWalletIframeSrc}; bluetooth ${peraWalletIframeSrc}`; - - // peraWalletIframe.setAttribute("id", PERA_WALLET_IFRAME_ID); - // peraWalletIframe.setAttribute("src", peraWalletIframeSrc); - // peraWalletIframe.setAttribute("allow", peraWalletIframeAllow); - - // peraWalletSignTxnModalIFrameWrapper?.appendChild(peraWalletIframe); - - // setupPeraWalletConnectModalCloseListener(PERA_WALLET_SIGN_TXN_MODAL_ID, () => - // reject( - // new PeraWalletConnectError( - // { - // type: `${method}_CANCELLED` - // }, - // "Transaction signing is cancelled by user." - // ) - // ) - // ); - - // if (peraWalletIframe.contentWindow) { - // let count = 0; - - // const isIframeInitializedChecker = setInterval(() => { - // count += 1; - - // if (count === WAIT_FOR_TAB_MAX_TRY_COUNT) { - // clearInterval(isIframeInitializedChecker); - - // return; - // } - - // appTellerManager.sendMessage({ - // message: { - // type: "IFRAME_INITIALIZED" - // }, - - // origin: webWalletURLs.TRANSACTION_SIGN, - // targetWindow: peraWalletIframe.contentWindow! - // }); - - // getHeaderCloseButton("sign-txn")?.addEventListener("click", () => { - // clearInterval(isIframeInitializedChecker); - // }); - // }, WAIT_FOR_TAB_TRY_INTERVAL); - - // appTellerManager.setupListener({ - // onReceiveMessage: (event: MessageEvent>) => - // embeddedSignTransactionFlowTellerReducer({ - // event, - // signTxnRequestParams, - // peraWalletIframe, - // isIframeInitializedChecker, - // webWalletURLs, - // method, - // signer, - // chainId, - // resolve, - // reject - // }) - // }); - // } - // } - + // =========== New Tab Sign Flow =========== async function runNewTabSignFlow() { try {