UNPKG

@strandgeek/powerup

Version:

PowerUP is a javascript library that makes managing a LUKSO Universal Profile easy

149 lines (148 loc) 5.96 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Profile = void 0; const erc725_js_1 = __importDefault(require("@erc725/erc725.js")); const lsp_factory_js_1 = require("@lukso/lsp-factory.js"); const UniversalProfile_json_1 = __importDefault(require("@lukso/lsp-smart-contracts/artifacts/UniversalProfile.json")); // Parameters for ERC725 Instance const LSP3UniversalProfileMetadata_json_1 = __importDefault(require("@erc725/erc725.js/schemas/LSP3UniversalProfileMetadata.json")); class Profile { constructor(opts) { this.address = opts.address; this.web3 = opts.web3; this.lspFactory = new lsp_factory_js_1.LSPFactory(this.web3.currentProvider); this.upContract = new this.web3.eth.Contract(UniversalProfile_json_1.default.abi, this.address); this.ipfsGateway = opts.ipfsGateway; } getCurrentAccount() { return __awaiter(this, void 0, void 0, function* () { const accounts = yield this.web3.eth.getAccounts(); return accounts[0]; }); } get name() { return this.lsp3Profile.name; } get description() { return this.lsp3Profile.description; } get links() { return this.lsp3Profile.links; } get tags() { return this.lsp3Profile.tags; } get avatar() { return this.lsp3Profile.avatar; } get profileImage() { return this.lsp3Profile.profileImage; } get backgroundImage() { return this.lsp3Profile.backgroundImage; } load() { return __awaiter(this, void 0, void 0, function* () { const { ipfsGateway } = this; const erc725 = new erc725_js_1.default(LSP3UniversalProfileMetadata_json_1.default, this.address, this.web3.currentProvider, { ipfsGateway, }); const data = yield erc725.fetchData('LSP3Profile'); this.lsp3Profile = data.value.LSP3Profile; this.currentAccount = yield this.getCurrentAccount(); }); } get handle() { const name = this.name; const tag = this.address.substring(0, 4).toUpperCase(); return { name, tag, toString: () => `${name}#${tag}`, }; } generateUploadData() { return __awaiter(this, void 0, void 0, function* () { return { LSP3Profile: { name: this.name, description: this.description, avatar: [], backgroundImage: [], profileImage: [], links: [], tags: [], } }; }); } update(data) { return __awaiter(this, void 0, void 0, function* () { const newProfileData = Object.assign({}, this.lsp3Profile); if (data.name) { newProfileData.name = data.name; } if (data.description) { newProfileData.description = data.description; } if (data.links) { newProfileData.links = data.links; } if (data.avatar) { newProfileData.avatar = data.avatar; } if (data.backgroundImage) { newProfileData.backgroundImage = data.backgroundImage; } if (data.profileImage) { newProfileData.profileImage = data.profileImage; } if (data.tags) { newProfileData.tags = data.tags; } const uploadData = { LSP3Profile: newProfileData }; const uploadResult = yield this.lspFactory.UniversalProfile.uploadProfileData(uploadData.LSP3Profile); const lsp3ProfileIPFSUrl = uploadResult.url; const schema = [ { name: "LSP3Profile", key: "0x5ef83ad9559033e6e941db7d7c495acdce616347d28e90c7ce47cbfcfcad3bc5", keyType: "Singleton", valueContent: "JSONURL", valueType: "bytes", }, ]; const erc725 = new erc725_js_1.default(schema, this.address, this.web3.currentProvider, { ipfsGateway: this.ipfsGateway, }); const encodedData = erc725.encodeData([{ keyName: "LSP3Profile", value: { hashFunction: "keccak256(utf8)", hash: this.web3.utils.keccak256(JSON.stringify(uploadResult.json)), url: lsp3ProfileIPFSUrl, }, }]); const universalProfileContract = new this.web3.eth.Contract(UniversalProfile_json_1.default.abi, this.address); yield universalProfileContract.methods["setData(bytes32,bytes)"](encodedData.keys[0], encodedData.values[0]) .send({ from: this.currentAccount, }); }); } } exports.Profile = Profile;