Skip to content

Commit ac3ca0c

Browse files
committed
improve transaction data processing
Signed-off-by: kenkosmowski <ken.kosmowski@gmx.de>
1 parent 21d986f commit ac3ca0c

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

src/WalletFramework.Oid4Vc/Oid4Vp/Models/PresentationCandidate.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ public PresentationCandidate(
4949

5050
public static class PresentationCandidateFun
5151
{
52-
public static PresentationCandidate AddTransactionData(
52+
public static PresentationCandidate AddTransactionDatas(
5353
this PresentationCandidate candidate,
54-
TransactionData transactionData)
54+
IEnumerable<TransactionData> transactionDatas)
5555
{
56-
var td = candidate.TransactionData.Match(
57-
list => list.Append(transactionData),
58-
() => [transactionData]);
59-
6056
return candidate with
6157
{
62-
TransactionData = td.ToList()
58+
TransactionData = transactionDatas.ToList()
6359
};
6460
}
6561

src/WalletFramework.Oid4Vc/Oid4Vp/Services/Oid4VpClientService.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -709,24 +709,28 @@ public async Task<Validation<AuthorizationRequestCancellation, PresentationReque
709709

710710
private static Validation<AuthorizationRequestCancellation, PresentationRequest> ProcessVpTransactionData(
711711
PresentationRequest presentationRequest,
712-
IEnumerable<TransactionData> transactionDatas)
712+
IEnumerable<TransactionData> vpTransactionDatas)
713713
{
714714
var result = presentationRequest.Candidates.Match(
715715
candidates =>
716716
{
717-
var candidatesValidation = transactionDatas.TraverseAll(transactionData =>
717+
var transactionDatas = vpTransactionDatas.ToList();
718+
candidates = candidates.Select(candidate =>
718719
{
719-
return candidates.FindCandidateForTransactionData(transactionData).Match(
720-
candidate => candidate.AddTransactionData(transactionData),
721-
() => (Validation<PresentationCandidate>)new InvalidTransactionDataError(
722-
$"No credentials found that satisfy the transaction data with type {transactionData.GetTransactionDataType().AsString()}",
723-
presentationRequest));
724-
});
725-
726-
return candidatesValidation.OnSuccess(enumerable => presentationRequest with
727-
{
728-
Candidates = enumerable.ToList()
729-
});
720+
return candidate.AddTransactionDatas(transactionDatas.Where(transactionData => transactionData
721+
.GetCredentialIds().Select(id => id.AsString)
722+
.Contains(candidate.Identifier)));
723+
}).ToList();
724+
725+
var satisfiedTransactionDatas = candidates.Where(x => x.TransactionData.IsSome)
726+
.SelectMany(x => x.TransactionData.UnwrapOrThrow());
727+
if (transactionDatas.Any(transactionData => !satisfiedTransactionDatas.Contains(transactionData)))
728+
return new InvalidTransactionDataError(
729+
"Not enough credentials found to satisfy the authorization request with transaction data",
730+
presentationRequest)
731+
.ToInvalid<PresentationRequest>();
732+
733+
return presentationRequest with { Candidates = candidates };
730734
},
731735
() => new InvalidTransactionDataError(
732736
"No credentials found that satisfy the authorization request with transaction data",

src/WalletFramework.Oid4Vc/Oid4Vp/TransactionDatas/TransactionData.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ public static IEnumerable<TransactionDataHashesAlg> GetHashesAlg(this Transactio
5252

5353
public static Base64UrlString GetEncoded(this TransactionData transactionData) =>
5454
transactionData.GetTransactionDataProperties().Encoded;
55-
56-
public static Option<PresentationCandidate> FindCandidateForTransactionData(
57-
this IEnumerable<PresentationCandidate> candidates,
58-
TransactionData transactionData) => candidates.FirstOrDefault(candidate =>
59-
transactionData.GetTransactionDataProperties().CredentialIds.Select(id => id.AsString)
60-
.Contains(candidate.Identifier));
55+
56+
public static IEnumerable<TransactionDataCredentialId> GetCredentialIds(this TransactionData transactionData) =>
57+
transactionData.GetTransactionDataProperties().CredentialIds;
6158

6259
private static TransactionDataProperties GetTransactionDataProperties(this TransactionData transactionData) =>
6360
transactionData.Match(

0 commit comments

Comments
 (0)