-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.dart
More file actions
206 lines (170 loc) · 5.4 KB
/
Copy pathmain.dart
File metadata and controls
206 lines (170 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'dart:convert';
import 'package:dart_nostr/dart_nostr.dart';
import '_example_shared.dart';
// End-to-end demo: keys, relay connection, publish, subscribe, count,
// delete, sign/verify, NIP-05 verification, and error handling.
Future<void> main() async {
final nostr = exampleNostr(enableLogs: true);
// keys
print(divider('keys'));
final keyPair = nostr.keys.generateKeyPair();
print('public : ${keyPair.public}');
print('private: ${keyPair.private}');
print('valid : ${NostrKeyPairs.isValidPrivateKey(keyPair.private)}');
final npub = nostr.bech32.encodePublicKeyToNpub(keyPair.public);
final nsec = nostr.bech32.encodePrivateKeyToNsec(keyPair.private);
print('npub : $npub');
print('nsec : $nsec');
// connect
print(divider('connect'));
final connectResult = await nostr.connect(exampleRelays);
printResult('connect', connectResult);
for (final relay in exampleRelays) {
try {
final info = await nostr.relays
.relayInformationsDocumentNip11(relayUrl: relay);
if (info != null) {
final nips = info.supportedNips?.join(', ') ?? 'none';
print('$relay -> ${info.name}, NIPs: $nips');
}
} catch (e) {
print('NIP-11 fetch failed for $relay: $e');
}
}
// publish
print(divider('publish'));
final metadata = NostrEvent.fromPartialData(
kind: 0,
content: jsonEncode({
'name': 'dart_nostr example',
'about': 'example run',
}),
keyPairs: keyPair,
);
final metaResult = await nostr.publish(metadata);
metaResult.fold(
(ok) {
print('metadata accepted: ${ok.isEventAccepted}, message: ${ok.message}');
},
(failure) {
print('metadata failed: ${failure.message} (${failure.code})');
print('retryable: ${failure.isRetryable}');
},
);
final note = NostrEvent.fromPartialData(
kind: 1,
content: 'hello from dart_nostr ${DateTime.now().toIso8601String()}',
keyPairs: keyPair,
tags: [
['t', 'dart'],
['t', 'nostr'],
],
);
final noteResult = await nostr.publish(note);
noteResult.fold(
(ok) => print('note accepted: ${ok.isEventAccepted}'),
(failure) => print('note failed: ${failure.message}'),
);
// subscribe
print(divider('subscribe'));
final subResult = nostr.subscribeRequest(
NostrRequest(
filters: [
NostrFilter(
kinds: [1],
limit: 10,
since: DateTime.now().subtract(const Duration(hours: 1)),
),
NostrFilter(
kinds: [0],
limit: 5,
since: DateTime.now().subtract(const Duration(hours: 1)),
),
],
),
);
subResult.fold(
(stream) {
print('subscription id: ${stream.subscriptionId}');
var count = 0;
stream.stream.listen(
(event) {
count++;
final raw = event.content ?? '';
final preview =
raw.length > 60 ? '${raw.substring(0, 60)}...' : raw;
final author = event.pubkey.substring(0, 8);
print('[$count] kind=${event.kind} author=$author "$preview"');
},
onError: (Object e) => print('stream error: $e'),
onDone: () => print('stream closed, $count events received'),
);
Future.delayed(const Duration(seconds: 3), () {
final stats = nostr.subscriptionStatistics;
print('active subs: ${nostr.activeSubscriptions.length}');
print('total events tracked: ${stats.totalEventCount}');
stream.close();
});
},
(failure) => print('subscribe failed: ${failure.message}'),
);
// count (NIP-45)
print(divider('count'));
final countResult = await nostr.count(
NostrCountEvent.fromPartialData(
eventsFilter: NostrFilter(kinds: [1], limit: 100),
),
);
countResult.fold(
(r) => print('count: ${r.count}'),
(failure) => print('count failed: ${failure.message}'),
);
// delete
print(divider('delete'));
final deletion = NostrEvent.deleteEvent(
keyPairs: keyPair,
reasonOfDeletion: 'cleanup',
eventIdsToBeDeleted: [note.id ?? ''],
);
final deleteResult = await nostr.publish(deletion);
deleteResult.fold(
(ok) => print('delete accepted: ${ok.isEventAccepted}'),
(failure) => print('delete failed: ${failure.message}'),
);
// sign / verify
print(divider('sign / verify'));
const msg = 'hello nostr';
final sig = nostr.keys.sign(privateKey: keyPair.private, message: msg);
final valid = nostr.keys.verify(
publicKey: keyPair.public,
message: msg,
signature: sig,
);
print('message : $msg');
print('signature: ${sig.substring(0, 32)}...');
print('valid : $valid');
// NIP-05
print(divider('nip-05'));
const knownPubkey =
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245';
try {
final verified = await nostr.utils.verifyNip05(
internetIdentifier: 'jb55@jb55.com',
pubKey: knownPubkey,
);
print('jb55@jb55.com verified: $verified');
} catch (e) {
print('nip-05 error: $e');
}
// error handling
print(divider('error handling'));
final badConnect = await nostr.client.connect(['https://invalid.com']);
printResult('bad relay url', badConnect);
final badSub = nostr.subscribeRequest(NostrRequest(filters: []));
printResult('empty filter', badSub);
// cleanup
print(divider('cleanup'));
nostr.closeAllSubscriptions();
final disconnectResult = await nostr.disconnect();
printResult('disconnect', disconnectResult);
}