-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Committing local changes before pull
- Loading branch information
1 parent
987bc46
commit 4b69103
Showing
18 changed files
with
856 additions
and
29 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
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
import 'package:breez/bloc/async_action.dart'; | ||
|
||
class GetPublicKey extends AsyncAction { | ||
GetPublicKey(); | ||
} | ||
|
||
class SignEvent extends AsyncAction { | ||
final Map<String, dynamic> eventObject; | ||
final String privateKey; | ||
|
||
SignEvent(this.eventObject, this.privateKey); | ||
} | ||
|
||
class GetRelays extends AsyncAction {} | ||
|
||
class Nip04Encrypt extends AsyncAction { | ||
final String data; | ||
final String publicKey; | ||
|
||
Nip04Encrypt(this.data, this.publicKey); | ||
} | ||
|
||
class Nip04Decrypt extends AsyncAction { | ||
final String encryptedData; | ||
final String privateKey; | ||
|
||
Nip04Decrypt(this.encryptedData, this.privateKey); | ||
} |
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,168 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:breez/bloc/async_actions_handler.dart'; | ||
import 'package:breez/bloc/nostr/nostr_actions.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:nostr_tools/nostr_tools.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class NostrBloc with AsyncActionsHandler { | ||
String nostrPublicKey; | ||
String nostrPrivateKey; | ||
|
||
FlutterSecureStorage _secureStorage; | ||
SharedPreferences sharedPreferences; | ||
|
||
NostrBloc() { | ||
_secureStorage = const FlutterSecureStorage(); | ||
|
||
initNostr(); | ||
|
||
registerAsyncHandlers({ | ||
GetPublicKey: _handleGetPublicKey, | ||
SignEvent: _handleSignEvent, | ||
GetRelays: _handleGetRelays, | ||
Nip04Encrypt: _handleNip04Encrypt, | ||
Nip04Decrypt: _handleNip04Decrypt, | ||
}); | ||
listenActions(); | ||
} | ||
|
||
void initNostr() async { | ||
nostrPublicKey = await _secureStorage.read(key: 'nostrPublicKey'); | ||
nostrPrivateKey = await _secureStorage.read(key: 'nostrPrivateKey'); | ||
SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); | ||
|
||
if (nostrPublicKey == null) { | ||
sharedPreferences.setBool('rememberGetPubKeyChoice', false); | ||
sharedPreferences.setBool('rememberSignEventChoice', false); | ||
} | ||
} | ||
|
||
final StreamController<String> _publicKeyController = | ||
StreamController<String>.broadcast(); | ||
Stream<String> get publicKeyStream => _publicKeyController.stream; | ||
|
||
final StreamController<Map<String, dynamic>> _eventController = | ||
StreamController<Map<String, dynamic>>.broadcast(); | ||
|
||
Stream<Map<String, dynamic>> get eventStream => _eventController.stream; | ||
|
||
Future<void> _handleGetPublicKey(GetPublicKey action) async { | ||
// to get the public key | ||
String publicKey = await _fetchPublicKey(); | ||
_publicKeyController.add(publicKey); | ||
action.resolve(publicKey); | ||
} | ||
|
||
Future<void> _handleSignEvent(SignEvent action) async { | ||
// to sign the event | ||
Map<String, dynamic> signedEvent = | ||
await _signEvent(action.eventObject, action.privateKey); | ||
|
||
_eventController.add(signedEvent); | ||
action.resolve(signedEvent); | ||
} | ||
|
||
Future<void> _handleGetRelays(GetRelays action) async { | ||
// to get the relays | ||
List<String> relays = await _fetchRelays(); | ||
// Perform any processing on the relays if needed | ||
action.resolve(relays); | ||
} | ||
|
||
Future<void> _handleNip04Encrypt(Nip04Encrypt action) async { | ||
// to encrypt the data` | ||
String encryptedData = await _encryptData(action.data, action.publicKey); | ||
action.resolve(encryptedData); | ||
} | ||
|
||
Future<void> _handleNip04Decrypt(Nip04Decrypt action) async { | ||
// to decrypt the data | ||
String decryptedData = | ||
await _decryptData(action.encryptedData, action.privateKey); | ||
action.resolve(decryptedData); | ||
} | ||
|
||
// Methods to simulate the actual logic | ||
|
||
Future<String> _fetchPublicKey() async { | ||
// check if key pair already exists otherwise generate it | ||
|
||
if (nostrPublicKey == null) { | ||
final keyGenerator = KeyApi(); | ||
nostrPrivateKey = keyGenerator.generatePrivateKey(); | ||
nostrPublicKey = keyGenerator.getPublicKey(nostrPrivateKey); | ||
|
||
// Write value | ||
await _secureStorage.write( | ||
key: 'nostrPrivateKey', value: nostrPrivateKey); | ||
await _secureStorage.write(key: 'nostrPublicKey', value: nostrPublicKey); | ||
|
||
Future.delayed(const Duration(seconds: 1)); | ||
} | ||
|
||
return nostrPublicKey; | ||
} | ||
|
||
Future<Map<String, dynamic>> _signEvent( | ||
Map<String, dynamic> eventObject, String nostrPrivateKey) async { | ||
final eventApi = EventApi(); | ||
|
||
if (eventObject['pubkey'] == null) { | ||
eventObject['pubkey'] = nostrPublicKey; | ||
} | ||
|
||
List<dynamic> dynamicList = eventObject['tags']; | ||
List<List<String>> stringList = | ||
dynamicList.map((innerList) => List<String>.from(innerList)).toList(); | ||
|
||
final event = Event( | ||
content: eventObject['content'], | ||
created_at: eventObject['created_at'], | ||
kind: eventObject['kind'], | ||
pubkey: eventObject['pubkey'], | ||
tags: stringList, | ||
); | ||
|
||
if (eventObject['id'] == null || eventObject['id'] == '') { | ||
event.id = eventApi.getEventHash(event); | ||
eventObject['id'] = event.id; | ||
} else { | ||
event.id = eventObject['id']; | ||
} | ||
|
||
event.sig = eventApi.signEvent(event, nostrPrivateKey); | ||
|
||
if (eventApi.verifySignature(event)) { | ||
eventObject['sig'] = event.sig; | ||
} | ||
await Future.delayed(const Duration(seconds: 1)); | ||
|
||
return eventObject; | ||
} | ||
|
||
Future<List<String>> _fetchRelays() async { | ||
await Future.delayed(const Duration(seconds: 1)); | ||
return ['Relay1', 'Relay2', 'Relay3']; | ||
} | ||
|
||
Future<String> _encryptData(String data, String publicKey) async { | ||
// Simulating an encryption operation | ||
await Future.delayed(const Duration(seconds: 1)); | ||
return 'EncryptedData'; | ||
} | ||
|
||
Future<String> _decryptData(String encryptedData, String privateKey) async { | ||
// Simulating a decryption operation | ||
await Future.delayed(const Duration(seconds: 1)); | ||
return 'DecryptedData'; | ||
} | ||
|
||
@override | ||
Future dispose() { | ||
// _actionsHandler.dispose(); | ||
_publicKeyController.close(); | ||
return super.dispose(); | ||
} | ||
} |
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,34 @@ | ||
class Event { | ||
String id; | ||
int kind; | ||
String pubKey; | ||
String content; | ||
List<List<String>> tags; | ||
int created_at; | ||
String sig; | ||
|
||
Event({ | ||
this.id, | ||
this.kind, | ||
this.pubKey, | ||
this.content, | ||
this.tags, | ||
this.created_at, | ||
this.sig, | ||
}); | ||
} | ||
|
||
Map<int, String> eventKind = { | ||
0: 'metadata', | ||
1: 'Text', | ||
2: 'RecommendRelay', | ||
3: 'Contacts', | ||
4: 'EncryptedDirectMessage', | ||
5: 'EventDeletion', | ||
6: 'Repost', | ||
7: 'Reaction', | ||
9734: 'ZapRequest', | ||
9735: 'Zap', | ||
10002: 'RelayList', | ||
30078: 'Application Specific Data' | ||
}; |
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
Oops, something went wrong.