diff --git a/static/components/market-config/market-config.js b/static/components/market-config/market-config.js index 7a5d2fc..2a99614 100644 --- a/static/components/market-config/market-config.js +++ b/static/components/market-config/market-config.js @@ -14,12 +14,20 @@ async function marketConfig(path) { } }, methods: { - addMerchant: async function() { + addMerchant: async function () { console.log('### market config', this.merchants) - this.$emit('add-merchant', this.inputPubkey) + if (!isValidKey(this.inputPubkey, 'npub')) { + this.$q.notify({ + message: 'Invalid Public Key!', + type: 'warning' + }) + return + } + const publicKey = isValidKeyHex(this.inputPubkey) ? this.inputPubkey : NostrTools.nip19.decode(this.inputPubkey).data + this.$emit('add-merchant', publicKey) this.inputPubkey = null }, - removeMerchant: async function(publicKey) { + removeMerchant: async function (publicKey) { this.$emit('remove-merchant', publicKey) }, // async addPubkey(pubkey) { diff --git a/static/js/market.js b/static/js/market.js index a4996ce..5abe9c0 100644 --- a/static/js/market.js +++ b/static/js/market.js @@ -115,19 +115,16 @@ const market = async () => { hasExtension() { return window.nostr }, - isValidKey() { - let key = this.accountDialog.data.key - if (key && key.startsWith('n')) { - let { type, data } = NostrTools.nip19.decode(key) - key = data - } - return key?.toLowerCase()?.match(/^[0-9a-f]{64}$/) + isValidAccountKey() { + return isValidKey(this.accountDialog.data.key) }, canEditConfig() { return this.account && this.account.pubkey == this.config?.pubkey } }, async created() { + this.merchants = this.$q.localStorage.getItem('nostrmarket.merchants') || [] + // Check for user stored this.account = this.$q.localStorage.getItem('diagonAlley.account') || null //console.log("UUID", crypto.randomUUID()) @@ -340,7 +337,6 @@ const market = async () => { this.accountMetadata = this.profiles.get(this.account.pubkey) } this.merchants.filter(m => m.publicKey === e.pubkey).forEach(m => m.profile = e.content) - this.merchants = this.merchants.concat([]) console.log('### this.merchants', this.merchants) return } else if (e.kind == 30018) { @@ -538,17 +534,22 @@ const market = async () => { this.$q.localStorage.set(`diagonAlley.relays`, Array.from(this.relays)) this.initNostr() }, + + addMerchant(publicKey){ console.log('### addMerchat', publicKey) + this.merchants.unshift({ publicKey, profile: null }) + this.$q.localStorage.set('nostrmarket.merchants', this.merchants) this.initNostr() // todo: improve }, removeMerchant(publicKey){ console.log('### removeMerchat', publicKey) this.merchants = this.merchants.filter(m => m.publicKey !== publicKey) + this.$q.localStorage.set('nostrmarket.merchants', this.merchants) this.initNostr() // todo: improve } } diff --git a/static/js/utils.js b/static/js/utils.js index 5158371..7cd1392 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -23,7 +23,7 @@ function imgSizeFit(img, maxWidth = 1024, maxHeight = 768) { maxWidth / img.naturalWidth, maxHeight / img.naturalHeight ) - return {width: img.naturalWidth * ratio, height: img.naturalHeight * ratio} + return { width: img.naturalWidth * ratio, height: img.naturalHeight * ratio } } async function hash(string) { @@ -52,8 +52,8 @@ function satOrBtc(val, showUnit = true, showSats = false) { const value = showSats ? LNbits.utils.formatSat(val) : val == 0 - ? 0.0 - : (val / 100000000).toFixed(8) + ? 0.0 + : (val / 100000000).toFixed(8) if (!showUnit) return value return showSats ? value + ' sat' : value + ' BTC' } @@ -121,3 +121,19 @@ function isValidImageUrl(string) { } return url.protocol === 'http:' || url.protocol === 'https:' } + +function isValidKey(key, prefix = 'n') { + try { + if (key && key.startsWith(prefix)) { + let { _, data } = NostrTools.nip19.decode(key) + key = data + } + return isValidKeyHex(key) + } catch (error) { + return false + } +} + +function isValidKeyHex(key) { + return key?.toLowerCase()?.match(/^[0-9a-f]{64}$/) +} \ No newline at end of file diff --git a/templates/nostrmarket/market.html b/templates/nostrmarket/market.html index d3a9c6d..e5d6442 100644 --- a/templates/nostrmarket/market.html +++ b/templates/nostrmarket/market.html @@ -231,7 +231,7 @@ @@ -253,7 +253,7 @@ - +