Skip to content

Commit

Permalink
NIP-07 support for getting relays & signing events.
Browse files Browse the repository at this point in the history
Signed-off-by: Yonle <yonle@lecturify.net>
  • Loading branch information
Yonle committed Jun 21, 2023
1 parent bb34146 commit 72ccc93
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h2 style="margin-top: 15vh;">Undelete my Nostr!</h2>
<p>Undelete my Nostr account!</p>

<div>
<input type="password" placeholder="nsec / hex private key" /><br>
<input type="password" placeholder="nsec / hex private key (or use NIP-07)" /><br>
<textarea placeholder="You need a working JavaScript browser enabled"></textarea><br>
<button onclick="scan()">Undelete!</button>
<button onclick="switch_keepProfile()">Keep Profile</button>
Expand All @@ -155,16 +155,34 @@ <h2 style="margin-top: 15vh;">Undelete my Nostr!</h2>
$("pre").innerText += "\n* Keep Profile is ON.";
keepProfile = true;
}

async function signEvent(event, privkey) {
event.created_at = Math.floor(Date.now() / 1000);

if (!privkey && window.nostr && typeof(window.nostr) === "object") {
event = await window.nostr.signEvent(event);
} else {
event.pubkey = window.NostrTools.getPublicKey(privkey);
event.id = window.NostrTools.getEventHash(event);
event.sig = window.NostrTools.getSignature(event, privkey);
}

$("pre").innerText += `\n* Signed ${event.id} with ${event.pubkey}`

return event;
}

async function conn() {
if (!$("textarea").value || !$('input').value) return $("pre").innerText += '\n* Please insert a valid private key & list of nostr relays.';
const privkey = $("input").value.startsWith("nsec") ? window.NostrTools.nip19.decode($('input').value).data : $('input').value;
const pubkey = window.NostrTools.getPublicKey(privkey);
const relays = $("textarea").value.split("\n");
const pool = new window.NostrTools.SimplePool();

if (!window.nostr && (!$("textarea").value || !$('input').value)) return $("pre").innerText += '\n* Please insert a valid private key & list of nostr relays.';
let privkey = $("input").value.startsWith("nsec") ? window.NostrTools.nip19.decode($('input').value).data : $('input').value;
let pubkey = (!privkey && window.nostr) ? await window.nostr.getPublicKey() : window.NostrTools.getPublicKey(privkey);
let relays = $("textarea").value.split("\n").filter(i => i);
let pool = new window.NostrTools.SimplePool();

if (!relays.length && window.nostr) relays = Object.keys(await window.nostr.getRelays());
localStorage.setItem("relays", JSON.stringify(relays));


$("textarea").value = relays.join("\n");
$("pre").innerText += "\n* Pubkey is " + pubkey;
$("pre").innerText += `\n* Connecting to ${relays.length} relays....`
$("button").disabled = true;
Expand All @@ -174,36 +192,29 @@ <h2 style="margin-top: 15vh;">Undelete my Nostr!</h2>
kinds: [0]
}]);

events.forEach(data => {
events.forEach(async data => {
try {
let meta = JSON.parse(data.content);
// $("pre").innerText += '\n* Data: ' + data.content;
if (!meta.deleted) return;
$("pre").innerText += `\n* Found deleted user event: ${data.id}`;

let deletemeta = {
created_at: Math.floor(Date.now() / 1000),
kind: 5,
pubkey,
content: "",
tags: [["e", data.id]]
}

delete meta.deleted;

let newmeta = {
created_at: Math.floor(Date.now() / 1000),
kind: 0,
pubkey,
content: JSON.stringify(meta),
tags: []
}

deletemeta.id = window.NostrTools.getEventHash(deletemeta);
deletemeta.sig = window.NostrTools.getSignature(deletemeta, privkey);

newmeta.id = window.NostrTools.getEventHash(newmeta);
newmeta.sig = window.NostrTools.getSignature(newmeta, privkey);
deletemeta = await signEvent(deletemeta, privkey);
newmeta = await signEvent(newmeta, privkey);

const ok = window.NostrTools.validateEvent(deletemeta);
const veryOk = window.NostrTools.verifySignature(deletemeta);
Expand Down Expand Up @@ -231,9 +242,13 @@ <h2 style="margin-top: 15vh;">Undelete my Nostr!</h2>
newpub.on('failed', data =>
$("pre").innerText += `\n* FIX FAILED: ${data}`
);
} else {
$("pre").innerText += "\n* Signed event message is invalid. Please check the following:"
$("pre").innerText += "\n* " + JSON.parse(deletemeta);
if (!keepProfile) $("pre").innerText += "\n* " + JSON.parse(newmeta);
}
} catch {
$("pre").innerText += `\n* Invalid JSON metadata at ${data.id}`;
} catch (e) {
$("pre").innerText += `\n* ${e.toString()}`;
}
});

Expand All @@ -245,7 +260,7 @@ <h2 style="margin-top: 15vh;">Undelete my Nostr!</h2>
await conn();
$("pre").innerText += '\n* Done.'
} catch (e) {
$("pre").innerText += `\n* Error: ${e.toString}`;
$("pre").innerText += `\n* ${e.toString()}`;
}

$("button").disabled = false;
Expand All @@ -260,11 +275,6 @@ <h2 style="margin-top: 15vh;">Undelete my Nostr!</h2>

if (localStorage.getItem("relays")) {
$("textarea").value = JSON.parse(localStorage.getItem("relays")).join("\n");
} else if (typeof(window.nostr) === "object") {
$("pre").innerText += "\n* NIP-07 detected. Getting relay list...."
window.nostr.getRelays().then(relays => {
$("textarea").value = Object.keys(relays).join("\n");
}).catch(_ => $("pre").innerText += `\n* window.nostr.getRelays() rejected: ${_.toString()}`);
}
}
</script>
Expand Down

0 comments on commit 72ccc93

Please sign in to comment.