@pushchain/ui-kit
Version:
Push Chain is a true universal L1 that is 100% EVM compatible. It allows developers to deploy once and make their apps instantly compatible with users from all other L1s (Ethereum, Solana, etc) with zero on-chain code change.
82 lines • 3.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGuardedPushChain = createGuardedPushChain;
function createGuardedPushChain(baseClient, handleExternalWalletConnection, requestPushWalletConnection, checkAndShowUpgradeIfNeeded, universalSigner, intializeProps, uid, callback) {
const clientRef = { current: baseClient };
let promoting = null;
const promoteIfNeeded = async () => {
if (!clientRef.current.isReadMode)
return;
if (!promoting) {
promoting = (async () => {
const walletInfo = localStorage.getItem(`walletInfo_${uid}`);
const walletData = walletInfo ? JSON.parse(walletInfo) : null;
if (!walletData) {
return;
}
if (walletData.wallet.providerName) {
await handleExternalWalletConnection({
chain: walletData.wallet.chainType,
provider: walletData.wallet.providerName
});
}
else {
await requestPushWalletConnection();
}
const pushChainClient = await clientRef.current.reinitialize(universalSigner, intializeProps);
callback?.();
clientRef.current = pushChainClient;
})().finally(() => {
promoting = null;
});
}
await promoting;
};
const checkUpgradeNeeded = async () => {
const status = await checkAndShowUpgradeIfNeeded(clientRef.current);
if (!status) {
throw new Error('Account upgrade failed.');
}
};
const wrapWrite = (getter) => {
const wrapped = async (...args) => {
await promoteIfNeeded();
await checkUpgradeNeeded();
const fn = getter();
return fn(...args);
};
return wrapped;
};
const universalProxy = new Proxy({}, {
get(_t, p, _r) {
const u = clientRef.current.universal;
if (p === "sendTransaction") {
return wrapWrite(() => clientRef.current.universal.sendTransaction);
}
if (p === "executeTransactions") {
return wrapWrite(() => clientRef.current.universal.executeTransactions);
}
if (p === "prepareTransaction") {
return wrapWrite(() => clientRef.current.universal.prepareTransaction);
}
if (p === "signMessage") {
return wrapWrite(() => clientRef.current.universal.signMessage);
}
if (p === "signTypedData") {
return wrapWrite(() => clientRef.current.universal.signTypedData);
}
// @ts-expect-error: index access for dynamic property
return u[p];
},
});
const clientProxy = new Proxy(baseClient, {
get(_target, prop, _receiver) {
if (prop === "universal")
return universalProxy;
// @ts-expect-error: index access for dynamic property
return clientRef.current[prop];
},
});
return clientProxy;
}
//# sourceMappingURL=txnAuthGuard.js.map