Skip to content

Commit

Permalink
add login to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Sep 15, 2024
1 parent 92a600c commit 6ce19aa
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 16 deletions.
34 changes: 32 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
code {
font-size: 30px;
}

#user {
max-width: 600px;
margin:10px 0
}

#relaysContainer {
background:#f0f0f0;
padding:4px 6px;
}
</style>

<!-- Include NostrTools library -->
Expand All @@ -39,6 +49,11 @@
<h1><code>note⛏️</code></h1>

<p>this is a demo of <strong>notemine</strong>, a wasm nostr note miner written in rust. </p>

<!-- Optional: Button for Nostr authentication -->
<button id="loginButton" style="display: none">
login
</button>

<!-- Optional: Button for Nostr authentication -->
<button
Expand Down Expand Up @@ -69,10 +84,25 @@ <h1><code>note⛏️</code></h1>
>
📦️ crates
</button>

<br/><br/>

<textarea id="eventInput" rows="10" placeholder="Enter event content here"></textarea>
<div id="user">
posting as: <img id="userPhoto" width="20" height="20" src="" /> <span id="userName"></span> <small id="relaysToggle" style="cursor: pointer; color:#333;">(relays)</small>

<div id="relaysContainer" style="display: none">
<small><em>
<span id="myRelaysContainer">
<strong>my relays:</strong> <span id="myRelays"></span>
</span>
<span style="display:block">
<input checked="true" type="checkbox" id="powRelaysEnable" name="powRelaysEnable">
<strong>pow relays: </strong> <span id="powRelays"></span>
</span>
</em></small>
</div>
</div>

<textarea id="eventInput" rows="10" placeholder="140 characters or less." maxlength="140"></textarea>
<br><br>

<label for="difficulty">Difficulty:</label>
Expand Down
16 changes: 16 additions & 0 deletions demo/lib/img/anon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
227 changes: 213 additions & 14 deletions demo/main.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
const CLIENT = 'notemine';
const TOPIC = 'notemine';
const RELAYS = ['wss://nostr.bitcoiner.social', 'wss://nostr.mom', 'wss://nos.lol', 'wss://powrelay.xyz', 'wss://labour.fiatjaf.com/', 'wss://nostr.lu.ke', 'wss://140.f7z.io'];
const CLIENT = 'https://sandwichfarm.github.io/notemine';
const MINER = 'notemine';
let POW_RELAYS = ['wss://nostr.bitcoiner.social', 'wss://nostr.mom', 'wss://nos.lol', 'wss://powrelay.xyz', 'wss://labour.fiatjaf.com/', 'wss://nostr.lu.ke', 'wss://140.f7z.io'];
let MY_RELAYS = []

const Relay = window.NostrTools.Relay;
const SimplePool = window.NostrTools.SimplePool;

const secret = window.NostrTools.generateSecretKey();
const pubkey = window.NostrTools.getPublicKey(secret);
let user = { name: 'anon', photo: './lib/img/anon.svg' };

let isAnon = false
let secret
let pubkey

let k0
let k3
let k10002

const pool = new SimplePool();
let pubs = [];
let usub = null;

let totalWorkers = navigator.hardwareConcurrency-1 || 2; // Or set a fixed number
let workers = [];
let isWorkerReady = 0;
let isMining = false;

const mineButton = document.getElementById('mineButton');
const cancelButton = document.getElementById('cancelButton');
const loginButton = document.getElementById('loginButton');

const eventInput = document.getElementById('eventInput');

const difficultyInput = document.getElementById('difficulty');
const numberOfWorkers = document.getElementById('numberOfWorkers')

const cancelButton = document.getElementById('cancelButton');
const relayStatus = document.getElementById('relayStatus');

const myRelaysContainer = document.getElementById('myRelaysContainer');
const powRelays = document.getElementById('powRelays');
const powRelaysEnable = document.getElementById('powRelaysEnable');
const relaysToggle = document.getElementById('relaysToggle');
const relaysContainer = document.getElementById('relaysContainer');


const minersBestPowOutput = document.getElementById('minersBestPow');
const overallBestPowOutput = document.getElementById('overallBestPow');
const hashrateOutput = document.getElementById('hashrate');

const resultOutput = document.getElementById('result');
const neventOutput = document.getElementById('neventOutput');

const userName = document.getElementById('userName');
const userPhoto = document.getElementById('userPhoto');

numberOfWorkers.value = totalWorkers;
// numberOfWorkers.max = navigator.hardwareConcurrency || 3;
Expand All @@ -41,12 +61,103 @@ overallBestPowOutput.style.display = 'none';
neventOutput.style.display = 'none';
relayStatus.style.display = 'none';

if(window?.nostr !== undefined) {
loginButton.style.display = 'inline-block';
}

authAnon()
refreshUserDom()
refreshRelaysDom()

let workerHashRates = {};
let minersBestPow
let overallBestPow

let halt = false

function activeRelays(){
let relays = MY_RELAYS
if(powRelaysEnable.checked) {
relays = [ ...relays, ...POW_RELAYS ]
}
return relays
}


async function toggleAuth(){
const doLogin = isAnon
if(doLogin) {
await authUser()
loginButton.textContent = 'logout'
}
else { //logout
console.log('logouot')
authAnon()
loginButton.textContent = 'login'
powRelaysEnable.checked = true
}
refreshUserDom()
refreshRelaysDom()

console.log('active relays:', activeRelays())
}

loginButton.addEventListener('click', toggleAuth)

function authAnon(){
isAnon = true
secret = window.NostrTools.generateSecretKey();
pubkey = window.NostrTools.getPublicKey(secret);
MY_RELAYS = []
user.name = 'anon';
user.photo = './lib/img/anon.svg';
}

