-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnip_101.dart
166 lines (149 loc) · 4.26 KB
/
nip_101.dart
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
/// nip 101 - Key exchange
import 'dart:convert';
import 'package:nostr_core_dart/nostr.dart';
class Nip101 {
static Future<Event> request(
String myAliasPubkey, String toPubkey, String pubkey, String privkey,
{int? expiration, int? interval, String? relay}) async {
Map map = {};
String content = '';
if (expiration != null && expiration > 0) map["expiration"] = expiration;
if (interval != null && interval > 0) map["interval"] = interval;
if (relay != null && relay.isNotEmpty) map["r"] = relay;
if (map.isNotEmpty) content = jsonEncode(map);
return await Event.from(
kind: 10100,
tags: [
['p', toPubkey, myAliasPubkey]
],
content: content,
pubkey: pubkey,
privkey: privkey);
}
static Future<Event> accept(String myAliasPubkey, String toPubkey,
String sessionId, String pubkey, String privkey) async {
return await Event.from(
kind: 10101,
tags: [
['p', toPubkey, myAliasPubkey],
['e', sessionId]
],
content: '',
pubkey: pubkey,
privkey: privkey);
}
static Future<Event> reject(
String toPubkey, String sessionId, String pubkey, String privkey) async {
return await Event.from(
kind: 10102,
tags: [
['p', toPubkey],
['e', sessionId]
],
content: '',
pubkey: pubkey,
privkey: privkey);
}
static Future<Event> close(
String toPubkey, String sessionId, String pubkey, String privkey) async {
return await Event.from(
kind: 10103,
tags: [
['p', toPubkey],
['e', sessionId]
],
content: '',
pubkey: pubkey,
privkey: privkey);
}
static Future<Event> update(String myNewAliasPubkey, String toPubkey,
String sessionId, String pubkey, String privkey) async {
return await Event.from(
kind: 10104,
tags: [
['p', toPubkey, myNewAliasPubkey],
['e', sessionId]
],
content: '',
pubkey: pubkey,
privkey: privkey);
}
static KeyExchangeSession decode(Event event) {
String fromAliasPubkey = '', toPubkey = '', sessionId = '';
for (var tag in event.tags) {
if (tag[0] == 'p' && tag.length > 2) {
toPubkey = tag[1];
fromAliasPubkey = tag[2];
}
if (tag[0] == 'e' && tag.length > 1) {
sessionId = tag[1];
}
}
if (event.kind == 10100) {
sessionId = event.id;
}
int? expiration, interval;
String? relay;
if (event.content.isNotEmpty) {
Map map = jsonDecode(event.content);
if (map.isNotEmpty) {
expiration = map["expiration"];
interval = map["interval"];
relay = map["r"].toString();
}
}
return KeyExchangeSession(event.pubkey, fromAliasPubkey, toPubkey,
sessionId, event.kind, event.createdAt, expiration, interval, relay);
}
static KeyExchangeSession getRequest(Event event) {
if (event.kind == 10100) {
return decode(event);
}
throw Exception("${event.kind} is not nip101 compatible");
}
static KeyExchangeSession getAccept(Event event) {
if (event.kind == 10101) {
return decode(event);
}
throw Exception("${event.kind} is not nip101 compatible");
}
static KeyExchangeSession getReject(Event event) {
if (event.kind == 10102) {
return decode(event);
}
throw Exception("${event.kind} is not nip101 compatible");
}
static KeyExchangeSession getClose(Event event) {
if (event.kind == 10103) {
return decode(event);
}
throw Exception("${event.kind} is not nip101 compatible");
}
static KeyExchangeSession getUpdate(Event event) {
if (event.kind == 10104) {
return decode(event);
}
throw Exception("${event.kind} is not nip101 compatible");
}
}
class KeyExchangeSession {
String sessionId;
int kind;
int createTime;
String fromPubkey; // sender
String fromAliasPubkey;
String toPubkey; // receiver
int? expiration;
int? interval;
String? relay;
KeyExchangeSession(
this.fromPubkey,
this.fromAliasPubkey,
this.toPubkey,
this.sessionId,
this.kind,
this.createTime,
this.expiration,
this.interval,
this.relay);
}