Skip to content

Commit

Permalink
Merge pull request #18 from ethicnology/develop
Browse files Browse the repository at this point in the history
v1.3.3
  • Loading branch information
ethicnology committed Feb 10, 2023
2 parents a50337c + 4828104 commit 9b9424f
Show file tree
Hide file tree
Showing 12 changed files with 488 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -38,3 +38,8 @@
- refactor: Event with optional verification
- remove tests with encoding problem
- improve coverage

## 1.3.3
- add comments about verify and fix typo
- nip 002 implementation, unit tests, examples and documentation
- Event.partial to init an empty event that you validate later, documentation
133 changes: 115 additions & 18 deletions README.md
Expand Up @@ -14,11 +14,10 @@ flutter pub add nostr


## [NIPS](https://github.com/nostr-protocol/nips)
- [x] [NIP01 Events and signature](https://github.com/nostr-protocol/nips/blob/master/01.md#events-and-signatures)
- [x] [NIP01 Request and filters](https://github.com/nostr-protocol/nips/blob/master/01.md#communication-between-clients-and-relays)
- [x] [NIP01 Close](https://github.com/nostr-protocol/nips/blob/master/01.md#communication-between-clients-and-relays)
- [x] [NIP15 EOSE](https://github.com/nostr-protocol/nips/blob/master/15.md)
- [x] [NIP20 OK](https://github.com/nostr-protocol/nips/blob/master/20.md)
- [x] [NIP 01 Basic protocol flow description](https://github.com/nostr-protocol/nips/blob/master/01.md)
- [x] [NIP 02 Contact List and Petnames](https://github.com/nostr-protocol/nips/blob/master/02.md)
- [x] [NIP 15 End of Stored Events Notice](https://github.com/nostr-protocol/nips/blob/master/15.md)
- [x] [NIP 20 Command Results](https://github.com/nostr-protocol/nips/blob/master/20.md)

## Usage
### Events messages
Expand All @@ -38,7 +37,7 @@ void main() async {
var randomKeys = Keychain.generate();
print(randomKeys.private);
// Instanciate an event with all the field
// Instantiate an event with all the field
String id =
"4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49";
String pubkey = keys.public;
Expand All @@ -62,7 +61,7 @@ void main() async {
print(oneEvent.id);
// 4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49
// Instanciate an event with a partial data and let the library sign the event with your private key
// Instantiate an event with a partial data and let the library sign the event with your private key
Event anotherEvent = Event.from(
kind: 1,
tags: [],
Expand Down Expand Up @@ -102,14 +101,63 @@ import 'dart:io';
import 'package:nostr/nostr.dart';
void main() async {
// Create a subscription message request with one or many filters
Request requestWithFilter = Request(generate64RandomHexChars(), [
Filter(
kinds: [0, 1, 2, 7],
since: 1674063680,
limit: 450,
)
]);
// Use the Keychain class to manipulate private/public keys and use handy methods encapsulated from dart-bip340
var keys = Keychain(
"5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
);
assert(keys.public ==
"981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b");
// or generate random keys
var randomKeys = Keychain.generate();
print(randomKeys.private);
// Instantiate an event with all the field
String id =
"4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49";
String pubkey = keys.public;
int createdAt = 1672175320;
int kind = 1;
List<List<String>> tags = [];
String content = "Ceci est une analyse du websocket";
String sig =
"797c47bef50eff748b8af0f38edcb390facf664b2367d72eb71c50b5f37bc83c4ae9cc9007e8489f5f63c66a66e101fd1515d0a846385953f5f837efb9afe885";
Event oneEvent = Event(
id,
pubkey,
createdAt,
kind,
tags,
content,
sig,
);
assert(oneEvent.id ==
"4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49");
// Create a partial event from nothing and fill it with data until it is valid
var partialEvent = Event.partial();
assert(partialEvent.isValid() == false);
partialEvent.createdAt = currentUnixTimestampSeconds();
partialEvent.pubkey =
"981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b";
partialEvent.id = partialEvent.getEventId();
partialEvent.sig = partialEvent.getSignature(
"5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
);
assert(partialEvent.isValid() == true);
// Instantiate an event with a partial data and let the library sign the event with your private key
Event anotherEvent = Event.from(
kind: 1,
tags: [],
content: "vi veri universum vivus vici",
privkey:
"5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12", // DO NOT REUSE THIS PRIVATE KEY
);
assert(anotherEvent.pubkey ==
"981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b");
// Connecting to a nostr relay using websocket
WebSocket webSocket = await WebSocket.connect(
Expand All @@ -119,8 +167,8 @@ void main() async {
// wss://nostr.sandwich.farm
// wss://relay.damus.io
// Send a request message to the WebSocket server
webSocket.add(requestWithFilter.serialize());
// Send an event to the WebSocket server
webSocket.add(anotherEvent.serialize());
// Listen for events from the WebSocket server
await Future.delayed(Duration(seconds: 1));
Expand Down Expand Up @@ -187,4 +235,53 @@ void main() async {
var ok = Message.deserialize(okPayload);
assert(ok.type == "OK");
}
```
```

### NIP 02 Contact List and Petnames
```dart
import 'package:nostr/nostr.dart';
void main() {
// Decode profiles from an event of kind=3
var event = Event.from(
kind: 3,
tags: [
["p", "91cf9..4e5ca", "wss://alicerelay.com/", "alice"],
["p", "14aeb..8dad4", "wss://bobrelay.com/nostr", "bob"],
["p", "612ae..e610f", "ws://carolrelay.com/ws", "carol"],
],
content: "",
privkey: "5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
);
List<Profile> someProfiles = Nip2.decode(event);
assert(someProfiles[0].key == "91cf9..4e5ca");
assert(someProfiles[1].relay == "wss://bobrelay.com/nostr");
assert(someProfiles[2].petname == "carol");
// Instantiate a new nip2 profile
String key = "91cf9..4e5ca";
String relay = "wss://alicerelay.com/";
String petname = "alice";
var alice = Profile(key, relay, petname);
List<Profile> profiles = [
alice,
Profile("21df6d143fb96c2ec9d63726bf9edc71", "", "erin")
];
// Encode profiles to nostr event.tags
List<List<String>> tags = Nip2.toTags(profiles);
assert(tags[1][0] == "p");
assert(tags[1][3] == "erin");
// Decode event.tags to profiles list
List<Profile> newProfiles = Nip2.toProfiles([
["p", "91cf9..4e5ca", "wss://alicerelay.com/", "alice"],
["p", "14aeb..8dad4", "wss://bobrelay.com/nostr", "bob"],
["p", "612ae..e610f", "ws://carolrelay.com/ws", "carol"]
]);
assert(newProfiles[2].petname == "carol");
}
```
44 changes: 44 additions & 0 deletions example/nip_002_example.dart
@@ -0,0 +1,44 @@
import 'package:nostr/nostr.dart';

void main() {
// Decode profiles from an event of kind=3
var event = Event.from(
kind: 3,
tags: [
["p", "91cf9..4e5ca", "wss://alicerelay.com/", "alice"],
["p", "14aeb..8dad4", "wss://bobrelay.com/nostr", "bob"],
["p", "612ae..e610f", "ws://carolrelay.com/ws", "carol"],
],
content: "",
privkey: "5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
);

List<Profile> someProfiles = Nip2.decode(event);
assert(someProfiles[0].key == "91cf9..4e5ca");
assert(someProfiles[1].relay == "wss://bobrelay.com/nostr");
assert(someProfiles[2].petname == "carol");

// Instantiate a new nip2 profile
String key = "91cf9..4e5ca";
String relay = "wss://alicerelay.com/";
String petname = "alice";
var alice = Profile(key, relay, petname);

List<Profile> profiles = [
alice,
Profile("21df6d143fb96c2ec9d63726bf9edc71", "", "erin")
];

// Encode profiles to nostr event.tags
List<List<String>> tags = Nip2.toTags(profiles);
assert(tags[1][0] == "p");
assert(tags[1][3] == "erin");

// Decode event.tags to profiles list
List<Profile> newProfiles = Nip2.toProfiles([
["p", "91cf9..4e5ca", "wss://alicerelay.com/", "alice"],
["p", "14aeb..8dad4", "wss://bobrelay.com/nostr", "bob"],
["p", "612ae..e610f", "ws://carolrelay.com/ws", "carol"]
]);
assert(newProfiles[2].petname == "carol");
}
27 changes: 19 additions & 8 deletions example/nostr_example.dart
Expand Up @@ -6,14 +6,14 @@ void main() async {
var keys = Keychain(
"5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
);
print(keys.public ==
assert(keys.public ==
"981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b");

// Generate random keys
// or generate random keys
var randomKeys = Keychain.generate();
print(randomKeys.private);

// Instanciate an event with all the field
// Instantiate an event with all the field
String id =
"4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49";
String pubkey = keys.public;
Expand All @@ -33,11 +33,22 @@ void main() async {
content,
sig,
);
assert(oneEvent.id ==
"4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49");

print(oneEvent.id);
// 4b697394206581b03ca5222b37449a9cdca1741b122d78defc177444e2536f49
// Create a partial event from nothing and fill it with data until it is valid
var partialEvent = Event.partial();
assert(partialEvent.isValid() == false);
partialEvent.createdAt = currentUnixTimestampSeconds();
partialEvent.pubkey =
"981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b";
partialEvent.id = partialEvent.getEventId();
partialEvent.sig = partialEvent.getSignature(
"5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12",
);
assert(partialEvent.isValid() == true);

// Instanciate an event with a partial data and let the library sign the event with your private key
// Instantiate an event with a partial data and let the library sign the event with your private key
Event anotherEvent = Event.from(
kind: 1,
tags: [],
Expand All @@ -46,8 +57,8 @@ void main() async {
"5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12", // DO NOT REUSE THIS PRIVATE KEY
);

print(anotherEvent.pubkey);
// 981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b
assert(anotherEvent.pubkey ==
"981cc2078af05b62ee1f98cff325aac755bf5c5836a265c254447b5933c6223b");

// Connecting to a nostr relay using websocket
WebSocket webSocket = await WebSocket.connect(
Expand Down
1 change: 1 addition & 0 deletions lib/nostr.dart
Expand Up @@ -10,3 +10,4 @@ export 'src/filter.dart';
export 'src/close.dart';
export 'src/message.dart';
export 'src/utils.dart';
export 'src/nips/nip_002.dart';

0 comments on commit 9b9424f

Please sign in to comment.