Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use the protobuf oneof keyword for VerificationRelationship #169

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions proto/panacea/did/v2/did.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ message VerificationMethod {

// VerificationRelationship defines a W3C verification relationship
message VerificationRelationship {
string verificationMethodID = 1;
VerificationMethod dedicatedVerificationMethod = 2;
oneof content {
string verification_method_id = 1;
VerificationMethod verification_method = 2;
}
}

// Service defines a service in the W3C DID Document.
Expand Down
81 changes: 38 additions & 43 deletions x/did/client/cli/txDID_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (suite txTestSuite) TestReadDIDDocOneContext() {
suite.Require().Equal("hfiFwEqzHPx3RbQBmkgg4UEMtejfbL27CspYNKiVuURN", doc.VerificationMethods[0].PubKeyBase58)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.VerificationMethods[0].Type)
suite.Require().Equal(1, len(doc.Authentications))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.Authentications[0].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.Authentications[0].GetVerificationMethodId())
}

func (suite txTestSuite) TestReadDIDDocTwoContexts() {
Expand All @@ -148,7 +148,7 @@ func (suite txTestSuite) TestReadDIDDocTwoContexts() {
suite.Require().Equal("hfiFwEqzHPx3RbQBmkgg4UEMtejfbL27CspYNKiVuURN", doc.VerificationMethods[0].PubKeyBase58)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.VerificationMethods[0].Type)
suite.Require().Equal(1, len(doc.Authentications))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.Authentications[0].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.Authentications[0].GetVerificationMethodId())
}

func (suite txTestSuite) TestReadDIDDocMultiRelationship() {
Expand All @@ -167,53 +167,48 @@ func (suite txTestSuite) TestReadDIDDocMultiRelationship() {
suite.Require().Equal("Secp256k1VerificationKey2018", doc.VerificationMethods[0].Type)
suite.Require().Equal(2, len(doc.Authentications))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.VerificationMethods[0].Id)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.Authentications[0].VerificationMethodID)
suite.Require().Nil(doc.Authentications[0].DedicatedVerificationMethod)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.Authentications[1].DedicatedVerificationMethod.Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.Authentications[1].DedicatedVerificationMethod.Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.Authentications[1].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.Authentications[1].DedicatedVerificationMethod.Id)
suite.Require().Equal("zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV", doc.Authentications[1].DedicatedVerificationMethod.PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.Authentications[0].GetVerificationMethodId())
suite.Require().Nil(doc.Authentications[0].GetVerificationMethod())
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.Authentications[1].GetVerificationMethod().Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.Authentications[1].GetVerificationMethod().Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.Authentications[1].GetVerificationMethod().Id)
suite.Require().Equal("zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV", doc.Authentications[1].GetVerificationMethod().PubKeyBase58)

