damus.io

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

commit f5ba022bbad6a49c7f9d074371eaa0a1d6dcd6a7
parent 758c7ee6dff12cbc90354883edfccaa6c7e81354
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 28 Oct 2022 11:04:34 -0700

nos2x support

your pubkey and privkey will be loaded from nos2x if you have that
configured. Test with alby and the nos2x extension.

Diffstat:
Mwebv2/damus.js | 62+++++++++++++++++++++++++++++++++++++++++++++-----------------
Mwebv2/index.html | 7++++---
Mwebv2/nostr.js | 8+++++++-
3 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/webv2/damus.js b/webv2/damus.js @@ -46,13 +46,22 @@ function init_home_model() { } } +const BOOTSTRAP_RELAYS = [ + "wss://relay.damus.io", + "wss://nostr-relay.wlvs.space", + "wss://nostr-pub.wellorder.net" +] + async function damus_web_init() { - const {RelayPool} = nostrjs - const pool = RelayPool(["wss://relay.damus.io"]) - const now = (new Date().getTime()) / 1000 const model = init_home_model() DSTATE = model + model.pubkey = await get_pubkey() + if (!model.pubkey) + return + const {RelayPool} = nostrjs + const pool = RelayPool(BOOTSTRAP_RELAYS) + const now = (new Date().getTime()) / 1000 const ids = { comments: "comments",//uuidv4(), @@ -64,9 +73,6 @@ async function damus_web_init() dms: "dms",//uuidv4(), } - model.pubkey = get_pubkey() - if (!model.pubkey) - return model.pool = pool model.view_el = document.querySelector("#view") redraw_home_view(model) @@ -78,6 +84,7 @@ async function damus_web_init() if (!model.done_init) { model.loading = false + send_initial_filters(ids.account, model.pubkey, relay) } else { send_home_filters(ids, model, relay) @@ -148,6 +155,7 @@ function handle_profile_event(model, ev) { function send_initial_filters(account_id, pubkey, relay) { const filter = {authors: [pubkey], kinds: [3], limit: 1} + console.log("sending initial filter", filter) relay.subscribe(account_id, filter) } @@ -350,20 +358,34 @@ async function send_post() { const created_at = Math.floor(new Date().getTime() / 1000) const kind = 1 const tags = [] - const pubkey = get_pubkey() - const privkey = get_privkey() + const pubkey = await get_pubkey() const {pool} = DSTATE let post = { pubkey, tags, content, created_at, kind } post.id = await nostrjs.calculate_id(post) - post.sig = await sign_id(privkey, post.id) + post = await sign_event(post) pool.send(["EVENT", post]) input_el.value = "" } +async function sign_event(ev) { + if (window.nostr && window.nostr.signEvent) { + const signed = await window.nostr.signEvent(ev) + if (typeof signed === 'string') { + ev.sig = signed + return ev + } + return signed + } + + const privkey = get_privkey() + ev.sig = await sign_id(privkey, ev.id) + return ev +} + function render_home_view(model) { return ` <div id="newpost"> @@ -498,7 +520,7 @@ function gather_reply_tags(pubkey, from) { return tags } -async function create_reply(privkey, pubkey, content, from) { +async function create_reply(pubkey, content, from) { const tags = gather_reply_tags(pubkey, from) const created_at = Math.floor(new Date().getTime() / 1000) const kind = from.kind @@ -506,8 +528,7 @@ async function create_reply(privkey, pubkey, content, from) { let reply = { pubkey, tags, content, created_at, kind } reply.id = await nostrjs.calculate_id(reply) - reply.sig = await sign_id(privkey, reply.id) - + reply = await sign_event(reply) return reply } @@ -518,12 +539,13 @@ async function send_reply() { const ev = DSTATE.all_events[evid] const { pool } = DSTATE - const content = document.querySelector("#reply-content").value - const pubkey = get_pubkey() - const privkey = get_privkey() + const reply_content_el = document.querySelector("#reply-content") + const content = reply_content_el.value + const pubkey = await get_pubkey() - let reply = await create_reply(privkey, pubkey, content, ev) + let reply = await create_reply(pubkey, content, ev) pool.send(["EVENT", reply]) + reply_content_el.value = "" close_reply() } @@ -546,12 +568,18 @@ function set_local_state(key, val) { localStorage.setItem(key, val) } -function get_pubkey() { +async function get_pubkey() { let pubkey = get_local_state('pubkey') if (pubkey) return pubkey + if (window.nostr && window.nostr.getPublicKey) { + const pubkey = await window.nostr.getPublicKey() + console.log("got %s pubkey from nos2x", pubkey) + return pubkey + } + pubkey = prompt("Enter pubkey (hex or npub)") if (!pubkey) diff --git a/webv2/index.html b/webv2/index.html @@ -40,10 +40,11 @@ </div> <script src="noble-secp256k1.js?v=1"></script> <script src="bech32.js?v=1"></script> - <script src="nostr.js?v=3"></script> - <script src="damus.js?v=12"></script> + <script src="nostr.js?v=5"></script> + <script src="damus.js?v=27"></script> <script> - const relay = damus_web_init() + // I have to delay loading to wait for nos2x + const relay = setTimeout(damus_web_init, 100) </script> </body> </html> diff --git a/webv2/nostr.js b/webv2/nostr.js @@ -153,7 +153,11 @@ function Relay(relay, opts={}) const me = this me.onfn = {} - init_websocket(me) + try { + init_websocket(me) + } catch (e) { + console.log(e) + } return this } @@ -187,6 +191,8 @@ function init_websocket(me) { ws.onopen = () => { if (me.onfn.open) me.onfn.open() + else + console.log("no onopen???", me) if (resolved) return