web: nip05 profile loading

This commit is contained in:
William Casarin 2022-11-18 11:02:53 -08:00
parent 9230a11a72
commit 72346522eb
2 changed files with 33 additions and 10 deletions

View file

@ -12,7 +12,7 @@
<script defer src="js/noble-secp256k1.js?v=1"></script> <script defer src="js/noble-secp256k1.js?v=1"></script>
<script defer src="js/bech32.js?v=1"></script> <script defer src="js/bech32.js?v=1"></script>
<script defer src="js/nostr.js?v=6"></script> <script defer src="js/nostr.js?v=6"></script>
<script defer src="js/damus.js?v=89"></script> <script defer src="js/damus.js?v=90"></script>
</head> </head>
<body> <body>
<script> <script>

View file

@ -92,7 +92,7 @@ function update_favicon(path)
// number of notifications that are unseen by the user. // number of notifications that are unseen by the user.
function update_title(model) { function update_title(model) {
// TODO rename update_title to update_notification_state or similar // TODO rename update_title to update_notification_state or similar
// TODO only clear notifications once they have seen all targeted events // TODO only clear notifications once they have seen all targeted events
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
model.notifications = 0 model.notifications = 0
} }
@ -640,6 +640,10 @@ function load_our_relays(our_pubkey, pool, ev) {
} }
} }
function log_error(fmt, ...args) {
console.log("[ERROR] " + fmt, ...args)
}
function log_debug(fmt, ...args) { function log_debug(fmt, ...args) {
console.log("[debug] " + fmt, ...args) console.log("[debug] " + fmt, ...args)
} }
@ -708,9 +712,9 @@ function leading_zero_bits(id)
return total return total
} }
function min(a, b) { function min(a, b) {
return a < b ? a : b; return a < b ? a : b;
} }
function difficulty_to_prefix(d) function difficulty_to_prefix(d)
{ {
@ -1212,10 +1216,29 @@ function get_qs(loc=location.href) {
return new URL(loc).searchParams return new URL(loc).searchParams
} }
function handle_pubkey(pubkey) { async function get_nip05_pubkey(email) {
const [user, host] = email.split("@")
const url = `https://${host}/.well-known/nostr.json?name=${user}`
try {
const res = await fetch(url)
const json = await res.json()
log_debug("nip05 data", json)
return json.names[user]
} catch (e) {
log_error("fetching nip05 entry for %s", email, e)
throw e
}
}
async function handle_pubkey(pubkey) {
if (pubkey[0] === "n") if (pubkey[0] === "n")
pubkey = bech32_decode(pubkey) pubkey = bech32_decode(pubkey)
if (pubkey.includes("@"))
pubkey = await get_nip05_pubkey(pubkey)
set_local_state('pubkey', pubkey) set_local_state('pubkey', pubkey)
return pubkey return pubkey
@ -1227,7 +1250,7 @@ async function get_pubkey() {
// qs pk overrides stored key // qs pk overrides stored key
const qs_pk = get_qs().get("pk") const qs_pk = get_qs().get("pk")
if (qs_pk) if (qs_pk)
return handle_pubkey(qs_pk) return await handle_pubkey(qs_pk)
if (pubkey) if (pubkey)
return pubkey return pubkey
@ -1237,15 +1260,15 @@ async function get_pubkey() {
console.log("calling window.nostr.getPublicKey()...") console.log("calling window.nostr.getPublicKey()...")
const pubkey = await window.nostr.getPublicKey() const pubkey = await window.nostr.getPublicKey()
console.log("got %s pubkey from nos2x", pubkey) console.log("got %s pubkey from nos2x", pubkey)
return handle_pubkey(pubkey) return await handle_pubkey(pubkey)
} }
pubkey = prompt("Enter pubkey (hex or npub)") pubkey = prompt("Enter nostr id (eg: jb55@jb55.com) or pubkey (hex or npub)")
if (!pubkey) if (!pubkey)
throw new Error("Need pubkey to continue") throw new Error("Need pubkey to continue")
return handle_pubkey(pubkey) return await handle_pubkey(pubkey)
} }
function get_privkey() { function get_privkey() {
@ -1304,7 +1327,7 @@ function is_video_url(path) {
const URL_REGEX = /(^|\s)(https?:\/\/[^\s]+)[,:)]?(\w|$)/g; const URL_REGEX = /(^|\s)(https?:\/\/[^\s]+)[,:)]?(\w|$)/g;
function linkify(text, show_media) { function linkify(text, show_media) {
return text.replace(URL_REGEX, function(match, p1, p2, p3) { return text.replace(URL_REGEX, function(match, p1, p2, p3) {
const url = p2+p3 const url = p2+p3
const parsed = new URL(url) const parsed = new URL(url)
let html; let html;
if (show_media && is_img_url(parsed.pathname)) { if (show_media && is_img_url(parsed.pathname)) {