suite.Require().Equal(3, len(doc.AssertionMethods))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.AssertionMethods[0].VerificationMethodID)
suite.Require().Nil(doc.AssertionMethods[0].DedicatedVerificationMethod)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.AssertionMethods[1].DedicatedVerificationMethod.Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.AssertionMethods[1].DedicatedVerificationMethod.Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.AssertionMethods[1].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.AssertionMethods[1].DedicatedVerificationMethod.Id)
suite.Require().Equal("aH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPs", doc.AssertionMethods[1].DedicatedVerificationMethod.PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.AssertionMethods[2].DedicatedVerificationMethod.Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.AssertionMethods[2].DedicatedVerificationMethod.Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key3", doc.AssertionMethods[2].DedicatedVerificationMethod.Id)
suite.Require().Equal("bH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPo", doc.AssertionMethods[2].DedicatedVerificationMethod.PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.AssertionMethods[0].GetVerificationMethodId())
suite.Require().Nil(doc.AssertionMethods[0].GetVerificationMethod())
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.AssertionMethods[1].GetVerificationMethod().Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.AssertionMethods[1].GetVerificationMethod().Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.AssertionMethods[1].GetVerificationMethod().Id)
suite.Require().Equal("aH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPs", doc.AssertionMethods[1].GetVerificationMethod().PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.AssertionMethods[2].GetVerificationMethod().Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.AssertionMethods[2].GetVerificationMethod().Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key3", doc.AssertionMethods[2].GetVerificationMethod().Id)
suite.Require().Equal("bH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPo", doc.AssertionMethods[2].GetVerificationMethod().PubKeyBase58)

suite.Require().Equal(3, len(doc.KeyAgreements))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.KeyAgreements[0].VerificationMethodID)
suite.Require().Nil(doc.KeyAgreements[0].DedicatedVerificationMethod)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.KeyAgreements[1].DedicatedVerificationMethod.Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.KeyAgreements[1].DedicatedVerificationMethod.Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.KeyAgreements[1].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.KeyAgreements[1].DedicatedVerificationMethod.Id)
suite.Require().Equal("oH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPP", doc.KeyAgreements[1].DedicatedVerificationMethod.PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key3", doc.KeyAgreements[2].VerificationMethodID)
suite.Require().Nil(doc.KeyAgreements[2].DedicatedVerificationMethod)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.KeyAgreements[0].GetVerificationMethodId())
suite.Require().Nil(doc.KeyAgreements[0].GetVerificationMethod())
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.KeyAgreements[1].GetVerificationMethod().Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.KeyAgreements[1].GetVerificationMethod().Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.KeyAgreements[1].GetVerificationMethod().Id)
suite.Require().Equal("oH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPP", doc.KeyAgreements[1].GetVerificationMethod().PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key3", doc.KeyAgreements[2].GetVerificationMethodId())
suite.Require().Nil(doc.KeyAgreements[2].GetVerificationMethod())

suite.Require().Equal(2, len(doc.CapabilityInvocations))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.CapabilityInvocations[0].VerificationMethodID)
suite.Require().Nil(doc.CapabilityInvocations[0].DedicatedVerificationMethod)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.CapabilityInvocations[1].DedicatedVerificationMethod.Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.CapabilityInvocations[1].DedicatedVerificationMethod.Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.CapabilityInvocations[1].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.CapabilityInvocations[1].DedicatedVerificationMethod.Id)
suite.Require().Equal("PH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPp", doc.CapabilityInvocations[1].DedicatedVerificationMethod.PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.CapabilityInvocations[0].GetVerificationMethodId())
suite.Require().Nil(doc.CapabilityInvocations[0].GetVerificationMethod())
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.CapabilityInvocations[1].GetVerificationMethod().Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.CapabilityInvocations[1].GetVerificationMethod().Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.CapabilityInvocations[1].GetVerificationMethod().Id)
suite.Require().Equal("PH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPp", doc.CapabilityInvocations[1].GetVerificationMethod().PubKeyBase58)

