damus.io

damus.io website
git clone git://jb55.com/damus.io
Log | Files | Refs | README | LICENSE

key.js (948B)


      1 
      2 function hex_char(val)
      3 {
      4 	if (val < 10)
      5 		return String.fromCharCode(48 + val)
      6 	if (val < 16)
      7 		return String.fromCharCode(97 + val - 10)
      8 }
      9 
     10 function hex_encode(buf)
     11 {
     12 	str = ""
     13 	for (let i = 0; i < buf.length; i++) {
     14 		const c = buf[i]
     15 		str += hex_char(c >> 4)
     16 		str += hex_char(c & 0xF)
     17 	}
     18 	return str
     19 }
     20 
     21 function go() {
     22 	const el = document.querySelector("#damus-key")
     23 	const hex_el = document.querySelector("#hex-key")
     24 	const note_link_el = document.querySelector("#note-link")
     25 	const profile_link_el = document.querySelector("#profile-link")
     26 
     27 	el.addEventListener("input", () => {
     28 		const decoded = bech32.decode(el.value)
     29 		const bytes = fromWords(decoded.words)
     30 		hex_el.value = hex_encode(bytes)
     31 		update_note_link(hex_el.value)
     32 	});
     33 
     34 	hex_el.addEventListener("input", () => {
     35 		update_note_link(hex_el.value)
     36 	})
     37 
     38 	function update_note_link(id) {
     39 		note_link_el.href = `nostr:e:${id}`
     40 		profile_link_el.href = `nostr:p:${id}`
     41 	}
     42 }
     43 
     44 go()