diff --git a/.gitignore b/.gitignore index c818180b1..9d99cb176 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ zcnbridge/example/logs/ winsdk/zcn.windows.h wasmsdk/cypress/node_modules/ wasmsdk/demo/demo +sdkversion.go +gosdk.code-workspace +sdkversion diff --git a/constants/context_key.go b/constants/context_key.go index 30764cfee..b4c264035 100644 --- a/constants/context_key.go +++ b/constants/context_key.go @@ -16,5 +16,6 @@ const ( ContextKeyAllocationID ContextKey = "allocation_id" // ContextKeyClientSignatureHeaderKey represents key for context value passed with common.ClientSignatureHeader request header. - ContextKeyClientSignatureHeaderKey ContextKey = "signature" + ContextKeyClientSignatureHeaderKey ContextKey = "signature" + ContextKeyClientSignatureHeaderV2Key ContextKey = "signature_v2" ) diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index fcbc14c93..866ff3449 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -839,11 +839,19 @@ func upload(allocationID, remotePath string, fileBytes, thumbnailBytes []byte, w } // download download file blocks -func downloadBlocks(alloc *sdk.Allocation, remotePath, authTicket, lookupHash string, startBlock, endBlock int64) ([]byte, error) { +func downloadBlocks(allocId string, remotePath, authTicket, lookupHash string, startBlock, endBlock int64) ([]byte, error) { + if len(remotePath) == 0 && len(authTicket) == 0 { return nil, RequiredArg("remotePath/authTicket") } + alloc, err := getAllocation(allocId) + + if err != nil { + PrintError("Error fetching the allocation", err) + return nil, err + } + var ( wg = &sync.WaitGroup{} statusBar = &StatusBar{wg: wg} diff --git a/wasmsdk/player_file.go b/wasmsdk/player_file.go index 440f65b9c..ccafe75f2 100644 --- a/wasmsdk/player_file.go +++ b/wasmsdk/player_file.go @@ -66,7 +66,7 @@ func (p *FilePlayer) download(startBlock int64) { } fmt.Println("start:", startBlock, "end:", endBlock, "numBlocks:", p.numBlocks, "total:", p.playlistFile.NumBlocks) - data, err := downloadBlocks(p.allocationObj, p.remotePath, p.authTicket, p.lookupHash, startBlock, endBlock) + data, err := downloadBlocks(p.allocationObj.ID, p.remotePath, p.authTicket, p.lookupHash, startBlock, endBlock) // data, err := downloadBlocks2(int(startBlock), int(endBlock), p.allocationObj, p.remotePath) if err != nil { PrintError(err.Error()) diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 9420375fd..b4e48f9ab 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -136,6 +136,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock() { header.NumBlocks = req.numBlocks header.VerifyDownload = req.shouldVerify header.ConnectionID = req.connectionID + header.Version = "v2" if req.authTicket != nil { header.AuthToken, _ = json.Marshal(req.authTicket) //nolint: errcheck diff --git a/zboxcore/sdk/download_reqeust_header.go b/zboxcore/sdk/download_reqeust_header.go index 3606e4618..c447e0db9 100644 --- a/zboxcore/sdk/download_reqeust_header.go +++ b/zboxcore/sdk/download_reqeust_header.go @@ -18,6 +18,7 @@ type DownloadRequestHeader struct { DownloadMode string VerifyDownload bool ConnectionID string + Version string } // ToHeader update header @@ -51,5 +52,9 @@ func (h *DownloadRequestHeader) ToHeader(req *http.Request) { req.Header.Set("X-Connection-ID", h.ConnectionID) } + if h.Version != "" { + req.Header.Set("X-Version", h.Version) + } + req.Header.Set("X-Verify-Download", fmt.Sprint(h.VerifyDownload)) } diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index e0f40fb2c..4aabd0683 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -116,6 +116,7 @@ func (o *ObjectTreeRequest) getFileRefs(oTR *oTreeResponse, bUrl string) { oReq, err := zboxutil.NewRefsRequest( bUrl, o.allocationID, + o.allocationTx, o.remotefilepath, o.pathHash, o.authToken, diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 73c0519e0..8b30c32d5 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -76,8 +76,9 @@ const ( REDEEM_ENDPOINT = "/v1/connection/redeem/" // CLIENT_SIGNATURE_HEADER represents http request header contains signature. - CLIENT_SIGNATURE_HEADER = "X-App-Client-Signature" - ALLOCATION_ID_HEADER = "ALLOCATION-ID" + CLIENT_SIGNATURE_HEADER = "X-App-Client-Signature" + CLIENT_SIGNATURE_HEADER_V2 = "X-App-Client-Signature-V2" + ALLOCATION_ID_HEADER = "ALLOCATION-ID" ) func getEnvAny(names ...string) string { @@ -167,15 +168,22 @@ func setClientInfo(req *http.Request) { req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) } -func setClientInfoWithSign(req *http.Request, allocation string) error { +func setClientInfoWithSign(req *http.Request, allocation, baseURL string) error { setClientInfo(req) - sign, err := client.Sign(encryption.Hash(allocation)) + hashData := allocation + sign, err := client.Sign(encryption.Hash(hashData)) if err != nil { return err } req.Header.Set(CLIENT_SIGNATURE_HEADER, sign) + hashData = allocation + baseURL + sign, err = client.Sign(encryption.Hash(hashData)) + if err != nil { + return err + } + req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sign) return nil } @@ -216,7 +224,7 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -262,7 +270,7 @@ func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, pat return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -271,7 +279,7 @@ func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, pat return req, nil } -func NewRefsRequest(baseUrl, allocationID, path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*http.Request, error) { +func NewRefsRequest(baseUrl, allocationID, allocationTx, path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*http.Request, error) { nUrl, err := joinUrl(baseUrl, REFS_ENDPOINT, allocationID) if err != nil { return nil, err @@ -295,7 +303,7 @@ func NewRefsRequest(baseUrl, allocationID, path, pathHash, authToken, offsetPath req.Header.Set(ALLOCATION_ID_HEADER, allocationID) - if err := setClientInfoWithSign(req, allocationID); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -321,7 +329,7 @@ func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx string, fromDate, offset req.Header.Set(ALLOCATION_ID_HEADER, allocID) - if err = setClientInfoWithSign(req, allocID); err != nil { + if err := setClientInfoWithSign(req, allocTx, bUrl); err != nil { return nil, err } @@ -356,7 +364,7 @@ func NewCollaboratorRequest(baseUrl string, allocationID string, allocationTx st return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -376,7 +384,7 @@ func GetCollaboratorsRequest(baseUrl string, allocationID string, allocationTx s return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -397,7 +405,7 @@ func DeleteCollaboratorRequest(baseUrl string, allocationID string, allocationTx return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -416,8 +424,7 @@ func NewFileMetaRequest(baseUrl string, allocationID string, allocationTx string return nil, err } - err = setClientInfoWithSign(req, allocationTx) - if err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -436,7 +443,7 @@ func NewFileStatsRequest(baseUrl string, allocationID string, allocationTx strin return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -487,7 +494,7 @@ func NewUploadRequestWithMethod(baseURL, allocationID string, allocationTx strin } // set header: X-App-Client-Signature - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseURL); err != nil { return nil, err } @@ -513,8 +520,7 @@ func NewWriteMarkerLockRequest( return nil, err } - err = setClientInfoWithSign(req, allocationTx) - if err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseURL); err != nil { return nil, err } @@ -536,8 +542,7 @@ func NewWriteMarkerUnLockRequest( return nil, err } - err = setClientInfoWithSign(req, allocationTx) - if err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseURL); err != nil { return nil, err } @@ -562,7 +567,7 @@ func NewUploadRequest(baseUrl, allocationID string, allocationTx string, body io return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -581,7 +586,7 @@ func NewConnectionRequest(baseUrl, allocationID string, allocationTx string, bod return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -602,7 +607,7 @@ func NewRenameRequest(baseUrl, allocationID string, allocationTx string, body io return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -622,7 +627,7 @@ func NewCopyRequest(baseUrl, allocationID string, allocationTx string, body io.R return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -642,7 +647,7 @@ func NewMoveRequest(baseUrl, allocationID string, allocationTx string, body io.R return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -662,7 +667,9 @@ func NewDownloadRequest(baseUrl, allocationID, allocationTx string) (*http.Reque if err != nil { return nil, err } - setClientInfo(req) + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + return nil, err + } req.Header.Set(ALLOCATION_ID_HEADER, allocationID) @@ -696,7 +703,7 @@ func NewDeleteRequest(baseUrl, allocationID string, allocationTx string, query * return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -716,7 +723,7 @@ func NewCreateDirRequest(baseUrl, allocationID string, allocationTx string, body return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -736,7 +743,7 @@ func NewShareRequest(baseUrl, allocationID string, allocationTx string, body io. return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -756,7 +763,7 @@ func NewRevokeShareRequest(baseUrl, allocationID string, allocationTx string, qu return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err } @@ -777,7 +784,7 @@ func NewWritemarkerRequest(baseUrl, allocationID, allocationTx string) (*http.Re return nil, err } - if err := setClientInfoWithSign(req, allocationTx); err != nil { + if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { return nil, err }