From d9a81bd21268d348f0533791b3232c920dfb3f15 Mon Sep 17 00:00:00 2001 From: Tasos Derisiotis <50984242+Eengineer1@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:34:17 +0300 Subject: [PATCH] Added indicative integration tests + reusable types --- tests/integration/cli_diddoc_test.go | 131 +++++++++++++++++++++++++ x/did/types/diddoc_assertion_method.go | 10 ++ x/did/types/diddoc_diddoc_test.go | 34 +++---- 3 files changed, 153 insertions(+), 22 deletions(-) create mode 100644 x/did/types/diddoc_assertion_method.go diff --git a/tests/integration/cli_diddoc_test.go b/tests/integration/cli_diddoc_test.go index bb4742678..af94e1685 100644 --- a/tests/integration/cli_diddoc_test.go +++ b/tests/integration/cli_diddoc_test.go @@ -4,7 +4,9 @@ package integration import ( "crypto/ed25519" + "encoding/json" "fmt" + "strconv" "github.com/cheqd/cheqd-node/tests/integration/cli" "github.com/cheqd/cheqd-node/tests/integration/helpers" @@ -408,4 +410,133 @@ var _ = Describe("cheqd cli - positive did", func() { // Check that the DID Doc is deactivated Expect(resp2.Value.Metadata.Deactivated).To(BeTrue()) }) + + It("can create diddoc with augmented assertionMethod, update it and query the result (Ed25519VerificationKey2020)", func() { + AddReportEntry("Integration", fmt.Sprintf("%sPositive: %s", cli.Green, "can create diddoc with augmented assertionMethod (Ed25519VerificationKey2020)")) + // Create a new DID Doc + did := "did:cheqd:" + network.DidNamespace + ":" + uuid.NewString() + keyID := did + "#key1" + + publicKey, privateKey, err := ed25519.GenerateKey(nil) + Expect(err).To(BeNil()) + + publicKeyMultibase := testsetup.GenerateEd25519VerificationKey2020VerificationMaterial(publicKey) + publicKeyBase58 := testsetup.GenerateEd25519VerificationKey2018VerificationMaterial(publicKey) + + assertionMethodJSONEscaped := func() string { + b, _ := json.Marshal(types.AssertionMethodJSONUnescaped{ + Id: fmt.Sprintf("%s#fragment", did), + Type: "Ed25519VerificationKey2018", + Controller: did, + PublicKeyBase58: &publicKeyBase58, // arbitrarily chosen, loosely validated + }) + return strconv.Quote(string(b)) + }() + + payload := didcli.DIDDocument{ + ID: did, + VerificationMethod: []didcli.VerificationMethod{ + map[string]any{ + "id": keyID, + "type": "Ed25519VerificationKey2020", + "controller": did, + "publicKeyMultibase": publicKeyMultibase, + }, + }, + Authentication: []string{keyID}, + AssertionMethod: []string{keyID, assertionMethodJSONEscaped}, + } + + signInputs := []didcli.SignInput{ + { + VerificationMethodID: keyID, + PrivKey: privateKey, + }, + } + + versionID := uuid.NewString() + + res, err := cli.CreateDidDoc(tmpDir, payload, signInputs, versionID, testdata.BASE_ACCOUNT_1, helpers.GenerateFees(feeParams.CreateDid.String())) + Expect(err).To(BeNil()) + Expect(res.Code).To(BeEquivalentTo(0)) + + AddReportEntry("Integration", fmt.Sprintf("%sPositive: %s", cli.Green, "can update diddoc with augmented assertionMethod (Ed25519VerificationKey2020)")) + // Update the DID Doc + + assertionMethodJSONEscaped2 := func() string { + b, _ := json.Marshal(types.AssertionMethodJSONUnescaped{ + Id: fmt.Sprintf("%s#fragment", did), + Type: "Ed25519VerificationKey2020", + Controller: did, + PublicKeyMultibase: &publicKeyMultibase, // arbitrarily chosen, loosely validated + }) + return strconv.Quote(string(b)) + }() + + payload2 := didcli.DIDDocument{ + ID: did, + VerificationMethod: []didcli.VerificationMethod{ + map[string]any{ + "id": keyID, + "type": "Ed25519VerificationKey2020", + "controller": did, + "publicKeyMultibase": publicKeyMultibase, + }, + }, + Authentication: []string{keyID}, + AssertionMethod: []string{keyID, assertionMethodJSONEscaped, assertionMethodJSONEscaped2}, + } + + versionID = uuid.NewString() + + res2, err := cli.UpdateDidDoc(tmpDir, payload2, signInputs, versionID, testdata.BASE_ACCOUNT_1, helpers.GenerateFees(feeParams.UpdateDid.String())) + Expect(err).To(BeNil()) + Expect(res2.Code).To(BeEquivalentTo(0)) + + AddReportEntry("Integration", fmt.Sprintf("%sPositive: %s", cli.Green, "can query diddoc with augmented assertionMethod (Ed25519VerificationKey2020)")) + // Query the DID Doc + resp, err := cli.QueryDidDoc(did) + Expect(err).To(BeNil()) + + didDoc := resp.Value.DidDoc + Expect(didDoc.Id).To(BeEquivalentTo(did)) + Expect(didDoc.Authentication).To(HaveLen(1)) + Expect(didDoc.Authentication[0]).To(BeEquivalentTo(keyID)) + Expect(didDoc.VerificationMethod).To(HaveLen(1)) + Expect(didDoc.VerificationMethod[0].Id).To(BeEquivalentTo(keyID)) + Expect(didDoc.VerificationMethod[0].VerificationMethodType).To(BeEquivalentTo("Ed25519VerificationKey2020")) + Expect(didDoc.VerificationMethod[0].Controller).To(BeEquivalentTo(did)) + Expect(didDoc.VerificationMethod[0].VerificationMaterial).To(BeEquivalentTo(publicKeyMultibase)) + Expect(didDoc.AssertionMethod).To(HaveLen(3)) + Expect(didDoc.AssertionMethod[0]).To(BeEquivalentTo(keyID)) + Expect(didDoc.AssertionMethod[1]).To(BeEquivalentTo(assertionMethodJSONEscaped)) + Expect(didDoc.AssertionMethod[2]).To(BeEquivalentTo(assertionMethodJSONEscaped2)) + + // Check that DIDDoc is not deactivated + Expect(resp.Value.Metadata.Deactivated).To(BeFalse()) + + AddReportEntry("Integration", fmt.Sprintf("%sPositive: %s", cli.Green, "can deactivate diddoc with augmented assertionMethod (Ed25519VerificationKey2020)")) + // Deactivate the DID Doc + payload3 := types.MsgDeactivateDidDocPayload{ + Id: did, + } + + versionID = uuid.NewString() + + res3, err := cli.DeactivateDidDoc(tmpDir, payload3, signInputs, versionID, testdata.BASE_ACCOUNT_1, helpers.GenerateFees(feeParams.DeactivateDid.String())) + Expect(err).To(BeNil()) + Expect(res3.Code).To(BeEquivalentTo(0)) + + AddReportEntry("Integration", fmt.Sprintf("%sPositive: %s", cli.Green, "can query deactivated diddoc with augmented assertionMethod (Ed25519VerificationKey2020)")) + // Query the DID Doc + + resp2, err := cli.QueryDidDoc(did) + Expect(err).To(BeNil()) + + didDoc2 := resp2.Value.DidDoc + Expect(didDoc2).To(BeEquivalentTo(didDoc)) + + // Check that the DID Doc is deactivated + Expect(resp2.Value.Metadata.Deactivated).To(BeTrue()) + }) }) diff --git a/x/did/types/diddoc_assertion_method.go b/x/did/types/diddoc_assertion_method.go new file mode 100644 index 000000000..9d579da72 --- /dev/null +++ b/x/did/types/diddoc_assertion_method.go @@ -0,0 +1,10 @@ +package types + +type AssertionMethodJSONUnescaped struct { + Id string `json:"id"` + Type string `json:"type"` + Controller string `json:"controller"` + PublicKeyBase58 *string `json:"publicKeyBase58,omitempty"` + PublicKeyMultibase *string `json:"publicKeyMultibase,omitempty"` + PublicKeyJwk *string `json:"publicKeyJwk,omitempty"` +} diff --git a/x/did/types/diddoc_diddoc_test.go b/x/did/types/diddoc_diddoc_test.go index 70c22aa7e..d6fb80d58 100644 --- a/x/did/types/diddoc_diddoc_test.go +++ b/x/did/types/diddoc_diddoc_test.go @@ -246,16 +246,11 @@ var _ = DescribeTable("DIDDoc Validation tests", func(testCase DIDDocTestCase) { }, }, AssertionMethod: []string{fmt.Sprintf("%s#fragment", ValidTestDID), func() string { - b, _ := json.Marshal(struct { - Id string - Type string - Controller string - PublicKeyBase58 string - }{ + b, _ := json.Marshal(AssertionMethodJSONUnescaped{ Id: fmt.Sprintf("%s#fragment", ValidTestDID), Type: "Ed25519VerificationKey2018", Controller: ValidTestDID, - PublicKeyBase58: "base58", // arbitrarily chosen + PublicKeyBase58: &ValidEd25519VerificationKey2018VerificationMaterial, // arbitrarily chosen, loosely validated }) return strconv.Quote(string(b)) }()}, @@ -278,16 +273,11 @@ var _ = DescribeTable("DIDDoc Validation tests", func(testCase DIDDocTestCase) { }, }, AssertionMethod: []string{fmt.Sprintf("%s#fragment", ValidTestDID), func() string { - b, _ := json.Marshal(struct { - Id string - Type string - Controller string - PublicKeyBase58 string - }{ + b, _ := json.Marshal(AssertionMethodJSONUnescaped{ Id: fmt.Sprintf("%s#fragment-1", ValidTestDID), Type: "Ed25519VerificationKey2018", Controller: ValidTestDID, - PublicKeyBase58: "base58", // arbitrarily chosen + PublicKeyBase58: &ValidEd25519VerificationKey2018VerificationMaterial, // arbitrarily chosen, loosely validated }) return strconv.Quote(string(b)) }()}, @@ -311,10 +301,10 @@ var _ = DescribeTable("DIDDoc Validation tests", func(testCase DIDDocTestCase) { }, AssertionMethod: []string{func() string { b, _ := json.Marshal(struct { - Id string - Type string - Controller string - InvalidField map[string]interface{} + Id string `json:"id"` + Type string `json:"type"` + Controller string `json:"controller"` + InvalidField map[string]interface{} `json:"invalidField"` }{ Id: fmt.Sprintf("%s#fragment", ValidTestDID), Type: "Ed25519VerificationKey2018", @@ -343,8 +333,8 @@ var _ = DescribeTable("DIDDoc Validation tests", func(testCase DIDDocTestCase) { }, AssertionMethod: []string{func() string { b, _ := json.Marshal(struct { - Id string - Type string + Id string `json:"id"` + Type string `json:"type"` }{ Id: fmt.Sprintf("%s#fragment", ValidTestDID), Type: "Ed25519VerificationKey2018", @@ -371,8 +361,8 @@ var _ = DescribeTable("DIDDoc Validation tests", func(testCase DIDDocTestCase) { }, AssertionMethod: []string{func() string { b, _ := json.Marshal(struct { - Id string - Type string + Id string `json:"id"` + Type string `json:"type"` // controller is intentionally missing, no additional fields are necessary as the focal point is the unescaped JSON string, i.e. deserialisation should fail first, before any other validation }{ Id: fmt.Sprintf("%s#fragment", ValidTestDID), Type: "Ed25519VerificationKey2018",