-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
252 lines (229 loc) · 9.8 KB
/
index.html
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css" />
<title>All friends</title>
</head>
<style>
/*
Choose white text on a black background so you can add color in.
Pick your favorite font and choose a font size.
*/
@import url('https://fonts.googleapis.com/css2?family=Dosis:wght@200;300;400;500;600;700;800&display=swap');
html,
body {
font-family: "Dosis", sans-serif;
}
</style>
<body class="black whitet">
<!-- Welcome Screen -->
<div class="hold full hue2" id="app">
<div class="center" id="welcome">
<h1 class="fat">All<span class="blackt">Friends</span></h1>
<div class="center" style="width: 50%" id="register-section">
<input class="crack center inside row" type="text" id="username" placeholder="Enter Username" />
<button class="white blackt sap row higher " id="register-button" onclick="register()">Register</button>
</div>
<div class="crack">
<div class="center" id="login-section" style="display: none; width:50%">
<input class="crack center inside row" type="text" id="key" placeholder="Paste Key" />
<button class="white blackt sap row higher" id="login-button"
onclick="login(JSON.parse($('#key').val()))">Login</button>
</div>
</div>
<button style="cursor:pointer; display: none" onMouseOver="this.style.color='#ffffff1f'"
onMouseOut="this.style.color='#ffffff99'" class=" row" id="forgot-navigate">Lost Key?</button>
<button style="cursor:pointer" class="whitet sit" id="login-navigate">Already have an account?</button>
<button class="whitet row" id="register-navigate" style="display: none;cursor:pointer">Create an
account!</button>
</div>
<div id="home" style="display: none">
<div class="page center" style="width: 50%">
<h1 id="title"></h1>
<button class="whitet black sap crack" onclick="testClick()">Copy
Public Address</button>
<p class="tintt crack thin">Enter public key of your friends (3 to 5 unique friends)</p>
<div class="crack " style="display: flex; flex-direction: column;">
<div class="crack" id="friends"></div>
<input class="crack row" placeholder="Enter public key of your friend" type="text" id="pub" />
<button class="blackt white sap crack" id="addPub" onclick="addPubs()">Add</button>
<button class="hide whitet black sap crack" id="setup3FA" onclick="setup3FA()">Submit</button>
</div>
<div class="">
<button class="shade whitet sap crack" id="logout-button" onclick="logout()">Logout</button>
</div>
</div>
</div>
<div id="lost" class="center" style="display: none">
<h1 class="fat">Lost<span class="blackt">Key?</span></h1>
<div class="center" style="width: 50%" id="register-section">
<input class="crack center inside row" type="text" id="lostPub" placeholder="Enter Your public key" />
<button class="white blackt sap row " id="forgot-button" onclick="forgotPassword()">Share
URL</button>
</div>
</div>
<div id="help" class="center" style="display: none">
<h1 class="fat">Is <span class="blackt" id="lostUsername"></span> the one who sent you the link?</h1>
<div class="center" style="width: 50%" display:flex; direction: column; justify-content: center; width:
50%">
<button id="accept" class="white blackt sap crack row">Yes</button>
<button id="ignore" class="black whitet sap row">Go Back</button>
</div>
</div>
<div id="waiting" class="center" style="display: none">
<p id="status" class="fat">Waiting for approval</p>
<div class="center" style="width: 50%" id="goback-container"></div>
</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/lib/then.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/lib/fun.js"></script>
<script src="./secrets.min.js"></script>
<script src="./3fa.js"></script>
<script>
const gun = Gun(['https://gun-manhattan.herokuapp.com/gun']);
const profile = {
username: "",
key: {},
secretPubs: []
}
let realKey = {};
const user = gun.user();
const swap = (current, next) => {
$("#" + current).hide()
$("#" + next).show()
}
const appendUser = (to, name, pub) => {
var user = `<div id='${pub}' class="bolder blackt"> ${name}</div>`;
to.append(user)
}
//Register account
const register = () => {
var name = $("#username").val()
return SEA.pair().then(async (k) => {
login(k)
name && user.get("profile").get("name").put(name)
user.get("epub").put(user._.sea.epub)
$("#username").val("")
})
}
const login = async (k) => {
user.auth(k);
if (user.is) {
realKey = JSON.stringify(user.is);
var username = await user.get("profile").get("name").then()
$("#title").html(`Welcome <span class="blackt">${username}</span>`)
localStorage.setItem("userkey", realKey);
console.log(username)
profile.username = username
profile.key = realKey;
console.log("profile.key.pub",realKey)
swap("welcome", "home")
}
$("#key").val("")
$("#waiting").hide()
gun.init3fa(pubs => {
pubs.length === 3 && pubs.forEach(async (pub) => {
var name = await gun.user(pub).get("profile").get("name").then()
appendUser($("#friends"), name, pub)
})
})
}
const testClick = () => {
console.log("11111",realKey);
const pub = JSON.stringify(JSON.parse(realKey).pub).split("\"")[1];
console.log("22222",pub);
navigator.clipboard.writeText(pub);
}
const addPubs = async () => {
var pub = $("#pub").val()
var name = await gun.user(pub).get("profile").get("name").then()
if (name) {
if (!(profile.secretPubs.includes(pub)) && pub !== profile.key.pub) {
appendUser($("#friends"), name, pub)
}
else {
console.log("error")
}
profile.secretPubs.length >= 2 && $("#setup3FA").removeClass("hide")
profile.secretPubs = [...profile.secretPubs, pub]
}
$("#pub").val("")
}
const forgotPassword = async () => {
var pub = $("#lostPub").val()
var name = await gun.user(pub).get('profile').get('name').then()
gun.forget3fa(pub, (pass, hash) => {
var url = `${location.href}?p=${pass}#${hash}`
var myUrl = `${location.href}?pub=${pub}#${hash}`
navigator.clipboard.writeText(url).then(() => {
$("#forgot-button").html("Copied URL")
})
history.pushState({}, '', myUrl)
location.reload()
$("#lostPub").val("")
})
}
const logout = () => {
localStorage.removeItem("userkey")
user.leave()
swap("home", "welcome")
}
// Encryptes deffie helman encryption with key and epub of the shared instance
const secretEncrypt = async (data, key, epub) => await SEA.encrypt(data, await SEA.secret(epub, key))
const secretDecrypt = async (data, key, epub) => await SEA.decrypt(data, await SEA.secret(epub, key))
const setup3FA = async () => {
gun.setup3fa(profile.secretPubs, profile.key)
}
const helpFriend = async () => {
var searchParam = new URLSearchParams(window.location.search)
var hash = window.location.hash.slice(1)
var key = localStorage.getItem("userkey")
if (searchParam.has("p") && key) {
swap("home", "help")
var pass = searchParam.get("p");
gun.help3fa(hash, pass, JSON.parse(key), async (pub, dec) => {
var u = await gun.user(pub).get("profile").get("name").then()
$("#lostUsername").text(u)
$("#accept").click(async () => {
gun.get("approved").get(hash).set(dec)
swap("help", "home")
})
})
} else if (searchParam.has("pub")) {
swap("welcome", "waiting")
var pub = searchParam.get("pub");
gun.wait3fa(pub, hash)
}
}
$("#ignore").click(() => {
window.location.href = "/allfriends"
})
const init = async () => {
helpFriend()
let key = localStorage.getItem("userkey")
key && login(JSON.parse(key))
// Navigation Logic
$("#login-navigate").click(() => {
swap("login-navigate", "register-navigate");
swap("register-section", "login-section")
$("#forgot-navigate").show();
})
$("#forgot-navigate").click(() => {
swap("welcome", "lost")
})
$("#register-navigate").click(() => {
swap("register-navigate", "login-navigate");
swap("login-section", "register-section")
$("#forgot-navigate").hide();
})
}
init();
</script>
</html>