From 28880511608771cd151e645e34dcb75c3f77aa01 Mon Sep 17 00:00:00 2001 From: Sarthak Gupta Date: Sun, 9 Jul 2023 20:37:11 +0530 Subject: [PATCH] adding key generation to golang side --- lib/bloc/nostr/nostr_bloc.dart | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/bloc/nostr/nostr_bloc.dart b/lib/bloc/nostr/nostr_bloc.dart index d283cbe74..f27c8963a 100644 --- a/lib/bloc/nostr/nostr_bloc.dart +++ b/lib/bloc/nostr/nostr_bloc.dart @@ -2,11 +2,15 @@ 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; @@ -14,6 +18,8 @@ class NostrBloc with AsyncActionsHandler { SharedPreferences sharedPreferences; NostrBloc() { + ServiceInjector injector = ServiceInjector(); + _breezLib = injector.breezBridge; _secureStorage = const FlutterSecureStorage(); initNostr(); @@ -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); @@ -87,19 +94,22 @@ class NostrBloc with AsyncActionsHandler { // Methods to simulate the actual logic Future _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; @@ -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> _fetchRelays() async { - await Future.delayed(const Duration(seconds: 1)); return ['Relay1', 'Relay2', 'Relay3']; } Future _encryptData(String data, String publicKey) async { // Simulating an encryption operation - await Future.delayed(const Duration(seconds: 1)); return 'EncryptedData'; } Future _decryptData(String encryptedData, String privateKey) async { // Simulating a decryption operation - await Future.delayed(const Duration(seconds: 1)); return 'DecryptedData'; }