UNPKG

@citizenwallet/sdk

Version:

An sdk to easily work with citizen wallet.

79 lines 3.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProfileFromUsername = 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"); 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) => { const rpc = new ethers_1.JsonRpcProvider(config.primaryRPCUrl); 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 getProfileFromUsername = async (ipfsDomain, config, username) => { const rpc = new ethers_1.JsonRpcProvider(config.primaryRPCUrl); 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; //# sourceMappingURL=index.js.map