Skip to content

Commit

Permalink
New function TypeIDFromContext to get @id attribute from JSON-LD type (
Browse files Browse the repository at this point in the history
  • Loading branch information
olomix authored May 26, 2023
1 parent ad98530 commit e46585e
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
35 changes: 35 additions & 0 deletions merklize/merklize.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,41 @@ func NewFieldPathFromContext(ctxBytes []byte, ctxType, fieldPath string) (Path,
return resPath, nil
}

// TypeIDFromContext returns @id attribute for type from JSON-LD context
func TypeIDFromContext(ctxBytes []byte, typeName string) (string, error) {
var ctxObj map[string]interface{}
err := json.Unmarshal(ctxBytes, &ctxObj)
if err != nil {
return "", err
}

ldCtx, err := ld.NewContext(nil, nil).Parse(ctxObj["@context"])
if err != nil {
return "", err
}

typeDef := ldCtx.GetTermDefinition(typeName)

_, isType := typeDef["@context"]
if !isType {
return "", fmt.Errorf("looks like %v is not a type", typeName)
}

typeID, idFound := typeDef["@id"]
if !idFound {
return "", fmt.Errorf("@id attribute is not found for type %v",
typeName)
}

typeIDStr, ok := typeID.(string)
if !ok {
return "", fmt.Errorf("@id attribute is not a string for type %v",
typeName)
}

return typeIDStr, nil
}

// TypeFromContext returns type of field from context by path.
func TypeFromContext(ctxBytes []byte, path string) (string, error) {
var ctxObj map[string]interface{}
Expand Down
25 changes: 25 additions & 0 deletions merklize/merklize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,31 @@ func TestTypeFromContext(t *testing.T) {
require.Equal(t, "http://www.w3.org/2001/XMLSchema#integer", typ)
}

func TestTypeIDFromContext(t *testing.T) {
t.Run("url-like id", func(t *testing.T) {
ctxBytes, err := os.ReadFile("testdata/kyc_schema.json-ld")
require.NoError(t, err)

typeName := "KYCCountryOfResidenceCredential"
typeID, err := TypeIDFromContext(ctxBytes, typeName)
require.NoError(t, err)
require.Equal(t,
"https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-v4.json-ld#KYCCountryOfResidenceCredential",
typeID)
})

t.Run("urn-like id", func(t *testing.T) {
ctxBytes, err := os.ReadFile("testdata/kyc-v102.jsonld")
require.NoError(t, err)

typeName := "KYCCountryOfResidenceCredential"
typeID, err := TypeIDFromContext(ctxBytes, typeName)
require.NoError(t, err)
require.Equal(t, "urn:uuid:a81d4fae-7dec-11d0-a765-00a0c91e6bf0",
typeID)
})
}

func TestHashValues_FromDocument(t *testing.T) {
ctxBytes, err := os.ReadFile("testdata/kyc_schema.json-ld")
require.NoError(t, err)
Expand Down
79 changes: 79 additions & 0 deletions merklize/testdata/kyc-v102.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"@context": [
{
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"KYCAgeCredential": {
"@id": "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"kyc-vocab": "https://github.com/iden3/claim-schema-vocab/blob/main/credentials/kyc.md#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"birthday": {
"@id": "kyc-vocab:birthday",
"@type": "xsd:integer"
},
"documentType": {
"@id": "kyc-vocab:documentType",
"@type": "xsd:integer"
}
}
},
"KYCCountryOfResidenceCredential": {
"@id": "urn:uuid:a81d4fae-7dec-11d0-a765-00a0c91e6bf0",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"kyc-vocab": "https://github.com/iden3/claim-schema-vocab/blob/main/credentials/kyc.md#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"countryCode": {
"@id": "kyc-vocab:countryCode",
"@type": "xsd:integer"
},
"documentType": {
"@id": "kyc-vocab:documentType",
"@type": "xsd:integer"
}
}
},
"KYCEmployee": {
"@id": "urn:uuid:b71d4fae-7dec-11d0-a765-00a0c91e6bf1",
"@context": {
"@version": 1.1,
"@protected": true,
"id": "@id",
"type": "@type",
"kyc-vocab": "https://github.com/iden3/claim-schema-vocab/blob/main/credentials/kyc.md#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"documentType": {
"@id": "kyc-vocab:documentType",
"@type": "xsd:integer"
},
"ZKPexperiance": {
"@id": "kyc-vocab:hasZKPexperiance",
"@type": "xsd:boolean"
},
"hireDate": {
"@id": "kyc-vocab:hireDate",
"@type": "xsd:dateTime"
},
"position": {
"@id": "kyc-vocab:position",
"@type": "xsd:string"
},
"salary": {
"@id": "kyc-vocab:salary",
"@type": "xsd:double"
}
}
}
}
]
}

0 comments on commit e46585e

Please sign in to comment.