From 46a9f9c01ee15a77352a2981ca52a24e8b4324ed Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Tue, 7 Mar 2023 13:25:16 +0000 Subject: [PATCH] login/account functionality --- static/js/market.js | 70 ++++++++++++++++- templates/nostrmarket/market.html | 120 ++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 3 deletions(-) diff --git a/static/js/market.js b/static/js/market.js index a7c724e..88042ce 100644 --- a/static/js/market.js +++ b/static/js/market.js @@ -2,6 +2,7 @@ const market = async () => { Vue.component(VueQrcode.name, VueQrcode) const NostrTools = window.NostrTools + const defaultRelays = [ 'wss://relay.damus.io', 'wss://relay.snort.social', @@ -23,7 +24,8 @@ const market = async () => { customerMarket('static/components/customer-market/customer-market.html'), customerStall('static/components/customer-stall/customer-stall.html'), productDetail('static/components/product-detail/product-detail.html'), - shoppingCart('static/components/shopping-cart/shopping-cart.html') + shoppingCart('static/components/shopping-cart/shopping-cart.html'), + chatDialog('static/components/chat-dialog/chat-dialog.html') ]) new Vue({ @@ -31,7 +33,15 @@ const market = async () => { mixins: [windowMixin], data: function () { return { - drawer: false, + account: null, + accountDialog: { + show: false, + data: { + watchOnly: false, + key: null + } + }, + drawer: true, pubkeys: new Set(), relays: new Set(), events: [], @@ -72,9 +82,20 @@ const market = async () => { }, isLoading() { return this.$q.loading.isActive + }, + hasExtension() { + return window.nostr + }, + isValidKey() { + return this.accountDialog.data.key + ?.toLowerCase() + ?.match(/^[0-9a-f]{64}$/) } }, async created() { + // Check for user stored + this.account = this.$q.localStorage.getItem('diagonAlley.account') || null + // Check for stored merchants and relays on localStorage try { let merchants = this.$q.localStorage.getItem(`diagonAlley.merchants`) @@ -115,7 +136,10 @@ const market = async () => { } // Get notes from Nostr - await this.initNostr() + //await this.initNostr() + + // Get fiat rates (i think there's an LNbits endpoint for this) + //await this.getRates() this.$q.loading.hide() }, methods: { @@ -129,6 +153,46 @@ const market = async () => { }) console.log(naddr) }, + async deleteAccount() { + await LNbits.utils + .confirmDialog( + `This will delete all stored data. If you didn't backup the Key Pair (Private and Public Keys), you will lose it. Continue?` + ) + .onOk(() => { + window.localStorage.removeItem('diagonAlley.account') + this.account = null + }) + }, + async createAccount(useExtension = false) { + let nip07 + if (useExtension) { + await this.getFromExtension() + nip07 = true + } + if (this.isValidKey) { + let {key, watchOnly} = this.accountDialog.data + this.$q.localStorage.set('diagonAlley.account', { + privkey: watchOnly ? null : key, + pubkey: watchOnly ? key : NostrTools.getPublicKey(key), + useExtension: nip07 ?? false + }) + this.accountDialog.data = { + watchOnly: false, + key: null + } + this.accountDialog.show = false + this.account = this.$q.localStorage.getItem('diagonAlley.account') + } + }, + generateKeyPair() { + this.accountDialog.data.key = NostrTools.generatePrivateKey() + this.accountDialog.data.watchOnly = false + }, + async getFromExtension() { + this.accountDialog.data.key = await window.nostr.getPublicKey() + this.accountDialog.data.watchOnly = true + return + }, async initNostr() { this.$q.loading.show() const pool = new NostrTools.SimplePool() diff --git a/templates/nostrmarket/market.html b/templates/nostrmarket/market.html index 5f01c41..77c0cac 100644 --- a/templates/nostrmarket/market.html +++ b/templates/nostrmarket/market.html @@ -5,6 +5,32 @@ Settings +
+
+ + Delete account data +
+
+ Login or Create account +
+ +
+ + + + +
Account Setup
+ + +
+ +

Type your Nostr private key or generate a new one.

+ You can also use a Nostr-capable extension. +
+ + + + + + + + + + Is this a Public Key? + + If not using an Nostr capable extension, you'll have to sign + events manually! Better to use a Private Key that you can delete + later, or just generate an ephemeral key pair to use in the + Marketplace! + + + + + + + + + + +
+
{% endblock %} {% block scripts %} @@ -165,6 +247,44 @@ + + {% endblock %}