UNPKG

@keccak256-evg/passport-sdk

Version:

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

136 lines 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyContractMethods = verifyContractMethods; exports.quickContractCheck = quickContractCheck; const thirdweb_1 = require("thirdweb"); const abi_1 = require("./abi"); /** * 验证合约地址并检查支持的方法 */ async function verifyContractMethods(client, chain, contractAddress) { const result = { contractAddress, chainId: chain.id, isAccessible: false, supportedMethods: [], recommendations: [] }; try { const contract = (0, thirdweb_1.getContract)({ client, chain, address: contractAddress, abi: abi_1.PASSPORT_REGISTRY_ABI, }); // 测试基本连接 try { await (0, thirdweb_1.readContract)({ contract, method: "getPassportIdCounter", params: [], }); result.isAccessible = true; } catch (error) { result.isAccessible = false; result.recommendations.push("合约无法访问,请检查地址和网络配置"); return result; } // 检查各个方法(根据实际合约定义) const methodsToCheck = [ { name: "isWalletAbolished", params: ["0x0000000000000000000000000000000000000000"] }, { name: "isWalletUnboundFromPassport", params: [1n, "0x0000000000000000000000000000000000000000"] }, { name: "walletToPassportId", params: ["0x0000000000000000000000000000000000000000"] }, { name: "passportIdToContractAddress", params: [1n] }, { name: "getPassportIdCounter", params: [] }, { name: "getPassportImplementation", params: [] }, { name: "predictPassportAddress", params: ["0x0000000000000000000000000000000000000000"] }, { name: "hasPassport", params: ["0x0000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000"] }, ]; for (const method of methodsToCheck) { const check = { method: method.name, exists: false }; try { await (0, thirdweb_1.readContract)({ contract, method: method.name, params: method.params, }); check.exists = true; } catch (error) { check.exists = false; check.error = error.message; // 如果是方法不存在的错误 if (error.message?.includes('does not exist') || error.message?.includes('not found') || error.message?.includes('execution reverted')) { check.error = `方法 ${method.name} 在此合约中不存在`; } } result.supportedMethods.push(check); } // 生成建议 const missingMethods = result.supportedMethods.filter(m => !m.exists); if (missingMethods.length > 0) { result.recommendations.push(`以下方法不受支持: ${missingMethods.map(m => m.method).join(', ')}`); // 检查是否应该使用标准地址 if (contractAddress !== '0x1B326360Ec9E3cEF6129173D35b86a6803e5751F') { result.recommendations.push('建议使用标准Registry地址: 0x1B326360Ec9E3cEF6129173D35b86a6803e5751F'); } // 检查核心方法的存在性 const hasIsWalletAbolished = result.supportedMethods.find(m => m.method === 'isWalletAbolished')?.exists; const hasIsWalletUnbound = result.supportedMethods.find(m => m.method === 'isWalletUnboundFromPassport')?.exists; if (!hasIsWalletAbolished) { result.recommendations.push('合约缺少 isWalletAbolished 方法,可能是不兼容的版本'); } if (!hasIsWalletUnbound) { result.recommendations.push('合约缺少 isWalletUnboundFromPassport 方法,可能是不兼容的版本'); } } return result; } catch (error) { result.recommendations.push(`合约验证失败: ${error}`); return result; } } /** * 快速检查合约兼容性 */ async function quickContractCheck(client, chain, contractAddress) { const verification = await verifyContractMethods(client, chain, contractAddress); const requiredMethods = ['walletToPassportId', 'getPassportIdCounter']; const optionalMethods = ['isWalletAbolished', 'abolishedWallets', 'isWalletUnboundFromPassport']; const issues = []; const suggestions = []; if (!verification.isAccessible) { issues.push('合约无法访问'); suggestions.push('检查合约地址和网络配置'); return { isCompatible: false, issues, suggestions }; } // 检查必需方法 for (const method of requiredMethods) { const methodCheck = verification.supportedMethods.find(m => m.method === method); if (!methodCheck?.exists) { issues.push(`缺少必需方法: ${method}`); } } // 检查可选方法 const isWalletAbolishedExists = verification.supportedMethods.find(m => m.method === 'isWalletAbolished')?.exists; const isWalletUnboundExists = verification.supportedMethods.find(m => m.method === 'isWalletUnboundFromPassport')?.exists; if (!isWalletAbolishedExists) { issues.push('isWalletAbolished 方法不存在'); } if (!isWalletUnboundExists) { issues.push('isWalletUnboundFromPassport 方法不存在'); } const isCompatible = issues.length === 0; if (!isCompatible && contractAddress !== '0x1B326360Ec9E3cEF6129173D35b86a6803e5751F') { suggestions.push('尝试使用标准Registry地址: 0x1B326360Ec9E3cEF6129173D35b86a6803e5751F'); } return { isCompatible, issues, suggestions }; } //# sourceMappingURL=ContractVerification.js.map