forked from tonyinit/nostrogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
74 lines (66 loc) · 1.83 KB
/
generator.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
importScripts("https://unpkg.com/nostr-tools/lib/nostr.bundle.js");
let privatekey;
let publickey;
let nsec;
let npub;
let start;
let found = false;
let counter = 0;
let bech32words;
let wordOfInterest = "";
let searchWords;
onmessage = async (e) => {
const { prefix, isPrefix, isSearchingWords } = e.data;
searchWords = isSearchingWords;
if(isSearchingWords) {
await fetch('bech32words.txt')
.then(response => response.text())
.then(text => {
bech32words = text.split('\n').filter(word => word !== '');
});
}
start = Date.now();
while(!found) {
counter++;
// update counter UI
postMessage({counter: counter});
// generate the keys
privatekey = NostrTools.generatePrivateKey();
publickey = NostrTools.getPublicKey(privatekey);
nsec = NostrTools.nip19.nsecEncode(privatekey);
npub = NostrTools.nip19.npubEncode(publickey);
if(isMatched(prefix, npub, isPrefix)) {
if(wordOfInterest !== "") {
postMessage({wordOfInterest, npub, privatekey});
wordOfInterest = "";
} else {
let end = Date.now();
let time = (end - start) / 1000;
// send the keys back to the main thread
postMessage({ npub, nsec, publickey, privatekey, time });
}
}
}
};
function isMatched(prefix, npub, isPrefix){
if(isPrefix) {
if (npub.substring(5, 5 + prefix.length) === prefix) {
found = true;
return true;
}
} else {
if (npub.substring(npub.length - prefix.length) === prefix) {
found = true;
return true;
}
}
if(searchWords) {
for(const word of bech32words) {
if(npub.substring(5).startsWith(word) || npub.substring(5).endsWith(word)){
wordOfInterest = word;
return true;
}
}
}
return false;
}