-
Notifications
You must be signed in to change notification settings - Fork 10
/
keys.dart
220 lines (195 loc) · 6.66 KB
/
keys.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import 'package:dart_nostr/nostr/core/constants.dart';
import 'package:dart_nostr/nostr/core/key_pairs.dart';
import '../../core/utils.dart';
import '../../dart_nostr.dart';
import 'base/keys.dart';
import 'package:bip32_bip44/dart_bip32_bip44.dart' as bip32_bip44;
import 'package:bip39/bip39.dart' as bip39;
/// {@template nostr_keys}
/// This class is responsible for generating key pairs and deriving public keys from private keys..
/// {@endtemplate}
class NostrKeys implements NostrKeysBase {
/// Derives a public key from a [privateKey] directly, use this if you want a quick way to get a public key from a private key.
///
///
/// ```dart
/// final publicKey = Nostr.instance.keysService.derivePublicKey(privateKey: yourPrivateKey);
/// print(publicKey); // ...
/// ```
@override
String derivePublicKey({required String privateKey}) {
final nostrKeyPairs = NostrKeyPairs(private: privateKey);
NostrClientUtils.log(
"derived public key from private key, with it's value is: ${nostrKeyPairs.public}",
);
return nostrKeyPairs.public;
}
/// You can use this method to generate a key pair for your end users.
///
///
/// ```dart
/// final keyPair = Nostr.instance.keysService.generateKeyPair();
/// print(keyPair.public); // ...
/// print(keyPair.private); // ...
/// ```
@override
NostrKeyPairs generateKeyPair() {
final nostrKeyPairs = NostrKeyPairs.generate();
NostrClientUtils.log(
"generated key pairs, with it's public key is: ${nostrKeyPairs.public}",
);
return nostrKeyPairs;
}
/// Generates a key pair from an existing [privateKey], use this if you want to generate a key pair from an existing private key.
///
/// ```dart
/// final keyPair = Nostr.instance.keysService.generateKeyPairFromExistingPrivateKey(yourPrivateKey);
/// print(keyPair.public); // ...
/// print(keyPair.private); // ...
/// ```
@override
NostrKeyPairs generateKeyPairFromExistingPrivateKey(
String privateKey,
) {
return NostrKeyPairs(private: privateKey);
}
/// You can use this method to generate a key pair for your end users.
/// it returns the private key of the generated key pair.
///
/// ```dart
/// final privateKey = Nostr.instance.keysService.generatePrivateKey();
/// print(privateKey); // ...
/// ```
@override
String generatePrivateKey() {
return generateKeyPair().private;
}
/// Encodes a Nostr [publicKey] to an npub key (bech32 encoding).
///
/// ```dart
/// final npubString = Nostr.instance.keysService.encodePublicKeyToNpub(yourPublicKey);
/// print(npubString); // ...
/// ```
@override
String encodePublicKeyToNpub(String publicKey) {
return Nostr.instance.utilsService
.encodeBech32(publicKey, NostrConstants.npub);
}
/// Encodes a Nostr [privateKey] to an nsec key (bech32 encoding).
/// ```dart
/// final nsecString = Nostr.instance.keysService.encodePrivateKeyToNsec(yourPrivateKey);
/// print(nsecString); // ...
///
@override
String encodePrivateKeyToNsec(String privateKey) {
return Nostr.instance.utilsService
.encodeBech32(privateKey, NostrConstants.nsec);
}
/// Decodes a Nostr [npubKey] to a public key.
///
/// ```dart
/// final publicKey = Nostr.instance.keysService.decodeNpubKeyToPublicKey(yourNpubKey);
/// print(publicKey); // ...
/// ```
@override
String decodeNpubKeyToPublicKey(String npubKey) {
assert(npubKey.startsWith(NostrConstants.npub));
final List<String> decodedKeyComponents =
Nostr.instance.utilsService.decodeBech32(npubKey);
return decodedKeyComponents.first;
}
/// Decodes a Nostr [nsecKey] to a private key.
///
/// ```dart
/// final privateKey = Nostr.instance.keysService.decodeNsecKeyToPrivateKey(yourNsecKey);
/// print(privateKey); // ...
/// ```
@override
String decodeNsecKeyToPrivateKey(String nsecKey) {
assert(nsecKey.startsWith(NostrConstants.nsec));
final List<String> decodedKeyComponents =
Nostr.instance.utilsService.decodeBech32(nsecKey);
return decodedKeyComponents.first;
}
/// You can use this method to sign a [message] with a [privateKey].
///
/// ```dart
/// final signature = Nostr.instance.keysService.sign(
/// privateKey: yourPrivateKey,
/// message: yourMessage,
/// );
///
/// print(signature); // ...
/// ```
@override
String sign({
required String privateKey,
required String message,
}) {
final nostrKeyPairs = NostrKeyPairs(private: privateKey);
final hexEncodedMessage =
Nostr.instance.utilsService.hexEncodeString(message);
final signature = nostrKeyPairs.sign(hexEncodedMessage);
NostrClientUtils.log(
"signed message with private key, with it's value is: $signature",
);
return signature;
}
/// You can use this method to verify a [message] with a [publicKey] and it's [signature].
/// it returns a [bool] that indicates if the [message] is verified or not.
///
/// ```dart
/// final isVerified = Nostr.instance.keysService.verify(
/// publicKey: yourPublicKey,
/// message: yourMessage,
/// signature: yourSignature,
/// );
///
/// print(isVerified); // ...
/// ```
bool verify({
required String publicKey,
required String message,
required String signature,
}) {
final hexEncodedMessage =
Nostr.instance.utilsService.hexEncodeString(message);
final isVerified =
NostrKeyPairs.verify(publicKey, hexEncodedMessage, signature);
NostrClientUtils.log(
"verified message with public key: $publicKey, with it's value is: $isVerified",
);
return isVerified;
}
/// Weither the [key] is a valid Nostr private key or not.
///
/// ```dart
/// Nostr.instance.keysService.isValidPrivateKey('something that is not a key'); // false
/// ```
@override
bool isValidPrivateKey(String key) {
return NostrKeyPairs.isValidPrivateKey(key);
}
/// Wether the given [text] is a valid mnemonic or not.
///
/// ```dart
/// final isValid = Nostr.instance.keysService.isMnemonicValid('your mnemonic');
/// print(isValid); // ...
/// ```
static bool isMnemonicValid(String text) {
return bip39.validateMnemonic(text);
}
static String getPrivateKeyFromMnemonic(String mnemonic) {
String seed = bip39.mnemonicToSeedHex(mnemonic);
bip32_bip44.Chain chain = bip32_bip44.Chain.seed(seed);
bip32_bip44.ExtendedPrivateKey key =
chain.forPath("m/44'/1237'/0'/0") as bip32_bip44.ExtendedPrivateKey;
bip32_bip44.ExtendedPrivateKey? childKey =
bip32_bip44.deriveExtendedPrivateChildKey(key, 0);
String hexChildKey = "";
if (childKey.key != null) {
hexChildKey = childKey.key!.toRadixString(16);
}
return hexChildKey;
}
}