send/receive messages
This commit is contained in:
parent
9e7f7e584d
commit
e0345be18e
2 changed files with 159 additions and 87 deletions
|
|
@ -95,6 +95,13 @@
|
||||||
label="Email *optional"
|
label="Email *optional"
|
||||||
hint="Merchant may not use email"
|
hint="Merchant may not use email"
|
||||||
></q-input>
|
></q-input>
|
||||||
|
<q-input
|
||||||
|
v-model="checkoutDialog.data.message"
|
||||||
|
filled
|
||||||
|
dense
|
||||||
|
type="text"
|
||||||
|
label="Message *optional"
|
||||||
|
></q-input>
|
||||||
<p>Select the shipping zone:</p>
|
<p>Select the shipping zone:</p>
|
||||||
<div class="row q-mt-lg">
|
<div class="row q-mt-lg">
|
||||||
<q-option-group
|
<q-option-group
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,14 @@ async function customerStall(path) {
|
||||||
},
|
},
|
||||||
cartMenu: [],
|
cartMenu: [],
|
||||||
hasNip07: false,
|
hasNip07: false,
|
||||||
|
customerPubkey: null,
|
||||||
|
customerPrivKey: null,
|
||||||
|
nostrMessages: new Map(),
|
||||||
checkoutDialog: {
|
checkoutDialog: {
|
||||||
show: false,
|
show: false,
|
||||||
data: {}
|
data: {
|
||||||
|
pubkey: null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
qrCodeDialog: {
|
qrCodeDialog: {
|
||||||
data: {
|
data: {
|
||||||
|
|
@ -112,9 +117,18 @@ async function customerStall(path) {
|
||||||
products: new Map()
|
products: new Map()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
resetCheckout() {
|
||||||
|
this.checkoutDialog = {
|
||||||
|
show: false,
|
||||||
|
data: {
|
||||||
|
pubkey: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
async getPubkey() {
|
async getPubkey() {
|
||||||
try {
|
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
|
this.checkoutDialog.data.privkey = null
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(
|
||||||
|
|
@ -125,99 +139,150 @@ async function customerStall(path) {
|
||||||
generateKeyPair() {
|
generateKeyPair() {
|
||||||
let sk = NostrTools.generatePrivateKey()
|
let sk = NostrTools.generatePrivateKey()
|
||||||
let pk = NostrTools.getPublicKey(sk)
|
let pk = NostrTools.getPublicKey(sk)
|
||||||
this.checkoutDialog.data.pubkey = pk
|
this.customerPubkey = pk
|
||||||
this.checkoutDialog.data.privkey = sk
|
this.customerPrivKey = sk
|
||||||
|
this.checkoutDialog.data.pubkey = this.customerPubkey
|
||||||
|
this.checkoutDialog.data.privkey = this.customerPrivKey
|
||||||
},
|
},
|
||||||
async placeOrder() {
|
async placeOrder() {
|
||||||
// LNbits.utils
|
LNbits.utils
|
||||||
// .confirmDialog(
|
.confirmDialog(
|
||||||
// `Send the order to the merchant? You should receive a message with the payment details.`
|
`Send the order to the merchant? You should receive a message with the payment details.`
|
||||||
// )
|
)
|
||||||
// .onOk(async () => {
|
.onOk(async () => {
|
||||||
let orderData = this.checkoutDialog.data
|
let orderData = this.checkoutDialog.data
|
||||||
let orderObj = {
|
let orderObj = {
|
||||||
name: orderData?.username,
|
name: orderData?.username,
|
||||||
description: null,
|
address: orderData.address,
|
||||||
address: orderData.address,
|
message: orderData?.message,
|
||||||
message: null,
|
contact: {
|
||||||
contact: {
|
nostr: this.customerPubkey,
|
||||||
nostr: orderData.pubkey,
|
phone: null,
|
||||||
phone: null,
|
email: orderData?.email
|
||||||
email: orderData?.email
|
},
|
||||||
},
|
items: Array.from(this.cart.products, p => {
|
||||||
items: Array.from(this.cart.products, p => {
|
return {product_id: p[0], quantity: p[1].quantity}
|
||||||
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) {
|
async sendOrder(order) {
|
||||||
for (const url of Array.from(this.relays)) {
|
for (const url of Array.from(this.relays)) {
|
||||||
let relay = NostrTools.relayInit(url)
|
try {
|
||||||
relay.on('connect', () => {
|
let relay = NostrTools.relayInit(url)
|
||||||
console.log(`connected to ${relay.url}`)
|
relay.on('connect', () => {
|
||||||
})
|
console.log(`connected to ${relay.url}`)
|
||||||
relay.on('error', () => {
|
})
|
||||||
console.log(`failed to connect to ${relay.url}`)
|
relay.on('error', () => {
|
||||||
})
|
console.log(`failed to connect to ${relay.url}`)
|
||||||
|
})
|
||||||
|
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
let pub = relay.publish(order)
|
let pub = relay.publish(order)
|
||||||
pub.on('ok', () => {
|
pub.on('ok', () => {
|
||||||
console.log(`${relay.url} has accepted our event`)
|
console.log(`${relay.url} has accepted our event`)
|
||||||
})
|
})
|
||||||
pub.on('failed', reason => {
|
pub.on('failed', reason => {
|
||||||
console.log(`failed to publish to ${relay.url}: ${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() {
|
created() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue