Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
XueMoMo committed Oct 23, 2024
1 parent a6c269d commit 6666cb7
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 94 deletions.
6 changes: 4 additions & 2 deletions lib/hooks/useOnClickOut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export default function useOnClickOutside(handler: (e: any) => void) {
if (!ref.current || ref.current === e.target || ref.current.contains(e.target)) return;
if (handRef.current) handRef.current(e);
};
document?.addEventListener("click", handleClickOutside);
document?.addEventListener('mousedown', handleClickOutside)
document?.addEventListener('touchstart', handleClickOutside)
return () => {
document?.removeEventListener("click", handleClickOutside);
document?.removeEventListener("mousedown", handleClickOutside);
document?.removeEventListener("touchend", handleClickOutside);
};
}, [ref.current]);
return ref;
Expand Down
5 changes: 3 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ export const getErrorMsg = (error: any, def = 'Network Error'): string => {
if (typeof error == 'string') msg = error
else if (typeof error?.msg == 'string') msg = error?.msg
else if (typeof error?.message == 'string') msg = error?.message
// replace
if (msg.includes('User denied') || msg.includes('user rejected transaction') || msg.includes('User rejected'))
// replace
const msgLow = msg.toLowerCase()
if (msgLow.includes('denied') || msgLow.includes('rejected') || msgLow.includes('declined'))
return 'You declined the action in your wallet.'
if (msg.includes('transaction failed')) return 'Transaction failed'
return msg
Expand Down
35 changes: 22 additions & 13 deletions lib/wallet/Algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { AlgorandChainIDs } from "@perawallet/connect/dist/util/peraWalletTypes"
import { algorandConfig } from "./config";
import { BaseWallet, LoginUser } from "./types";
import { PeraWalletConnect } from "@perawallet/connect";
import { getErrorMsg } from "../utils";
import { UserClosed } from "./tools";

export class Algorand extends BaseWallet {
name = "Algorand";
Expand Down Expand Up @@ -30,20 +32,27 @@ export class Algorand extends BaseWallet {
return [];
}
async connect(): Promise<LoginUser> {
if (this.isConnected && this.wallet.isConnected) {
return { account: this.account, wallet: "algorand" };
}
if (!this.wallet.isConnected) {
this.accounts = await this.wallet.connect();
this.isConnected = true;
try {
if (this.isConnected && this.wallet.isConnected) {
return { account: this.account, wallet: "algorand" };
}
if (!this.wallet.isConnected) {
this.accounts = await this.wallet.connect();
this.isConnected = true;
}
this.wallet.connector?.on("disconnect", () => {
this.wallet.disconnect();
this.account = null;
this.isInit = false;
this.isConnected = false;
});
this.account = this.accounts[0];
} catch (error) {
if (getErrorMsg(error).includes("Connect modal is closed by user")) {
throw new Error(UserClosed);
}
throw error;
}
this.wallet.connector?.on("disconnect", () => {
this.wallet.disconnect();
this.account = null;
this.isInit = false;
this.isConnected = false;
});
this.account = this.accounts[0];
return { account: this.account, wallet: "algorand" };
}

Expand Down
52 changes: 33 additions & 19 deletions lib/wallet/Crust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { typesBundleForPolkadot } from "@crustio/type-definitions";
import { ApiPromise } from "@polkadot/api";
import { InjectedExtension, InjectedWindowProvider } from "@polkadot/extension-inject/types";
import { stringToHex } from "@polkadot/util";
import { formatToCrustAccount } from "../utils";
import { getPerfix, sleep } from "./tools";
import { formatToCrustAccount, getErrorMsg } from "../utils";
import { getPerfix, sleep, UserRejectError } from "./tools";
import { BaseWallet, LoginUser } from "./types";

export class Crust extends BaseWallet {
Expand Down Expand Up @@ -42,27 +42,41 @@ export class Crust extends BaseWallet {
}

async connect(): Promise<LoginUser> {
if (!this.isConnected) {
if (!this.provider) throw "Crust Wallet not installed";
const hasAuth = await this.enable();
if (!hasAuth) throw "Error: cancel";
const accounts = await this.fetchAccounts();
if (accounts.length === 0) throw "Error: no account";
this.isConnected = true;
this.account = accounts[0];
try {
if (!this.isConnected) {
if (!this.provider) throw "Crust Wallet not installed";
const hasAuth = await this.enable();
if (!hasAuth) throw UserRejectError;
const accounts = await this.fetchAccounts();
if (accounts.length === 0) throw UserRejectError;
this.isConnected = true;
this.account = accounts[0];
}
return { account: this.account, wallet: "crust" };
} catch (error) {
console.info("error:", getErrorMsg(error), error);
if (getErrorMsg(error) == "Cancelled") {
throw UserRejectError;
}
throw error;
}
return { account: this.account, wallet: "crust" };
}

async sign(data: string, account: string | undefined): Promise<string> {
if (!this.provider) throw "Error: no wallet";
if (!this.wallet.signer) throw "Error: wallet error no signer";
const res: { signature } = await this.wallet.signer.signRaw({
address: account,
type: "bytes",
data: stringToHex(data),
});
return res.signature;
try {
const res: { signature } = await this.wallet.signer.signRaw({
address: account,
type: "bytes",
data: stringToHex(data),
});
return res.signature;
} catch (error) {
console.info("error:", getErrorMsg(error), error);
if (getErrorMsg(error) == "The request was cancelled.") {
throw UserRejectError;
}
throw error;
}
}

async enable(): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions lib/wallet/Metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export class Metamask extends BaseWallet {

async sign(data: string, account?: string): Promise<string> {
console.log("data:::", data);
const msg = Buffer.from(data, "utf8").toString("hex");
// const msg = data;
// const msg = Buffer.from(data, "utf8").toString("hex");
const msg = data;

console.info("msg::", msg);
if (!this.ethereum?.request) return Promise.reject("Error");
Expand Down
50 changes: 32 additions & 18 deletions lib/wallet/PolkadotJs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BaseWallet, LoginUser } from "./types";
import { InjectedExtension, InjectedWindowProvider } from "@polkadot/extension-inject/types";
import { stringToHex } from "@polkadot/util";
import { sleep } from "./tools";
import { sleep, UserRejectError } from "./tools";
import { getErrorMsg } from "../utils";

export class PolkadotJs extends BaseWallet {
name = "Polkadot";
Expand Down Expand Up @@ -32,27 +33,41 @@ export class PolkadotJs extends BaseWallet {
}

async connect(): Promise<LoginUser> {
if (!this.isConnected) {
if (!this.provider) throw "PolkadotJs Wallet not installed";
const hasAuth = await this.enable();
if (!hasAuth) throw "Error: cancel";
const accounts = await this.fetchAccounts();
if (accounts.length === 0) throw "Error: no account";
this.isConnected = true;
this.account = accounts[0];
try {
if (!this.isConnected) {
if (!this.provider) throw "PolkadotJs Wallet not installed";
const hasAuth = await this.enable();
if (!hasAuth) throw UserRejectError;
const accounts = await this.fetchAccounts();
if (accounts.length === 0) throw UserRejectError;
this.isConnected = true;
this.account = accounts[0];
}
} catch (error) {
console.info("error:", getErrorMsg(error), error);
if (getErrorMsg(error) == "Cancelled") {
throw UserRejectError;
}
throw error;
}
return { account: this.account, wallet: "polkadot-js" };
}

async sign(data: string, account: string | undefined): Promise<string> {
if (!this.provider) throw "Error: no wallet";
if (!this.wallet.signer) throw "Error: wallet error no signer";
const res: { signature } = await this.wallet.signer.signRaw({
address: account,
type: "bytes",
data: stringToHex(data),
});
return res.signature;
try {
const res: { signature } = await this.wallet.signer.signRaw({
address: account,
type: "bytes",
data: stringToHex(data),
});
return res.signature;
} catch (error) {
console.info("sign error:", getErrorMsg(error), error);
if (getErrorMsg(error) == "Cancelled") {
throw UserRejectError;
}
throw error;
}
}

async enable(): Promise<boolean> {
Expand All @@ -72,7 +87,6 @@ export class PolkadotJs extends BaseWallet {
}
}


async login(): Promise<string[]> {
const hasAuth = await this.enable();
if (!hasAuth) throw "Error: cancel";
Expand Down
69 changes: 42 additions & 27 deletions lib/wallet/Talisman.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getWallets, Wallet } from "@talismn/connect-wallets";
import { sleep } from "./tools";
import { sleep, UserRejectError } from "./tools";
import { BaseWallet, LoginUser } from "./types";
import { getErrorMsg } from "../utils";

interface AccountInfo {
address: string;
Expand Down Expand Up @@ -37,37 +38,51 @@ export class Talisman extends BaseWallet {
}

async connect(): Promise<LoginUser> {
if (!this.isConnected) {
if (!this.provider) throw "Talisman Wallet not installed";
const hasAuth = await this.enable();
if (!hasAuth) throw "Error: cancel";
this.accounts = await this.fetchAccounts();
if (this.accounts.length === 0) throw "Error: no account";
this.isConnected = true;
this.account = this.accounts[0];
try {
if (!this.isConnected) {
if (!this.provider) throw "Talisman Wallet not installed";
const hasAuth = await this.enable();
if (!hasAuth) throw UserRejectError;
this.accounts = await this.fetchAccounts();
if (this.accounts.length === 0) throw UserRejectError;
this.isConnected = true;
this.account = this.accounts[0];
}
} catch (error) {
console.info("error:", getErrorMsg(error), error);
if (getErrorMsg(error) == "Cancelled") {
throw UserRejectError;
}
throw error;
}
return { account: this.account, wallet: "talisman" };
}
async sign(data: string, account: string | undefined): Promise<string> {
if (!this.provider) throw "Error: no wallet";
if (!this.wallet.signer) throw "Error: wallet error no signer";
const accounts = ((await new Promise((resolve, _) => {
this.provider.subscribeAccounts((accounts) => {
resolve(accounts);
});
})) as unknown) as any[];
const walletAccount = accounts.find((e) => e.address === account);
/**
* walletAccount.type: "ethereum" | "sr25519"
*/
console.log("walletAccount:::", walletAccount);
try {
const accounts = ((await new Promise((resolve, _) => {
this.provider.subscribeAccounts((accounts) => {
resolve(accounts);
});
})) as unknown) as any[];
const walletAccount = accounts.find((e) => e.address === account);
/**
* walletAccount.type: "ethereum" | "sr25519"
*/
console.log("walletAccount:::", walletAccount);

const res: { signature } = await walletAccount.signer.signRaw({
address: account,
type: "bytes",
data,
});
return res.signature;
const res: { signature } = await walletAccount.signer.signRaw({
address: account,
type: "bytes",
data,
});
return res.signature;
} catch (error) {
console.info("error:", getErrorMsg(error), error);
if (getErrorMsg(error) == "Cancelled") {
throw UserRejectError;
}
throw error;
}
}

async enable(): Promise<boolean> {
Expand Down
6 changes: 3 additions & 3 deletions lib/wallet/TonConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TonConnectUI, toUserFriendlyAddress } from "@tonconnect/ui-react";
import { createHash } from "crypto";
import { numberToBytes } from "viem";
import { IS_DEV } from "../env";
import { getPerfix } from "./tools";
import { getPerfix, UserClosed } from "./tools";
import { BaseWallet, LoginUser } from "./types";

interface Domain {
Expand Down Expand Up @@ -159,8 +159,8 @@ export class TonConnect extends BaseWallet {
this.unSubModalStatusChange();
this.unSubModalStatusChange = this.tonconnectui.onModalStateChange((s) => {
console.info("ton modal:", s);
s.status == "closed" && !isResloved && reject(new Error("Cancelled"));
this.unSubModalStatusChange();
s.status == "closed" && !isResloved && reject(new Error(UserClosed));
s.status == "closed" && this.unSubModalStatusChange();
});
this.tonconnectui.openModal();
});
Expand Down
3 changes: 3 additions & 0 deletions lib/wallet/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ export async function updateAuth(u: LoginUser, w: BaseWallet = WALLETMAP[u.walle
u.authBasic = authBasic;
u.authBearer = authBearer;
}

export const UserRejectError = "User rejected action";
export const UserClosed = "UserClosed";
Loading

0 comments on commit 6666cb7

Please sign in to comment.