UNPKG

@keccak256-evg/passport-sdk

Version:

T-REX Passport SDK for interacting with Passport and Registry contracts

294 lines 9.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnifiedPassportSDK = void 0; const viem_1 = require("viem"); const abi_1 = require("./abi"); const constants_1 = require("./constants"); /** * 统一的 Passport SDK - 支持任何 EIP-1193 兼容的 provider * * 兼容的 provider 包括: * - MetaMask (window.ethereum) * - WalletConnect * - Coinbase Wallet * - Trust Wallet * - 任何实现 EIP-1193 标准的钱包 */ class UnifiedPassportSDK { constructor(config) { this.config = config; // 将用户的链配置转换为 Viem Chain 格式 const viemChain = { id: config.chain.id, name: config.chain.name, nativeCurrency: config.chain.nativeCurrency || { name: 'Ether', symbol: 'ETH', decimals: 18, }, rpcUrls: config.chain.rpcUrls, blockExplorers: config.chain.blockExplorers, }; // 从 EIP-1193 provider 创建 Viem 客户端 this.publicClient = (0, viem_1.createPublicClient)({ chain: viemChain, transport: (0, viem_1.custom)(config.provider), }); this.walletClient = (0, viem_1.createWalletClient)({ chain: viemChain, transport: (0, viem_1.custom)(config.provider), }); // 获取注册表地址 this.registryAddress = (config.registryAddress || constants_1.CONTRACT_ADDRESSES[config.chain.id]?.registryAddress || constants_1.CONTRACT_ADDRESSES[1962].registryAddress); } /** * 获取当前使用的 provider 信息 */ getProviderInfo() { const provider = this.config.provider; return { isMetaMask: provider.isMetaMask || false, isCoinbaseWallet: provider.isCoinbaseWallet || false, chainId: provider.chainId, selectedAddress: provider.selectedAddress, }; } /** * 请求连接钱包 */ async connectWallet() { const accounts = await this.config.provider.request({ method: 'eth_requestAccounts', }); return accounts; } /** * 获取当前账户 */ async getAccounts() { const accounts = await this.config.provider.request({ method: 'eth_accounts', }); return accounts; } /** * 切换网络 */ async switchChain() { const chainId = `0x${this.config.chain.id.toString(16)}`; try { await this.config.provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId }], }); } catch (error) { // 如果网络不存在,尝试添加网络 if (error.code === 4902) { await this.addChain(); } else { throw error; } } } /** * 添加网络到钱包 */ async addChain() { const chainConfig = { chainId: `0x${this.config.chain.id.toString(16)}`, chainName: this.config.chain.name, rpcUrls: this.config.chain.rpcUrls.default.http, nativeCurrency: this.config.chain.nativeCurrency, blockExplorerUrls: this.config.chain.blockExplorers ? [this.config.chain.blockExplorers.default.url] : undefined, }; await this.config.provider.request({ method: 'wallet_addEthereumChain', params: [chainConfig], }); } // ========== Passport 业务方法 ========== /** * 创建 Passport */ async createPassport() { const contract = (0, viem_1.getContract)({ address: this.registryAddress, abi: abi_1.PASSPORT_REGISTRY_ABI, client: this.walletClient, }); const hash = await contract.write.createPassport(); return hash; } /** * 检查钱包是否有 Passport */ async checkWalletHasPassport(walletAddress) { try { const contract = (0, viem_1.getContract)({ address: this.registryAddress, abi: abi_1.PASSPORT_REGISTRY_ABI, client: this.publicClient, }); const passportId = await contract.read.walletToPassportId([walletAddress]); if (passportId === 0n) { return { hasPassport: false }; } const passportAddress = await contract.read.passportIdToContractAddress([passportId]); return { hasPassport: true, passportId: Number(passportId), passportAddress: passportAddress, }; } catch (error) { console.error("Error checking wallet passport:", error); throw error; } } /** * 获取 Passport 信息 */ async getPassportInfo(passportAddress) { try { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.publicClient, }); const [passportId, boundWallets, walletCount] = await Promise.all([ contract.read.passportId(), contract.read.getBoundWallets(), contract.read.getWalletCount(), ]); return { passportId: Number(passportId), passportAddress: passportAddress, boundWallets: boundWallets, walletCount: Number(walletCount), }; } catch (error) { console.error("Error getting passport info:", error); throw error; } } /** * 请求绑定钱包 */ async requestBindWallet(passportAddress, walletAddress) { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.walletClient, }); const hash = await contract.write.requestBindWallet([walletAddress]); return hash; } /** * 接受绑定请求 */ async acceptBindRequest(passportAddress) { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.walletClient, }); const hash = await contract.write.acceptBindRequest(); return hash; } /** * 拒绝绑定请求 */ async rejectBindRequest(passportAddress) { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.walletClient, }); const hash = await contract.write.rejectBindRequest(); return hash; } /** * 解绑钱包 */ async unbindWallet(passportAddress) { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.walletClient, }); const hash = await contract.write.unbindWallet(); return hash; } /** * 取消绑定请求 */ async cancelBindRequest(passportAddress, walletAddress) { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.walletClient, }); const hash = await contract.write.cancelBindRequest([walletAddress]); return hash; } /** * 获取待处理绑定请求信息 */ async getPendingBindRequest(passportAddress, walletAddress) { try { const contract = (0, viem_1.getContract)({ address: passportAddress, abi: abi_1.PASSPORT_ABI, client: this.publicClient, }); const [exists, requester, timestamp, expired] = await contract.read.getPendingBindRequest([walletAddress]); return { exists: Boolean(exists), requester: requester, timestamp: Number(timestamp), expired: Boolean(expired), }; } catch (error) { console.error("Error getting pending bind request:", error); throw error; } } /** * 监听 provider 事件 */ onAccountsChanged(callback) { if (this.config.provider.on) { this.config.provider.on('accountsChanged', (accounts) => { callback(accounts); }); } } /** * 监听链切换事件 */ onChainChanged(callback) { if (this.config.provider.on) { this.config.provider.on('chainChanged', (chainId) => { callback(chainId); }); } } /** * 移除事件监听器 */ removeListener(eventName, callback) { if (this.config.provider.removeListener) { this.config.provider.removeListener(eventName, callback); } } } exports.UnifiedPassportSDK = UnifiedPassportSDK; //# sourceMappingURL=UnifiedPassportSDK.js.map