Skip to content

Commit

Permalink
Merge branch 'dev' into integration2
Browse files Browse the repository at this point in the history
  • Loading branch information
confxsd authored Feb 21, 2024
2 parents 5e95083 + 0b1fb44 commit 9324790
Show file tree
Hide file tree
Showing 23 changed files with 788 additions and 190 deletions.
25 changes: 21 additions & 4 deletions api/lit_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@
"use strict";
(() => {
// lit_actions/src/session.action.ts
var getCreatorConditions = () => {
return __REPLACE_THIS_AS_CONDITIONS_ARRAY__;
var getCreatorConditions = (transform=true) => {
let conditionsArray = __REPLACE_THIS_AS_CONDITIONS_ARRAY__;

if(conditionsArray.length < 1){
return [];
}

if(!transform){
return conditionsArray
}

return conditionsArray
.map(c => c.value)
.flatMap((v, i) => i < conditionsArray.length - 1 ? [v, {"operator": "or"}] : [v])
.map(v => {
delete v.metadata
return v
})
};
var toSiweMessage = (message) => {
const header = `${message.domain} wants you to sign in with your Ethereum account:`;
Expand Down Expand Up @@ -53,13 +69,13 @@
};
var go = async () => {
if (typeof ACTION_CALL_MODE !== "undefined") {
console.log(JSON.stringify(getCreatorConditions()));
console.log(JSON.stringify(getCreatorConditions(false)));
return;
}
const context = { isPermittedAddress: false, isCreator: false, siweMessage: false };
const pkpTokenId = Lit.Actions.pubkeyToTokenId({ publicKey });
const pkpAddress = ethers.utils.computeAddress(publicKey).toLowerCase();
const isPermittedAddress = await Lit.Actions.isPermittedAddress({ tokenId: pkpTokenId, address: Lit.Auth.authSigAddress });
const isPermittedAddress = await Lit.Actions.isPermittedAddress({ tokenId: pkpTokenId, address: authSig.address });
context.isPermittedAddress = isPermittedAddress;
const conditions = getCreatorConditions();
let isCreator = false;
Expand Down Expand Up @@ -88,6 +104,7 @@
publicKey,
sigName
});
context.litAuth = Lit.Auth;
context.siweMessage = siweMessage;
LitActions.setResponse({
response: JSON.stringify({
Expand Down
5 changes: 4 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@lit-protocol/uint8arrays": "3.1.4",
"@mailchimp/mailchimp_marketing": "^3.0.80",
"@pinata/sdk": "^2.1.0",
"@sentry/node": "^7.101.1",
"@sentry/profiling-node": "^7.101.1",
"aws-sdk": "^2.1327.0",
"cid": "multiformats/cid",
"cookie-parser": "~1.4.6",
Expand Down Expand Up @@ -51,7 +53,8 @@
"striptags": "^3.2.0",
"uint8arrays": "^4.0.3",
"vm2": "^3.9.17",
"web3": "^4.1.1"
"web3": "^4.1.1",
"winston": "^3.11.0"
},
"type": "module"
}
38 changes: 38 additions & 0 deletions api/src/clients/lit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as LitJsSdk from "@lit-protocol/lit-node-client-nodejs";


class LitNodeClientSingleton {
constructor() {
if (!LitNodeClientSingleton.instance) {
// Inline configuration
const config = {
litNetwork: 'habanero',
debug: true,
};

this.client = new LitJsSdk.LitNodeClientNodeJs(config);
this.connect();
LitNodeClientSingleton.instance = this;
}
return LitNodeClientSingleton.instance.client;
}

async connect() {
try {
await this.client.connect();
console.log('LitNodeClient connected successfully.');
} catch (error) {
console.error('Failed to connect LitNodeClient:', error);
}
}
}

// Ensuring the object is a singleton
LitNodeClientSingleton.instance = null;

export function getLitNodeClient() {
if (!LitNodeClientSingleton.instance) {
return new LitNodeClientSingleton();
}
return LitNodeClientSingleton.instance.client;
}
12 changes: 10 additions & 2 deletions api/src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export const createIndex = async (req, res, next) => {

const indexService = new IndexService().setSession(pkpSession); //PKP
const newIndex = await indexService.createIndex(req.body);
if(!newIndex){
return res.status(500).json({ error: "Create index error" });
}

//Cache pkp session after index creation.
const sessionCacheKey = `${pkpSession.did.parent}:${newIndex.id}:${newIndex.signerFunction}`
await redis.hSet("sessions", sessionCacheKey, pkpSession.serialize());

const didService = new DIDService().setSession(req.session); //Personal
const newIndexDID = await didService.addIndex(newIndex.id, "owned");
Expand Down Expand Up @@ -70,7 +77,8 @@ export const updateIndex = async (req, res, next) => {
.setSession(pkpSession)
.updateIndex(req.params.id, req.body);

res.status(200).json(newIndex);
return await getIndexById(req, res, next);

} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down Expand Up @@ -114,4 +122,4 @@ export const getQuestions = async (req, res, next) => {
} catch (error) {
res.status(500).json({ error: error.message });
}
};
};
86 changes: 42 additions & 44 deletions api/src/controllers/lit-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,77 @@ import { getNftMetadataApi, getCollectionMetadataApi, getENSProfileByWallet } fr

const enrichConditions = async (conditions) => {

conditions = await Promise.all(conditions.map( async (condition) => {
conditions = await Promise.all(conditions.map( async (c) => {

if(condition.operator === "or"){
return condition;
}

if(!condition.contractAddress){
condition.metadata = {
let { value, tag } = c;
if(!value.contractAddress){
value.metadata = {
ruleType: "individualWallet",
walletAddress: condition.returnValueTest.value,
chain: condition.chain,
walletAddress: value.returnValueTest.value,
chain: value.chain,
};

let profile = await getENSProfileByWallet(condition.returnValueTest.value);
let profile = await getENSProfileByWallet(value.returnValueTest.value);
if(profile){
condition.metadata.ensName = profile.ensName;
value.metadata.ensName = profile.ensName;
if(profile.image){
condition.metadata.image = profile.image;
value.metadata.image = profile.image;
}
}
return condition;
c.value = value;
return c;
}

if(condition.standardContractType) {
if (condition.standardContractType === "ERC20") {
condition.metadata = {
if(value.standardContractType) {
if (value.standardContractType === "ERC20") {
value.metadata = {
ruleType: "nftOwner",
chain: condition.chain,
contractAddress: condition.contractAddress,
chain: value.chain,
contractAddress: value.contractAddress,
}
} else if (condition.standardContractType === "ERC721") {
condition.metadata = {
} else if (value.standardContractType === "ERC721") {
value.metadata = {
ruleType: "nftOwner",
chain: condition.chain,
contractAddress: condition.contractAddress,
chain: value.chain,
contractAddress: value.contractAddress,
}
if (condition.method === "ownerOf") {
condition.metadata.tokenId = condition.parameters[0];
if (value.method === "ownerOf") {
value.metadata.tokenId = value.parameters[0];
}
} else if (condition.standardContractType === "ERC1155") {
condition.metadata = {
} else if (value.standardContractType === "ERC1155") {
value.metadata = {
ruleType: "nftOwner",
chain: condition.chain,
contractAddress: condition.contractAddress,
tokenId: condition.parameters[1],
chain: value.chain,
contractAddress: value.contractAddress,
tokenId: value.parameters[1],
}
}
let collectionMetadata = await getCollectionMetadataApi(condition.chain, condition.contractAddress);
let collectionMetadata = await getCollectionMetadataApi(value.chain, value.contractAddress);
if(collectionMetadata){
condition.metadata.standardContractType = condition.standardContractType;
condition.metadata.symbol = collectionMetadata.symbol;
if(condition.metadata.tokenId){
let tokenMetadata = await getNftMetadataApi(condition.chain, condition.contractAddress, condition.metadata.tokenId);
value.metadata.standardContractType = value.standardContractType;
value.metadata.symbol = collectionMetadata.symbol;
if(value.metadata.tokenId){
let tokenMetadata = await getNftMetadataApi(value.chain, value.contractAddress, value.metadata.tokenId);
if(tokenMetadata){
if(condition.standardContractType === "ERC721"){
condition.metadata.name = `${collectionMetadata.name} - ${tokenMetadata.metadata.name}`;
condition.metadata.image = tokenMetadata.metadata.image;
if(value.standardContractType === "ERC721"){
value.metadata.name = `${collectionMetadata.name} - ${tokenMetadata.metadata.name}`;
value.metadata.image = tokenMetadata.metadata.image;
}
if(condition.standardContractType === "ERC1155"){
condition.metadata.name = tokenMetadata.metadata.name;
condition.metadata.image = tokenMetadata.metadata.image;
if(value.standardContractType === "ERC1155"){
value.metadata.name = tokenMetadata.metadata.name;
value.metadata.image = tokenMetadata.metadata.image;
}
}else{
condition.metadata.name = collectionMetadata.name;
value.metadata.name = collectionMetadata.name;
}
}else{
condition.metadata.name = collectionMetadata.name
value.metadata.name = collectionMetadata.name
}
}
}

return condition;

c.value = value;
return c;
}));

return conditions;
Expand Down
Loading

0 comments on commit 9324790

Please sign in to comment.