feat: validate merchant pubkeys

This commit is contained in:
Vlad Stan 2023-07-06 12:41:40 +03:00
parent 8061894057
commit 99a98f0df9
4 changed files with 41 additions and 16 deletions

View file

@ -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) {

View file

@ -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
}
}

View file

@ -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}$/)
}

View file

@ -231,7 +231,7 @@
<q-card-section class="q-pt-none">
<q-input dense label="nsec/npub/hex" v-model="accountDialog.data.key" autofocus @keyup.enter="createAccount"
:error="accountDialog.data.key && !isValidKey"
:error="accountDialog.data.key && !isValidAccountKey"
hint="Enter you private key (recommended) or public key"></q-input>
<q-item tag="label">
@ -253,7 +253,7 @@
<q-card-actions align="right" class="text-primary">
<q-btn v-if="hasExtension" flat label="Use Public Key from Extension"
@click="() => createAccount(true)"></q-btn>
<q-btn v-if="isValidKey" label="Add key" color="primary" @click="() => createAccount()"></q-btn>
<q-btn v-if="isValidAccountKey" label="Add key" color="primary" @click="() => createAccount()"></q-btn>
<q-btn v-else flat label="Generate" @click="generateKeyPair"></q-btn>
</q-card-actions>
</q-card>