generated from cheqd/.github
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Change DID Doc Metadata for following the ADR + fix resource che…
…cksum [DEV-1640] (#37) * fix: Change metadata format * fix: checksum calculation * fix: linter * feat: change to pretty JSONs * fix tests * fix tests * fix linter * fix tests * linter * change data model approach * Update resource output + tests * Update integration tests * Fixed typo * Update go.mod * Pickup go.mod from files Co-authored-by: Ankur Banerjee <[email protected]>
- Loading branch information
Showing
21 changed files
with
596 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,178 +1,24 @@ | ||
package services | ||
|
||
import ( | ||
|
||
// jsonpb Marshaller is deprecated, but is needed because there's only one way to proto | ||
// marshal in combination with our proto generator version | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" | ||
resource "github.com/cheqd/cheqd-node/x/resource/types" | ||
"github.com/cheqd/did-resolver/types" | ||
"github.com/golang/protobuf/jsonpb" //nolint | ||
"github.com/iancoleman/orderedmap" | ||
"google.golang.org/protobuf/runtime/protoiface" | ||
) | ||
|
||
type DIDDocService struct{} | ||
|
||
const ( | ||
verificationMethod = "verificationMethod" | ||
publicKeyJwk = "publicKeyJwk" | ||
didContext = "context" | ||
) | ||
|
||
func (DIDDocService) MarshallProto(protoObject protoiface.MessageV1) (string, error) { | ||
var m jsonpb.Marshaler | ||
jsonObject, err := m.MarshalToString(protoObject) | ||
if err != nil { | ||
return "", err | ||
} | ||
return jsonObject, nil | ||
} | ||
|
||
func (ds DIDDocService) MarshallDID(didDoc cheqd.Did) (string, error) { | ||
mapDID, err := ds.protoToMap(&didDoc) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// VerKey changes | ||
formatedVerificationMethod, err := ds.MarshallVerificationMethod(didDoc.VerificationMethod) | ||
if err != nil { | ||
return "", err | ||
} | ||
mapDID.Set(verificationMethod, json.RawMessage(formatedVerificationMethod)) | ||
|
||
// Context changes | ||
if val, ok := mapDID.Get(didContext); ok { | ||
mapDID.Set("@"+didContext, val) | ||
mapDID.Delete(didContext) | ||
mapDID.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool { | ||
return a.Key() == "@"+didContext | ||
}) | ||
} | ||
|
||
result, err := json.Marshal(mapDID) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(result), nil | ||
} | ||
|
||
func (ds DIDDocService) MarshallContentStream(contentStream protoiface.MessageV1, contentType types.ContentType) (string, error) { | ||
var mapContent orderedmap.OrderedMap | ||
var err error | ||
var context []string | ||
if contentType == types.DIDJSONLD || contentType == types.JSONLD { | ||
context = []string{types.DIDSchemaJSONLD} | ||
} | ||
switch contentStream := contentStream.(type) { | ||
case *cheqd.VerificationMethod: | ||
mapContent, err = ds.prepareJWKPubkey(contentStream) | ||
case *cheqd.Did: | ||
contentStream.Context = context | ||
jsonDid, err := ds.MarshallDID(*contentStream) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(jsonDid), nil | ||
case *resource.ResourceHeader: | ||
dResource := types.DereferencedResource{ | ||
Context: context, | ||
CollectionId: contentStream.CollectionId, | ||
Id: contentStream.Id, | ||
Name: contentStream.Name, | ||
ResourceType: contentStream.ResourceType, | ||
MediaType: contentStream.MediaType, | ||
Created: contentStream.Created, | ||
Checksum: fmt.Sprintf("%x", contentStream.Checksum), | ||
PreviousVersionId: contentStream.PreviousVersionId, | ||
NextVersionId: contentStream.NextVersionId, | ||
} | ||
jsonResource, err := json.Marshal(dResource) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(jsonResource), nil | ||
default: | ||
mapContent, err = ds.protoToMap(contentStream) | ||
} | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Context changes | ||
if len(context) != 0 { | ||
mapContent.Set("@"+didContext, context[0]) | ||
mapContent.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool { | ||
return a.Key() == "@"+didContext | ||
}) | ||
} | ||
|
||
result, err := json.Marshal(mapContent) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(result), nil | ||
} | ||
|
||
func (DIDDocService) GetDIDFragment(fragmentId string, didDoc cheqd.Did) protoiface.MessageV1 { | ||
func (DIDDocService) GetDIDFragment(fragmentId string, didDoc types.DidDoc) types.ContentStreamI { | ||
for _, verMethod := range didDoc.VerificationMethod { | ||
if strings.Contains(verMethod.Id, fragmentId) { | ||
return verMethod | ||
return &verMethod | ||
} | ||
} | ||
for _, service := range didDoc.Service { | ||
if strings.Contains(service.Id, fragmentId) { | ||
return service | ||
return &service | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ds DIDDocService) prepareJWKPubkey(verificationMethod *cheqd.VerificationMethod) (orderedmap.OrderedMap, error) { | ||
methodJson, err := ds.protoToMap(verificationMethod) | ||
if err != nil { | ||
return *orderedmap.New(), err | ||
} | ||
if len(verificationMethod.PublicKeyJwk) > 0 { | ||
jsonKey, err := cheqd.PubKeyJWKToJson(verificationMethod.PublicKeyJwk) | ||
if err != nil { | ||
return *orderedmap.New(), err | ||
} | ||
methodJson.Set(publicKeyJwk, json.RawMessage(jsonKey)) | ||
} | ||
return methodJson, nil | ||
} | ||
|
||
func (ds DIDDocService) MarshallVerificationMethod(verificationMethod []*cheqd.VerificationMethod) ([]byte, error) { | ||
var verMethodList []orderedmap.OrderedMap | ||
for _, value := range verificationMethod { | ||
methodJson, err := ds.prepareJWKPubkey(value) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
verMethodList = append(verMethodList, methodJson) | ||
} | ||
return json.Marshal(verMethodList) | ||
} | ||
|
||
func (ds DIDDocService) protoToMap(protoObject protoiface.MessageV1) (orderedmap.OrderedMap, error) { | ||
mapObj := orderedmap.New() | ||
jsonObj, err := ds.MarshallProto(protoObject) | ||
if err != nil { | ||
return *mapObj, err | ||
} | ||
|
||
err = json.Unmarshal([]byte(jsonObj), &mapObj) | ||
if err != nil { | ||
return *mapObj, err | ||
} | ||
|
||
return *mapObj, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,49 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" | ||
"github.com/cheqd/did-resolver/types" | ||
"github.com/cheqd/did-resolver/utils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMarshallDID(t *testing.T) { | ||
didDocService := DIDDocService{} | ||
verificationMethod1 := cheqd.VerificationMethod{ | ||
Id: "did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue#verkey", | ||
Type: "Ed25519VerificationKey2020", | ||
Controller: "did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue", | ||
PublicKeyMultibase: "zAKJP3f7BD6W4iWEQ9jwndVTCBq8ua2Utt8EEjJ6Vxsf", | ||
} | ||
func TestDIDDocFragment(t *testing.T) { | ||
validDIDDoc := types.NewDidDoc(utils.ValidDIDDoc()) | ||
|
||
verificationMethod2 := cheqd.VerificationMethod{ | ||
Id: "did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue#verkey", | ||
Type: "JsonWebKey2020", | ||
Controller: "did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue", | ||
PublicKeyJwk: []*cheqd.KeyValuePair{ | ||
{Key: "kty", Value: "OKP"}, | ||
{Key: "crv", Value: "Ed25519"}, | ||
{Key: "x", Value: "VCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ"}, | ||
subtests := []struct { | ||
name string | ||
fragmentId string | ||
didDoc types.DidDoc | ||
expectedFragment types.ContentStreamI | ||
}{ | ||
{ | ||
name: "successful VerificationMethod finding", | ||
fragmentId: validDIDDoc.VerificationMethod[0].Id, | ||
didDoc: *validDIDDoc, | ||
expectedFragment: &validDIDDoc.VerificationMethod[0], | ||
}, | ||
{ | ||
name: "successful Service finding", | ||
fragmentId: validDIDDoc.Service[0].Id, | ||
didDoc: *validDIDDoc, | ||
expectedFragment: &validDIDDoc.Service[0], | ||
}, | ||
{ | ||
name: "Fragment is not found", | ||
fragmentId: "fake_id", | ||
didDoc: *validDIDDoc, | ||
expectedFragment: nil, | ||
}, | ||
} | ||
didDoc := cheqd.Did{ | ||
Context: []string{"test"}, | ||
Id: "did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue", | ||
VerificationMethod: []*cheqd.VerificationMethod{&verificationMethod1, &verificationMethod2}, | ||
} | ||
|
||
expectedDID := "{\"@context\":[\"test\"],\"id\":\"did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue\",\"verificationMethod\":[{\"id\":\"did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue#verkey\",\"type\":\"Ed25519VerificationKey2020\",\"controller\":\"did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue\",\"publicKeyMultibase\":\"zAKJP3f7BD6W4iWEQ9jwndVTCBq8ua2Utt8EEjJ6Vxsf\"},{\"id\":\"did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue#verkey\",\"type\":\"JsonWebKey2020\",\"controller\":\"did:cheqd:mainnet:N22KY2Dyvmuu2PyyqSFKue\",\"publicKeyJwk\":{\"crv\":\"Ed25519\",\"kty\":\"OKP\",\"x\":\"VCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ\"}}]}" | ||
jsonDID, err := didDocService.MarshallDID(didDoc) | ||
for _, subtest := range subtests { | ||
t.Run(subtest.name, func(t *testing.T) { | ||
didDocService := DIDDocService{} | ||
|
||
fragment := didDocService.GetDIDFragment(subtest.fragmentId, subtest.didDoc) | ||
|
||
fmt.Println(jsonDID) | ||
require.EqualValues(t, expectedDID, jsonDID) | ||
require.Empty(t, err) | ||
require.EqualValues(t, subtest.expectedFragment, fragment) | ||
}) | ||
} | ||
} |
Oops, something went wrong.