# IssuerRegisteredIdMatch — issuer registeredId matches the DIA pair.

@prefix sh:     <http://www.w3.org/ns/shacl#> .
@prefix rdfs:   <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:    <http://www.w3.org/2002/07/owl#> .
@prefix xsd:    <http://www.w3.org/2001/XMLSchema#> .
@prefix cred:   <https://www.w3.org/2018/credentials#> .
@prefix unece:  <https://test.uncefact.org/vocabulary/> .
@prefix gv:     <https://vocabulary.uncefact.org/untp/shapes/> .

gv:IssuerRegisteredIdMatch
    a owl:Ontology, sh:NodeShape ;
    owl:versionInfo "0.8.0" ;
    rdfs:label "Issuer registeredId matches DIA"@en ;
    rdfs:comment "If a credential issuer states a registeredId under an identifier scheme, a Digital Identity Anchor for that issuer DID must carry the same (idScheme, registeredId) pair. Otherwise the DID may verify while the stated company number is not the one on the register."@en ;
    rdfs:seeAlso <https://untp.unece.org/docs/specification/DigitalIdentityAnchor> ;
    sh:declare
        [ sh:prefix "cred" ; sh:namespace "https://www.w3.org/2018/credentials#"^^xsd:anyURI ] ,
        [ sh:prefix "unece" ; sh:namespace "https://test.uncefact.org/vocabulary/"^^xsd:anyURI ] ;
    sh:targetSubjectsOf cred:issuer ;
    sh:sparql [
        a sh:SPARQLConstraint ;
        sh:message "This issuer's stated registeredId does not match the Digital Identity Anchor for that DID and identifier scheme."@en ;
        sh:prefixes gv:IssuerRegisteredIdMatch ;
        sh:select """
            PREFIX cred:  <https://www.w3.org/2018/credentials#>
            PREFIX unece: <https://test.uncefact.org/vocabulary/>
            SELECT $this ?issuer ?scheme ?regId
            WHERE {
              $this cred:issuer ?issuer .
              ?issuer unece:alsoKnownAs ?aka .
              ?aka unece:idScheme ?scheme ;
                   unece:registeredId ?regId .
              ?issuer a unece:RegisteredIdentity .
              FILTER NOT EXISTS {
                ?issuer unece:idScheme ?scheme ;
                        unece:registeredId ?regId .
              }
            }
        """ ;
    ] .
