-
Notifications
You must be signed in to change notification settings - Fork 4
/
account.js
447 lines (412 loc) · 15.5 KB
/
account.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
function displayAccount() {
getMuhPubkey().then(pubkey => {
storedPubkey = pubkey
document.getElementById("maincontent").replaceChildren(prepWindow())
document.getElementById("heading").innerText = "Account Details (Currently logged in)"
document.getElementById("content").replaceChildren(renderAccountDetails(pubkey))
document.getElementById("details").replaceChildren(updateAccountDetails(pubkey), document.createElement("br"), recoverSeed(pubkey))
})
}
function isParticipant(pubkey) {
let ident;
ident = identityObjects.get(pubkey)
if (typeof ident === "undefined") {
//
} else {
if (ident.UshBy.length > 0) {
return true
}
}
return false
}
function hasPermanym(pubkey) {
let ident;
ident = identityObjects.get(pubkey)
if (typeof ident === "undefined") {
//
} else {
if (ident.Name.length > 0) {
return true
}
}
return false
}
function renderAccountDetails(pubkey) {
// console.log("redering")
deets = document.createElement("div")
deets.className = "content"
let ident;
ident = identityObjects.get(pubkey)
if (typeof ident === "undefined") {
ident = {Account: pubkey, Name: "", About: "", UshBy: ""}
}
deets.appendChild(createElement("Help", "A new account and corrosponding seed words are generated when you first load this page. \nTo generate a new account, clear your browser's data for this site and reload it.\nIf you start using this account, you should write down your seed words (especially if you claim a Username that you want to keep)."))
deets.appendChild(createElement("Pubkey", ident.Account))
if (window.nostr) {
deets.appendChild(createElement("Seed Words", "You are using a browser plugin to manage your private key(s), please refer to that for your recovery options."))
} else {
deets.appendChild(createElement("Seed Words - write these down if you want to continue using this Account.", localStorage.getItem('backupwords')))
}
deets.appendChild(createElement("Username (permanent psudonym)", ident.Name))
deets.appendChild(createElement("About", ident.About))
var ushby;
if (ident.UshBy.length > 0) {
ushby = ident.UshBy
} else {
ushby = "No one has validated that you are human yet. Post a message in the Samizdat tree to ask."
}
LeadTimeLockedShares = 0
LeadTime = 0
LeadTimeUnlockedShares = 0
approvedExpensesnum = 0
filedExpenses = 0
if (sharesObjects.get(pubkey)){
sharesObjects.get(pubkey).LeadTimeLockedShares ? LeadTimeLockedShares = sharesObjects.get(pubkey).LeadTimeLockedShares : 0 ;
sharesObjects.get(pubkey).LeadTime ? LeadTime = sharesObjects.get(pubkey).LeadTime : 0 ;
sharesObjects.get(pubkey).LeadTimeUnlockedShares ? LeadTimeUnlockedShares = sharesObjects.get(pubkey).LeadTimeUnlockedShares : 0 ;
approvedExpensesnum = approvedExpenses(sharesObjects.get(pubkey).Expenses)
sharesObjects.get(pubkey).Expenses ? filedExpenses = sharesObjects.get(pubkey).Expenses.length : 0 ;
}
deets.appendChild(createElement("Added to the Participant Tree by:", ushby))
deets.appendChild(createElement("Vouched for by", "no one has vouched for you yet"))
deets.appendChild(createElement("Total Shares", LeadTimeUnlockedShares+LeadTimeLockedShares))
deets.appendChild(createElement("Lead Time", LeadTime))
deets.appendChild(createElement("Lead Time Unlocked Shares", LeadTimeUnlockedShares))
deets.appendChild(createElement("Lead Time Locked Shares", LeadTimeLockedShares))
deets.appendChild(createElement("Voting Power", LeadTime*LeadTimeLockedShares))
deets.appendChild(createElement("Maintainer", accountIsInMaintainerTree(pubkey)))
deets.appendChild(createElement("Problems Logged", "0"))
deets.appendChild(createElement("Problems Claimed", "0"))
deets.appendChild(createElement("Problems Solved", "0"))
deets.appendChild(createElement("Expenses Filed", filedExpenses))
deets.appendChild(createElement("Expenses Approved", approvedExpensesnum))
return deets
}
function approvedExpenses(expensesObject){
let num = 0
if (expensesObject) {
expensesObject.forEach(function(v){
if (v.Approved === true){ num++}
})
}
return num
}
function createElement(key, value) {
box = document.createElement("div")
box.className = "box"
if (key) {
k = document.createElement("strong")
k.innerText = key
box.appendChild(k)
box.appendChild(document.createElement("br"))
}
if (value) {
v = document.createElement("p")
v.innerText = value
box.appendChild(v)
}
return box
}
function recoverSeed(pubkey) {
if (!window.nostr) {
div = document.createElement("div")
ident = identityObjects.get(pubkey)
div.appendChild(makeH3("Import your existing account"))
div.appendChild(makeParagraph("If you previously created an account on Stackerstan you can import it here, but you **should** use a browser extension such as getAlby or Nos2x instead."))
form = document.createElement("div")
form.innerHTML = `
<div class="field">
<label class="label">Seed Words OR Private Key</label>
<div class="control">
<textarea class="textarea" placeholder="Paste your seed words OR your private key here" id="seed input"></textarea>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button class="button is-link" onclick="restoreAccount( document.getElementById('seed input').value); location.reload()">Submit</button>
</div>
<div class="control">
<button class="button is-link is-light" onclick="document.getElementById('seed input').value = '';">Cancel</button>
</div>
</div>
`
div.appendChild(form)
return div
}
return document.createElement("p")
}
function restoreAccount(data) {
if (data.length === 64) {
localStorage.removeItem("backupwords")
localStorage.setItem("privatekey", data)
location.reload()
}
localStorage.setItem('backupwords', data)
location.reload()
}
function updateAccountDetails() {
form = document.createElement("div")
form.appendChild(usernameAndBioForm())
form.appendChild(bioButtons(function () {
if (document.getElementById( 'name input' ).valueOf().readOnly) {
setBio( "", document.getElementById( 'about input' ).value, storedPubkey )
//location.reload()
} else {
validateUnique(document.getElementById( 'name input' ).value).then(res => {
if (res) {
setBio( document.getElementById( 'name input' ).value, document.getElementById( 'about input' ).value, storedPubkey )
//location.reload()
} else {
console.log()
alert(document.getElementById( 'name input' ).value + " has been taken, please try another username")
}
})
}
}))
return form
}
async function validateUnique(name) {
p = new Promise((resolve, reject) => {
identityObjects.forEach(function (v) {
if (v.Name === name) {
resolve(false)
}
})
resolve(true)
})
return p
}
function bioButtons(onclick) {
submit = document.createElement("button")
submit.onclick = onclick
submit.className = "button is-link"
submit.innerText = "Submit"
cancel = document.createElement("button")
cancel.onclick = function () {
document.getElementById('name input').value = '';document.getElementById('about input').value = '';
}
cancel.className = "button is-link is-light"
cancel.innerText = "Clear"
buttons = document.createElement("div")
buttons.className = "field is-grouped"
control = document.createElement("div")
control.className = "control"
control.appendChild(submit)
control.appendChild(spacer())
control.appendChild(spacer())
control.appendChild(cancel)
buttons.appendChild(control)
return buttons
}
function usernameAndBioForm() {
div = document.createElement("div")
let username = ""
let about = ""
let haveExistingKind0 = false
if (kind0Objects.get(storedPubkey) !== undefined) {
if (kind0Objects.get(storedPubkey).name.length > 0) {
username = kind0Objects.get(storedPubkey).name
haveExistingKind0 = true
}
if (kind0Objects.get(storedPubkey).about.length > 0) {
about = kind0Objects.get(storedPubkey).about
haveExistingKind0 = true
}
}
div.appendChild(makeH3("Create or modify your Stackerstan profile"))
div.appendChild(makeParagraph("* Stackerstan usernames **cannot** be changed once set for your Pubkey \n* Stackerstan usernames **must** be unique \n* Protocol: [Non-fungible Identity](superprotocolo://b66541b20c8a05260966393938e2af296c1a39ca5aba8e21bd86fcce2db72715)"))
if (haveExistingKind0) {
div.appendChild(makeParagraph("Submit this form to claim _**" + kind0Objects.get(storedPubkey).name + "**_ now."))
}
div.appendChild(makeTextInput("Username", "Name or Pseudonym", "name input", 20, username))
div.appendChild(makeTextField("About Me", "Introduce yourself to the community", "about input", 560, about))
return div
}
function makeParagraph(markdown) {
d = document.createElement("div")
md = new showdown.Converter({
extensions: [...bindings]
})
ht = md.makeHtml(markdown)
mdht = document.createElement("div")
mdht.innerHTML = ht
d.appendChild(mdht)
d.appendChild(document.createElement("br"))
return d
}
function makeLink(url, text) {
a = document.createElement("a")
a.href = url
a.innerText = text
return a
}
function makeH3(title) {
h3 = document.createElement("h3")
h3.className = "is-3"
h3.innerText = title
return h3
}
function makeTextField(label, placeholder, id, maxlength, existing) {
input = document.createElement("textarea")
input.className = "textarea"
if (existing.length > 0) {
input.value = existing
}
input.placeholder = placeholder
input.id = id
input.maxLength = maxlength
return makeFormField(label, input)
}
function makeTextInput(label, placeholder, id, maxlength, existing) {
d = document.createElement("div")
textInput = document.createElement("input")
d.appendChild(textInput)
textInput.className = "input"
textInput.type = "text"
if (existing.length > 0) {
textInput.value = existing
}
textInput.placeholder = placeholder
textInput.id = id
textInput.maxLength = maxlength
if (label === "Username") {
var userameIsAlreadySet = false
identityObjects.forEach(function (v) {
if (v.Account === storedPubkey) {
if (v.Name.length > 0) {
textInput.value = v.Name
textInput.readOnly = true
userameIsAlreadySet = true
}
}
})
if (!userameIsAlreadySet) {
warn = document.createElement("p")
warn.style.display = "none"
identityObjects.forEach(function (v) {
if (v.Name === existing) {
warn.style.display = "block"
}
})
warn.innerText = "username is taken!"
if (existing.length < 1) {
warn.innerText = "username is too short!"
}
warn.style.color = "#FF9900"
textInput.onkeyup = function () {
warn.style.display = "none"
if (textInput.value.length < 1) {
warn.innerText = "username is too short!"
warn.style.display = "block"
}
identityObjects.forEach(function (v) {
if ((v.Name === textInput.value)&&(textInput.value.length > 0)) {
warn.innerText = "username is taken!"
warn.style.display = "block"
}
})
}
d.appendChild(warn)
}
}
if (label === "Amount") {
warn = document.createElement("p")
warn.style.display = "none"
textInput.type = "number"
textInput.onkeyup = function () {
warn.style.display = "none"
int = parseInt(textInput.value, 10)
if (!int) {
warn.style.color = "#FF9900"
warn.innerText = "must be a number!"
warn.style.display = "block"
} else {
if (USD) {
usdAmount = ((int/100000000)*USD).toFixed(2)
warn.innerText = "Approximate amount in Cuckbucks: $" + usdAmount
warn.style.color = "#35be33"
warn.style.display = "block"
} else {
console.log("could not get USD")
}
}
}
d.appendChild(warn)
}
return makeFormField(label, d)
}
function makeFormField(label, input) {
if (label === "About") {
//todo populate from existing data if exists
}
field = document.createElement("div")
field.className = "field"
field.appendChild(makeLabel(label))
control = document.createElement("div")
control.className = "control"
control.appendChild(input)
field.appendChild(control)
return field
}
function makeLabel(name) {
label = document.createElement("label")
label.className = "label"
label.innerText = name
return label
}
function setBio(name, about, pubkey) {
if ((name.length > 0) || (about.length > 0)) {
sequence = 0
if (identityObjects.get(pubkey) !== undefined) {
sequence = identityObjects.get(pubkey).Sequence
}
sequence++
content = JSON.stringify({name: name, about: about, sequence: sequence})
sendEventToMindmachine(content, "", 640400, pubkey).then(x =>{
location.reload()
//if (reload) {location.reload()}
})
} else {
console.log("username and bio can't both be empty")
}
}
function opReturnForm() {
let div = document.createElement("div")
head = document.createElement("h3")
head.className = "is-3"
head.innerText = "Register OP_RETURN address for your pubkey"
div.appendChild(head)
let address = document.createElement("input")
address.type = "text"
address.placeholder = "Bitcoin Address"
address.className = "input is-primary"
let proof = document.createElement("textarea")
proof.className = "textarea"
proof.placeholder = "Proof (BIP322 of your pubkey)"
proof.className = "textarea is-primary"
let submitbtn = document.createElement("button")
submitbtn.textContent = "Make it so"
submitbtn.className = "button is-primary"
submitbtn.onclick = function () {
setOpReturn(address.value, proof.value)
proof.value = ""
address.value = ""
}
div.appendChild(address)
div.appendChild(proof)
div.appendChild(submitbtn)
return div
}
function setOpReturn(address, proof, pubkey) {
sequence = 0
if (identityObjects.get(pubkey) !== undefined) {
sequence = identityObjects.get(pubkey).Sequence
}
sequence++
let content = {"address": address, "proof": proof, sequence: sequence}
let c = JSON.stringify(content)
sendEventToMindmachine(c, "", 640406, pubkey).then(res => {
console.log(res)
location.reload()
})
}