-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marin Petrunic <[email protected]>
- Loading branch information
Showing
10 changed files
with
2,309 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SERVER_PRIVATE_KEY=b30fbb9af7dd8b265d00f588c42ba98d1ebcbcfdc88c1a58ba9f79b8e0aeeff0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
PORT=3000 | ||
GITHUB_CLIENT_ID= | ||
GITHUB_CLIENT_SECRET= | ||
GITHUB_CLIENT_SECRET= | ||
SERVER_PRIVATE_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { MessagePCDPackage, type MessagePCD } from "@pcd/message-pcd"; | ||
import { | ||
FeedHost, | ||
PollFeedRequest, | ||
PollFeedResponseValue, | ||
verifyCredential, | ||
} from "@pcd/passport-interface"; | ||
import { | ||
DeleteFolderPermission, | ||
PCDAction, | ||
PCDActionType, | ||
PCDPermissionType, | ||
type ReplaceInFolderPermission | ||
} from "@pcd/pcd-collection"; | ||
import { ArgumentTypeName, SerializedPCD } from "@pcd/pcd-types"; | ||
import { FastifyInstance, type FastifyPluginAsync } from "fastify"; | ||
|
||
const FOLDER = "Github Ranks" | ||
|
||
export async function registerFeedService(server: FastifyInstance) { | ||
const feedPlugin: FastifyPluginAsync = async (instance, opts) => { | ||
const feedHost = new FeedHost( | ||
[ | ||
{ | ||
feed: { | ||
id: "999999999999", | ||
name: "zu-git-rank", | ||
description: "Feed containing your Git badges", | ||
permissions: [ | ||
{ | ||
folder: FOLDER, | ||
type: PCDPermissionType.DeleteFolder | ||
} as DeleteFolderPermission, | ||
{ | ||
folder: FOLDER, | ||
type: PCDPermissionType.ReplaceInFolder | ||
} as ReplaceInFolderPermission, | ||
], | ||
credentialRequest: { | ||
signatureType: "sempahore-signature-pcd", | ||
pcdType: "email-pcd" | ||
} | ||
}, | ||
handleRequest: async ( | ||
req: PollFeedRequest | ||
): Promise<PollFeedResponseValue> => { | ||
if (req.pcd === undefined) { | ||
throw new Error(`Missing credential`); | ||
} | ||
|
||
const { emailClaim, emailSignatureClaim } = await verifyCredential(req.pcd); | ||
|
||
if (emailClaim && emailSignatureClaim) { | ||
// const verified = | ||
// _.isEqual(emailSignatureClaim.publicKey, ZUPASS_PUBLIC_KEY); | ||
return { | ||
actions: await feedActionsForEmail( | ||
server.config.SERVER_PRIVATE_KEY, | ||
emailClaim.emailAddress | ||
) | ||
}; | ||
} | ||
return { actions: [] }; | ||
} | ||
} | ||
], | ||
"https://8c57-185-199-104-14.ngrok-free.app/feeds", | ||
"Zu Git Proof Feed Server" | ||
); | ||
instance.decorate("feed", feedHost) | ||
} | ||
//@ts-expect-error | ||
feedPlugin[Symbol.for('skip-override')] = true | ||
|
||
await server.register(feedPlugin); | ||
} | ||
|
||
async function feedActionsForEmail( | ||
privateKey: string, | ||
emailAddress: string | ||
): Promise<PCDAction[]> { | ||
|
||
const actions = []; | ||
|
||
// Clear out the folder | ||
actions.push({ | ||
type: PCDActionType.DeleteFolder, | ||
folder: FOLDER, | ||
recursive: false | ||
}); | ||
|
||
//TODO: check bacdge by email | ||
actions.push({ | ||
type: PCDActionType.ReplaceInFolder, | ||
folder: FOLDER, | ||
pcds: [ | ||
await issueBadgePCD(privateKey, "Core Contributor") | ||
] | ||
}); | ||
|
||
return actions; | ||
} | ||
|
||
async function issueBadgePCD( | ||
privateKey: string, | ||
badge: string | ||
): Promise<SerializedPCD<MessagePCD>> { | ||
|
||
const pcd = await MessagePCDPackage.prove({ | ||
message: { | ||
argumentType: ArgumentTypeName.Object, | ||
value: { | ||
displayName: "Github Rank", | ||
mdBody: badge | ||
} | ||
}, | ||
privateKey: { | ||
value: privateKey, | ||
argumentType: ArgumentTypeName.String | ||
}, | ||
id: { | ||
value: undefined, | ||
argumentType: ArgumentTypeName.String | ||
} | ||
}); | ||
|
||
return MessagePCDPackage.serialize(pcd); | ||
} | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
feed: FeedHost | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import type { FastifyInstance } from "fastify"; | ||
import { pingRoute } from "./routes/ping"; | ||
import { getEcdsaKey, getFeed, getFeeds, postFeeds } from "./routes/feeds"; | ||
import { authRoute } from "./routes/auth"; | ||
|
||
export async function registerRoutes(server: FastifyInstance) { | ||
pingRoute(server); | ||
authRoute(server); | ||
pingRoute(server) | ||
authRoute(server); | ||
getFeeds(server) | ||
getFeed(server) | ||
postFeeds(server) | ||
getEcdsaKey(server) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getEdDSAPublicKey } from "@pcd/eddsa-pcd"; | ||
import type { ListFeedsRequest, PollFeedRequest } from "@pcd/passport-interface"; | ||
import type { FastifyInstance } from "fastify"; | ||
|
||
export function getFeeds(server: FastifyInstance) { | ||
server.get("/feeds", async (req, res) => { | ||
const request = req.body as ListFeedsRequest; | ||
|
||
return res.send(await server.feed.handleListFeedsRequest(request)); | ||
}); | ||
} | ||
|
||
export function postFeeds(server: FastifyInstance) { | ||
server.post("/feeds", async (req, res) => { | ||
const request = req.body as PollFeedRequest; | ||
|
||
return res.send(await server.feed.handleFeedRequest(request)); | ||
}); | ||
} | ||
|
||
export function getFeed(server: FastifyInstance) { | ||
server.get("/feeds/:feedId", async (req, res) => { | ||
//@ts-expect-error | ||
const feedId = req.params.feedId; | ||
if (server.feed.hasFeedWithId(feedId)) { | ||
const request = { feedId }; | ||
return res.send(await server.feed.handleListSingleFeedRequest(request)); | ||
} else { | ||
return res.status(404).send("not found"); | ||
} | ||
}); | ||
} | ||
|
||
export function getEcdsaKey(server: FastifyInstance) { | ||
server.get("/issue/eddsa-public-key", async (req, res) => { | ||
return res.send( | ||
await getEdDSAPublicKey(server.config.SERVER_PRIVATE_KEY) | ||
); | ||
}); | ||
} |
Oops, something went wrong.