-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9c671a
commit 3d18224
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'package:nostr/nostr.dart'; | ||
|
||
class Nip17 { | ||
static Future<Event> encode({ | ||
required String message, | ||
required String authorPrivkey, | ||
required String receiverPubkey, | ||
}) async { | ||
final authorPubkey = Keychain(authorPrivkey).public; | ||
|
||
final rumor = Event.partial( | ||
pubkey: authorPubkey, | ||
kind: 14, | ||
content: message, | ||
); | ||
rumor.id = rumor.getEventId(); | ||
// Kind 14s MUST never be signed. | ||
// If it is signed, the message might leak to relays and become fully public. | ||
rumor.sig = ''; | ||
|
||
return await Nip59.wrap( | ||
rumor: rumor, | ||
authorPrivkey: authorPrivkey, | ||
recipientPubkey: receiverPubkey, | ||
); | ||
} | ||
|
||
static Future<Event> decode({ | ||
required Event giftWrap, | ||
required String receiverPrivkey, | ||
}) async { | ||
final dm = await Nip59.unwrap( | ||
giftWrap: giftWrap, | ||
recipientPrivkey: receiverPrivkey, | ||
); | ||
|
||
if (dm.kind != 14) { | ||
throw Exception('NIP-17 define private direct messages with kind=14'); | ||
} | ||
|
||
return dm; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:nostr/src/nips/nip_017.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:nostr/nostr.dart'; | ||
|
||
void main() async { | ||
final authorNsec = | ||
'nsec1w8udu59ydjvedgs3yv5qccshcj8k05fh3l60k9x57asjrqdpa00qkmr89m'; | ||
final authorPrivkey = Nip19.decodePrivkey(authorNsec); | ||
final author = Keychain(authorPrivkey); | ||
|
||
final recipientNsec = | ||
'nsec12ywtkplvyq5t6twdqwwygavp5lm4fhuang89c943nf2z92eez43szvn4dt'; | ||
final recipientPrivkey = Nip19.decodePrivkey(recipientNsec); | ||
final recipient = Keychain(recipientPrivkey); | ||
|
||
final message = 'Hola, que tal?'; | ||
|
||
final dm = await Nip17.encode( | ||
authorPrivkey: author.private, | ||
receiverPubkey: recipient.public, | ||
message: message, | ||
); | ||
|
||
group('NIP-17 Direct Message', () { | ||
test('encode a direct message', () async { | ||
expect(dm.kind, 1059); | ||
expect(dm.tags, [ | ||
[ | ||
"p", | ||
"918e2da906df4ccd12c8ac672d8335add131a4cf9d27ce42b3bb3625755f0788" | ||
] | ||
]); | ||
}); | ||
|
||
test('decode a direct message', () async { | ||
final x = await Nip17.decode( | ||
giftWrap: dm, | ||
receiverPrivkey: recipient.private, | ||
); | ||
|
||
expect(x.kind, 14); | ||
expect(x.pubkey, author.public); | ||
expect(x.sig, isEmpty); | ||
}); | ||
}); | ||
} |