Skip to content

Commit

Permalink
Merge branch 'sprint-1.18' into fix/refactor-zboxcore-mobilesdk-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yash10019coder authored Oct 28, 2024
2 parents 6313913 + a02b652 commit d148f0b
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 182 deletions.
13 changes: 1 addition & 12 deletions core/client/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/0chain/gosdk/core/conf"
"strings"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/sys"
Expand Down Expand Up @@ -64,7 +63,6 @@ func init() {

sys.Verify = verifySignature
sys.VerifyWith = verifySignatureWith
sys.VerifyEd25519With = verifyEd25519With
}

func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) {
Expand Down Expand Up @@ -106,15 +104,6 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) {
return sch.Verify(signature, hash)
}

func verifyEd25519With(pubKey, signature, hash string) (bool, error) {
sch := zcncrypto.NewSignatureScheme(constants.ED25519.String())
err := sch.SetPublicKey(pubKey)
if err != nil {
return false, err
}
return sch.Verify(signature, hash)
}

func GetClientSysKeys(clients ...string) []sys.KeyPair {
var wallet *zcncrypto.Wallet
if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil {
Expand Down
2 changes: 0 additions & 2 deletions core/sys/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ var (
Authorize AuthorizeFunc

AuthCommon AuthorizeFunc

VerifyEd25519With VerifyWithFunc
)

// SetAuthorize sets the authorize callback function
Expand Down
1 change: 0 additions & 1 deletion zboxcore/fileref/fileref.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type FileRef struct {
EncryptedKey string `json:"encrypted_key" mapstructure:"encrypted_key"`
EncryptedKeyPoint string `json:"encrypted_key_point" mapstructure:"encrypted_key_point"`
Collaborators []Collaborator `json:"collaborators" mapstructure:"collaborators"`
SignatureVersion int `json:"signature_version" mapstructure:"signature_version"`
}

func (fRef *FileRef) MetaID() string {
Expand Down
33 changes: 4 additions & 29 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package sdk
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/transaction"
"io"
"math"
"mime/multipart"
Expand All @@ -22,9 +22,6 @@ import (
"sync/atomic"
"time"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/transaction"

"github.com/0chain/common/core/currency"
"github.com/0chain/errors"
thrown "github.com/0chain/errors"
Expand Down Expand Up @@ -279,9 +276,6 @@ type Allocation struct {

StorageVersion int `json:"storage_version"`

// Owner ecdsa public key
OwnerSigningPublicKey string `json:"owner_signing_public_key"`

// FileOptions to define file restrictions on an allocation for third-parties
// default 00000000 for all crud operations suggesting only owner has the below listed abilities.
// enabling option/s allows any third party to perform certain ops
Expand Down Expand Up @@ -309,9 +303,8 @@ type Allocation struct {
// conseususes
consensusThreshold int
fullconsensus int
sig string `json:"-"`
allocationRoot string `json:"-"`
privateSigningKey ed25519.PrivateKey `json:"-"`
sig string `json:"-"`
allocationRoot string `json:"-"`
}

// OperationRequest represents an operation request with its related options.
Expand Down Expand Up @@ -436,7 +429,6 @@ func (a *Allocation) InitAllocation() {
}
}
}
a.generateAndSetOwnerSigningPublicKey()
a.startWorker(a.ctx)
InitCommitWorker(a.Blobbers)
InitBlockDownloader(a.Blobbers, downloadWorkerCount)
Expand All @@ -446,21 +438,6 @@ func (a *Allocation) InitAllocation() {
a.initialized = true
}

func (a *Allocation) generateAndSetOwnerSigningPublicKey() {
//create ecdsa public key from signature
privateSigningKey, err := generateOwnerSigningKey(a.OwnerPublicKey, a.Owner)
if err != nil {
l.Logger.Error("Failed to generate owner signing key", zap.Error(err))
return
}
if a.OwnerSigningPublicKey == "" {
pubKey := privateSigningKey.Public().(ed25519.PublicKey)
a.OwnerSigningPublicKey = hex.EncodeToString(pubKey)
//TODO: save this public key to blockchain
}
a.privateSigningKey = privateSigningKey
}

func (a *Allocation) isInitialized() bool {
return a.initialized && client.IsSDKInitialized()
}
Expand Down Expand Up @@ -1362,7 +1339,6 @@ func (a *Allocation) generateDownloadRequest(
downloadReq.allocOwnerID = a.Owner
downloadReq.sig = a.sig
downloadReq.allocOwnerPubKey = a.OwnerPublicKey
downloadReq.allocOwnerSigningPubKey = a.OwnerSigningPublicKey
downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx)
downloadReq.fileHandler = fileHandler
downloadReq.localFilePath = localFilePath
Expand Down Expand Up @@ -2796,7 +2772,6 @@ func (a *Allocation) downloadFromAuthTicket(fileHandler sys.File, authTicket str
downloadReq.sig = a.sig
downloadReq.allocOwnerID = a.Owner
downloadReq.allocOwnerPubKey = a.OwnerPublicKey
downloadReq.allocOwnerSigningPubKey = a.OwnerSigningPublicKey
downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx)
downloadReq.fileHandler = fileHandler
downloadReq.localFilePath = localFilePath
Expand Down
33 changes: 1 addition & 32 deletions zboxcore/sdk/blobber_operations.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package sdk

import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"math"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/zboxcore/logger"
"go.uber.org/zap"
"math"
)

// CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`).
Expand Down Expand Up @@ -55,20 +48,12 @@ func CreateAllocationForOwner(
return "", 0, nil, sdkNotInitialized
}

privateSigningKey, err := generateOwnerSigningKey(ownerpublickey, owner)
if err != nil {
return "", 0, nil, errors.New("failed_generate_owner_signing_key", "failed to generate owner signing key: "+err.Error())
}
pub := privateSigningKey.Public().(ed25519.PublicKey)
pk := hex.EncodeToString(pub)

allocationRequest["owner_id"] = owner
allocationRequest["owner_public_key"] = ownerpublickey
allocationRequest["third_party_extendable"] = thirdPartyExtendable
allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams)
allocationRequest["is_enterprise"] = IsEnterprise
allocationRequest["storage_version"] = StorageV2
allocationRequest["owner_signing_public_key"] = pk

var sn = transaction.SmartContractTxnData{
Name: transaction.NEW_ALLOCATION_REQUEST,
Expand Down Expand Up @@ -320,19 +305,3 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err
hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true)
return
}

