-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
224 lines (197 loc) · 7.31 KB
/
store.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
import {sendVerifyCode, verifyCode, loginUser} from './common.js'
(async () => {
const metamaskWarning = document.getElementById("metamaskWarning");
let userData = null;
// Auto login event
window.addEventListener("load", async () => {
const localData = JSON.parse(localStorage.getItem("userData"))
if(localData){
const loginForm = document.getElementById("login-form")
const newUserData = await loginUser(localData.email, localData.token)
if(newUserData){
userData = newUserData
loginForm.classList.replace("phase-1", "phase-3")
document.getElementById("login-email-static").innerText = userData.email
localStorage.setItem("userData", JSON.stringify(newUserData))
}
}
})
const contract = await new Promise((accept, reject) => {
window.addEventListener('load', async () => {
const accessContract = async (web3) => {
window.web3 = web3;
const ress = await Promise.all([
fetch('abi.json'),
fetch('address.json'),
]);
const [jsonInterface, address] = await Promise.all(ress.map(res => res.json()));
return web3.eth.contract(jsonInterface).at(address);
}
let contract = null;
// Modern dapp browsers...
if (window.ethereum) {
try {
// Request account access if needed
await window.ethereum.enable();
contract = await accessContract(new window.top.Web3(ethereum))
} catch (error) {
metamaskWarning.style.display = "block";
console.warn("User denied access to Metamask account.")
}
}
// Legacy dapp browsers...
else if (window.web3) {
contract = await accessContract(new window.Web3(window.web3.currentProvider))
}
// Non-dapp browsers...
else {
metamaskWarning.style.display = "block";
console.warn("Non-Ethereum browser detected. You should consider trying MetaMask!")
}
if (contract) {
accept(contract)
}
else {
reject(new Error("No contract"))
}
});
});
const res = await fetch(`https://content.exokit.org/users/avaer`);
const root = await res.json();
const files = [];
const hashes = [];
let keypath = '';
const _recurse = node => {
keypath += (keypath ? '/' : '') + node.name;
if (node.hash) {
files.push(keypath);
hashes.push('0x' + node.hash);
}
if (node.children) {
for (let i = 0; i < node.children.length; i++) {
_recurse(node.children[i]);
}
}
};
_recurse(root);
const ids = await Promise.all(hashes.map(hash => new Promise((accept, reject) => {
// const hash = '0x' + filename.match(/([^\/]*)$/)[1];
contract.getId(hash, (err, id) => {
if (!err) {
accept(id.toNumber());
} else {
reject(err);
}
});
})));
// Define links for popout-nav
const navLinks = [
{
url: "https://store.exokit.org/",
title: "Store",
active: window.location.host.split(".")[0] === "store"
},
{
url: "https://avatars.exokit.org/",
title: "Avatars",
active: window.location.host.split(".")[0] === "avatars"
},
{
url: "https://multiplayer.exokit.org/",
title: "Multiplayer",
active: window.location.host.split(".")[0] === "multiplayer"
},
{
url: "https://editor.exokit.org/",
title: "Editor",
active: window.location.host.split(".")[0] === "editor"
},
{
url: "https://exokit.org/",
title: "Home",
active: window.location.host.split(".")[0] === "exokit"
}
]
// Populate nav-popout with links
document.getElementById('nav-popout').innerHTML = navLinks.map((link, index) => {
return `
<div>
<a href=${link.url} class=${link.active ? "activeNav" : ""}>${link.title}</a>
</div>
`
}).join(`<br/>`)
// Close nav-popout if user clicks away
document.getElementById("nav-popout").addEventListener("blur", (e) => {
const navPopout = document.getElementById('nav-popout');
// wait for <a> to fire
setTimeout(() => {
navPopout.style.display = "none"
}, 100)
})
// Handle logo click, open nav-popout
document.getElementById("logo").addEventListener("click", (e) => {
const navPopout = document.getElementById('nav-popout');
if (document.activeElement === navPopout) {
navPopout.blur()
}
else {
navPopout.style.display = "block";
navPopout.focus()
}
})
document.getElementById("logo").addEventListener("mousedown", (e) => {
e.preventDefault()
})
// Populate cardContainer with cards
document.getElementById('cardContainer').innerHTML = files.map((file, i) => {
const rawHash = hashes[i].slice(2);
const id = ids[i];
return `
<div class="app-card">
<img src="https://content.exokit.org/preview/${rawHash}" height=100>
<div class="cardLink">
<a href="https://viewer.exokit.org/${rawHash}">${file}</a>
</div>
<div class="openSeaBtn">
<a href="https://rinkeby.opensea.io/assets/${contract.address}/${id}" class="button first last external-link-button">OpenSea</a>
</div>
</div>
`
}).join('\n');
// Login and Verify stuff
let email = "";
let verificationCode = ""
document.getElementById("login-email").addEventListener("change", (e) => {
email = e.target.value;
})
document.getElementById("login-verification-code").addEventListener("change", (e) => {
verificationCode = e.target.value;
})
document.getElementById('login-form').addEventListener('submit', async (e) => {
e.preventDefault()
const loginForm = document.getElementById("login-form")
const formPhase = e.target.classList[1]
switch (formPhase){
case "phase-1":
if(await sendVerifyCode(email)){
loginForm.classList.replace("phase-1", "phase-2")
}
break;
case "phase-2":
userData = await verifyCode(email, verificationCode);
if(userData){
document.getElementById("login-email-static").innerText = userData.email
loginForm.classList.replace("phase-2", "phase-3")
localStorage.setItem("userData", JSON.stringify(userData))
}
break;
default:
break;
}
})
document.getElementById("logout").addEventListener("click", (e) => {
userData = null;
localStorage.removeItem("userData")
window.location.reload();
})
})();