@citizenwallet/sdk
Version:
An sdk to easily work with citizen wallet.
108 lines • 4.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSafeOwner = exports.verifyAccountOwnership = exports.getAccountBalance = exports.getAccountAddress = exports.getENSAddress = void 0;
const AccountFactory_abi_json_1 = __importDefault(require("../abi/AccountFactory.abi.json"));
const ERC20_abi_json_1 = __importDefault(require("../abi/ERC20.abi.json"));
const Account_abi_json_1 = __importDefault(require("../abi/Account.abi.json"));
const Safe_abi_json_1 = __importDefault(require("../abi/Safe.abi.json"));
const ethers_1 = require("ethers");
const getENSAddress = async (mainnetRpcUrl, domain) => {
try {
const provider = new ethers_1.JsonRpcProvider(mainnetRpcUrl);
const address = await provider.resolveName(domain);
return address;
}
catch (error) {
console.error("Failed to resolve ENS name", error);
return null;
}
};
exports.getENSAddress = getENSAddress;
const getAccountAddress = async (config, address, salt = BigInt(0), options) => {
const { accountFactoryAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(config.getAccountConfig(accountFactoryAddress).account_factory_address, AccountFactory_abi_json_1.default, rpc);
try {
const accountAddress = await contract.getFunction("getAddress")(address, salt);
return accountAddress;
}
catch (error) {
console.error("Error fetching account address:", error);
return null;
}
};
exports.getAccountAddress = getAccountAddress;
const getAccountBalance = async (config, address, options) => {
const { accountFactoryAddress, tokenAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(tokenAddress ?? config.primaryToken.address, ERC20_abi_json_1.default, rpc);
try {
const balance = await contract.getFunction("balanceOf")(address);
return balance;
}
catch (error) {
console.error("Error fetching account balance:", error);
return null;
}
};
exports.getAccountBalance = getAccountBalance;
const verifyAccountOwnership = async (config, accountAddress, message, signature, options) => {
const { accountFactoryAddress } = options ?? {};
const recoveredAddress = (0, ethers_1.verifyMessage)(message.startsWith("0x") ? (0, ethers_1.getBytes)(message) : message, signature);
if (recoveredAddress.toLowerCase() === accountAddress.toLowerCase()) {
return true;
}
try {
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(accountAddress, Account_abi_json_1.default, rpc);
// Check if isValidSignature is implemented by calling it
try {
const hash = (0, ethers_1.hashMessage)(message);
const magicValue = await contract.getFunction("isValidSignature")(hash, signature);
if (magicValue === "0x1626ba7e") {
return true;
}
}
catch (error) {
console.warn(error);
// Function is not implemented
console.warn("isValidSignature is not implemented on this contract");
}
try {
const owner = await contract.getFunction("owner")();
if (owner.toLowerCase() === recoveredAddress.toLowerCase()) {
return true;
}
}
catch (error) {
console.warn("owner function not implemented or failed:", error);
// If owner function doesn't exist or fails, we continue with other checks
}
const safeContract = new ethers_1.Contract(accountAddress, Safe_abi_json_1.default, rpc);
const isOwner = await safeContract.getFunction("isOwner")(recoveredAddress);
return isOwner;
}
catch (error) {
console.error("Error verifying account ownership:", error);
}
return false;
};
exports.verifyAccountOwnership = verifyAccountOwnership;
const isSafeOwner = async (config, accountAddress, ownerAddress, options) => {
const { accountFactoryAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(accountAddress, Safe_abi_json_1.default, rpc);
try {
const isOwner = await contract.getFunction("isOwner")(ownerAddress);
return isOwner;
}
catch (error) {
console.error("Error verifying safe owner:", error);
return false;
}
};
exports.isSafeOwner = isSafeOwner;
//# sourceMappingURL=index.js.map