async function authUser(){
return new Promise( async (resolve, reject) => {
loginButton.disabled = true
const pubkey = await window.nostr.getPublicKey()
const relay = await Relay.connect('wss://purplepag.es')
usub = relay.subscribe(
[{kinds: [0,3,10002], authors: [pubkey]}],
{ onevent, oneose, onclose: onclose(resolve) }
);

})
}

function refreshUserDom (){
console.log(user)
userName.textContent = user.name;
userPhoto.src = user.photo;
}

function refreshRelaysDom(){
console.log('anon?', isAnon)
if(isAnon) {
myRelaysContainer.style.display = 'none';
powRelaysEnable.style.display = 'none';
}
else {
myRelaysContainer.style.display = 'block';
powRelaysEnable.style.display = 'inline';
}
refreshPowRelaysDom()
refreshMyRelaysDom()
}

function refreshMyRelaysDom (){
myRelays.textContent = MY_RELAYS.join(', ');
}

function refreshPowRelaysDom (){
powRelays.textContent = POW_RELAYS.join(', ');
}

function setMyRelays( relays ){
MY_RELAYS = Array.from(new Set([...MY_RELAYS, ...relays]))
}

function resetBestPow() {
minersBestPow = {};
overallBestPow = {
Expand Down Expand Up @@ -169,7 +280,7 @@ function cancelOtherWorkers(excludeWorkerId) {
workers.forEach((worker, index) => {
if (index !== excludeWorkerId) {
worker.postMessage({ type: 'cancel' });
setTimeout( () => worker.terminate(), 1000);
setTimeout( () => worker.terminate(), 1);
}
});
}
Expand Down Expand Up @@ -202,8 +313,91 @@ numberOfWorkers.addEventListener('change', () => {
enableInputs()
})

function onK0(event){
let profile
try {
profile = JSON.parse(event.content)
user.name = profile.name
let photo
if(profile?.photo) photo = profile.photo
else if(profile?.picture) photo = profile.picture
else if(profile?.avatar) photo = profile.avatar
user.photo = photo

}
catch(e){
console.error('Error parsing K0 content:', e)
}
console.log('K0 profile:', profile)
k0 = event
refreshUserDom()
}

function onK3(event){
let relays = []
try{
relays = Object.keys(JSON.parse(event.content))
}
catch(e){
console.error('Error parsing K3 content:', e)
}

console.log('K3 relays:', relays)
setMyRelays(relays)
k3 = event
refreshMyRelaysDom()
}

function onK10002(event){
const relays = event.tags.filter( t => t[0] === 'r' ).map( r => r[1] )
console.log('K10002 relays:', relays)
setMyRelays(relays?.length? relays : [])
refreshMyRelaysDom()
k10002 = event
}


function onevent(event){
switch(event.kind){
case 0: return onK0(event)
case 3: return onK3(event)
case 10002: return onK10002(event)
}
}

function oneose(){
try {
usub.close()
}
catch(e){
console.warn('Error closing subscription:', e)
}
}

function onclose( resolve ){
loginButton.disabled = false
powRelaysEnable.style.display = 'inline';
isAnon = false
resolve()
}

powRelaysEnable.addEventListener('change', () => {
console.log('active relays:', activeRelays())
})

relaysToggle.addEventListener('click', () => {
if(relaysContainer.style.display === 'none'){
relaysContainer.style.display = 'block'
}
else {
relaysContainer.style.display = 'none'
}
})


mineButton.addEventListener('click', () => {
if (isMining) return;
halt = false

resetBestPow();
minersBestPowOutput.style.display = 'block';
Expand Down Expand Up @@ -265,9 +459,9 @@ cancelButton.addEventListener('click', () => {
resultOutput.textContent = 'Mining cancellation requested.';
hashrateOutput.textContent = '0 H/s';
mineButton.disabled = false;
cancelButton.disabled = true; // Disable the cancel button
cancelButton.disabled = true;
isMining = false;
workerHashRates = {}; // Reset hash rates after cancellation
workerHashRates = {};
spawnWorkers()
}
});
Expand Down Expand Up @@ -304,14 +498,14 @@ const generateEvent = (content) => {
return {
pubkey,
kind: 1,
tags: [['t', TOPIC], ['client', CLIENT]],
tags: [['l', MINER, 'miner'],['l', CLIENT, 'client']],
content,
}
}

const generateNEvent = (event) => {
const { id, pubkey: author } = event;
const pointer = { id, pubkey, relays: RELAYS };
const pointer = { id, pubkey, relays: POW_RELAYS };
return window.NostrTools.nip19.neventEncode(pointer);
}

Expand All @@ -324,10 +518,15 @@ const publishEvent = async (ev) => {
}
console.log('Publishing event:', ev);
try {
ev = window.NostrTools.finalizeEvent(ev, secret);
if(isAnon) {
ev = window.NostrTools.finalizeEvent(ev, secret);
}
else {
ev = await window.nostr.signEvent(ev)
}
let isGood = window.NostrTools.verifyEvent(ev);
if (!isGood) throw new Error('Event is not valid');
pubs = pool.publish(RELAYS, ev);
pubs = pool.publish(activeRelays(), ev);
await Promise.allSettled(pubs);
relayStatus.style.display = '';
showRelayStatus();
Expand All @@ -353,7 +552,7 @@ const showRelayStatus = () => {

pubs.forEach((pub, index) => {
pub.finally(() => {
relayStatus.textContent = `Published to all relays [${RELAYS.join(', ')}]`;
relayStatus.textContent = `Published to all relays [${POW_RELAYS.join(', ')}]`;
settled[index] = true;
});
});
Expand Down

0 comments on commit 6ce19aa

Please sign in to comment.