github.com/0chain/gosdk@v1.17.11/zboxcore/marker/readmarker.go (about)

     1  package marker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/0chain/errors"
     7  	"github.com/0chain/gosdk/core/common"
     8  	"github.com/0chain/gosdk/core/encryption"
     9  	"github.com/0chain/gosdk/core/sys"
    10  	"github.com/0chain/gosdk/zboxcore/client"
    11  )
    12  
    13  type ReadMarker struct {
    14  	ClientID        string           `json:"client_id"`
    15  	ClientPublicKey string           `json:"client_public_key"`
    16  	BlobberID       string           `json:"blobber_id"`
    17  	AllocationID    string           `json:"allocation_id"`
    18  	OwnerID         string           `json:"owner_id"`
    19  	Timestamp       common.Timestamp `json:"timestamp"`
    20  	ReadCounter     int64            `json:"counter"`
    21  	Signature       string           `json:"signature"`
    22  	SessionRC       int64            `json:"session_rc"`
    23  }
    24  
    25  func (rm *ReadMarker) GetHash() string {
    26  	sigData := fmt.Sprintf("%v:%v:%v:%v:%v:%v:%v", rm.AllocationID,
    27  		rm.BlobberID, rm.ClientID, rm.ClientPublicKey, rm.OwnerID,
    28  		rm.ReadCounter, rm.Timestamp)
    29  	return encryption.Hash(sigData)
    30  }
    31  
    32  func (rm *ReadMarker) Sign() error {
    33  	var err error
    34  	rm.Signature, err = client.Sign(rm.GetHash())
    35  	return err
    36  }
    37  
    38  // ValidateWithOtherRM will validate rm1 assuming rm is valid. It checks parameters equality and validity of signature
    39  func (rm *ReadMarker) ValidateWithOtherRM(rm1 *ReadMarker) error {
    40  	if rm.ClientPublicKey != rm1.ClientPublicKey {
    41  		return errors.New("validate_rm", fmt.Sprintf("client public key %s is not same as %s", rm1.ClientPublicKey, rm.ClientPublicKey))
    42  	}
    43  
    44  	signatureHash := rm1.GetHash()
    45  
    46  	signOK, err := sys.Verify(rm1.Signature, signatureHash)
    47  	if err != nil {
    48  		return errors.New("validate_rm", err.Error())
    49  	}
    50  
    51  	if !signOK {
    52  		return errors.New("validate_rm", "signature is not valid")
    53  	}
    54  	return nil
    55  }