Skip to content

Commit

Permalink
Add fragment validation & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Oct 11, 2024
1 parent 2c0371f commit bdec2ab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion x/did/types/diddoc_diddoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (didDoc DidDoc) Validate(allowedNamespaces []string) error {
IsUniqueStrList(), validation.Each(IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(didDoc.Id)),
),
validation.Field(&didDoc.AssertionMethod,
IsUniqueStrList(), validation.Each(IsAssertionMethod(allowedNamespaces, didDoc.Id))),
IsUniqueStrList(), validation.Each(IsAssertionMethod(allowedNamespaces, didDoc))),
validation.Field(&didDoc.CapabilityInvocation,
IsUniqueStrList(), validation.Each(IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(didDoc.Id)),
),
Expand Down
32 changes: 32 additions & 0 deletions x/did/types/diddoc_diddoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,36 @@ var _ = DescribeTable("DIDDoc Validation tests", func(testCase DIDDocTestCase) {
isValid: true,
errorMsg: "",
}),
Entry(
"Assertion method is has wrong fragment",
DIDDocTestCase{
didDoc: &DidDoc{
Id: ValidTestDID,
Controller: []string{ValidTestDID},
VerificationMethod: []*VerificationMethod{
{
Id: fmt.Sprintf("%s#fragment", ValidTestDID),
VerificationMethodType: "Ed25519VerificationKey2020",
Controller: ValidTestDID,
VerificationMaterial: ValidEd25519VerificationKey2020VerificationMaterial,
},
},
AssertionMethod: []string{fmt.Sprintf("%s#fragment", ValidTestDID), func() string {
b, _ := json.Marshal(struct {
Id string
Type string
Controller string
PublicKeyBase58 string
}{
Id: fmt.Sprintf("%s#fragment-1", ValidTestDID),
Type: "Ed25519VerificationKey2020",
Controller: ValidTestDID,
PublicKeyBase58: "base58",
})
return strconv.Quote(string(b))
}()},
},
isValid: false,
errorMsg: "assertionMethod should be a valid key reference within the DID document's verification method",
}),
)
24 changes: 15 additions & 9 deletions x/did/types/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ type AssertionMethod struct {
Controller string
}

func IsAssertionMethod(allowedNamespaces []string, prefix string) *CustomErrorRule {
func IsAssertionMethod(allowedNamespaces []string, didDoc DidDoc) *CustomErrorRule {
return NewCustomErrorRule(func(value interface{}) error {
err := validation.Validate(value, IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(prefix))
if err != nil {
casted, ok := value.(string)
if !ok {
panic("IsAssertionMethod must be only applied on string properties")
}
err := validation.Validate(value, IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(didDoc.Id))
casted, ok := value.(string)
if !ok {
panic("IsAssertionMethod must be only applied on string properties")
}

if err != nil {
unescapedJSON, err := strconv.Unquote(casted)
if err != nil {
return errors.New("assertionMethod should be a DIDUrl or an Escaped JSON string")
Expand All @@ -134,13 +134,19 @@ func IsAssertionMethod(allowedNamespaces []string, prefix string) *CustomErrorRu
}

return validation.ValidateStruct(&result,
validation.Field(&result.Id, validation.Required, IsDIDUrl(allowedNamespaces, Empty, Empty, Required), HasPrefix(prefix)),
validation.Field(&result.Id, validation.Required, IsAssertionMethod(allowedNamespaces, didDoc)),
validation.Field(&result.Controller, validation.Required, IsDID(allowedNamespaces)),
validation.Field(&result.Type, IsURI()),
)
}

return nil
for _, v := range didDoc.VerificationMethod {
if v.Id == casted {
return nil
}
}

return errors.New("assertionMethod should be a valid key reference within the DID document's verification method")
})
}

Expand Down

0 comments on commit bdec2ab

Please sign in to comment.