mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-06-13 18:11:00 +00:00
fix: cache web3Enable to prevent authorization flooding
Consolidated all web3Enable/web3FromAddress calls through getSigner helper. Cached web3Enable promise so it only runs once per session.
This commit is contained in:
@@ -9,9 +9,9 @@ import { usePezkuwi } from './PezkuwiContext';
|
|||||||
import { WALLET_ERRORS, formatBalance, ASSET_IDS } from '@pezkuwi/lib/wallet';
|
import { WALLET_ERRORS, formatBalance, ASSET_IDS } from '@pezkuwi/lib/wallet';
|
||||||
import type { InjectedAccountWithMeta } from '@pezkuwi/extension-inject/types';
|
import type { InjectedAccountWithMeta } from '@pezkuwi/extension-inject/types';
|
||||||
import type { Signer } from '@pezkuwi/api/types';
|
import type { Signer } from '@pezkuwi/api/types';
|
||||||
import { web3Enable, web3FromAddress } from '@pezkuwi/extension-dapp';
|
|
||||||
import { isMobileApp, signTransactionNative, type TransactionPayload } from '@/lib/mobile-bridge';
|
import { isMobileApp, signTransactionNative, type TransactionPayload } from '@/lib/mobile-bridge';
|
||||||
import { createWCSigner, isWCConnected, validateSession } from '@/lib/walletconnect-service';
|
import { createWCSigner, isWCConnected, validateSession } from '@/lib/walletconnect-service';
|
||||||
|
import { getSigner as getUniversalSigner } from '@/lib/get-signer';
|
||||||
|
|
||||||
interface TokenBalances {
|
interface TokenBalances {
|
||||||
HEZ: string;
|
HEZ: string;
|
||||||
@@ -275,9 +275,7 @@ export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Desktop / pezWallet DApps browser: Use extension signer
|
// Desktop / pezWallet DApps browser: Use extension signer
|
||||||
const { web3Enable: enable, web3FromAddress: fromAddress } = await import('@pezkuwi/extension-dapp');
|
const injector = await getUniversalSigner(pezkuwi.selectedAccount.address, pezkuwi.walletSource, pezkuwi.api);
|
||||||
await enable('PezkuwiChain');
|
|
||||||
const injector = await fromAddress(pezkuwi.selectedAccount.address);
|
|
||||||
|
|
||||||
const hash = await (tx as { signAndSend: (address: string, options: { signer: unknown }) => Promise<{ toHex: () => string }> }).signAndSend(
|
const hash = await (tx as { signAndSend: (address: string, options: { signer: unknown }) => Promise<{ toHex: () => string }> }).signAndSend(
|
||||||
pezkuwi.selectedAccount.address,
|
pezkuwi.selectedAccount.address,
|
||||||
@@ -318,9 +316,7 @@ export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extension signing
|
// Extension signing
|
||||||
const { web3Enable: enable, web3FromAddress: fromAddress } = await import('@pezkuwi/extension-dapp');
|
const injector = await getUniversalSigner(pezkuwi.selectedAccount.address, pezkuwi.walletSource, pezkuwi.api);
|
||||||
await enable('PezkuwiChain');
|
|
||||||
const injector = await fromAddress(pezkuwi.selectedAccount.address);
|
|
||||||
|
|
||||||
if (!injector.signer.signRaw) {
|
if (!injector.signer.signRaw) {
|
||||||
throw new Error('Wallet does not support message signing');
|
throw new Error('Wallet does not support message signing');
|
||||||
@@ -348,17 +344,9 @@ export const WalletProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (pezkuwi.walletSource === 'walletconnect' && isWCConnected() && validateSession() && pezkuwi.api) {
|
const injector = await getUniversalSigner(pezkuwi.selectedAccount.address, pezkuwi.walletSource, pezkuwi.api);
|
||||||
const genesisHash = pezkuwi.api.genesisHash.toHex();
|
setSigner(injector.signer as unknown as Signer);
|
||||||
const wcSigner = createWCSigner(genesisHash, pezkuwi.selectedAccount.address);
|
if (import.meta.env.DEV) console.log('✅ Signer obtained for', pezkuwi.selectedAccount.address);
|
||||||
setSigner(wcSigner as unknown as Signer);
|
|
||||||
if (import.meta.env.DEV) console.log('✅ WC Signer obtained for', pezkuwi.selectedAccount.address);
|
|
||||||
} else if (pezkuwi.walletSource !== 'walletconnect') {
|
|
||||||
await web3Enable('PezkuwiChain');
|
|
||||||
const injector = await web3FromAddress(pezkuwi.selectedAccount.address);
|
|
||||||
setSigner(injector.signer);
|
|
||||||
if (import.meta.env.DEV) console.log('✅ Extension Signer obtained for', pezkuwi.selectedAccount.address);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (import.meta.env.DEV) console.error('Failed to get signer:', error);
|
if (import.meta.env.DEV) console.error('Failed to get signer:', error);
|
||||||
setSigner(null);
|
setSigner(null);
|
||||||
|
|||||||
@@ -16,6 +16,16 @@ interface SignerResult {
|
|||||||
signer: any; // Compatible with @pezkuwi/api Signer
|
signer: any; // Compatible with @pezkuwi/api Signer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache web3Enable to avoid "Too many authorization requests" error
|
||||||
|
let enablePromise: Promise<unknown[]> | null = null;
|
||||||
|
|
||||||
|
async function ensureWeb3Enabled(): Promise<void> {
|
||||||
|
if (!enablePromise) {
|
||||||
|
enablePromise = web3Enable('PezkuwiChain');
|
||||||
|
}
|
||||||
|
await enablePromise;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getSigner(
|
export async function getSigner(
|
||||||
address: string,
|
address: string,
|
||||||
walletSource: WalletSource,
|
walletSource: WalletSource,
|
||||||
@@ -34,7 +44,7 @@ export async function getSigner(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extension or native: use web3FromAddress
|
// Extension or native: use web3FromAddress
|
||||||
await web3Enable('PezkuwiChain');
|
await ensureWeb3Enabled();
|
||||||
const injector = await web3FromAddress(address);
|
const injector = await web3FromAddress(address);
|
||||||
return injector;
|
return injector;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user