func generateOwnerSigningKey(ownerPublicKey, ownerID string) (ed25519.PrivateKey, error) {
if ownerPublicKey == "" {
return nil, errors.New("owner_public_key_required", "owner public key is required")
}
hashData := fmt.Sprintf("%s:%s", ownerPublicKey, "owner_signing_public_key")
sig, err := client.Sign(encryption.Hash(hashData), ownerID)
if err != nil {
logger.Logger.Error("error during sign", zap.Error(err))
return nil, err
}
//use this signature as entropy to generate ecdsa key pair
decodedSig, _ := hex.DecodeString(sig)
privateSigningKey := ed25519.NewKeyFromSeed(decodedSig[:32])
return privateSigningKey, nil
}
2 changes: 1 addition & 1 deletion zboxcore/sdk/chunked_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func CreateChunkedUpload(

su.chunkReader = cReader

su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion, su.allocationObj.privateSigningKey)
su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion)

su.isRepair = isRepair
uploadWorker, uploadRequest := calculateWorkersAndRequests(su.allocationObj.DataShards, len(su.blobbers), su.chunkNumber)
Expand Down
51 changes: 13 additions & 38 deletions zboxcore/sdk/chunked_upload_form_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package sdk

import (
"bytes"
"crypto"
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"sync"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/client"

"golang.org/x/crypto/sha3"
)
Expand All @@ -39,16 +37,14 @@ type ChunkedUploadFormMetadata struct {
}

