@citizenwallet/sdk
Version:
An sdk to easily work with citizen wallet.
165 lines • 7.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyAndSuggestUsername = exports.checkUsernameAvailability = exports.hasProfileAdminRole = exports.getProfileFromUsername = exports.getProfileUriFromId = exports.getProfileFromAddress = exports.getProfileFromId = exports.formatUsernameToBytes32 = exports.formatProfileImageLinks = void 0;
const ethers_1 = require("ethers");
const ipfs_1 = require("../ipfs");
const Profile_abi_json_1 = __importDefault(require("../abi/Profile.abi.json"));
const dotenv_1 = __importDefault(require("dotenv"));
const utils_1 = require("./utils");
const crypto_1 = require("../utils/crypto");
const random_1 = require("../utils/random");
dotenv_1.default.config();
const formatProfileImageLinks = (ipfsUrl, profile) => {
if (profile.image_small.startsWith("ipfs://")) {
profile.image_small = `${ipfsUrl}/${profile.image_small.replace("ipfs://", "")}`;
}
if (profile.image_medium.startsWith("ipfs://")) {
profile.image_medium = `${ipfsUrl}/${profile.image_medium.replace("ipfs://", "")}`;
}
if (profile.image.startsWith("ipfs://")) {
profile.image = `${ipfsUrl}/${profile.image.replace("ipfs://", "")}`;
}
return profile;
};
exports.formatProfileImageLinks = formatProfileImageLinks;
const padBytesWithSpace = (bytes, length) => {
const spaceByte = new TextEncoder().encode(" ");
while (bytes.length < length) {
bytes = new Uint8Array([...spaceByte, ...bytes]);
}
return bytes;
};
const formatUsernameToBytes32 = (username) => {
return (0, ethers_1.hexlify)(padBytesWithSpace((0, ethers_1.toUtf8Bytes)(username.replace("@", "")), 32));
};
exports.formatUsernameToBytes32 = formatUsernameToBytes32;
const getProfileFromId = async (ipfsDomain, config, id, options) => {
const { accountFactoryAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(config.community.profile.address, Profile_abi_json_1.default, rpc);
try {
const address = (0, utils_1.idToAddress)(BigInt(id));
const uri = await contract.getFunction("tokenURI")(address);
const profile = await (0, ipfs_1.downloadJsonFromIpfs)(ipfsDomain, uri);
return {
...(0, exports.formatProfileImageLinks)(`https://${ipfsDomain}`, profile),
token_id: id,
};
}
catch (error) {
console.error("Error fetching profile:", error);
return null;
}
};
exports.getProfileFromId = getProfileFromId;
const getProfileFromAddress = async (ipfsDomain, config, address) => {
const id = (0, utils_1.addressToId)(address);
return (0, exports.getProfileFromId)(ipfsDomain, config, id.toString());
};
exports.getProfileFromAddress = getProfileFromAddress;
const getProfileUriFromId = async (config, token_id, options) => {
const { accountFactoryAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(config.community.profile.address, Profile_abi_json_1.default, rpc);
try {
const uri = await contract.getFunction("tokenURI")(token_id);
return uri;
}
catch (error) {
console.error("Error fetching profile:", error);
return null;
}
};
exports.getProfileUriFromId = getProfileUriFromId;
const getProfileFromUsername = async (ipfsDomain, config, username, accountFactoryAddress) => {
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(config.community.profile.address, Profile_abi_json_1.default, rpc);
try {
const formattedUsername = (0, exports.formatUsernameToBytes32)(username);
const uri = await contract.getFunction("getFromUsername")(formattedUsername);
const profile = await (0, ipfs_1.downloadJsonFromIpfs)(ipfsDomain, uri);
const id = (0, utils_1.addressToId)(profile.account);
return {
...(0, exports.formatProfileImageLinks)(`https://${ipfsDomain}`, profile),
token_id: id.toString(),
};
}
catch (error) {
console.error("Error fetching profile:", error);
return null;
}
};
exports.getProfileFromUsername = getProfileFromUsername;
const hasProfileAdminRole = async (config, address, options) => {
const { accountFactoryAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(config.community.profile.address, Profile_abi_json_1.default, rpc);
try {
const isAdmin = await contract.getFunction("hasRole")(crypto_1.PROFILE_ADMIN_ROLE, address);
return isAdmin;
}
catch (error) {
console.error("Error checking profile admin role:", error);
}
try {
const isOwner = await contract.getFunction("owner")();
return isOwner.toLowerCase() === address.toLowerCase();
}
catch (error) {
console.error("Error checking profile owner:", error);
}
return false;
};
exports.hasProfileAdminRole = hasProfileAdminRole;
const checkUsernameAvailability = async (config, username, options) => {
const { accountFactoryAddress } = options ?? {};
const rpc = new ethers_1.JsonRpcProvider(config.getRPCUrl(accountFactoryAddress));
const contract = new ethers_1.Contract(config.community.profile.address, Profile_abi_json_1.default, rpc);
try {
const formattedUsername = (0, exports.formatUsernameToBytes32)(username);
const uri = await contract.getFunction("getFromUsername")(formattedUsername);
return uri === null || uri === undefined || uri === "";
}
catch (error) {
console.error("Error checking username availability:", error);
return true;
}
};
exports.checkUsernameAvailability = checkUsernameAvailability;
const verifyAndSuggestUsername = async (config, username, options) => {
const { accountFactoryAddress } = options ?? {};
const formattedUsername = (0, utils_1.limitStringLength)(username, 32 - (1 + (options?.randomLetterLength ?? 4)));
try {
return _generateUniqueUsername(config, formattedUsername, formattedUsername, {
accountFactoryAddress,
});
}
catch (error) {
console.error("Error generating unique username:", error);
}
return null;
};
exports.verifyAndSuggestUsername = verifyAndSuggestUsername;
const _generateUniqueUsername = async (config, username, originalUsername, options) => {
const { accountFactoryAddress } = options ?? {};
try {
const available = await (0, exports.checkUsernameAvailability)(config, username, {
accountFactoryAddress,
});
if (available) {
return username;
}
const randomLetters = (0, random_1.getRandomLetters)(options?.randomLetterLength);
return _generateUniqueUsername(config, `${originalUsername}-${randomLetters}`, originalUsername, {
accountFactoryAddress,
});
}
catch (error) {
console.error("Error generating unique username:", error);
}
return null;
};
//# sourceMappingURL=index.js.map