implement v4 decode

This commit is contained in:
KraXen72
2024-07-19 17:09:29 +02:00
parent 29316c3b1b
commit f1e2eb3692
2 changed files with 14 additions and 11 deletions
+1 -1
View File
@@ -181,7 +181,7 @@ export const ScanScreen: FC<WalletStackScreenProps<'Scan'>> = function ScanScree
default:
try {
// generic scan button on wallet screen
const incomingData = IncomingParser.findAndExtract(incoming)
const incomingData = IncomingParser.findAndExtract(incoming)
return IncomingParser.navigateWithIncomingData(incomingData, navigation, unit)
} catch (e: any) {
e.name = Err.VALIDATION_ERROR
+13 -10
View File
@@ -10,30 +10,33 @@ import {TokenEntry} from '../../models/TokenEntry'
import {Proof} from '../../models/Proof'
import { log } from '../logService'
const findEncodedCashuToken = function (content: string) {
const words = content.split(/\s+|\n+/) // Split text into words
const maybeToken = words.find(word => word.includes("cashuA"))
return maybeToken || null
const words = content.split(/\s+|\n+/) // Split text into words
const maybeToken = words.find(word => CASHU_TOKEN_PREFIXES.some(pref => word.includes(pref)))
return maybeToken || null
}
const cashuUriPrefixes = [
const CASHU_URI_PREFIXES = [
'https://wallet.nutstash.app/#',
'https://wallet.cashu.me/?token=',
'web+cashu://',
'cashu://',
'cashu:'
]
const CASHU_TOKEN_PREFIXES = [
'cashuA',
'cashuB'
]
export function validCashuAToken(text: string) {
for (const prefix of cashuUriPrefixes) {
for (const prefix of CASHU_URI_PREFIXES) {
if (text && text.startsWith(prefix)) {
text = text.slice(prefix.length)
break // necessary
}
}
return text && text.startsWith('cashuA')
return text && CASHU_TOKEN_PREFIXES.some(pref => text.startsWith(pref))
}
const extractEncodedCashuToken = function (maybeToken: string): string {
@@ -43,12 +46,12 @@ const extractEncodedCashuToken = function (maybeToken: string): string {
let encodedToken: string | undefined = undefined
let decoded: Token | undefined = undefined
if (maybeToken && maybeToken.startsWith('cashuA')) {
if (maybeToken && CASHU_TOKEN_PREFIXES.some(pref => maybeToken.startsWith(pref))) {
decoded = decodeToken(maybeToken) // throws
return maybeToken
}
for (const prefix of cashuUriPrefixes) {
for (const prefix of CASHU_URI_PREFIXES) {
if (maybeToken && maybeToken.startsWith(prefix)) {
encodedToken = maybeToken.slice(prefix.length)
break // necessary