// CreateChunkedUploadFormBuilder create ChunkedUploadFormBuilder instance
func CreateChunkedUploadFormBuilder(storageVersion int, privateSigningKey ed25519.PrivateKey) ChunkedUploadFormBuilder {
func CreateChunkedUploadFormBuilder(storageVersion int) ChunkedUploadFormBuilder {
return &chunkedUploadFormBuilder{
storageVersion,
privateSigningKey,
}
}

type chunkedUploadFormBuilder struct {
storageVersion int
privateSigningKey ed25519.PrivateKey
storageVersion int
}

const MAX_BLOCKS = 80 // 5MB(CHUNK_SIZE*80)
Expand Down Expand Up @@ -100,10 +96,6 @@ func (b *chunkedUploadFormBuilder) Build(
CustomMeta: fileMeta.CustomMeta,
}

if b.privateSigningKey != nil {
formData.SignatureVersion = SignatureV2
}

for i := 0; i < numBodies; i++ {

startRange := i * MAX_BLOCKS
Expand Down Expand Up @@ -176,42 +168,25 @@ func (b *chunkedUploadFormBuilder) Build(
for err := range errChan {
return res, err
}
if b.privateSigningKey != nil {
decodedHash, _ := hex.DecodeString(fileMeta.ActualHash)
sig, err := b.privateSigningKey.Sign(nil, decodedHash, crypto.Hash(0))
if err != nil {
return res, err
}
formData.ActualFileHashSignature = hex.EncodeToString(sig)
} else {
sig, err := client.Sign(fileMeta.ActualHash)
if err != nil {
return res, err
}
formData.ActualFileHashSignature = sig
actualHashSignature, err := client.Sign(fileMeta.ActualHash)
if err != nil {
return res, err
}
hash := formData.ActualFileHashSignature + formData.ValidationRoot
hash := actualHashSignature + formData.ValidationRoot
if b.storageVersion == StorageV2 {
hashData := fmt.Sprintf("%s:%s:%s:%s", fileMeta.ActualHash, formData.ValidationRoot, formData.FixedMerkleRoot, blobberID)
hash = encryption.Hash(hashData)
}
if b.privateSigningKey != nil {
decodedHash, _ := hex.DecodeString(hash)
sig, err := b.privateSigningKey.Sign(nil, decodedHash, crypto.Hash(0))
if err != nil {
return res, err
}
formData.ValidationRootSignature = hex.EncodeToString(sig)
} else {
rootSig, err := client.Sign(hash)
if err != nil {
return res, err
}
formData.ValidationRootSignature = rootSig
validationRootSignature, err := client.Sign(hash)
if err != nil {
return res, err
}

formData.ActualHash = fileMeta.ActualHash
formData.ActualFileHashSignature = actualHashSignature
formData.ValidationRootSignature = validationRootSignature
formData.ActualSize = fileMeta.ActualSize

}

thumbnailSize := len(thumbnailChunkData)
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/chunked_upload_form_builder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) {

for i := 0; i < b.N; i++ {

builder := CreateChunkedUploadFormBuilder(0, nil)
builder := CreateChunkedUploadFormBuilder(0)

isFinal := false

Expand Down
16 changes: 7 additions & 9 deletions zboxcore/sdk/chunked_upload_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"golang.org/x/crypto/sha3"
)

const SignatureV2 = 1

// ChunkedUpload upload manager with chunked upload feature
type ChunkedUpload struct {
consensus Consensus
Expand Down Expand Up @@ -166,13 +164,13 @@ type UploadFormData struct {
EncryptedKey string `json:"encrypted_key,omitempty"`
EncryptedKeyPoint string `json:"encrypted_key_point,omitempty"`

IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded
ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks.
ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash
ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default
UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to
Size int64 `json:"size"` // total size of shard
SignatureVersion int `json:"signature_version,omitempty"`
IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded
ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks.
ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash
ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default
UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to
Size int64 `json:"size"` // total size of shard

}

// UploadProgress progress of upload
Expand Down
5 changes: 1 addition & 4 deletions zboxcore/sdk/chunked_upload_process_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package sdk
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -55,7 +54,6 @@ type ChunkedUploadFormInfo struct {
AllocationTx string
OnlyHash bool
StorageVersion int
PrivateSigningKey ed25519.PrivateKey
}

// createUploadProgress create a new UploadProgress
Expand Down Expand Up @@ -120,7 +118,6 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int,
AllocationTx: su.allocationObj.Tx,
OnlyHash: chunkEndIndex <= su.progress.ChunkIndex,
StorageVersion: su.allocationObj.StorageVersion,
PrivateSigningKey: su.allocationObj.privateSigningKey,
}
formInfoJSON, err := json.Marshal(formInfo)
if err != nil {
Expand Down Expand Up @@ -458,7 +455,7 @@ func ProcessEventData(data safejs.Value) {
defer delete(hasherMap, fileMeta.RemotePath)
}
blobberID := os.Getenv("BLOBBER_ID")
formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion, formInfo.PrivateSigningKey)
formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion)
uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint,
fileShards, thumbnailChunkData, formInfo.ShardSize)
if err != nil {
Expand Down
Loading

0 comments on commit d148f0b

Please sign in to comment.