feat: optimize QR code value for bach32 (#3900)
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -165,5 +165,26 @@ window.app.component('lnbits-qrcode', {
|
|||||||
this.$refs.qrCode.$el.style.maxWidth = this.maxWidth + 'px'
|
this.$refs.qrCode.$el.style.maxWidth = this.maxWidth + 'px'
|
||||||
this.$refs.qrCode.$el.setAttribute('width', '100%')
|
this.$refs.qrCode.$el.setAttribute('width', '100%')
|
||||||
this.$refs.qrCode.$el.removeAttribute('height')
|
this.$refs.qrCode.$el.removeAttribute('height')
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
optimizedValue() {
|
||||||
|
const separatorIndex = this.value.indexOf(':')
|
||||||
|
const type =
|
||||||
|
separatorIndex === -1 ? '' : this.value.substring(0, separatorIndex)
|
||||||
|
const value =
|
||||||
|
separatorIndex === -1
|
||||||
|
? this.value
|
||||||
|
: this.value.substring(separatorIndex + 1)
|
||||||
|
|
||||||
|
if (this.utils.isValidBech32(value)) {
|
||||||
|
const normalizedValue = value.toUpperCase()
|
||||||
|
if (type) {
|
||||||
|
return `${type.toUpperCase()}:${normalizedValue}`
|
||||||
|
}
|
||||||
|
return normalizedValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -160,6 +160,46 @@ window._lnbitsUtils = {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
isValidBech32(value) {
|
||||||
|
if (typeof value !== 'string') {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const candidate = value.trim()
|
||||||
|
if (
|
||||||
|
!candidate ||
|
||||||
|
(candidate !== candidate.toLowerCase() &&
|
||||||
|
candidate !== candidate.toUpperCase())
|
||||||
|
) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = candidate.toLowerCase()
|
||||||
|
const splitPosition = normalized.lastIndexOf('1')
|
||||||
|
if (splitPosition <= 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const humanReadablePart = normalized.substring(0, splitPosition)
|
||||||
|
const data = normalized.substring(splitPosition + 1)
|
||||||
|
if (data.length < 6) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof bech32ToFiveBitArray !== 'function' ||
|
||||||
|
typeof verify_checksum !== 'function'
|
||||||
|
) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const words = bech32ToFiveBitArray(data)
|
||||||
|
if (words.some(word => word < 0)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return verify_checksum(humanReadablePart, words)
|
||||||
|
},
|
||||||
async notifyApiError(error) {
|
async notifyApiError(error) {
|
||||||
if (!error.response) {
|
if (!error.response) {
|
||||||
return console.error(error)
|
return console.error(error)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
>
|
>
|
||||||
<qrcode-vue
|
<qrcode-vue
|
||||||
ref="qrCode"
|
ref="qrCode"
|
||||||
:value="value"
|
:value="optimizedValue"
|
||||||
:margin="margin"
|
:margin="margin"
|
||||||
:size="size"
|
:size="size"
|
||||||
level="Q"
|
level="Q"
|
||||||
|
|||||||
Reference in New Issue
Block a user