Skip to content

Commit

Permalink
feat: Add support for anchor origin
Browse files Browse the repository at this point in the history
Add optional anchor origin in:
- suffix data for create operation
- signed model for recover operation

Closes trustbloc#546

Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask committed Feb 17, 2021
1 parent a0aa65c commit 1130769
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/versions/0_1/client/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type CreateRequestInfo struct {
// required
UpdateCommitment string

// AnchorOrigin signifies the system(s) that know the most recent anchor for this DID (optional)
AnchorOrigin interface{}

// Type signifies the type of entity a DID represents (optional)
Type string

// latest hashing algorithm supported by protocol
MultihashCode uint
}
Expand Down Expand Up @@ -66,6 +72,8 @@ func NewCreateRequest(info *CreateRequestInfo) ([]byte, error) {
suffixData := &model.SuffixDataModel{
DeltaHash: deltaHash,
RecoveryCommitment: info.RecoveryCommitment,
AnchorOrigin: info.AnchorOrigin,
Type: info.Type,
}

schema := &model.CreateRequest{
Expand Down
27 changes: 27 additions & 0 deletions pkg/versions/0_1/client/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/trustbloc/sidetree-core-go/pkg/commitment"
"github.com/trustbloc/sidetree-core-go/pkg/patch"
"github.com/trustbloc/sidetree-core-go/pkg/util/pubkey"
"github.com/trustbloc/sidetree-core-go/pkg/versions/0_1/model"
)

const (
Expand Down Expand Up @@ -144,6 +146,31 @@ func TestNewCreateRequest(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, request)
})

t.Run("success - optional params (entity type and anchor origin)", func(t *testing.T) {
p, err := patch.NewAddPublicKeysPatch(addKeys)
require.NoError(t, err)

info := &CreateRequestInfo{
Patches: []patch.Patch{p},
RecoveryCommitment: recoveryCommitment,
UpdateCommitment: updateCommitment,
AnchorOrigin: "anchor-origin",
Type: "did-entity-type",
MultihashCode: sha2_256,
}

bytes, err := NewCreateRequest(info)
require.NoError(t, err)
require.NotEmpty(t, bytes)

var request model.CreateRequest
err = json.Unmarshal(bytes, &request)
require.NoError(t, err)

require.Contains(t, request.SuffixData.AnchorOrigin, "anchor-origin")
require.Contains(t, request.SuffixData.Type, "did-entity-type")
})
}