suite.Require().Equal(2, len(doc.CapabilityDelegations))
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.CapabilityDelegations[0].VerificationMethodID)
suite.Require().Nil(doc.CapabilityDelegations[0].DedicatedVerificationMethod)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.CapabilityDelegations[1].DedicatedVerificationMethod.Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.CapabilityDelegations[1].DedicatedVerificationMethod.Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.CapabilityDelegations[1].VerificationMethodID)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.CapabilityDelegations[1].DedicatedVerificationMethod.Id)
suite.Require().Equal("qH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPQ", doc.CapabilityDelegations[1].DedicatedVerificationMethod.PubKeyBase58)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key1", doc.CapabilityDelegations[0].GetVerificationMethodId())
suite.Require().Nil(doc.CapabilityDelegations[0].GetVerificationMethod())
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ", doc.CapabilityDelegations[1].GetVerificationMethod().Controller)
suite.Require().Equal("Secp256k1VerificationKey2018", doc.CapabilityDelegations[1].GetVerificationMethod().Type)
suite.Require().Equal("did:panacea:27FnaDeQZApXhsRZZDARhWYs2nKFaw3p7evGd9zUSrBZ#key2", doc.CapabilityDelegations[1].GetVerificationMethod().Id)
suite.Require().Equal("qH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPQ", doc.CapabilityDelegations[1].GetVerificationMethod().PubKeyBase58)
}
47 changes: 25 additions & 22 deletions x/did/types/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (doc DIDDocument) validVerificationRelationships(relationships []Verificati
if !relationship.hasDedicatedMethod() {
// if the relationship isn't a dedicated verification method,
// the referenced verification method must be presented in the 'verificationMethod' property.
if _, ok := doc.VerificationMethodByID(relationship.VerificationMethodID); !ok {
if _, ok := doc.VerificationMethodByID(relationship.GetVerificationMethodId()); !ok {
youngjoon-lee marked this conversation as resolved.
Show resolved Hide resolved
return false
}
}
Expand Down Expand Up @@ -313,11 +313,15 @@ func (doc DIDDocument) VerificationMethodByID(id string) (VerificationMethod, bo
// If the relationship has only a ID of VerificationMethod, this function tries to find a corresponding VerificationMethod in the DIDDocument.
func (doc DIDDocument) VerificationMethodFrom(relationships []VerificationRelationship, id string) (VerificationMethod, bool) {
for _, relationship := range relationships {
if relationship.VerificationMethodID == id {
if relationship.hasDedicatedMethod() {
return *relationship.DedicatedVerificationMethod, true
} else {
return doc.VerificationMethodByID(id)
if relationship.hasDedicatedMethod() {
veriMethod := relationship.GetVerificationMethod()
if veriMethod.Id == id {
return *veriMethod, true
}
} else {
veriMethodID := relationship.GetVerificationMethodId()
if veriMethodID == id {
return doc.VerificationMethodByID(veriMethodID)
}
}
}
Expand Down Expand Up @@ -420,36 +424,35 @@ func (pk VerificationMethod) Valid(did string) bool {
}

func NewVerificationRelationship(verificationMethodID string) VerificationRelationship {
return VerificationRelationship{VerificationMethodID: verificationMethodID, DedicatedVerificationMethod: nil}
return VerificationRelationship{
Content: &VerificationRelationship_VerificationMethodId{VerificationMethodId: verificationMethodID},
}
}

func NewVerificationRelationshipDedicated(verificationMethod VerificationMethod) VerificationRelationship {
return VerificationRelationship{VerificationMethodID: verificationMethod.Id, DedicatedVerificationMethod: &verificationMethod}
return VerificationRelationship{
Content: &VerificationRelationship_VerificationMethod{VerificationMethod: &verificationMethod},
}
}

func (v VerificationRelationship) hasDedicatedMethod() bool {
return v.DedicatedVerificationMethod != nil
return v.GetVerificationMethod() != nil
}

func (v VerificationRelationship) Valid(did string) bool {
if !ValidateVerificationMethodID(v.VerificationMethodID, did) {
return false
}
if v.DedicatedVerificationMethod != nil {
if !v.DedicatedVerificationMethod.Valid(did) || v.DedicatedVerificationMethod.Id != v.VerificationMethodID {
return false
}
if v.hasDedicatedMethod() {
return v.GetVerificationMethod().Valid(did)
} else {
return ValidateVerificationMethodID(v.GetVerificationMethodId(), did)
}
return true
}

func (v VerificationRelationship) MarshalJSON() ([]byte, error) {
// if dedicated
if v.DedicatedVerificationMethod != nil {
return json.Marshal(v.DedicatedVerificationMethod)
if v.hasDedicatedMethod() {
return json.Marshal(v.GetVerificationMethod())
} else {
return json.Marshal(v.GetVerificationMethodId())
}
// if not dedicated
return json.Marshal(v.VerificationMethodID)
}

func (v *VerificationRelationship) UnmarshalJSON(bz []byte) error {
Expand Down
Loading