From e0345be18ec725de90f134fa245ce7bf6b10ddc3 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Mon, 6 Mar 2023 12:03:32 +0000 Subject: [PATCH] send/receive messages --- .../customer-stall/customer-stall.html | 7 + .../customer-stall/customer-stall.js | 239 +++++++++++------- 2 files changed, 159 insertions(+), 87 deletions(-) diff --git a/static/components/customer-stall/customer-stall.html b/static/components/customer-stall/customer-stall.html index aedec55..c6ac229 100644 --- a/static/components/customer-stall/customer-stall.html +++ b/static/components/customer-stall/customer-stall.html @@ -95,6 +95,13 @@ label="Email *optional" hint="Merchant may not use email" > +

Select the shipping zone:

{ - let orderData = this.checkoutDialog.data - let orderObj = { - name: orderData?.username, - description: null, - address: orderData.address, - message: null, - contact: { - nostr: orderData.pubkey, - phone: null, - email: orderData?.email - }, - items: Array.from(this.cart.products, p => { - return {product_id: p[0], quantity: p[1].quantity} + LNbits.utils + .confirmDialog( + `Send the order to the merchant? You should receive a message with the payment details.` + ) + .onOk(async () => { + let orderData = this.checkoutDialog.data + let orderObj = { + name: orderData?.username, + address: orderData.address, + message: orderData?.message, + contact: { + nostr: this.customerPubkey, + phone: null, + email: orderData?.email + }, + items: Array.from(this.cart.products, p => { + return {product_id: p[0], quantity: p[1].quantity} + }) + } + let created_at = Math.floor(Date.now() / 1000) + orderObj.id = await hash( + [this.customerPubkey, created_at, JSON.stringify(orderObj)].join( + ':' + ) + ) + let event = { + ...(await NostrTools.getBlankEvent()), + kind: 4, + created_at, + tags: [['p', this.stall.pubkey]], + pubkey: this.customerPubkey + } + if (this.customerPrivKey) { + event.content = await NostrTools.nip04.encrypt( + this.customerPrivKey, + this.stall.pubkey, + JSON.stringify(orderObj) + ) + } else { + event.content = await window.nostr.nip04.encrypt( + this.stall.pubkey, + JSON.stringify(orderObj) + ) + let userRelays = Object.keys( + (await window.nostr?.getRelays?.()) || [] + ) + if (userRelays.length != 0) { + userRelays.map(r => this.relays.add(r)) + } + } + event.id = NostrTools.getEventHash(event) + if (this.customerPrivKey) { + event.sig = await NostrTools.signEvent( + event, + this.customerPrivKey + ) + } else if (this.hasNip07) { + event = await window.nostr.signEvent(event) + } + console.log(event, orderObj) + await this.sendOrder(event) }) - } - let event = { - ...(await NostrTools.getBlankEvent()), - kind: 4, - created_at: Math.floor(Date.now() / 1000), - tags: [['p', this.stall.pubkey]], - pubkey: orderData.pubkey - } - if (orderData.privkey) { - event.content = await NostrTools.nip04.encrypt( - orderData.privkey, - this.stall.pubkey, - JSON.stringify(orderObj) - ) - } else { - console.log('use extension') - event.content = await window.nostr.nip04.encrypt( - orderData.pubkey, - JSON.stringify(orderObj) - ) - let userRelays = Object.keys( - (await window.nostr?.getRelays?.()) || [] - ) - if (userRelays.length != 0) { - userRelays.map(r => this.relays.add(r)) - } - } - event.id = NostrTools.getEventHash(event) - if (orderData.privkey) { - event.sig = await NostrTools.signEvent(event, orderData.privkey) - } else if (this.hasNip07) { - event = await window.nostr.signEvent(event) - } - console.log(event, orderData) - await this.sendOrder(event) - // }) }, async sendOrder(order) { for (const url of Array.from(this.relays)) { - let relay = NostrTools.relayInit(url) - relay.on('connect', () => { - console.log(`connected to ${relay.url}`) - }) - relay.on('error', () => { - console.log(`failed to connect to ${relay.url}`) - }) + try { + let relay = NostrTools.relayInit(url) + relay.on('connect', () => { + console.log(`connected to ${relay.url}`) + }) + relay.on('error', () => { + console.log(`failed to connect to ${relay.url}`) + }) - await relay.connect() - let pub = relay.publish(order) - pub.on('ok', () => { - console.log(`${relay.url} has accepted our event`) - }) - pub.on('failed', reason => { - console.log(`failed to publish to ${relay.url}: ${reason}`) - }) + await relay.connect() + let pub = relay.publish(order) + pub.on('ok', () => { + console.log(`${relay.url} has accepted our event`) + }) + pub.on('failed', reason => { + console.log(`failed to publish to ${relay.url}: ${reason}`) + }) + } catch (err) { + console.error(`Error: ${err}`) + } + } + this.resetCheckout() + this.listenMessages() + }, + async listenMessages() { + try { + const pool = new NostrTools.SimplePool() + const filters = [ + { + kinds: [4], + authors: [this.customerPubkey] + }, + { + kinds: [4], + '#p': [this.customerPubkey] + } + ] + let relays = Array.from(this.relays) + let subs = pool.sub(relays, filters) + subs.on('event', async event => { + let mine = event.pubkey == this.customerPubkey + let sender = mine + ? event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1] + : event.pubkey + if ( + (mine && sender != this.stall.pubkey) || + (!mine && sender != this.customerPubkey) + ) { + console.log(`Not relevant message!`) + return + } + try { + let plaintext = this.customerPrivKey + ? await NostrTools.nip04.decrypt( + this.customerPrivKey, + sender, + event.content + ) + : await window.nostr.nip04.decrypt(sender, event.content) + // console.log(`${mine ? 'Me' : 'Customer'}: ${plaintext}`) + this.nostrMessages.set(event.id, { + msg: plaintext, + timestamp: event.created_at, + sender: `${mine ? 'Me' : 'Merchant'}` + }) + } catch { + console.error('Unable to decrypt message!') + return + } + }) + } catch (err) { + console.error(`Error: ${err}`) } - this.checkoutDialog = {show: false, data: {}} - // const pool = new NostrTools.SimplePool() - // let relays = Array.from(this.relays) - // try { - // let pubs = await pool.publish(relays, order) - // pubs.on('ok', relay => { - // console.log(`${relay.url} has accepted our event`) - // }) - // pubs.on('failed', (reason, err) => { - // console.log(`failed to publish to ${reason}: ${err}`) - // }) - // } catch (err) { - // console.error(err) - // } } }, created() {