github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/sharerequest.go (about)

     1  package sdk
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  
     7  	"github.com/0chain/errors"
     8  
     9  	"github.com/0chain/gosdk/core/common"
    10  	"github.com/0chain/gosdk/zboxcore/blockchain"
    11  	"github.com/0chain/gosdk/zboxcore/client"
    12  	"github.com/0chain/gosdk/zboxcore/encryption"
    13  	"github.com/0chain/gosdk/zboxcore/fileref"
    14  	"github.com/0chain/gosdk/zboxcore/marker"
    15  )
    16  
    17  type ShareRequest struct {
    18  	allocationID      string
    19  	allocationTx      string
    20  	sig               string
    21  	remotefilepath    string
    22  	remotefilename    string
    23  	refType           string
    24  	expirationSeconds int64
    25  	blobbers          []*blockchain.StorageNode
    26  	ctx               context.Context
    27  }
    28  
    29  func (req *ShareRequest) GetFileRef() (*fileref.FileRef, error) {
    30  	filePathHash := fileref.GetReferenceLookup(req.allocationID, req.remotefilepath)
    31  
    32  	var fileRef *fileref.FileRef
    33  	listReq := &ListRequest{
    34  		remotefilepathhash: filePathHash,
    35  		allocationID:       req.allocationID,
    36  		allocationTx:       req.allocationTx,
    37  		sig:                req.sig,
    38  		blobbers:           req.blobbers,
    39  		ctx:                req.ctx,
    40  		Consensus:          Consensus{RWMutex: &sync.RWMutex{}},
    41  	}
    42  	_, _, fileRef, _ = listReq.getFileConsensusFromBlobbers()
    43  	if fileRef == nil {
    44  		return nil, errors.New("file_meta_error", "Error getting object meta data from blobbers")
    45  	}
    46  	return fileRef, nil
    47  }
    48  
    49  func (req *ShareRequest) getAuthTicket(clientID, encPublicKey string) (*marker.AuthTicket, error) {
    50  	fRef, err := req.GetFileRef()
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	at := &marker.AuthTicket{
    56  		AllocationID:   req.allocationID,
    57  		OwnerID:        client.GetClientID(),
    58  		ClientID:       clientID,
    59  		FileName:       req.remotefilename,
    60  		FilePathHash:   fileref.GetReferenceLookup(req.allocationID, req.remotefilepath),
    61  		RefType:        req.refType,
    62  		ActualFileHash: fRef.ActualFileHash,
    63  	}
    64  
    65  	at.Timestamp = int64(common.Now())
    66  
    67  	if req.expirationSeconds > 0 {
    68  		at.Expiration = at.Timestamp + req.expirationSeconds
    69  	}
    70  
    71  	if encPublicKey != "" { // file is encrypted
    72  		encScheme := encryption.NewEncryptionScheme()
    73  		if _, err := encScheme.Initialize((client.GetClient().Mnemonic)); err != nil {
    74  			return nil, err
    75  		}
    76  
    77  		reKey, err := encScheme.GetReGenKey(encPublicKey, "filetype:audio")
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  
    82  		at.ReEncryptionKey = reKey
    83  		at.Encrypted = true
    84  	}
    85  
    86  	if err := at.Sign(); err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	return at, nil
    91  }