Skip to content

Commit

Permalink
feat: add VcsService (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Aug 16, 2024
1 parent aa683fd commit fb2aac3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
57 changes: 27 additions & 30 deletions lib/features/payment/payment_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:didpay/features/tbdex/tbdex_quote_notifier.dart';
import 'package:didpay/features/tbdex/tbdex_service.dart';
import 'package:didpay/features/transaction/transaction.dart';
import 'package:didpay/features/vcs/vcs_notifier.dart';
import 'package:didpay/features/vcs/vcs_service.dart';
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/error_message.dart';
import 'package:didpay/shared/header.dart';
Expand All @@ -23,7 +24,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:tbdex/tbdex.dart';
import 'package:web5/web5.dart';

class PaymentDetailsPage extends HookConsumerWidget {
final PaymentState paymentState;
Expand Down Expand Up @@ -151,11 +151,17 @@ class PaymentDetailsPage extends HookConsumerWidget {
.paymentAmountState?.selectedOffering?.data.requiredClaims;

if (presentationDefinition != null) {
final credentials = await _getRequiredCredentials(
context, ref, presentationDefinition,);
var credentials =
ref.read(vcsServiceProvider).getRequiredCredentials(
presentationDefinition,
ref.read(vcsProvider),
);

if (credentials == null) {
return;
final issuedCredential = await _startKccFlow(context);
if (issuedCredential == null) return;

credentials = issuedCredential;
}

state.value = state.value.copyWith(credentialsJwt: credentials);
Expand Down Expand Up @@ -256,6 +262,23 @@ class PaymentDetailsPage extends HookConsumerWidget {
);
}

Future<List<String>?> _startKccFlow(BuildContext context) async {
final credential = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ModalFlow(
initialWidget: KccConsentPage(
pfi: Pfi(
did: paymentState.paymentAmountState?.pfiDid ?? '',
),
),
),
fullscreenDialog: true,
),
);

return credential == null ? null : [credential as String];
}

Future<void> _sendRfq(
BuildContext context,
WidgetRef ref,
Expand Down Expand Up @@ -287,30 +310,4 @@ class PaymentDetailsPage extends HookConsumerWidget {
rfq.value = AsyncError(e, StackTrace.current);
}
}

Future<List<String>?> _getRequiredCredentials(
BuildContext context,
WidgetRef ref,
PresentationDefinition presentationDefinition,
) async {
final credentials =
presentationDefinition.selectCredentials(ref.read(vcsProvider));

if (credentials.isNotEmpty) {
return credentials;
}

final issuedCredential = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ModalFlow(
initialWidget: KccConsentPage(
pfi: Pfi(did: paymentState.paymentAmountState?.pfiDid ?? ''),
),
),
fullscreenDialog: true,
),
);

return issuedCredential == null ? null : [issuedCredential as String];
}
}
15 changes: 15 additions & 0 deletions lib/features/vcs/vcs_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:web5/web5.dart';

final vcsServiceProvider = Provider((_) => VcsService());

class VcsService {
List<String>? getRequiredCredentials(
PresentationDefinition presentationDefinition,
List<String> vcJwts,
) {
final credentials = presentationDefinition.selectCredentials(vcJwts);

return credentials.isEmpty ? null : credentials;
}
}

0 comments on commit fb2aac3

Please sign in to comment.