forked from tonyinit/nostrogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nostrogen.js
147 lines (131 loc) · 4.26 KB
/
nostrogen.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
// consts
const prefixRadioEl = document.getElementById("prefixRadio");
const suffixRadioEl = document.getElementById("suffixRadio");
const wordsOfInterestEl = document.getElementById("wordsOfInterestCheck");
const prefixEl = document.getElementById("prefix");
const progressEl = document.querySelector(".progress-bar");
const npubEl = document.getElementById("npub");
const nsecEl = document.getElementById("nsec");
const publicEl = document.getElementById("public");
const privateEl = document.getElementById("private");
const timeEl = document.getElementById("time");
const counterEl = document.getElementById("counter");
const tableOfInterestEl = document.getElementById("tableOfInterest");
const wordsOfInterestContainer = document.getElementById("wordsOfInterestContainer");
// lets
let worker;
let isPrefix = prefixRadioEl.checked;
let isSearchingWords = wordsOfInterestEl.checked;
function startWorker() {
worker = new Worker('generator.js');
worker.addEventListener('message', (e) => {
// handle the received data
if(e.data.counter) {
counterEl.innerHTML = e.data.counter;
}
if(e.data.wordOfInterest) {
tableOfInterestEl.innerHTML += `<tr><th>${e.data.wordOfInterest}</th><td>${e.data.npub}</td><td>${e.data.privatekey}</td></tr>`
}
if(e.data.npub && !e.data.wordOfInterest) {
const { npub, nsec, publickey, privatekey, time } = e.data;
stopWorker();
finishUp(npub, nsec, publickey, privatekey, time);
}
})
}
function stopWorker() {
worker.terminate();
}
prefixRadioEl.onclick = function() {
suffixRadioEl.checked = false;
prefixEl.placeholder = 'prefix';
isPrefix = true;
}
suffixRadioEl.onclick = function() {
prefixRadioEl.checked = false;
prefixEl.placeholder = 'suffix';
isPrefix = false;
}
// disallow b's, i's, o's, and 1's
prefixEl.onkeypress = function(e) {
var chr = String.fromCharCode(e.which);
if ("acdefghjklmnpqrstuvwxyz234567890".indexOf(chr) < 0)
return false;
};
// input npub to decode public key
npubEl.addEventListener('input', (e) => {
const { data } = NostrTools.nip19.decode(npubEl.value);
publicEl.value = data;
})
// input private key to fill inputs
privateEl.addEventListener('input', (e) => {
publicEl.value = NostrTools.getPublicKey(privateEl.value);
nsecEl.value = NostrTools.nip19.nsecEncode(privateEl.value);
npubEl.value = NostrTools.nip19.npubEncode(publicEl.value);
})
// handle words of interest checkbox
wordsOfInterestEl.onclick = function() {
if(!isSearchingWords) {
isSearchingWords = wordsOfInterestEl.checked;
wordsOfInterestContainer.style.display = "block";
} else {
isSearchingWords = wordsOfInterestEl.checked;
wordsOfInterestContainer.style.display = "none";
}
}
// start looking...
async function generate() {
progressEl.style="width: 100%";
clear();
startWorker();
// gather the necessary data for the worker
const prefix = prefixEl.value;
const data = { prefix, isPrefix, isSearchingWords };
// start the worker
worker.postMessage(data);
}
// clear values
function clear() {
npubEl.value = '';
nsecEl.value = '';
publicEl.value = '';
privateEl.value = '';
timeEl.innerHTML = '0';
}
function finishUp(npub, nsec, publickey, privatekey, time) {
progressEl.style="width: 0%";
npubEl.value = npub;
nsecEl.value = nsec;
publicEl.value = publickey;
privateEl.value = privatekey;
timeEl.innerHTML = time;
}
// copy to clipboard
function copy(element) {
switch (element) {
case 'npub':
npubEl.select();
npubEl.setSelectionRange(0, 99999);
navigator.clipboard.writeText(npubEl.value);
alert('copied!')
break;
case 'nsec':
nsecEl.select();
nsecEl.setSelectionRange(0, 99999);
navigator.clipboard.writeText(nsecEl.value);
alert('copied!')
break;
case 'public':
publicEl.select();
publicEl.setSelectionRange(0, 99999);
navigator.clipboard.writeText(publicEl.value);
alert('copied!')
break;
case 'private':
privateEl.select();
privateEl.setSelectionRange(0, 99999);
navigator.clipboard.writeText(privateEl.value);
alert('copied!')
break;
}
}