From 8b28b9dae0ea80a8af163738e73b0260e9db0cde Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Mon, 3 Apr 2023 18:56:22 +0200 Subject: [PATCH] Include Java nostr --- .github/workflows/android-build.yml | 2 +- android/app/build.gradle | 2 ++ .../main/java/com/nostros/classes/Event.java | 28 ++++++++++++++++--- android/build.gradle | 6 ++++ .../gradle/wrapper/gradle-wrapper.properties | 2 +- frontend/Components/MenuItems/index.tsx | 4 +-- .../TextContent/LinksPreview/index.tsx | 2 +- frontend/Contexts/WalletContext.tsx | 1 + frontend/Pages/ConversationPage/index.tsx | 4 +-- .../Pages/HomePage/HomeFeed/MyFeed/index.tsx | 2 +- .../HomePage/NotificationsFeed/index.tsx | 6 ++-- frontend/Pages/NotePage/index.tsx | 6 ++-- .../Pages/ProfilePage/NotesFeed/index.tsx | 2 +- frontend/Pages/WalletPage/index.tsx | 22 +++++++-------- frontend/lib/Lightning/LndHub/index.ts | 4 +-- 15 files changed, 60 insertions(+), 33 deletions(-) diff --git a/.github/workflows/android-build.yml b/.github/workflows/android-build.yml index e2dadaa..43a8667 100644 --- a/.github/workflows/android-build.yml +++ b/.github/workflows/android-build.yml @@ -21,7 +21,7 @@ jobs: - uses: actions/setup-java@v3 with: distribution: temurin - java-version: 11 + java-version: 19 - name: Setup Gradle uses: gradle/gradle-build-action@v2 diff --git a/android/app/build.gradle b/android/app/build.gradle index fe68154..004fe14 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -165,6 +165,8 @@ dependencies { implementation 'com.facebook.fresco:animated-webp:2.6.0' implementation 'com.facebook.fresco:webpsupport:2.6.0' + implementation 'com.github.KoalaSat.nostr-java:nostr-base:70e62e24ad' + implementation 'com.github.KoalaSat.nostr-java:nostr-event:70e62e24ad' implementation 'com.facebook.fresco:animated-gif:2.6.0' diff --git a/android/app/src/main/java/com/nostros/classes/Event.java b/android/app/src/main/java/com/nostros/classes/Event.java index 554a8b1..6f195ec 100644 --- a/android/app/src/main/java/com/nostros/classes/Event.java +++ b/android/app/src/main/java/com/nostros/classes/Event.java @@ -21,6 +21,11 @@ import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; +import nostr.crypto.schnorr.Schnorr; +import nostr.crypto.bech32.Bech32; +import nostr.util.NostrException; +import nostr.util.NostrUtil; + public class Event { private final int created_at; private final String content; @@ -87,7 +92,13 @@ public class Event { } protected boolean isValid() { - return !id.isEmpty() && !sig.isEmpty() && created_at <= System.currentTimeMillis() / 1000L; + boolean verified = false; + try { + verified = Schnorr.verify(NostrUtil.hexToBytes(id), NostrUtil.hexToBytes(pubkey), NostrUtil.hexToBytes(sig)); + } catch (Exception e) { + e.printStackTrace(); + } + return verified && created_at <= System.currentTimeMillis() / 1000L; } protected String getMainEventId() { @@ -143,9 +154,9 @@ public class Event { protected String getRepostId() { String match = null; - Matcher m = Pattern.compile("#\\[(\\d+)\\]").matcher(content); - while (m.find()) { - int position = Integer.parseInt(m.group(1)); + Matcher matcherTag = Pattern.compile("#\\[(\\d+)\\]").matcher(content); + if (matcherTag.find()) { + int position = Integer.parseInt(matcherTag.group(1)); try { JSONArray tag = tags.getJSONArray(position); String tagKind = tag.getString(0); @@ -156,6 +167,15 @@ public class Event { e.printStackTrace(); } } + Matcher matcherBech = Pattern.compile("nostr:((nevent1|note1)\\S+)").matcher(content); + if (matcherBech.find()) { + String bech32 = matcherBech.group(1); + try { + match = Bech32.fromBech32(bech32); + } catch (NostrException e) { + e.printStackTrace(); + } + } return match; } diff --git a/android/build.gradle b/android/build.gradle index 67d887b..588581a 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,5 +1,11 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. +allprojects { + repositories { + maven { url 'https://jitpack.io' } + } +} + buildscript { ext { buildToolsVersion = "33.0.0" diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 8fad3f5..31cca49 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/frontend/Components/MenuItems/index.tsx b/frontend/Components/MenuItems/index.tsx index b943237..5444107 100644 --- a/frontend/Components/MenuItems/index.tsx +++ b/frontend/Components/MenuItems/index.tsx @@ -45,7 +45,7 @@ export const MenuItems: React.FC = () => { React.useEffect(() => { setActiveRelays(relays.filter((relay) => relay.active).length) - }, [relays]) + }, [relays, balance]) const onPressLogout: () => void = () => { logout() @@ -154,7 +154,7 @@ export const MenuItems: React.FC = () => { onPress={() => onPressItem('wallet', 1)} onTouchEnd={() => setDrawerItemIndex(-1)} right={() => { - if (!type || !balance) return <> + if (!type || balance === undefined) return <> return ( {`${balance} `} diff --git a/frontend/Components/TextContent/LinksPreview/index.tsx b/frontend/Components/TextContent/LinksPreview/index.tsx index 5897410..291157e 100644 --- a/frontend/Components/TextContent/LinksPreview/index.tsx +++ b/frontend/Components/TextContent/LinksPreview/index.tsx @@ -78,6 +78,7 @@ export const LinksPreview: React.FC = ({ urls, lnUrl }) => { rowPosition: 'top' | 'bottom', ) => JSX.Element = (link, index, rowPosition) => ( navigate('ImageGallery', { urls: imageLinks, @@ -86,7 +87,6 @@ export const LinksPreview: React.FC = ({ urls, lnUrl }) => { } > void = () => { SInfo.deleteItem('lndHub', {}) + SInfo.deleteItem('lnBits', {}) setType(undefined) setConfig(undefined) setBalance(undefined) diff --git a/frontend/Pages/ConversationPage/index.tsx b/frontend/Pages/ConversationPage/index.tsx index 32b45c1..94b3933 100644 --- a/frontend/Pages/ConversationPage/index.tsx +++ b/frontend/Pages/ConversationPage/index.tsx @@ -75,7 +75,7 @@ export const ConversationPage: React.FC = ({ route }) => return () => relayPool?.unsubscribe([ - `conversation${route.params.pubKey}`, + `conversation${route.params.pubKey.substring(0, 8)}`, `conversation-replies${route.params.pubKey.substring(0, 8)}`, ]) }, []), @@ -272,7 +272,7 @@ export const ConversationPage: React.FC = ({ route }) => if (!publicKey || !privateKey || !otherUser) return <> const eTags = getETags(item) - const showAvatar = directMessages[index - 1]?.pubkey !== item.pubkey + const showAvatar = index < 1 || directMessages[index - 1]?.pubkey !== item.pubkey const isReply = eTags.length > 0 const repliedMessageId = eTags.length > 0 ? eTags[eTags.length - 1][1] : undefined const repliedMessage = directMessages.find((message) => message.id === repliedMessageId) diff --git a/frontend/Pages/HomePage/HomeFeed/MyFeed/index.tsx b/frontend/Pages/HomePage/HomeFeed/MyFeed/index.tsx index d59f069..42f7de2 100644 --- a/frontend/Pages/HomePage/HomeFeed/MyFeed/index.tsx +++ b/frontend/Pages/HomePage/HomeFeed/MyFeed/index.tsx @@ -92,7 +92,7 @@ export const MyFeed: React.FC = ({ const message: RelayFilters = { kinds: [Kind.Text, Kind.RecommendRelay], - authors: contacts + authors: contacts, } if (results.length >= pageSize) { message.since = results[pageSize - 1].created_at diff --git a/frontend/Pages/HomePage/NotificationsFeed/index.tsx b/frontend/Pages/HomePage/NotificationsFeed/index.tsx index 98ef5d2..44b43ac 100644 --- a/frontend/Pages/HomePage/NotificationsFeed/index.tsx +++ b/frontend/Pages/HomePage/NotificationsFeed/index.tsx @@ -119,16 +119,16 @@ export const NotificationsFeed: React.FC = () => { { kinds: [Kind.Text], '#p': [publicKey], - since: limitDate + since: limitDate, }, { kinds: [Kind.Text], '#e': [publicKey], - since: limitDate + since: limitDate, }, { kinds: [30001], - authors: [publicKey] + authors: [publicKey], }, ]) } diff --git a/frontend/Pages/NotePage/index.tsx b/frontend/Pages/NotePage/index.tsx index fd7bf19..969dd9a 100644 --- a/frontend/Pages/NotePage/index.tsx +++ b/frontend/Pages/NotePage/index.tsx @@ -37,9 +37,9 @@ export const NotePage: React.FC = ({ route }) => { return () => relayPool?.unsubscribe([ - `meta-notepage${route.params.noteId}`, - `notepage${route.params.noteId}`, - `notepage-replies-${route.params.noteId}`, + `meta-notepage${route.params.noteId.substring(0, 8)}`, + `notepage${route.params.noteId.substring(0, 8)}`, + `notepage-replies-${route.params.noteId.substring(0, 8)}`, ]) }, []), ) diff --git a/frontend/Pages/ProfilePage/NotesFeed/index.tsx b/frontend/Pages/ProfilePage/NotesFeed/index.tsx index a967bb5..c4f6c23 100644 --- a/frontend/Pages/ProfilePage/NotesFeed/index.tsx +++ b/frontend/Pages/ProfilePage/NotesFeed/index.tsx @@ -44,7 +44,7 @@ export const NotesFeed: React.FC = ({ }, [pageSize, lastEventId, activeTab]) const subscribe: () => Promise = async () => { - relayPool?.subscribe(`profile-user${publicKey}`, [ + relayPool?.subscribe(`profile-user${publicKey.substring(0, 8)}`, [ { kinds: [Kind.Text, Kind.RecommendRelay], authors: [publicKey], diff --git a/frontend/Pages/WalletPage/index.tsx b/frontend/Pages/WalletPage/index.tsx index b585a3f..2ca88d1 100644 --- a/frontend/Pages/WalletPage/index.tsx +++ b/frontend/Pages/WalletPage/index.tsx @@ -56,18 +56,16 @@ export const WalletPage: React.FC = () => { if (database && publicKey) { const preimages: string[] = actions.filter((item) => item.id !== '').map((item) => item.id) - getZaps(database, { preimages }).then( - (results) => { - if (results) { - const map: Record = {} - results.forEach((zap) => { - if (!zap.preimage || zap.preimage === '') return - map[zap.preimage] = zap - }) - setZaps(map) - } - }, - ) + getZaps(database, { preimages }).then((results) => { + if (results) { + const map: Record = {} + results.forEach((zap) => { + if (!zap.preimage || zap.preimage === '') return + map[zap.preimage] = zap + }) + setZaps(map) + } + }) relayPool?.subscribe(`profile-zaps${publicKey.substring(0, 8)}`, [ { kinds: [9735], diff --git a/frontend/lib/Lightning/LndHub/index.ts b/frontend/lib/Lightning/LndHub/index.ts index 7e14bc5..e1c69b9 100644 --- a/frontend/lib/Lightning/LndHub/index.ts +++ b/frontend/lib/Lightning/LndHub/index.ts @@ -53,7 +53,7 @@ class LndHub { private readonly getTransactions: () => Promise = async () => { if (!this.config) return const response = await axios.get(`${this.config.url}/gettxs`, { headers: this.getHeaders() }) - if (response) { + if (response?.data) { return response.data.map((item: any) => { return { id: item.payment_preimage, @@ -71,7 +71,7 @@ class LndHub { const response = await axios.get(`${this.config.url}/getuserinvoices`, { headers: this.getHeaders(), }) - if (response) { + if (response?.data) { return response.data .filter((item: any) => item.ispaid) .map((item: any) => {