11using LanguageExt ;
2+ using Newtonsoft . Json . Linq ;
3+ using WalletFramework . Core . ClaimPaths ;
24using WalletFramework . Core . Functional ;
35using WalletFramework . SdJwtLib . Models ;
46using WalletFramework . SdJwtLib . Roles ;
7+ using PathSet = System . Collections . Generic . HashSet < string > ;
58
69namespace WalletFramework . SdJwtVc . Services . SdJwtVcHolderService ;
710
@@ -11,24 +14,23 @@ public class SdJwtVcHolderService(IHolder holder, ISdJwtSigner signer) : ISdJwtV
1114 /// <inheritdoc />
1215 public async Task < string > CreatePresentation (
1316 SdJwtCredential credential ,
14- string [ ] disclosedClaimPaths ,
17+ ClaimPath [ ] disclosedClaimPaths ,
1518 Option < IEnumerable < string > > transactionDataHashes ,
1619 Option < string > transactionDataHashesAlg ,
1720 string ? audience = null ,
1821 string ? nonce = null )
1922 {
2023 var sdJwtDoc = credential . ToSdJwtDoc ( ) ;
21- var disclosures = new List < Disclosure > ( ) ;
22- foreach ( var disclosure in sdJwtDoc . Disclosures )
23- {
24- if ( disclosedClaimPaths . Any ( disclosedClaim => disclosedClaim . StartsWith ( disclosure . Path ?? string . Empty ) ) )
25- {
26- disclosures . Add ( disclosure ) ;
27- }
28- }
24+
25+ var requiredPaths = CollectRequiredDisclosurePaths ( sdJwtDoc . UnsecuredPayload , disclosedClaimPaths ) ;
26+
27+ var disclosures = sdJwtDoc
28+ . Disclosures
29+ . Where ( disclosure => ! string . IsNullOrEmpty ( disclosure . Path ) && requiredPaths . Contains ( disclosure . Path ! ) )
30+ . ToArray ( ) ;
2931
3032 var presentationFormat =
31- holder . CreatePresentationFormat ( credential . EncodedIssuerSignedJwt , disclosures . ToArray ( ) ) ;
33+ holder . CreatePresentationFormat ( credential . EncodedIssuerSignedJwt , disclosures ) ;
3234
3335 if ( credential . KeyId . IsSome
3436 && ! string . IsNullOrEmpty ( nonce )
@@ -49,4 +51,36 @@ public async Task<string> CreatePresentation(
4951
5052 return presentationFormat . Value ;
5153 }
54+
55+ private static PathSet CollectRequiredDisclosurePaths (
56+ JObject payload ,
57+ IEnumerable < ClaimPath > paths )
58+ {
59+ var result = new PathSet ( ) ;
60+
61+ foreach ( var path in paths )
62+ {
63+ path
64+ . ProcessWith ( payload )
65+ . OnSuccess ( selection =>
66+ {
67+ foreach ( var token in selection . GetValues ( ) )
68+ foreach ( var ancestorPath in AncestorPaths ( token ) )
69+ result . Add ( ancestorPath ) ;
70+
71+ return Unit . Default ;
72+ } ) ;
73+ }
74+
75+ return result ;
76+ }
77+
78+ private static IEnumerable < string > AncestorPaths ( JToken token )
79+ {
80+ for ( JToken ? current = token ; current is not null ; current = current . Parent )
81+ {
82+ if ( ! string . IsNullOrEmpty ( current . Path ) )
83+ yield return current . Path ;
84+ }
85+ }
5286}
0 commit comments