Skip to content

Commit

Permalink
adding key generation to golang side
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthak13gupta committed Jul 9, 2023
1 parent 3f10d79 commit 2888051
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/bloc/nostr/nostr_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import 'dart:async';

import 'package:breez/bloc/async_actions_handler.dart';
import 'package:breez/bloc/nostr/nostr_actions.dart';
import 'package:breez/services/breezlib/breez_bridge.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:nostr_tools/nostr_tools.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../services/injector.dart';

class NostrBloc with AsyncActionsHandler {
BreezBridge _breezLib;
String nostrPublicKey;
String nostrPrivateKey;

FlutterSecureStorage _secureStorage;
SharedPreferences sharedPreferences;

NostrBloc() {
ServiceInjector injector = ServiceInjector();
_breezLib = injector.breezBridge;
_secureStorage = const FlutterSecureStorage();

initNostr();
Expand All @@ -29,10 +35,11 @@ class NostrBloc with AsyncActionsHandler {
}

void initNostr() async {
nostrPublicKey = await _secureStorage.read(key: 'nostrPublicKey');
nostrPrivateKey = await _secureStorage.read(key: 'nostrPrivateKey');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

nostrPublicKey = await _secureStorage.read(key: "nostrPublicKey");
nostrPrivateKey = await _secureStorage.read(key: "nostrPrivateKey");

if (nostrPublicKey == null) {
sharedPreferences.setBool('rememberGetPubKeyChoice', false);
sharedPreferences.setBool('rememberSignEventChoice', false);
Expand Down Expand Up @@ -87,19 +94,22 @@ class NostrBloc with AsyncActionsHandler {
// 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);
// check if key pair already exists otherwise generate it
String nostrKeyPair;

nostrKeyPair = await _breezLib.getNostrKeyPair().catchError((error) {
throw error.toString();
});

int index = nostrKeyPair.indexOf('_');
nostrPrivateKey = nostrKeyPair.substring(0, index);
nostrPublicKey = nostrKeyPair.substring(index + 1);

// Write value
await _secureStorage.write(
key: 'nostrPrivateKey', value: nostrPrivateKey);
await _secureStorage.write(key: 'nostrPublicKey', value: nostrPublicKey);

Future.delayed(const Duration(seconds: 1));
}

return nostrPublicKey;
Expand Down Expand Up @@ -137,25 +147,21 @@ class NostrBloc with AsyncActionsHandler {
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';
}

Expand Down

0 comments on commit 2888051

Please sign in to comment.