Skip to content

Commit

Permalink
Make projectId optional
Browse files Browse the repository at this point in the history
  • Loading branch information
yasincaliskan committed May 1, 2024
1 parent 8331ba4 commit c7a40ae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 220 deletions.
68 changes: 11 additions & 57 deletions src/PeraWalletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from "./util/storage/storageUtils";
import {getPeraConnectConfig} from "./util/api/peraWalletConnectApi";
import {
PeraWalletArbitraryData,
PeraWalletTransaction,
SignerTransaction,
} from "./util/model/peraWalletModels";
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -743,31 +739,6 @@ class PeraWalletConnect {
}
}

private signDataWithWeb({
data,
signer,
chainId,
webWalletURL
}: {
data: PeraWalletArbitraryData[];
signer: string;
chainId: AlgorandChainIDs;
webWalletURL: string;
}): Promise<Uint8Array[]> {
return new Promise<Uint8Array[]>((resolve, reject) =>
runWebSignTransactionFlow({
method: "SIGN_DATA",
signTxnRequestParams: data,
signer,
chainId,
webWalletURL,
// isCompactMode: this.compactMode,
resolve,
reject
})
);
}

async signTransaction(
txGroups: SignerTransaction[][],
signerAddress?: string
Expand Down Expand Up @@ -804,7 +775,7 @@ class PeraWalletConnect {
// ================================================= //
}

async signData(data: PeraWalletArbitraryData[], signer: string): Promise<Uint8Array[]> {
signData(data: Uint8Array, signer: string, message: string,): Promise<Uint8Array[]> {
// eslint-disable-next-line no-magic-numbers
const chainId = (this.chainId || 4160) as AlgorandChainIDs;

Expand All @@ -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});
}
}

Expand Down
65 changes: 0 additions & 65 deletions src/util/connect/connectFlow.ts
Original file line number Diff line number Diff line change
@@ -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<TellerMessage<PeraTeller>>) =>
// embeddedConnectFlowTellerReducer({
// event,
// peraWalletIframe,
// chainId,
// isIframeInitializedChecker,
// webWalletURLs,
// resolve,
// reject
// })
// });
// }
// }

// =========== New Tab Connect Flow ===========
async function runNewTabWebConnectFlow() {
try {
Expand Down
99 changes: 1 addition & 98 deletions src/util/sign/signTransactionFlow.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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<TellerMessage<PeraTeller>>) =>
// embeddedSignTransactionFlowTellerReducer({
// event,
// signTxnRequestParams,
// peraWalletIframe,
// isIframeInitializedChecker,
// webWalletURLs,
// method,
// signer,
// chainId,
// resolve,
// reject
// })
// });
// }
// }


// =========== New Tab Sign Flow ===========
async function runNewTabSignFlow() {
try {
Expand Down

0 comments on commit c7a40ae

Please sign in to comment.