-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
234 lines (186 loc) · 7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const express = require("express");
const DIDKit = require('@spruceid/didkit-wasm-node');
const Database = require("@replit/database");
const { v4: uuidv4 } = require('uuid');
const path = require("path");
const port = 3000;
const server = express();
const db = new Database();
server.use(express.urlencoded({ extended: true }));
server.set('views', path.join(__dirname, 'views'));
server.set('view engine', 'ejs');
server.use(express.static(path.join(__dirname, 'public')));
server.get("/", (req, res) => {
res.render('pages/index')
});
server.post("/account", async (req, res) => {
var handle = req.body.fhandle;
var handleAlreadyTaken = await handleUniqueness(handle);
if (handleAlreadyTaken == false) {
var result = await createAccount(handle);
console.log(`Account created for handle:${handle}, did:${result["did"]}`);
res.redirect(`profile/${result["did"]}`);
} else {
console.log(`Handle: ${handle} already taken`);
res.status(404).send(`Handle: ${handle} already taken`);
};
});
server.post("/signin", async (req, res) => {
var handle = req.body.fhandle;
var handleAlreadyTaken = await handleUniqueness(handle);
if (handleAlreadyTaken == false) {
console.log("Create an account first");
res.status(404).send("Create an account first");
} else {
var did = await didByHandle(handle);
console.log(`Profile lookup for handle:${handle}, DID: ${did}`);
res.redirect(`profile/${did}`);
};
});
server.post("/request_follow", async (req, res) => {
var handle = req.body.fhandle;
var userExists = await handleUniqueness(handle);
if (userExists == true) {
var issuerDID = await didByHandle(handle);
var subjectDID = req.body.subjectDID;
var alreadyPending = await checkPendingRequest(subjectDID, issuerDID);
var alreadyFollowing = await checkFollowing(subjectDID, issuerDID);
if (alreadyPending == false && alreadyFollowing == false) {
issuerInbox = await db.get(`inbox-${issuerDID}`);
issuerInbox.push(subjectDID);
db.set(`inbox-${issuerDID}`, issuerInbox);
addPendingRequest(subjectDID, issuerDID);
res.send("Request Sent");
res.status(201);
} else {
res.status(409).send(`Follow Request already sent Or Already following ${handle}`);
}
} else {
console.log(`Account with handle:${handle} doesn't exist`);
res.status(404).send(`Account with handle:${handle} doesn't exist`);
};
});
server.post("/accept_follow", async (req, res) => {
var subjectDID = req.body.subjectDID;
var issuerDID = req.body.issuerDID;
var signedVC = await issueVC(subjectDID, issuerDID);
updateFollowing(subjectDID, signedVC);
updateFollowers(issuerDID, signedVC);
removeRequestFromInbox(subjectDID, issuerDID);
removeRequestFromPending(subjectDID, issuerDID);
res.redirect(`profile/${issuerDID}`);
});
server.get("/profile/:id", async (req, res) => {
var params = req.params;
var did = params["id"];
var result = await db.get(did);
var followers = await db.get(`followers-${did}`);
var following = await db.get(`following-${did}`);
res.render('pages/profile_v2',{
did: params["id"],
key: result["key"],
handle: result["handle"],
follower_count: followers.length,
following_count: following.length
});
});
server.get("/contactlist/:id", async (req, res) => {
var params = req.params;
var did = params["id"];
var doc = DIDKit.resolveDID(did, "{}");
var inbox = await db.get(`inbox-${did}`);
var followers = await db.get(`followers-${did}`);
var following = await db.get(`following-${did}`);
var pendingRequests = await db.get(`pending-${did}`);
res.render('pages/contact_list',{
did: did,
inbox: inbox,
doc: JSON.stringify(JSON.parse(await doc), null, 2),
followers: followers,
following: following,
pendingRequests: pendingRequests
});
});
server.listen(port, (err) => {
if (err) throw err;
console.log(
`> Ready on ${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
);
});
async function handleUniqueness(handle) {
var handlesTaken = await db.list("handle-");
return handlesTaken.includes(`handle-${handle}`);
};
async function checkPendingRequest(subjectDID, issuerDID) {
var pendingList = await db.get(`pending-${subjectDID}`);
return pendingList.includes(issuerDID);
};
async function checkFollowing(subjectDID, issuerDID) {
var followingList = await db.get(`following-${subjectDID}`);
var followingDIDs = []
followingList.forEach((element) => {
console.log(JSON.parse(element));
followingDIDs.push(JSON.parse(element).issuer);
})
return followingDIDs.includes(issuerDID);
};
async function createAccount(handle) {
var key = DIDKit.generateEd25519Key();
var did = DIDKit.keyToDID('key', key);
var doc = DIDKit.resolveDID(did, "{}");
db.set(did, {"key": key, "handle": handle});
db.set(`inbox-${did}`, []);
db.set(`followers-${did}`, []);
db.set(`following-${did}`, []);
db.set(`handle-${handle}`, did);
db.set(`pending-${did}`, []);
return {key: key, did: did, doc:doc}
};
async function didByHandle(handle) {
return db.get(`handle-${handle}`)
};
async function constructUnsignedVC(subjectDID, issuerDID) {
var uuid = uuidv4();
var date = new Date().toISOString();
return `{\"@context\": \"https://www.w3.org/2018/credentials/v1\",\"id\":\"urn:did:${uuid}\",\"type\": [\"VerifiableCredential\"],\"issuer\": \"${issuerDID}\",\"issuanceDate\": \"${date}\",\"credentialSubject\": {\"id\": \"${subjectDID}\"}}`
};
async function issueVC(subjectDID, issuerDID){
var unsignedVC = await constructUnsignedVC(subjectDID, issuerDID);
var issuer = await db.get(issuerDID);
var signingKey = issuer["key"];
var issuerVerificationMethod = await DIDKit.keyToVerificationMethod("key", signingKey);
var vcOptions = `{"proofPurpose": "assertionMethod", "verificationMethod": "${issuerVerificationMethod}"}`;
var signedVC = await DIDKit.issueCredential(unsignedVC, vcOptions, signingKey).catch(e => { console.log(e) });
return signedVC;
};
async function updateFollowing(did, message) {
var followingList = await db.get(`following-${did}`);
followingList.push(message);
db.set(`following-${did}`, followingList);
};
async function updateFollowers(did, message) {
var followersList = await db.get(`followers-${did}`);
followersList.push(message);
db.set(`followers-${did}`, followersList);
};
async function removeRequestFromInbox(subjectDID, issuerDID) {
var inbox = await db.get(`inbox-${issuerDID}`);
const index = inbox.indexOf(subjectDID);
if (index > -1) {
inbox.splice(index, 1);
}
db.set(`inbox-${issuerDID}`, inbox);
};
async function addPendingRequest(subjectDID, issuerDID) {
var pending = await db.get(`pending-${subjectDID}`);
pending.push(issuerDID);
db.set(`pending-${subjectDID}`, pending);
};
async function removeRequestFromPending(subjectDID, issuerDID) {
var pending = await db.get(`pending-${subjectDID}`);
const index = pending.indexOf(issuerDID);
if (index > -1) {
pending.splice(index, 1);
}
db.set(`pending-${subjectDID}`, pending);
};