Skip to content

Commit

Permalink
feat: regenerate did (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Jul 25, 2024
1 parent 9ba84cf commit 2dcb1f2
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 32 deletions.
3 changes: 2 additions & 1 deletion lib/features/did/did_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:web5/web5.dart';

final didProvider = Provider<BearerDid>((ref) => throw UnimplementedError());
final didProvider =
StateProvider<BearerDid>((ref) => throw UnimplementedError());
59 changes: 49 additions & 10 deletions lib/features/did/did_qr_code_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import 'dart:math';

import 'package:auto_size_text/auto_size_text.dart';
import 'package:didpay/features/did/did_provider.dart';
import 'package:didpay/features/did/did_storage_service.dart';
import 'package:didpay/features/vcs/vcs_notifier.dart';
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/confirm_dialog.dart';
import 'package:didpay/shared/theme/grid.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -14,6 +18,7 @@ class DidQrCodePage extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final did = ref.watch(didProvider);
final didStorageService = ref.watch(didServiceProvider);

const maxSize = 400.0;
final screenSize = MediaQuery.of(context).size;
Expand Down Expand Up @@ -51,16 +56,50 @@ class DidQrCodePage extends HookConsumerWidget {
left: (screenSize.width - qrSize) / 2,
top: offsetY + qrSize + Grid.sm,
width: qrSize,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: Grid.sm),
child: AutoSizeText(
'Scan to pay $dap',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.bold,
),
maxLines: 2,
textAlign: TextAlign.center,
),
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: Grid.sm),
child: AutoSizeText(
did.uri,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.bold,
),
maxLines: 2,
textAlign: TextAlign.center,
),
),
const SizedBox(height: Grid.sm),
FilledButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => ConfirmDialog(
title: Loc.of(context).areYouSure,
description: Loc.of(context)
.allOfYourCredentialsWillAlsoBeDeleted,
confirmText: Loc.of(context).regenerate,
cancelText: Loc.of(context).goBack,
onConfirm: () async {
final newDid =
await didStorageService.regenerateDid();
ref.read(didProvider.notifier).state = newDid;

await ref.read(vcsProvider.notifier).reset();

if (context.mounted) {
Navigator.of(context).pop();
}
},
onCancel: () async {
Navigator.of(context).pop();
},
),
);
},
child: const Text('Regenerate DID'),
),
],
),
),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/features/did/did_qr_tabs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DidQrTabs extends HookWidget {
Theme.of(context).colorScheme.onBackground,
tabs: [
Tab(text: Loc.of(context).scan),
Tab(text: Loc.of(context).myDap),
Tab(text: Loc.of(context).myDid),
],
),
),
Expand Down
18 changes: 18 additions & 0 deletions lib/features/did/did_storage_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import 'dart:convert';

import 'package:didpay/shared/constants.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:web5/web5.dart';

final didServiceProvider =
Provider<DidStorageService>((ref) => throw UnimplementedError());

class DidStorageService {
final FlutterSecureStorage storage;

Expand All @@ -29,4 +33,18 @@ class DidStorageService {
);
return did;
}

Future<BearerDid> regenerateDid() async {
await storage.delete(key: Constants.portableDidKey);

final newDid = await DidDht.create(publish: true);
final portableDid = await newDid.export();
final portableDidJson = jsonEncode(portableDid.map);

await storage.write(
key: Constants.portableDidKey,
value: portableDidJson,
);
return newDid;
}
}
5 changes: 5 additions & 0 deletions lib/features/vcs/vcs_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class VcsNotifier extends StateNotifier<List<String>> {
await _save();
}

Future<void> reset() async {
state = [];
await box.clear();
}

