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

     1  package marker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/0chain/errors"
     7  	"github.com/0chain/gosdk/core/encryption"
     8  	"github.com/0chain/gosdk/core/sys"
     9  	"github.com/0chain/gosdk/zboxcore/client"
    10  )
    11  
    12  type WriteMarker struct {
    13  	AllocationRoot         string `json:"allocation_root"`
    14  	PreviousAllocationRoot string `json:"prev_allocation_root"`
    15  	FileMetaRoot           string `json:"file_meta_root"`
    16  	AllocationID           string `json:"allocation_id"`
    17  	Size                   int64  `json:"size"`
    18  	ChainSize              int64  `json:"chain_size"`
    19  	ChainHash              string `json:"chain_hash"`
    20  	ChainLength            int    `json:"chain_length"`
    21  	BlobberID              string `json:"blobber_id"`
    22  	Timestamp              int64  `json:"timestamp"`
    23  	ClientID               string `json:"client_id"`
    24  	Signature              string `json:"signature"`
    25  }
    26  
    27  func (wm *WriteMarker) GetHashData() string {
    28  	var sigData string
    29  	if wm.ChainHash != "" {
    30  		sigData = fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s:%d:%d:%d",
    31  			wm.AllocationRoot, wm.PreviousAllocationRoot,
    32  			wm.FileMetaRoot, wm.ChainHash, wm.AllocationID, wm.BlobberID,
    33  			wm.ClientID, wm.Size, wm.ChainSize, wm.Timestamp)
    34  	} else {
    35  		sigData = fmt.Sprintf("%s:%s:%s:%s:%s:%s:%d:%d",
    36  			wm.AllocationRoot, wm.PreviousAllocationRoot,
    37  			wm.FileMetaRoot, wm.AllocationID,
    38  			wm.BlobberID, wm.ClientID, wm.Size,
    39  			wm.Timestamp)
    40  	}
    41  	return sigData
    42  }
    43  
    44  func (wm *WriteMarker) GetHash() string {
    45  	sigData := wm.GetHashData()
    46  	return encryption.Hash(sigData)
    47  }
    48  
    49  func (wm *WriteMarker) Sign() error {
    50  	var err error
    51  	wm.Signature, err = client.Sign(wm.GetHash())
    52  	return err
    53  }
    54  
    55  func (wm *WriteMarker) VerifySignature(clientPublicKey string) error {
    56  	hashData := wm.GetHashData()
    57  	signatureHash := encryption.Hash(hashData)
    58  	sigOK, err := sys.VerifyWith(clientPublicKey, wm.Signature, signatureHash)
    59  	if err != nil {
    60  		return errors.New("write_marker_validation_failed", "Error during verifying signature. "+err.Error())
    61  	}
    62  	if !sigOK {
    63  		return errors.New("write_marker_validation_failed", "Write marker signature is not valid")
    64  	}
    65  	return nil
    66  }