From 928459ad11302f29432abfaffad2c9ba588a7a4d Mon Sep 17 00:00:00 2001 From: serafettin Date: Fri, 12 Apr 2024 22:09:45 -0400 Subject: [PATCH] iterate --- api/src/controllers/index.js | 4 +- api/src/libs/lit/index.js | 15 ++-- api/src/services/did.js | 71 +++++++++++++++++-- api/src/services/embedding.js | 6 +- api/src/services/index.js | 6 +- .../modal/Common/ConfirmTransaction/index.tsx | 4 +- 6 files changed, 81 insertions(+), 25 deletions(-) diff --git a/api/src/controllers/index.js b/api/src/controllers/index.js index dd4d4c31..8eebf382 100644 --- a/api/src/controllers/index.js +++ b/api/src/controllers/index.js @@ -50,7 +50,7 @@ export const createIndex = async (req, res, next) => { const pkpSession = await getPKPSession(req.session, indexParams); const indexService = new IndexService().setSession(pkpSession); //PKP - const newIndex = await indexService.createIndex(indexParams); + let newIndex = await indexService.createIndex(indexParams); if(!newIndex){ return res.status(500).json({ error: "Create index error" }); } @@ -61,6 +61,8 @@ export const createIndex = async (req, res, next) => { const didService = new DIDService().setSession(req.session); //Personal const newIndexDID = await didService.addIndex(newIndex.id, "owned"); + newIndex = await indexService.getIndexById(newIndex.id); + newIndex.did = { owned: true, starred: false diff --git a/api/src/libs/lit/index.js b/api/src/libs/lit/index.js index 751d75ee..60f1b3b4 100644 --- a/api/src/libs/lit/index.js +++ b/api/src/libs/lit/index.js @@ -34,7 +34,7 @@ const litContracts = new LitContracts({ }); const litNodeClient = new LitJsSdk.LitNodeClientNodeJs({ - litNetwork: process.env.LIT_NETWORK, + litNetwork: config.litNetwork, debug: !!process.env.DEBUG || false, checkNodeAttestation: false, }); @@ -69,17 +69,16 @@ export const getOwner = async (pkpPubKey) => { return address; } -export const getOwnerProfile = async (pkpPubKey) => { - - const owner = await getOwner(pkpPubKey); //Make it through composeDB +export const getOwnerProfile = async (id) => { const didService = new DIDService() - const profile = await didService.getProfile(`did:pkh:eip155:1:${owner}`) - if(profile){ - return profile; + const owner = await didService.getOwner(id); + console.log(owner) + if(owner && owner.controllerDID && owner.controllerDID.id){ + return owner; }else{ - return { id: `did:pkh:eip155:1:${owner}` } + return { id: `did:pkh:eip155:1:0` } } } diff --git a/api/src/services/did.js b/api/src/services/did.js index 3e41d87e..426ad875 100644 --- a/api/src/services/did.js +++ b/api/src/services/did.js @@ -22,7 +22,66 @@ export class DIDService { return this; } - async getDIDIndex(indexId, type) { + + async getOwner(indexId) { + + try { + const {data, errors} = await this.client.executeQuery(` + query{ + dIDIndexIndex(first: 1, sorting: {createdAt: DESC}, filters: { where: {type: {equalTo: "owned"}, indexId: {equalTo: "${indexId}"}}}) { + edges { + node { + id + type + indexId + createdAt + updatedAt + deletedAt + controllerDID { + profile { + id + name + avatar + createdAt + updatedAt + deletedAt + controllerDID { + id + } + } + } + } + } + } + } + `); + + // Handle GraphQL errors + if (errors) { + throw new Error(`Error getting DIDIndex index: ${JSON.stringify(errors)}`); + } + + // Validate the data response + if (!data || !data.dIDIndexIndex || !data.dIDIndexIndex.edges) { + throw new Error('Invalid response data'); + } + + if (data.dIDIndexIndex.edges.length === 0) { + return null; + } + + return data.dIDIndexIndex.edges[0].node.controllerDID.profile; + + } catch (error) { + // Log the error and rethrow it for external handling + console.error('Exception occurred in dIDIndexIndex:', error); + throw error; + } + } + + + async getDIDIndexForViewer(indexId, type) { + if (!this.did) { throw new Error("DID not set. Use setDID() to set the did."); } @@ -54,7 +113,7 @@ export class DIDService { throw new Error(`Error getting DIDIndex index: ${JSON.stringify(errors)}`); } // Validate the data response - if (!data || !data.viewer || !data.viewer.didIndexList) { + if (!data || !data.viewer.didIndexList || !data.viewer.didIndexList.edges) { throw new Error('Invalid response data'); } @@ -66,7 +125,7 @@ export class DIDService { } catch (error) { // Log the error and rethrow it for external handling - console.error('Exception occurred in createDIDIndex:', error); + console.error('Exception occurred in getDIDIndexForViewer:', error); throw error; } } @@ -149,7 +208,7 @@ export class DIDService { Object.values(indexes) .filter(i => i.did.owned || i.did.starred) .map(async (i) => { - const ownerDID = await getOwnerProfile(i.signerPublicKey); + const ownerDID = await getOwnerProfile(i.id); return { ...i, ownerDID }; }) .sort((a, b) => { @@ -175,7 +234,7 @@ export class DIDService { try { //Duplicate check, it'll be refactored when ceramic release the set account relations. - const existingIndex = await this.getDIDIndex(indexId, type); + const existingIndex = await this.getDIDIndexForViewer(indexId, type); if (existingIndex && !existingIndex.deletedAt) { return existingIndex; } @@ -231,7 +290,7 @@ export class DIDService { try { // Check if the index exists and is not already deleted - const existingIndex = await this.getDIDIndex(indexId, type); + const existingIndex = await this.getDIDIndexForViewer(indexId, type); if (!existingIndex) { throw new Error('Index does not exist.'); } diff --git a/api/src/services/embedding.js b/api/src/services/embedding.js index 4039ee13..5d87da1d 100644 --- a/api/src/services/embedding.js +++ b/api/src/services/embedding.js @@ -58,7 +58,7 @@ export class EmbeddingService { id title signerPublicKey - signerFunction + signerFunction createdAt updatedAt deletedAt @@ -76,7 +76,7 @@ export class EmbeddingService { throw new Error('Invalid response data'); } try{ - data.node.index.ownerDID = await getOwnerProfile(data.node.index.signerPublicKey); + data.node.index.ownerDID = await getOwnerProfile(data.node.index.id); } catch(e) { console.log("Error fetching profile", e) } @@ -267,5 +267,3 @@ export class EmbeddingService { } } - - diff --git a/api/src/services/index.js b/api/src/services/index.js index fbbf051c..4634b62c 100644 --- a/api/src/services/index.js +++ b/api/src/services/index.js @@ -92,7 +92,7 @@ export class IndexService { index.did = did; } - index.ownerDID = await getOwnerProfile(index.signerPublicKey); + index.ownerDID = await getOwnerProfile(index.id); @@ -143,7 +143,7 @@ export class IndexService { // Return the created index document const createdIndex = data.createIndex.document; - createdIndex.ownerDID = await getOwnerProfile(createdIndex.signerPublicKey); + createdIndex.ownerDID = await getOwnerProfile(createdIndex.id); return createdIndex; @@ -246,5 +246,3 @@ export class IndexService { } } } - - diff --git a/web-app/src/components/site/modal/Common/ConfirmTransaction/index.tsx b/web-app/src/components/site/modal/Common/ConfirmTransaction/index.tsx index efb7349d..5c54e217 100644 --- a/web-app/src/components/site/modal/Common/ConfirmTransaction/index.tsx +++ b/web-app/src/components/site/modal/Common/ConfirmTransaction/index.tsx @@ -38,9 +38,9 @@ const ConfirmTransaction = ({ handleCancel, ...modalProps }: any) => { } header={ <> -
Waiting for transaction confirmation
+
Waiting for network confirmation
- Please confirm the transaction with your connected wallet. + Please wait for a few seconds. }