Skip to content

Commit

Permalink
feat: add reveal value to update, recover and deactivate requests
Browse files Browse the repository at this point in the history
Add reveal value to update, recover and deactivate requests

Closes #521

Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask committed Dec 11, 2020
1 parent b5b935b commit 188eb39
Show file tree
Hide file tree
Showing 33 changed files with 541 additions and 290 deletions.
3 changes: 1 addition & 2 deletions pkg/api/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/trustbloc/sidetree-core-go/pkg/api/operation"
"github.com/trustbloc/sidetree-core-go/pkg/api/txn"
"github.com/trustbloc/sidetree-core-go/pkg/document"
"github.com/trustbloc/sidetree-core-go/pkg/jws"
"github.com/trustbloc/sidetree-core-go/pkg/patch"
)

Expand Down Expand Up @@ -72,7 +71,7 @@ type TxnProcessor interface {
type OperationParser interface {
Parse(namespace string, operation []byte) (*operation.Operation, error)
ParseDID(namespace, shortOrLongFormDID string) (string, []byte, error)
GetRevealValue(operation []byte) (*jws.JWK, error)
GetRevealValue(operation []byte) (string, error)
GetCommitment(operation []byte) (string, error)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/batch/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,12 @@ func generateOperation(num int) (*operation.QueuedOperation, error) {
Y: "y",
}

updateCommitment, err := commitment.Calculate(updateJwk, sha2_256)
updateCommitment, err := commitment.GetCommitment(updateJwk, sha2_256)
if err != nil {
return nil, err
}

recoverComitment, err := commitment.Calculate(recoverJWK, sha2_256)
recoverComitment, err := commitment.GetCommitment(recoverJWK, sha2_256)
if err != nil {
return nil, err
}
Expand Down
31 changes: 29 additions & 2 deletions pkg/commitment/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package commitment

import (
"fmt"

"github.com/trustbloc/edge-core/pkg/log"

"github.com/trustbloc/sidetree-core-go/pkg/canonicalizer"
Expand All @@ -17,8 +19,8 @@ import (

var logger = log.New("sidetree-core-commitment")

// Calculate will calculate commitment hash from JWK.
func Calculate(jwk *jws.JWK, multihashCode uint) (string, error) {
// GetCommitment will calculate commitment from JWK.
func GetCommitment(jwk *jws.JWK, multihashCode uint) (string, error) {
data, err := canonicalizer.MarshalCanonical(jwk)
if err != nil {
return "", err
Expand All @@ -43,3 +45,28 @@ func Calculate(jwk *jws.JWK, multihashCode uint) (string, error) {

return encoder.EncodeToString(multiHash), nil
}

// GetRevealValue will calculate reveal value from JWK.
func GetRevealValue(jwk *jws.JWK, multihashCode uint) (string, error) {
rv, err := hashing.CalculateModelMultihash(jwk, multihashCode)
if err != nil {
return "", fmt.Errorf("failed to get reveal value: %s", err.Error())
}

return rv, nil
}

// GetCommitmentFromRevealValue will calculate commitment from reveal value.
func GetCommitmentFromRevealValue(rv string) (string, error) {
mh, err := hashing.GetMultihash(rv)
if err != nil {
return "", fmt.Errorf("failed to get commitment from reveal value (get multihash): %s", err.Error())
}

multiHash, err := hashing.ComputeMultihash(uint(mh.Code), mh.Digest)
if err != nil {
return "", fmt.Errorf("failed to get commitment from reveal value (compute multihash): %s", err.Error())
}

return encoder.EncodeToString(multiHash), nil
}
58 changes: 54 additions & 4 deletions pkg/commitment/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
sha2_256 uint = 18 // multihash code
)

func TestCalculate(t *testing.T) {
func TestGetCommitment(t *testing.T) {
jwk := &jws.JWK{
Crv: "crv",
Kty: "kty",
Expand All @@ -28,20 +28,20 @@ func TestCalculate(t *testing.T) {
}

t.Run("success", func(t *testing.T) {
commitment, err := Calculate(jwk, sha2_256)
commitment, err := GetCommitment(jwk, sha2_256)
require.NoError(t, err)
require.NotEmpty(t, commitment)
})

t.Run(" error - multihash not supported", func(t *testing.T) {
commitment, err := Calculate(jwk, 55)
commitment, err := GetCommitment(jwk, 55)
require.Error(t, err)
require.Empty(t, commitment)
require.Contains(t, err.Error(), "algorithm not supported, unable to compute hash")
})

t.Run("error - canonicalization failed", func(t *testing.T) {
commitment, err := Calculate(nil, sha2_256)
commitment, err := GetCommitment(nil, sha2_256)
require.Error(t, err)
require.Empty(t, commitment)
require.Contains(t, err.Error(), "Expected '{' but got 'n'")
Expand All @@ -62,3 +62,53 @@ func TestCalculate(t *testing.T) {
require.Equal(t, string(canonicalized), expected)
})
}

func TestGetRevealValue(t *testing.T) {
jwk := &jws.JWK{
Crv: "crv",
Kty: "kty",
X: "x",
Y: "y",
}

t.Run("success", func(t *testing.T) {
rv, err := GetRevealValue(jwk, sha2_256)
require.NoError(t, err)
require.NotEmpty(t, rv)
})

t.Run("error - wrong multihash code", func(t *testing.T) {
rv, err := GetRevealValue(jwk, 55)
require.Error(t, err)
require.Empty(t, rv)
require.Contains(t, err.Error(), "failed to get reveal value: algorithm not supported, unable to compute hash")
})
}

func TestGetCommitmentFromRevealValue(t *testing.T) {
jwk := &jws.JWK{
Crv: "crv",
Kty: "kty",
X: "x",
Y: "y",
}

t.Run("success", func(t *testing.T) {
rv, err := GetRevealValue(jwk, sha2_256)
require.NoError(t, err)

cFromRv, err := GetCommitmentFromRevealValue(rv)
require.NoError(t, err)

c, err := GetCommitment(jwk, sha2_256)
require.NoError(t, err)
require.Equal(t, c, cFromRv)
})

t.Run("error - reveal value is not a multihash", func(t *testing.T) {
cFromRv, err := GetCommitmentFromRevealValue("reveal")
require.Error(t, err)
require.Empty(t, cFromRv)
require.Contains(t, err.Error(), "failed to get commitment from reveal value")
})
}
2 changes: 1 addition & 1 deletion pkg/dochandler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func getSuffixData(delta *model.DeltaModel) (*model.SuffixDataModel, error) {
X: "x",
}

c, err := commitment.Calculate(jwk, sha2_256)
c, err := commitment.GetCommitment(jwk, sha2_256)
if err != nil {
return nil, err
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/hashing/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@ func IsComputedUsingMultihashAlgorithm(encodedMultihash string, code uint64) boo

// GetMultihashCode returns multihash code from encoded multihash.
func GetMultihashCode(encodedMultihash string) (uint64, error) {
multihashBytes, err := encoder.DecodeString(encodedMultihash)
mh, err := GetMultihash(encodedMultihash)
if err != nil {
return 0, err
return 0, fmt.Errorf("failed to get decoded multihash: %s", err.Error())
}

mh, err := multihash.Decode(multihashBytes)
return mh.Code, nil
}

// GetMultihash returns decoded multihash from encoded multihash.
func GetMultihash(encodedMultihash string) (*multihash.DecodedMultihash, error) {
multihashBytes, err := encoder.DecodeString(encodedMultihash)
if err != nil {
return 0, err
return nil, err
}

return mh.Code, nil
return multihash.Decode(multihashBytes)
}

// IsValidModelMultihash compares model with provided model multihash.
Expand Down
21 changes: 10 additions & 11 deletions pkg/mocks/operationparser.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 15 additions & 34 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/trustbloc/sidetree-core-go/pkg/api/operation"
"github.com/trustbloc/sidetree-core-go/pkg/api/protocol"
"github.com/trustbloc/sidetree-core-go/pkg/commitment"
"github.com/trustbloc/sidetree-core-go/pkg/jws"
)

var logger = log.New("sidetree-core-processor")
Expand Down Expand Up @@ -87,36 +86,25 @@ func (s *OperationProcessor) Resolve(uniqueSuffix string) (*protocol.ResolutionM
return rm, nil
}

func (s *OperationProcessor) createOperationHashMap(ops []*operation.AnchoredOperation, multihashAlg uint) map[string][]*operation.AnchoredOperation {
func (s *OperationProcessor) createOperationHashMap(ops []*operation.AnchoredOperation) map[string][]*operation.AnchoredOperation {
opMap := make(map[string][]*operation.AnchoredOperation)

previousVersions := make(map[uint]bool)
if multihashAlg != 0 {
previousVersions[multihashAlg] = true
}

for _, op := range ops {
r, p, err := s.getRevealValue(op)
rv, err := s.getRevealValue(op)
if err != nil {
logger.Infof("[%s] Skipped bad operation while creating operation hash map {UniqueSuffix: %s, Type: %s, TransactionTime: %d, TransactionNumber: %d}. Reason: %s", s.name, op.UniqueSuffix, op.Type, op.TransactionTime, op.TransactionNumber, err)

continue
}

if _, ok := previousVersions[p.MultihashAlgorithm]; !ok {
previousVersions[p.MultihashAlgorithm] = true
}

for key := range previousVersions {
c, err := commitment.Calculate(r, key)
if err != nil {
logger.Infof("[%s] Skipped calculating commitment while creating operation hash map {UniqueSuffix: %s, Type: %s, TransactionTime: %d, TransactionNumber: %d}. Reason: %s", s.name, op.UniqueSuffix, op.Type, op.TransactionTime, op.TransactionNumber, err)

continue
}
c, err := commitment.GetCommitmentFromRevealValue(rv)
if err != nil {
logger.Infof("[%s] Skipped calculating commitment while creating operation hash map {UniqueSuffix: %s, Type: %s, TransactionTime: %d, TransactionNumber: %d}. Reason: %s", s.name, op.UniqueSuffix, op.Type, op.TransactionTime, op.TransactionNumber, err)

opMap[c] = append(opMap[c], op)
continue
}

opMap[c] = append(opMap[c], op)
}

return opMap
Expand Down Expand Up @@ -164,14 +152,7 @@ func (s *OperationProcessor) applyOperations(ops []*operation.AnchoredOperation,

state := rm

p, err := s.pc.Get(rm.LastOperationProtocolGenesisTime)
if err != nil {
logger.Infof("[%s] Unable to apply operations due to protocol error '%s' {UniqueSuffix: %s}", s.name, uniqueSuffix)

return state
}

opMap := s.createOperationHashMap(ops, p.Protocol().MultihashAlgorithm)
opMap := s.createOperationHashMap(ops)

// holds applied commitments
commitmentMap := make(map[string]bool)
Expand Down Expand Up @@ -309,22 +290,22 @@ func sortOperations(ops []*operation.AnchoredOperation) {
})
}

func (s *OperationProcessor) getRevealValue(op *operation.AnchoredOperation) (*jws.JWK, protocol.Protocol, error) {
func (s *OperationProcessor) getRevealValue(op *operation.AnchoredOperation) (string, error) {
if op.Type == operation.TypeCreate {
return nil, protocol.Protocol{}, errors.New("create operation doesn't have reveal value")
return "", errors.New("create operation doesn't have reveal value")
}

p, err := s.pc.Get(op.ProtocolGenesisTime)
if err != nil {
return nil, protocol.Protocol{}, fmt.Errorf("get operation reveal value - retrieve protocol: %s", err.Error())
return "", fmt.Errorf("get operation reveal value - retrieve protocol: %s", err.Error())
}

commitmentKey, err := p.OperationParser().GetRevealValue(op.OperationBuffer)
rv, err := p.OperationParser().GetRevealValue(op.OperationBuffer)
if err != nil {
return nil, protocol.Protocol{}, fmt.Errorf("get operation reveal value from operation parser: %s", err.Error())
return "", fmt.Errorf("get operation reveal value from operation parser: %s", err.Error())
}

return commitmentKey, p.Protocol(), nil
return rv, nil
}

func (s *OperationProcessor) getCommitment(op *operation.AnchoredOperation) (string, error) {
Expand Down
Loading

0 comments on commit 188eb39

Please sign in to comment.