Skip to content

Commit

Permalink
Use saga to subscribe.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Oct 17, 2024
1 parent 4f34aca commit ab1d676
Showing 1 changed file with 9 additions and 41 deletions.
50 changes: 9 additions & 41 deletions nr-app/app/(tabs)/list.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// import Ionicons from "@expo/vector-icons/Ionicons";
import {
Button,
SafeAreaView,
ScrollView,
StyleSheet,
Text,
ScrollView,
SafeAreaView,
View,
} from "react-native";

import { ThemedText } from "@/components/ThemedText";
import { ThemedView } from "@/components/ThemedView";
import { addEvent, eventsSelectors } from "@/redux/slices/events.slice";
import { startSubscription } from "@/redux/actions/subscription.actions";
import { useAppDispatch, useAppSelector } from "@/redux/hooks";

import { Relay, finalizeEvent, verifyEvent } from "nostr-tools";
import { hexToBytes } from "@noble/hashes/utils";
import { generateSeedWords, accountFromSeedWords } from "nip06";
import { eventsSelectors } from "@/redux/slices/events.slice";

export default function TabTwoScreen() {
const events = useAppSelector(eventsSelectors.selectAll);
Expand All @@ -31,41 +28,12 @@ export default function TabTwoScreen() {
<Button
title="Load 10 notes"
onPress={async () => {
const { mnemonic } = generateSeedWords();
const account = accountFromSeedWords({ mnemonic });
console.log("#0GAjcE Generated seed and private key", {
mnemonic,
account,
});
const eventTemplate = {
kind: 0,
created_at: Math.round(Date.now() / 1e3),
content: JSON.stringify({ name: "Aarhus" }),
tags: [],
};
const event = finalizeEvent(
eventTemplate,
hexToBytes(account.privateKey.hex),
dispatch(
startSubscription({
filter: { kinds: [397], limit: 10 },
relayUrls: ["wss://relay.damus.io"],
}),
);
console.log("#cBiwGN Signed event", event);
const verificationResult = verifyEvent(event);
console.log("#QPwp7w Verification result", verificationResult);

const relay_uri = "wss://relay.damus.io";
const relay = new Relay(relay_uri);
await relay.connect();
const sub = relay.subscribe([{ kinds: [397], limit: 10 }], {
onevent: (event) =>
void dispatch(
addEvent({
event,
fromRelay: relay_uri,
}),
),
oneose: () => {
sub.close();
},
});
}}
/>
</View>
Expand Down

0 comments on commit ab1d676

Please sign in to comment.