UNPKG

origintrail-node

Version:

OriginTrail Node - Decentralized Knowledge Graph Node Library

60 lines (53 loc) 2.04 kB
class ParanetService { constructor(ctx) { this.blockchainModuleManager = ctx.blockchainModuleManager; this.repositoryModuleManager = ctx.repositoryModuleManager; this.ualService = ctx.ualService; this.cryptoService = ctx.cryptoService; } async initializeParanetRecord(blockchain, paranetId) { const paranetName = await this.blockchainModuleManager.getParanetName( blockchain, paranetId, ); const paranetDescription = await this.blockchainModuleManager.getDescription( blockchain, paranetId, ); if (!(await this.repositoryModuleManager.paranetExists(paranetId, blockchain))) { await this.repositoryModuleManager.createParanetRecord( paranetName, paranetDescription, paranetId, blockchain, ); } } constructParanetId(contract, knowledgeCollectionId, knowledgeAssetId) { return this.cryptoService.keccak256EncodePacked( ['address', 'uint256', 'uint256'], [contract, knowledgeCollectionId, knowledgeAssetId], ); } constructKnowledgeAssetId(contract, tokenId) { return this.cryptoService.keccak256EncodePacked( ['address', 'uint256'], [contract, tokenId], ); } getParanetRepositoryName(paranetUAL) { if (this.ualService.isUAL(paranetUAL)) { // Replace : and / with - return `paranet-${paranetUAL.replace(/[/:]/g, '-').toLowerCase()}`; } throw new Error( `Unable to get Paranet repository name. Paranet id doesn't have UAL format: ${paranetUAL}`, ); } getParanetIdFromUAL(paranetUAL) { const { contract, knowledgeCollectionId, knowledgeAssetId } = this.ualService.resolveUAL(paranetUAL); return this.constructParanetId(contract, knowledgeCollectionId, knowledgeAssetId); } } export default ParanetService;