Skip to content

Commit 8fbf7bc

Browse files
committed
fix credential sets
Signed-off-by: Kevin <kevin.dinh@lissi.id>
1 parent fbd905c commit 8fbf7bc

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

src/WalletFramework.Oid4Vc/Oid4Vp/Dcql/DcqlFun.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using LanguageExt;
2-
using WalletFramework.Core.Functional;
32
using WalletFramework.Oid4Vc.Oid4Vp.Dcql.Models;
43
using WalletFramework.Oid4Vc.Oid4Vp.Models;
54
using WalletFramework.Core.Credentials.Abstractions;
@@ -52,15 +51,26 @@ private static CandidateQueryResult BuildPresentationCandidateSets(
5251
IReadOnlyList<PresentationCandidate> candidates,
5352
List<CredentialRequirement> missing)
5453
{
55-
var matchingSets =
56-
from setQuery in credentialSetQueries
57-
from option in setQuery.Options
58-
let matchingIds = option.Ids.Select(id => id.AsString())
59-
let setCandidates = candidates.Where(c => matchingIds.Contains(c.Identifier)).ToList()
60-
where setCandidates.Count == option.Ids.Count
61-
select new PresentationCandidateSet(setCandidates, setQuery.Required);
54+
var sets = new List<PresentationCandidateSet>();
55+
foreach (var setQuery in credentialSetQueries)
56+
{
57+
var firstMatchingOption = setQuery.Options
58+
.Select(option =>
59+
{
60+
return (
61+
Option: option,
62+
SetCandidates: candidates
63+
.Where(c => option.Ids.Select(id => id.AsString()).Contains(c.Identifier))
64+
.ToList()
65+
);
66+
})
67+
.FirstOrDefault(x => x.SetCandidates.Count == x.Option.Ids.Count);
6268

63-
var sets = matchingSets.ToList();
69+
if (firstMatchingOption != default)
70+
{
71+
sets.Add(new PresentationCandidateSet(firstMatchingOption.SetCandidates, setQuery.Required));
72+
}
73+
}
6474
return new CandidateQueryResult(
6575
sets.Count > 0 ? sets : Option<List<PresentationCandidateSet>>.None,
6676
missing.Count > 0 ? missing : Option<List<CredentialRequirement>>.None

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ internal static Validation<AuthorizationRequestCancellation, PresentationRequest
110110
// Flatten to (setIndex, candidateIndex, candidate)
111111
var indexedCandidates = candidateSets
112112
.SelectMany((set, setIdx) =>
113-
set.Candidates.Select((candidate, candIdx) => (setIdx, candIdx, candidate)))
113+
{
114+
return set.Candidates.Select((candidate, candIdx) => (setIdx, candIdx, candidate));
115+
})
114116
.ToList();
115117

116118
var updatedCandidates = indexedCandidates.ToDictionary(

test/WalletFramework.Oid4Vc.Tests/Oid4Vp/Dcql/CredentialSets/CredentialSetTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,21 @@ public void Candidate_Query_Result_Can_Be_Built_Correctly_From_Dcql_With_One_Cre
6464
candidatesList.Count.Should().Be(2, "should have a candidate for each credential query in the set");
6565
candidateSet.IsRequired.Should().BeTrue();
6666
}
67+
68+
[Fact]
69+
public void Candidate_Set_Wont_Be_Built_When_A_Credential_is_Missing()
70+
{
71+
// Arrange
72+
var query = DcqlSamples.GetDcqlQueryWithOneCredentialSetAndMultipleOptions;
73+
var idCard = SdJwtSamples.GetIdCardCredential();
74+
// idCard2 is missing
75+
var idCard3 = SdJwtSamples.GetIdCard3Credential();
76+
var credentials = new List<ICredential> { idCard, idCard3 };
77+
78+
// Act
79+
var result = query.ProcessWith(credentials);
80+
81+
// Assert
82+
result.Candidates.IsNone.Should().BeTrue("no set should be satisfied if a required credential is missing");
83+
}
6784
}

test/WalletFramework.Oid4Vc.Tests/Oid4Vp/Dcql/Samples/DcqlSamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public static class DcqlSamples
100100
""credential_sets"": [
101101
{
102102
""options"": [
103-
[ ""idcard"", ""idcard2"" ],
104-
[ ""idcard2"", ""idcard3"" ]
103+
[ ""idcard"", ""idcard2""],
104+
[ ""idcard2""]
105105
]
106106
}
107107
]

0 commit comments

Comments
 (0)