Regexp NIP19 for notes (#153)

This commit is contained in:
KoalaSat 2023-01-23 11:37:33 +00:00 committed by GitHub
commit 98a229a096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View File

@ -177,8 +177,7 @@ const styles = StyleSheet.create({
padding: 16,
},
snackbar: {
margin: 16,
width: '100%',
marginBottom: 85
},
username: {
paddingLeft: 16,

View File

@ -9,7 +9,8 @@ import moment from 'moment'
import { Card, Text, useTheme } from 'react-native-paper'
import { getLinkPreview } from 'link-preview-js'
import { validImageUrl } from '../../Functions/NativeFunctions'
import { getNpub } from '../../lib/nostr/Nip19'
import { getNip19Key, getNpub } from '../../lib/nostr/Nip19'
import { navigate } from '../../lib/Navigation'
interface TextContentProps {
event?: Event
@ -62,6 +63,22 @@ export const TextContent: React.FC<TextContentProps> = ({
Linking.openURL(url)
}
const handleNip05NotePress: (nip19: string) => void = (nip19) => {
const noteId = getNip19Key(nip19)
if (noteId) {
navigate('Note', { noteId })
}
}
const handleNip05ProfilePress: (nip19: string) => void = (nip19) => {
const pubKey = getNip19Key(nip19)
if (pubKey) {
navigate('Profile', { pubKey })
}
}
const handleMentionPress: (text: string) => void = (text) => {
if (!event) return
@ -140,6 +157,8 @@ export const TextContent: React.FC<TextContentProps> = ({
pattern: /#\[(\d+)\]/,
},
{ pattern: /#(\w+)/, style: styles.hashTag },
{ pattern: /(note1)\S*/, style: styles.nip19, onPress: handleNip05NotePress },
{ pattern: /(npub1|nprofile1)\S*/, style: styles.nip19, onPress: handleNip05ProfilePress },
]}
childrenProps={{ allowFontScaling: false }}
>
@ -157,6 +176,9 @@ const styles = StyleSheet.create({
url: {
textDecorationLine: 'underline',
},
nip19: {
textDecorationLine: 'underline',
},
email: {
textDecorationLine: 'underline',
},

View File

@ -13,10 +13,10 @@ export function getNpub(key: string): string {
}
export function getNip19Key(nip19: string): string | null {
let result = nip19
let result = null
try {
const decoded = decode(result)
const decoded = decode(nip19)
if (decoded.type === 'nprofile') {
const data = decoded.data as ProfilePointer
result = data.pubkey
@ -30,7 +30,7 @@ export function getNip19Key(nip19: string): string | null {
console.log('Error decoding getPublicKey', e)
}
return null
return result
}
export function isPrivateKey(nip19: string): boolean {