const addKeys = `[{
Expand Down
4 changes: 4 additions & 0 deletions pkg/versions/0_1/client/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type RecoverRequestInfo struct {
// UpdateCommitment is update commitment to be used for the next update
UpdateCommitment string

// AnchorOrigin signifies the system(s) that know the most recent anchor for this DID (optional)
AnchorOrigin interface{}

// MultihashCode is the latest hashing algorithm supported by protocol
MultihashCode uint

Expand Down Expand Up @@ -79,6 +82,7 @@ func NewRecoverRequest(info *RecoverRequestInfo) ([]byte, error) {
DeltaHash: deltaHash,
RecoveryKey: info.RecoveryKey,
RecoveryCommitment: info.RecoveryCommitment,
AnchorOrigin: info.AnchorOrigin,
}

err = validateCommitment(info.RecoveryKey, info.MultihashCode, info.RecoveryCommitment)
Expand Down
27 changes: 27 additions & 0 deletions pkg/versions/0_1/client/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"github.com/stretchr/testify/require"

"github.com/trustbloc/sidetree-core-go/pkg/commitment"
internaljws "github.com/trustbloc/sidetree-core-go/pkg/internal/jws"
"github.com/trustbloc/sidetree-core-go/pkg/patch"
"github.com/trustbloc/sidetree-core-go/pkg/util/ecsigner"
"github.com/trustbloc/sidetree-core-go/pkg/util/pubkey"
"github.com/trustbloc/sidetree-core-go/pkg/versions/0_1/model"
)

func TestNewRecoverRequest(t *testing.T) {
Expand Down Expand Up @@ -154,6 +156,31 @@ func TestNewRecoverRequest(t *testing.T) {
require.Equal(t, "recover", request["type"])
require.Equal(t, didSuffix, request["didSuffix"])
})

t.Run("success - optional params (anchor origin)", func(t *testing.T) {
info := getRecoverRequestInfo()
info.AnchorOrigin = "test-anchor-origin"

bytes, err := NewRecoverRequest(info)
require.NoError(t, err)
require.NotEmpty(t, bytes)

var request map[string]interface{}
err = json.Unmarshal(bytes, &request)
require.NoError(t, err)

jws, ok := request["signedData"]
require.True(t, ok)

signedData, err := internaljws.ParseJWS(jws.(string))
require.NoError(t, err)

var signedModel model.RecoverSignedDataModel
err = json.Unmarshal(signedData.Payload, &signedModel)
require.NoError(t, err)

require.Equal(t, "test-anchor-origin", signedModel.AnchorOrigin)
})
}

func getRecoverRequestInfo() *RecoverRequestInfo {
Expand Down
13 changes: 11 additions & 2 deletions pkg/versions/0_1/model/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ type CreateRequest struct {
// SuffixDataModel is part of create request.
type SuffixDataModel struct {

// Hash of the delta object
// Hash of the delta object (required)
DeltaHash string `json:"deltaHash,omitempty"`

// Commitment hash for the next recovery or deactivate operation
// Commitment hash for the next recovery or deactivate operation (required)
RecoveryCommitment string `json:"recoveryCommitment,omitempty"`

// AnchorOrigin signifies the system(s) that know the most recent anchor for this DID (optional)
AnchorOrigin interface{} `json:"anchorOrigin,omitempty"`

// Type signifies the type of entity a DID represents (optional)
Type string `json:"type,omitempty"`
}

// DeltaModel contains patch data (patches used for create, recover, update).
Expand Down Expand Up @@ -102,6 +108,9 @@ type RecoverSignedDataModel struct {

// RecoveryCommitment is the commitment used for the next recovery/deactivate
RecoveryCommitment string `json:"recoveryCommitment"`

// AnchorOrigin signifies the system(s) that know the most recent anchor for this DID (optional)
AnchorOrigin interface{} `json:"anchorOrigin,omitempty"`
}

// DeactivateSignedDataModel defines data model for deactivate.
Expand Down
16 changes: 16 additions & 0 deletions pkg/versions/0_1/txnprovider/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/trustbloc/sidetree-core-go/pkg/api/operation"
"github.com/trustbloc/sidetree-core-go/pkg/commitment"
"github.com/trustbloc/sidetree-core-go/pkg/compression"
internaljws "github.com/trustbloc/sidetree-core-go/pkg/internal/jws"
"github.com/trustbloc/sidetree-core-go/pkg/jws"
"github.com/trustbloc/sidetree-core-go/pkg/mocks"
"github.com/trustbloc/sidetree-core-go/pkg/patch"
Expand All @@ -36,6 +37,9 @@ import (
const (
sha2_256 = 18
defaultNS = "did:sidetree"

createAnchorOrigin = "create-anchor-origin"
recoverAnchorOrigin = "recover-anchor-origin"
)

func TestNewOperationHandler(t *testing.T) {
Expand Down Expand Up @@ -88,6 +92,7 @@ func TestOperationHandler_PrepareTxnFiles(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cif)
require.Equal(t, createOpsNum, len(cif.Operations.Create))
require.Equal(t, createAnchorOrigin, cif.Operations.Create[0].SuffixData.AnchorOrigin)
require.Equal(t, recoverOpsNum, len(cif.Operations.Recover))
require.Equal(t, deactivateOpsNum, len(cif.Operations.Deactivate))

Expand Down Expand Up @@ -131,6 +136,15 @@ func TestOperationHandler_PrepareTxnFiles(t *testing.T) {
require.Equal(t, recoverOpsNum, len(cpf.Operations.Recover))
require.Equal(t, deactivateOpsNum, len(cpf.Operations.Deactivate))

signedData, err := internaljws.ParseJWS(cpf.Operations.Recover[0])
require.NoError(t, err)

var signedModel model.RecoverSignedDataModel
err = json.Unmarshal(signedData.Payload, &signedModel)
require.NoError(t, err)

require.Equal(t, recoverAnchorOrigin, signedModel.AnchorOrigin)

bytes, err = handler.cas.Read(mf.ProvisionalProofFileURI)
require.NoError(t, err)
require.NotNil(t, bytes)
Expand Down Expand Up @@ -424,6 +438,7 @@ func generateCreateOperation(num int) ([]byte, error) {
RecoveryCommitment: recoverCommitment,
UpdateCommitment: updateCommitment,
MultihashCode: sha2_256,
AnchorOrigin: createAnchorOrigin, // optional
}

return client.NewCreateRequest(info)
Expand Down Expand Up @@ -461,6 +476,7 @@ func generateRecoverOperation(num int) ([]byte, error) {
RecoveryCommitment: recoveryCommitment,
UpdateCommitment: updateCommitment,
RecoveryKey: jwk,
AnchorOrigin: recoverAnchorOrigin,
MultihashCode: sha2_256,
Signer: ecsigner.New(privKey, "ES256", ""),
RevealValue: rv,
Expand Down

0 comments on commit 1130769

Please sign in to comment.