Future<void> _save() async {
await box.put(storageKey, state);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"noDidQrCodeFound": "No DID QR Code found",
"noDapQrCodeFound": "No DAP QR Code found",
"myDap": "My DAP",
"myDid": "My DID",
"myVc": "My VC",
"vcNotFound": "VC not found",
"copiedDid": "Copied DID!",
Expand Down Expand Up @@ -166,5 +167,9 @@
"ifYouExitNow": "If you exit now, you'll lose all your progress",
"enterADap": "Enter a Decentralized Agnostic Paytag (DAP)",
"verifyingDap": "Verifying DAP...",
"placeholderDap": "@username/didpay.me"
"placeholderDap": "@username/didpay.me",
"areYouSure": "Are you sure?",
"allOfYourCredentialsWillAlsoBeDeleted": "All of your credentials will also be deleted",
"regenerate": "Regenerate",
"goBack": "Go back"
}
30 changes: 30 additions & 0 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ abstract class Loc {
/// **'My DAP'**
String get myDap;

/// No description provided for @myDid.
///
/// In en, this message translates to:
/// **'My DID'**
String get myDid;

/// No description provided for @myVc.
///
/// In en, this message translates to:
Expand Down Expand Up @@ -864,6 +870,30 @@ abstract class Loc {
/// In en, this message translates to:
/// **'@username/didpay.me'**
String get placeholderDap;

/// No description provided for @areYouSure.
///
/// In en, this message translates to:
/// **'Are you sure?'**
String get areYouSure;

/// No description provided for @allOfYourCredentialsWillAlsoBeDeleted.
///
/// In en, this message translates to:
/// **'All of your credentials will also be deleted'**
String get allOfYourCredentialsWillAlsoBeDeleted;

/// No description provided for @regenerate.
///
/// In en, this message translates to:
/// **'Regenerate'**
String get regenerate;

/// No description provided for @goBack.
///
/// In en, this message translates to:
/// **'Go back'**
String get goBack;
}

class _LocDelegate extends LocalizationsDelegate<Loc> {
Expand Down
15 changes: 15 additions & 0 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class LocEn extends Loc {
@override
String get myDap => 'My DAP';

@override
String get myDid => 'My DID';

@override
String get myVc => 'My VC';

Expand Down Expand Up @@ -400,4 +403,16 @@ class LocEn extends Loc {

@override
String get placeholderDap => '@username/didpay.me';

@override
String get areYouSure => 'Are you sure?';

@override
String get allOfYourCredentialsWillAlsoBeDeleted => 'All of your credentials will also be deleted';

@override
String get regenerate => 'Regenerate';

@override
String get goBack => 'Go back';
}
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void main() async {
overrides: [
sharedPreferencesProvider.overrideWithValue(sharedPreferences),
secureStorageProvider.overrideWithValue(secureStorage),
didProvider.overrideWithValue(did),
didServiceProvider.overrideWithValue(didService),
didProvider.overrideWith((ref) => did),
...overrides,
],
child: const App(),
Expand Down
25 changes: 14 additions & 11 deletions lib/shared/exit_dialog.dart → lib/shared/confirm_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/theme/grid.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

class ExitDialog extends HookWidget {
class ConfirmDialog extends HookWidget {
final String title;
final String description;
final Future<void> Function() onExit;
final Future<void> Function() onStay;
final String confirmText;
final String cancelText;
final Future<void> Function() onConfirm;
final Future<void> Function() onCancel;

const ExitDialog({
const ConfirmDialog({
required this.title,
required this.description,
required this.onExit,
required this.onStay,
required this.confirmText,
required this.cancelText,
required this.onConfirm,
required this.onCancel,
super.key,
});

Expand Down Expand Up @@ -52,7 +55,7 @@ class ExitDialog extends HookWidget {
),
),
),
onPressed: () async => onStay(),
onPressed: () async => onCancel(),
child: Container(
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(vertical: Grid.sm),
Expand All @@ -67,7 +70,7 @@ class ExitDialog extends HookWidget {
),
),
child: Text(
Loc.of(context).stay,
cancelText,
style: TextStyle(
color: Theme.of(context).colorScheme.outline,
),
Expand All @@ -85,12 +88,12 @@ class ExitDialog extends HookWidget {
),
),
),
onPressed: () async => onExit(),
onPressed: () async => onConfirm(),
child: Container(
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(vertical: Grid.sm),
child: Text(
Loc.of(context).exit,
confirmText,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
Expand Down
2 changes: 1 addition & 1 deletion test/features/app/app_tabs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void main() async {
Widget appTabsTestWidget() => WidgetHelpers.testableWidget(
child: const AppTabs(),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
tbdexServiceProvider.overrideWith((ref) => mockTbdexService),
pfisProvider.overrideWith((ref) => mockPfisNotifier),
vcsProvider.overrideWith((ref) => mockVcsNotifier),
Expand Down
2 changes: 1 addition & 1 deletion test/features/app/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main() async {
WidgetHelpers.testableWidget(
child: const AppTabs(),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
tbdexServiceProvider.overrideWith((ref) => mockTbdexService),
pfisProvider.overrideWith((ref) => mockPfisNotifier),
vcsProvider.overrideWith((ref) => mockVcsNotifier),
Expand Down
2 changes: 1 addition & 1 deletion test/features/home/home_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() async {
Widget homePageTestWidget() => WidgetHelpers.testableWidget(
child: const HomePage(),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
tbdexServiceProvider.overrideWith((ref) => mockTbdexService),
transactionProvider.overrideWith(MockTransactionNotifier.new),
accountBalanceProvider
Expand Down
2 changes: 1 addition & 1 deletion test/features/payment/payment_amount_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() async {
PaymentState(transactionType: TransactionType.deposit),
),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
tbdexServiceProvider.overrideWith((ref) => mockTbdexService),
pfisProvider.overrideWith((ref) => mockPfisNotifier),
],
Expand Down
2 changes: 1 addition & 1 deletion test/features/payment/payment_details_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void main() async {
),
),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
pfisProvider.overrideWith((ref) => mockPfisNotifier),
],
);
Expand Down
2 changes: 1 addition & 1 deletion test/features/payment/payment_review_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void main() async {
),
),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
quoteProvider.overrideWith(() => mockTbdexQuoteNotifier),
tbdexServiceProvider.overrideWith((ref) => mockTbdexService),
],
Expand Down
2 changes: 1 addition & 1 deletion test/features/send/send_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() async {
Widget sendDetailsPageTestWidget() => WidgetHelpers.testableWidget(
child: const SendPage(),
overrides: [
didProvider.overrideWithValue(did),
didProvider.overrideWith((ref) => did),
featureFlagsProvider
.overrideWith((ref) => mockFeatureFlagsNotifier),
],
Expand Down

0 comments on commit 2dcb1f2

Please sign in to comment.