Skip to content

Commit

Permalink
Merge pull request #5 from indexnetwork/pkp-session-to-api
Browse files Browse the repository at this point in the history
Initial
  • Loading branch information
serefyarar authored Jan 17, 2024
2 parents d57bb4c + 836127c commit 3a1998f
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 2,379 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
15 changes: 8 additions & 7 deletions api/src/controllers/did.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DIDService } from "../services/did.js";
export const getIndexes = async (req, res, next) => {
// sendLit(req.params.id) //TODO Fix later.

try {
const didService = new DIDService()
const didService = new DIDService().setSession(req.session)
const { type } = req.query;
const indexes = await didService.getIndexes(req.params.id, type)
res.status(200).json(indexes);
Expand All @@ -13,12 +14,12 @@ export const getIndexes = async (req, res, next) => {

export const addIndex = async (req, res, next) => {

if(req.params.id !== req.personalDID.parent) {
if(req.params.id !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}
const {indexId, type} = req.body;
try {
const didService = new DIDService().setDID(req.personalDID);
const didService = new DIDService().setSession(req.session);
const newIndex = await didService.addIndex(indexId, type)
res.status(201).json(newIndex);
} catch (error) {
Expand All @@ -28,13 +29,13 @@ export const addIndex = async (req, res, next) => {

export const removeIndex = async (req, res, next) => {

if(req.params.id !== req.personalDID.parent) {
if(req.params.id !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}

const {indexId, type} = req.body;
try {
const didService = new DIDService().setDID(req.personalDID);
const didService = new DIDService().setSession(req.session)
const newIndex = await didService.removeIndex(indexId, type)
res.status(200).json(newIndex);
} catch (error) {
Expand All @@ -44,11 +45,11 @@ export const removeIndex = async (req, res, next) => {


export const createProfile = async (req, res, next) => {
if(req.params.id !== req.personalDID.parent) {
if(req.params.id !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}
try {
const didService = new DIDService().setDID(req.personalDID);
const didService = new DIDService().setSession(req.session);
const profile = await didService.createProfile(req.body)
res.status(201).json(profile);
} catch (error) {
Expand Down
25 changes: 22 additions & 3 deletions api/src/controllers/embedding.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import {EmbeddingService} from "../services/embedding.js";
import {IndexService} from "../services/index.js";
import {getPKPSession} from "../libs/lit/index.js";

export const listEmbeddings = async (req, res, next) => {};
export const createEmbedding = async (req, res, next) => {
try {
const embeddingService = new EmbeddingService().setDID(req.pkpDID);
const { indexId } = req.body;
const indexService = new IndexService();
const index = await indexService.getIndexById(indexId);
const pkpSession = await getPKPSession(req.session, index);

const embeddingService = new EmbeddingService().setSession(pkpSession);
const embedding = await embeddingService.createEmbedding(req.body);
res.status(201).json(embedding);
} catch (error) {
Expand All @@ -15,7 +22,13 @@ export const createEmbedding = async (req, res, next) => {
};
export const updateEmbedding = async (req, res, next) => {
try {
const embeddingService = new EmbeddingService().setDID(req.pkpDID);

const { indexId } = req.body;
const indexService = new IndexService();
const index = await indexService.getIndexById(indexId);
const pkpSession = await getPKPSession(req.session, index);

const embeddingService = new EmbeddingService().setSession(pkpSession);
const embedding = await embeddingService.updateEmbedding(req.body);
res.status(200).json(embedding);
} catch (error) {
Expand All @@ -27,7 +40,13 @@ export const updateEmbedding = async (req, res, next) => {
export const deleteEmbedding = async (req, res, next) => {

try {
const embeddingService = new EmbeddingService().setDID(req.pkpDID);

const { indexId } = req.body;
const indexService = new IndexService();
const index = await indexService.getIndexById(indexId);
const pkpSession = await getPKPSession(req.session, index);

const embeddingService = new EmbeddingService().setSession(pkpSession);
const embedding = await embeddingService.deleteEmbedding(req.body);
res.status(200).json(embedding);
} catch (error) {
Expand Down
35 changes: 27 additions & 8 deletions api/src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { IndexService } from "../services/index.js";
import { DIDService } from "../services/did.js";
import {getPKPSession} from "../libs/lit/index.js";

export const getIndexById = async (req, res, next) => {
try {
const indexService = new IndexService().setDID(req.pkpDID);
const indexService = new IndexService().setSession(req.session);
const newIndex = await indexService.getIndexById(req.params.id);
res.status(200).json(newIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
export const createIndex = async (req, res, next) => {


try {
const indexService = new IndexService().setDID(req.pkpDID); //PKP

const pkpSession = getPKPSession(req.session, req.body);
const indexService = new IndexService().setSession(pkpSession); //PKP
const newIndex = await indexService.createIndex(req.body);

const didService = new DIDService().setDID(req.personalDID); //Personal
const didService = new DIDService().setSession(req.session); //Personal
const newIndexDID = await didService.addIndex(newIndex.id, "owner");

res.status(201).json(newIndex);
Expand All @@ -25,18 +30,32 @@ export const createIndex = async (req, res, next) => {
}
export const updateIndex = async (req, res, next) => {
try {
const indexService = new IndexService().setDID(req.pkpDID);
const newIndex = await indexService.updateIndex(req.params.id, req.body);

const indexService = new IndexService();
const index = await indexService.getIndexById(req.params.id);
const pkpSession = await getPKPSession(req.session, index);

const newIndex = await indexService
.setSession(pkpSession)
.updateIndex(req.params.id, req.body);

res.status(200).json(newIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
export const deleteIndex = async (req, res, next) => {
try {
const indexService = new IndexService().setDID(req.pkpDID);
const newIndex = await indexService.deleteIndex(req.params.id);
res.status(200).json(newIndex);

const indexService = new IndexService();
const index = await indexService.getIndexById(req.params.id);
const pkpSession = await getPKPSession(req.session, index);

const deletedIndex = await indexService
.setSession(pkpSession)
.deleteIndex(req.params.id);

res.status(200).json(deletedIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down
16 changes: 14 additions & 2 deletions api/src/controllers/item.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {ItemService} from "../services/item.js";
import {IndexService} from "../services/index.js";
import {getPKPSession} from "../libs/lit/index.js";

export const listItems = async (req, res, next) => {
//Todo without embeddings, use chromadb filters, accepts query param.
Expand All @@ -7,7 +9,12 @@ export const listItems = async (req, res, next) => {
export const addItem = async (req, res, next) => {
const {indexId, itemId} = req.body;
try {
const itemService = new ItemService().setDID(req.pkpDID);

const indexService = new IndexService();
const index = await indexService.getIndexById(indexId);
const pkpSession = await getPKPSession(req.session, index);

const itemService = new ItemService().setSession(pkpSession);
const item = await itemService.addItem(indexId, itemId);
res.status(201).json(item);
} catch (error) {
Expand All @@ -18,7 +25,12 @@ export const addItem = async (req, res, next) => {
export const removeItem = async (req, res, next) => {
const {indexId, itemId} = req.body;
try {
const itemService = new ItemService().setDID(req.pkpDID);

const indexService = new IndexService();
const index = await indexService.getIndexById(indexId);
const pkpSession = await getPKPSession(req.session, index);

const itemService = new ItemService().setSession(pkpSession);
const item = await itemService.removeItem(indexId, itemId);
res.status(200).json(item);
} catch (error) {
Expand Down
88 changes: 1 addition & 87 deletions api/src/controllers/lit-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,12 @@ import fs from 'fs/promises';
import { NodeVM } from 'vm2';
import { TextEncoder, TextDecoder } from "util";

import { LitContracts } from "@lit-protocol/contracts-sdk";
import { DID } from "dids";
import * as LitJsSdk from "@lit-protocol/lit-node-client-nodejs";
import { randomBytes, randomString } from "@stablelib/random";
import { Cacao } from "@didtools/cacao";
import { getResolver } from "key-did-resolver";
import { createDIDCacao, DIDSession } from "did-session";
import { Ed25519Provider } from "key-did-provider-ed25519";
import { ethers } from "ethers";
import { CID } from 'multiformats/cid';

import RedisClient from '../clients/redis.js';
import IPFSClient from '../clients/ipfs.js';
const redis = RedisClient.getInstance();

const config = {
litNetwork: "cayenne",
domain: "index.network",
authSig: ""
};


//import { Index } from '../protocol.ts';

Expand Down Expand Up @@ -158,77 +144,5 @@ export const postAction = async (req, res, next) => {
});
};

export const getPKPSession = async (index) => {

const existingSessionStr = await redis.hGet("sessions", index.signerFunction);

if (existingSessionStr) {
try {
const didSession = await DIDSession.fromSession(existingSessionStr);
await didSession.did.authenticate()
return didSession

} catch (error) {
//Expired or invalid session, remove cache.
console.warn(error);
await redis.hDel("sessions", index.signerFunction);
}
}



const keySeed = randomBytes(32);
const provider = new Ed25519Provider(keySeed);
// @ts-ignore
const didKey = new DID({ provider, resolver: getResolver() });
await didKey.authenticate();

try{
const litNodeClient = new LitJsSdk.LitNodeClientNodeJs({
litNetwork: 'cayenne',
});
await litNodeClient.connect();
const signerFunctionV0 = CID.parse(index.signerFunction).toV0().toString();
const resp = await litNodeClient.executeJs({
ipfsId: signerFunctionV0,
authSig: config.authSig,
jsParams: {
authSig: config.authSig,
chain: "ethereum",
publicKey: index.signerPublicKey,
didKey: didKey.id,
nonce: randomString(10),
domain: config.domain,
sigName: "sig1",
},
});

const { error } = resp.response; // TODO Handle.
if (error) {
console.log(error)
return null;
}

const { siweMessage } = JSON.parse(resp.response.context);
const signature = resp.signatures.sig1; // TODO Handle.
siweMessage.signature = ethers.Signature.from({
r: `0x${signature.r}`,
s: `0x${signature.s}`,
v: signature.recid,
}).serialized;

const cacao = Cacao.fromSiweMessage(siweMessage);

const did = await createDIDCacao(didKey, cacao);
const session = new DIDSession({ cacao, keySeed, did });
await redis.hSet("sessions", index.signerFunction, session.serialize());

await session.did.authenticate()
return session


}catch (e){
console.log("Error", e)
}

}
4 changes: 2 additions & 2 deletions api/src/controllers/web2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const crawlMetadata = async (req, res, next) => {

export const createWebPage = async (req, res, next) => {
try {
const webPageService = new WebPageService().setDID(req.personalDID);
const webPageService = new WebPageService().setSession(req.session);
const webPage = await webPageService.createWebPage(req.body);
res.status(201).json(webPage);
} catch (error) {
Expand All @@ -19,7 +19,7 @@ export const createWebPage = async (req, res, next) => {

export const crawlWebPage = async (req, res, next) => {
try {
const webPageService = new WebPageService().setDID(req.personalDID);
const webPageService = new WebPageService().setSession(req.session);
let params = req.body;
try{
const crawlResponse = await axios.post(`${process.env.LLM_INDEXER_HOST}/crawl`, {
Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/zapier.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ export const indexLink = async (req, res, next) => {
let linkData = await getMetadata(payload.url);
if (payload.content) linkData.content = payload.content;
if (payload.title) linkData.title = payload.title;

//TODO Refactor new client
//Create user owned webpage object.
const personalSesssion = await DIDSession.fromSession(auth.session.personal);
await personalSesssion.did.authenticate();

const webPageService = new WebPageService().setDID(personalSesssion.did);
const webPageService = new WebPageService().setSession(personalSesssion);
const webPage = await webPageService.createWebPage(payload);

//Index webpage object.
const indexSession = await DIDSession.fromSession(auth.session.index);
await indexSession.did.authenticate();

const itemService = new ItemService().setDID(indexSession.did);
const itemService = new ItemService().setSession(indexSession);
const item = await itemService.addItem(auth.indexId, webPage.id);

return res.json(item);
Expand Down
8 changes: 4 additions & 4 deletions api/src/libs/kafka-indexer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dotenv from 'dotenv'
import axios from 'axios'
import {ItemService} from "../services/item.js";
import {EmbeddingService} from "../services/embedding.js";
import {getPKPSession} from "../controllers/lit-protocol.js";
import { ItemService } from "../services/item.js";
import { EmbeddingService } from "../services/embedding.js";
import { getPKPSession } from "../libs/lit/index.js";

if(process.env.NODE_ENV !== 'production'){
dotenv.config()
Expand All @@ -22,7 +22,7 @@ export const createIndexItemEvent = async (id) => {
const embeddingResponse = await axios.post(`${process.env.LLM_INDEXER_HOST}/embeddings`, {
text: indexItem.item.content
})
const embeddingService = new EmbeddingService().setDID(indexSession.did)
const embeddingService = new EmbeddingService().setSession(indexSession)
const embedding = await embeddingService.createEmbedding({
"indexId": indexItem.indexId,
"itemId": indexItem.itemId,
Expand Down
Loading

0 comments on commit 3a1998f

Please sign in to comment.