send/receive messages

This commit is contained in:
Tiago Vasconcelos 2023-03-06 12:03:32 +00:00
parent 9e7f7e584d
commit e0345be18e
2 changed files with 159 additions and 87 deletions

View file

@ -95,6 +95,13 @@
label="Email *optional"
hint="Merchant may not use email"
></q-input>
<q-input
v-model="checkoutDialog.data.message"
filled
dense
type="text"
label="Message *optional"
></q-input>
<p>Select the shipping zone:</p>
<div class="row q-mt-lg">
<q-option-group

View file

@ -22,9 +22,14 @@ async function customerStall(path) {
},
cartMenu: [],
hasNip07: false,
customerPubkey: null,
customerPrivKey: null,
nostrMessages: new Map(),
checkoutDialog: {
show: false,
data: {}
data: {
pubkey: null
}
},
qrCodeDialog: {
data: {
@ -112,9 +117,18 @@ async function customerStall(path) {
products: new Map()
}
},
resetCheckout() {
this.checkoutDialog = {
show: false,
data: {
pubkey: null
}
}
},
async getPubkey() {
try {
this.checkoutDialog.data.pubkey = await window.nostr.getPublicKey()
this.customerPubkey = await window.nostr.getPublicKey()
this.checkoutDialog.data.pubkey = this.customerPubkey
this.checkoutDialog.data.privkey = null
} catch (err) {
console.error(
@ -125,99 +139,150 @@ async function customerStall(path) {
generateKeyPair() {
let sk = NostrTools.generatePrivateKey()
let pk = NostrTools.getPublicKey(sk)
this.checkoutDialog.data.pubkey = pk
this.checkoutDialog.data.privkey = sk
this.customerPubkey = pk
this.customerPrivKey = sk
this.checkoutDialog.data.pubkey = this.customerPubkey
this.checkoutDialog.data.privkey = this.customerPrivKey
},
async placeOrder() {
// 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,
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() {