@keccak256-evg/passport-sdk
Version:
T-REX Passport SDK for interacting with Passport and Registry contracts
477 lines • 15.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViemPassportSDK = void 0;
exports.createViemPassportSDK = createViemPassportSDK;
const viem_1 = require("viem");
const abi_1 = require("./abi");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
class ViemPassportSDK {
constructor(config) {
this.config = config;
this.publicClient = config.publicClient;
this.walletClient = config.walletClient;
// 获取合约地址
this.registryAddress = (config.registryAddress ||
constants_1.CONTRACT_ADDRESSES[config.network.id]?.registryAddress ||
constants_1.CONTRACT_ADDRESSES[1962].registryAddress);
}
/**
* 设置 Wallet Client
*/
setWalletClient(walletClient) {
this.walletClient = walletClient;
return this;
}
/**
* 获取 Registry 合约实例
*/
getRegistryContract() {
return (0, viem_1.getContract)({
address: this.registryAddress,
abi: abi_1.PASSPORT_REGISTRY_ABI,
client: this.publicClient,
});
}
/**
* 获取 Passport 合约实例
*/
getPassportContract(passportAddress) {
return (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.publicClient,
});
}
// Registry 方法实现
/**
* 创建 Passport
*/
async createPassport() {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: this.registryAddress,
abi: abi_1.PASSPORT_REGISTRY_ABI,
client: this.walletClient,
});
return contract.write.createPassport();
}
/**
* 检查钱包是否有 Passport
*/
async checkWalletHasPassport(walletAddress) {
try {
const contract = this.getRegistryContract();
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 = this.getPassportContract(passportAddress);
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;
}
}
/**
* 预测 Passport 地址
*/
async predictPassportAddress(walletAddress) {
try {
const contract = this.getRegistryContract();
const predictedAddress = await contract.read.predictPassportAddress([walletAddress]);
return predictedAddress;
}
catch (error) {
console.error("Error predicting passport address:", error);
throw error;
}
}
/**
* 升级我的 Passport
*/
async upgradeMyPassport() {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: this.registryAddress,
abi: abi_1.PASSPORT_REGISTRY_ABI,
client: this.walletClient,
});
return contract.write.upgradeMyPassport();
}
/**
* 获取 Passport 实现地址
*/
async getPassportImplementation() {
try {
const contract = this.getRegistryContract();
const implementation = await contract.read.getPassportImplementation();
return implementation;
}
catch (error) {
console.error("Error getting passport implementation:", error);
throw error;
}
}
/**
* 获取 Passport ID 计数器
*/
async getPassportIdCounter() {
try {
const contract = this.getRegistryContract();
const counter = await contract.read.getPassportIdCounter();
return Number(counter);
}
catch (error) {
console.error("Error getting passport ID counter:", error);
throw error;
}
}
/**
* 通过 Passport ID 获取合约地址
*/
async getPassportAddressById(passportId) {
try {
const contract = this.getRegistryContract();
const passportAddress = await contract.read.passportIdToContractAddress([BigInt(passportId)]);
return passportAddress;
}
catch (error) {
console.error("Error getting passport address by ID:", error);
throw error;
}
}
/**
* 检查钱包是否被废除
*/
async isWalletAbolished(walletAddress) {
try {
const contract = this.getRegistryContract();
const isAbolished = await contract.read.isWalletAbolished([walletAddress]);
return Boolean(isAbolished);
}
catch (error) {
console.error("Error checking if wallet is abolished:", error);
throw error;
}
}
/**
* 检查钱包是否从特定 Passport 解绑
*/
async isWalletUnboundFromPassport(passportId, walletAddress) {
try {
const contract = this.getRegistryContract();
const isUnbound = await contract.read.isWalletUnboundFromPassport([BigInt(passportId), walletAddress]);
return Boolean(isUnbound);
}
catch (error) {
console.error("Error checking if wallet is unbound from passport:", error);
throw error;
}
}
/**
* 检查钱包是否有特定 Passport
*/
async hasPassport(walletAddress, passportContractAddress) {
try {
const contract = this.getRegistryContract();
const hasPassport = await contract.read.hasPassport([walletAddress, passportContractAddress]);
return Boolean(hasPassport);
}
catch (error) {
console.error("Error checking if wallet has passport:", error);
throw error;
}
}
// Passport 方法实现
/**
* 解绑钱包
*/
async unbindWallet(passportAddress) {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.walletClient,
});
return contract.write.unbindWallet();
}
/**
* 请求绑定钱包
*/
async requestBindWallet(passportAddress, walletAddress) {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.walletClient,
});
return contract.write.requestBindWallet([walletAddress]);
}
/**
* 获取待处理绑定请求信息
*/
async getPendingBindRequest(passportAddress, walletAddress) {
try {
const contract = this.getPassportContract(passportAddress);
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;
}
}
/**
* 取消绑定请求
*/
async cancelBindRequest(passportAddress, walletAddress) {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.walletClient,
});
return contract.write.cancelBindRequest([walletAddress]);
}
/**
* 接受绑定请求
*/
async acceptBindRequest(passportAddress) {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.walletClient,
});
return contract.write.acceptBindRequest();
}
/**
* 拒绝绑定请求
*/
async rejectBindRequest(passportAddress) {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.walletClient,
});
return contract.write.rejectBindRequest();
}
/**
* 获取待处理绑定钱包列表
*/
async getPendingBindWallets(passportAddress, account) {
return (0, utils_1.getPendingBindWalletsWithViem)(this.publicClient, passportAddress, account);
}
/**
* 检查 Passport 是否需要升级
*/
async checkPassportUpgrade(passportAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const result = await contract.read.requireUpgrade();
return Boolean(result);
}
catch (error) {
console.error('Error checking passport upgrade:', error);
throw error;
}
}
/**
* 获取 Passport 当前实现地址
*/
async getPassportImplementationAddress(passportAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const implementation = await contract.read.implementation();
return implementation;
}
catch (error) {
console.error("Error getting passport implementation address:", error);
throw error;
}
}
/**
* 获取 Passport ID
*/
async getPassportId(passportAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const passportId = await contract.read.passportId();
return Number(passportId);
}
catch (error) {
console.error("Error getting passport ID:", error);
throw error;
}
}
/**
* 获取 PassportRegistry 合约地址
*/
async getRegistryAddress(passportAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const registry = await contract.read.registry();
return registry;
}
catch (error) {
console.error("Error getting registry address:", error);
throw error;
}
}
/**
* 检查钱包是否绑定到 Passport
*/
async isWalletBound(passportAddress, walletAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const isBound = await contract.read.isWalletBound([walletAddress]);
return Boolean(isBound);
}
catch (error) {
console.error("Error checking if wallet is bound:", error);
throw error;
}
}
/**
* 获取所有绑定钱包
*/
async getBoundWallets(passportAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const boundWallets = await contract.read.getBoundWallets();
return boundWallets;
}
catch (error) {
console.error("Error getting bound wallets:", error);
throw error;
}
}
/**
* 获取钱包数量
*/
async getWalletCount(passportAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const walletCount = await contract.read.getWalletCount();
return Number(walletCount);
}
catch (error) {
console.error("Error getting wallet count:", error);
throw error;
}
}
/**
* 通过索引获取绑定钱包
*/
async getBoundWalletByIndex(passportAddress, index) {
try {
const contract = this.getPassportContract(passportAddress);
const wallet = await contract.read.boundWallets([BigInt(index)]);
return wallet;
}
catch (error) {
console.error("Error getting bound wallet by index:", error);
throw error;
}
}
/**
* 检查是否有待处理绑定请求
*/
async hasPendingBindRequest(passportAddress, walletAddress) {
try {
const contract = this.getPassportContract(passportAddress);
const hasRequest = await contract.read.hasPendingBindRequest([walletAddress]);
return Boolean(hasRequest);
}
catch (error) {
console.error("Error checking if wallet has pending bind request:", error);
throw error;
}
}
/**
* 取消过期的绑定请求
*/
async cancelExpiredBindRequest(passportAddress, walletAddress) {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations');
}
const contract = (0, viem_1.getContract)({
address: passportAddress,
abi: abi_1.PASSPORT_ABI,
client: this.walletClient,
});
return contract.write.cancelExpiredBindRequest([walletAddress]);
}
}
exports.ViemPassportSDK = ViemPassportSDK;
// 便利的创建函数,参考模板中的使用方式
function createViemPassportSDK(config) {
const chain = config.chain || constants_1.tRexTestnet;
const publicClient = (0, utils_1.createViemPublicClient)({
rpcUrl: config.rpcUrl,
chain,
});
const walletClient = config.account ? (0, utils_1.createViemWalletClient)({
rpcUrl: config.rpcUrl,
chain,
account: config.account,
}) : undefined;
return new ViemPassportSDK({
publicClient,
walletClient,
network: chain,
env: config.env || constants_1.Environment.DEV,
registryAddress: config.registryAddress,
rpcUrl: config.rpcUrl,
});
}
//# sourceMappingURL=ViemPassportSDK.js.map