Skip to content

Commit

Permalink
Fix "Launch Web Wallet" action
Browse files Browse the repository at this point in the history
  • Loading branch information
yasincaliskan committed Sep 24, 2024
1 parent cab3c36 commit 08cbfc4
Showing 1 changed file with 78 additions and 80 deletions.
158 changes: 78 additions & 80 deletions src/PeraWalletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,82 +304,6 @@ class PeraWalletConnect {
});
}

private async createClient() {
try {
const client = await Client.init({
relayUrl: "wss://relay.walletconnect.com",
projectId: this.projectId,
metadata: getAppMetadata()
});

this.client = client;

this.checkPersistedState(client);

client.on("session_delete", () => {
resetWalletDetailsFromStorage();
});

return client;
} catch (err) {
throw err;
}
}

private checkPersistedState(client: Client) {
if (typeof this.session !== "undefined") return;
// populates (the last) existing session to state

if (client.session.length) {
const lastKeyIndex = client!.session.keys.length - 1;
const session = client.session.get(client.session.keys[lastKeyIndex]);

this.session = session;

const {namespace, reference, accounts} = formatWalletConnectSessionResponse(
this.session
);

saveWalletDetailsToStorage(
accounts || [],
"pera-wallet",
`${namespace}:${reference}`
);
}
}

disconnect() {
return new Promise(async (resolve, reject) => {
if (this.isConnected && this.platform === "mobile") {
if (typeof this.client === "undefined") {
reject(new Error("WalletConnect client could not initialized"));
}

if (typeof this.client?.session === "undefined") {
reject(new Error("WalletConnect session could not initialized"));
}

try {
if (this.session && this.client) {
await this.client.disconnect({
topic: this.session.topic,
reason: getSdkError("USER_DISCONNECTED")
});

this.client = null;
this.session = null;
resolve(null);
}
} catch (error) {
reject(error);
}
}

await resetWalletDetailsFromStorage();
resolve(null);
});
}

private connectWithWebWallet(
resolve: (accounts: string[]) => void,
reject: (reason?: any) => void,
Expand Down Expand Up @@ -444,7 +368,7 @@ class PeraWalletConnect {
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.`
`Your wallet is connected to a different network to this dApp. Update your wallet to the correct network (MainNet or TestNet) to continue.`
)
);

Expand All @@ -461,9 +385,83 @@ class PeraWalletConnect {
removeModalWrapperFromDOM(PERA_WALLET_CONNECT_MODAL_ID);
}

return {
onWebWalletConnect
};
return onWebWalletConnect;
}

private async createClient() {
try {
const client = await Client.init({
relayUrl: "wss://relay.walletconnect.com",
projectId: this.projectId,
metadata: getAppMetadata()
});

this.client = client;

this.checkPersistedState(this.client);

this.client.on("session_delete", () => {
resetWalletDetailsFromStorage();
});

return client;
} catch (err) {
throw err;
}
}

private checkPersistedState(client: Client) {
if (typeof this.session !== "undefined") return;
// populates (the last) existing session to state

if (client.session.length) {
const lastKeyIndex = client!.session.keys.length - 1;
const session = client.session.get(client.session.keys[lastKeyIndex]);

this.session = session;

const {namespace, reference, accounts} = formatWalletConnectSessionResponse(
this.session
);

saveWalletDetailsToStorage(
accounts || [],
"pera-wallet",
`${namespace}:${reference}`
);
}
}

disconnect() {
return new Promise(async (resolve, reject) => {
if (this.isConnected && this.platform === "mobile") {
if (typeof this.client === "undefined") {
reject(new Error("WalletConnect client could not initialized"));
}

if (typeof this.client?.session === "undefined") {
reject(new Error("WalletConnect session could not initialized"));
}

try {
if (this.session && this.client) {
await this.client.disconnect({
topic: this.session.topic,
reason: getSdkError("USER_DISCONNECTED")
});

this.client = null;
this.session = null;
resolve(null);
}
} catch (error) {
reject(error);
}
}

await resetWalletDetailsFromStorage();
resolve(null);
});
}

private async signTransactionWithMobile(signTxnRequestParams: PeraWalletTransaction[]) {
Expand Down

0 comments on commit 08cbfc4

Please sign in to comment.