@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.
196 lines • 7.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WalletConnectProvider = void 0;
const tslib_1 = require("tslib");
const ethereum_provider_1 = require("@walletconnect/ethereum-provider");
const ethers_1 = require("ethers");
const BaseWalletProvider_1 = require("../BaseWalletProvider");
const wallet_types_1 = require("../../../types/wallet.types");
const chains = tslib_1.__importStar(require("viem/chains"));
const viem_1 = require("viem");
const signAuthorization_1 = require("./signAuthorization");
class WalletConnectProvider extends BaseWalletProvider_1.BaseWalletProvider {
provider = null;
constructor() {
super('WalletConnect', 'https://walletconnect.com/walletconnect-logo.svg', [
wallet_types_1.ChainType.WALLET_CONNECT,
]);
}
isInstalled = async () => {
return true; // WalletConnect doesn't require installation
};
getProvider = () => {
if (!this.provider) {
throw new Error('WalletConnect provider not initialized');
}
return this.provider;
};
getSigner = async () => {
const browserProvider = new ethers_1.BrowserProvider(this.getProvider());
return await browserProvider.getSigner();
};
async initProvider(chainId) {
console.log('Provder >>', this.provider);
if (this.provider) {
const hasSession = this.provider.session
&& this.provider.session?.topic;
if (hasSession) {
return;
}
await this.provider.disconnect();
this.provider = null;
}
this.provider = await ethereum_provider_1.EthereumProvider.init({
projectId: 'd73cf3ac4a677153d241b2bd5092fb1d',
chains: [chainId],
methods: ['eth_sendTransaction', 'personal_sign', 'eth_signTypedData_v4', 'eth_requestAccounts', 'eth_chainId', 'eth_accounts'],
showQrModal: true,
rpcMap: {
'11155111': 'https://sepolia.gateway.tenderly.co/',
},
optionalChains: [],
});
await this.provider.enable();
}
async connect() {
try {
const chain = chains['sepolia'];
const chainId = chain.id;
await this.initProvider(chainId);
const accounts = (await this.provider.request({
method: 'eth_requestAccounts',
}));
if (!accounts || accounts.length === 0) {
throw new Error('No connected account');
}
const rawAddress = accounts[0];
const checksumAddress = (0, ethers_1.getAddress)(rawAddress);
const caipAddress = this.formatAddress(checksumAddress, wallet_types_1.ChainType.ETHEREUM, chainId);
return caipAddress;
}
catch (error) {
console.error('Failed to connect to MetaMask:', error);
throw error;
}
}
getChainId = async () => {
const provider = this.getProvider();
if (!provider) {
throw new Error('Provider is undefined');
}
const hexChainId = (await provider.request({
method: 'eth_chainId',
params: [],
}));
const chainId = parseInt(hexChainId.toString(), 16);
return chainId;
};
signAndSendTransaction = async (txn) => {
try {
const provider = this.getProvider();
if (!provider) {
throw new Error('Provider is undefined');
}
const accounts = (await provider.request({
method: 'eth_accounts',
}));
if (!accounts || accounts.length === 0) {
throw new Error('No connected account');
}
const hex = (0, viem_1.bytesToHex)(txn);
const parsed = (0, viem_1.parseTransaction)(hex);
const txParams = {
from: accounts[0],
to: parsed.to,
value: parsed.value ? '0x' + parsed.value.toString(16) : undefined,
data: parsed.data,
gas: parsed.gas ? '0x' + parsed.gas.toString(16) : undefined,
maxPriorityFeePerGas: parsed.maxPriorityFeePerGas
? '0x' + parsed.maxPriorityFeePerGas.toString(16)
: undefined,
maxFeePerGas: parsed.maxFeePerGas
? '0x' + parsed.maxFeePerGas.toString(16)
: undefined,
};
const signature = await provider.request({
method: 'eth_sendTransaction',
params: [txParams],
});
return (0, viem_1.hexToBytes)(signature);
}
catch (error) {
console.error('MetaMask signing error:', error);
throw error;
}
};
signMessage = async (message) => {
try {
const provider = this.getProvider();
if (!provider) {
throw new Error('Provider is undefined');
}
const accounts = (await provider.request({
method: 'eth_accounts',
}));
if (!accounts || accounts.length === 0) {
throw new Error('No connected account');
}
const hexMessage = (0, viem_1.bytesToHex)(message);
const signature = await provider.request({
method: 'personal_sign',
params: [hexMessage, accounts[0]],
});
return (0, viem_1.hexToBytes)(signature);
}
catch (error) {
console.error('MetaMask signing error:', error);
throw error;
}
};
signTypedData = async (typedData) => {
try {
const provider = this.getProvider();
if (!provider) {
throw new Error('Provider is undefined');
}
const accounts = (await provider.request({
method: 'eth_accounts',
}));
if (!accounts || accounts.length === 0) {
throw new Error('No connected account');
}
typedData.types = {
EIP712Domain: [
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
...(typedData.primaryType === 'MigrationPayload'
? { MigrationPayload: typedData.types['MigrationPayload'] }
: { UniversalPayload: typedData.types['UniversalPayload'] }),
};
const signature = await provider.request({
method: 'eth_signTypedData_v4',
params: [accounts[0], JSON.stringify(typedData)],
});
return (0, viem_1.hexToBytes)(signature);
}
catch (error) {
console.error('MetaMask signing error:', error);
throw error;
}
};
signAuthorization = async (params) => {
const signer = await this.getSigner();
return (0, signAuthorization_1.signAuthorizationWithEthersSigner)(signer, params);
};
disconnect = async () => {
const provider = this.getProvider();
if (provider && typeof provider.disconnect === 'function') {
await provider.disconnect();
}
this.provider = null;
};
}
exports.WalletConnectProvider = WalletConnectProvider;
//# sourceMappingURL=walletConnect.js.map