Skip to content

Commit

Permalink
iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Apr 13, 2024
1 parent 2f98ab8 commit 928459a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 25 deletions.
4 changes: 3 additions & 1 deletion api/src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions api/src/libs/lit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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` }
}
}

Expand Down
71 changes: 65 additions & 6 deletions api/src/services/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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');
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.');
}
Expand Down
6 changes: 2 additions & 4 deletions api/src/services/embedding.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class EmbeddingService {
id
title
signerPublicKey
signerFunction
signerFunction
createdAt
updatedAt
deletedAt
Expand All @@ -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)
}
Expand Down Expand Up @@ -267,5 +267,3 @@ export class EmbeddingService {
}

}


6 changes: 2 additions & 4 deletions api/src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class IndexService {
index.did = did;
}

index.ownerDID = await getOwnerProfile(index.signerPublicKey);
index.ownerDID = await getOwnerProfile(index.id);



Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -246,5 +246,3 @@ export class IndexService {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ const ConfirmTransaction = ({ handleCancel, ...modalProps }: any) => {
}
header={
<>
<Header level={2}>Waiting for transaction confirmation</Header>
<Header level={2}>Waiting for network confirmation</Header>
<Text className={"mt-4"} element={"p"}>
Please confirm the transaction with your connected wallet.
Please wait for a few seconds.
</Text>
</>
}
Expand Down

0 comments on commit 928459a

Please sign in to comment.