Skip to content

Commit 3da56c1

Browse files
authored
Merge pull request #1494 from 0chain/feat/session-key
Session key
2 parents 2c9787f + 2dbc985 commit 3da56c1

File tree

17 files changed

+144
-54
lines changed

17 files changed

+144
-54
lines changed

code/go/0chain.net/blobbercore/allocation/entity.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ type Allocation struct {
6262
TimeUnit time.Duration `gorm:"column:time_unit;not null;default:172800000000000"`
6363
StartTime common.Timestamp `gorm:"column:start_time;not null"`
6464
// Ending and cleaning
65-
CleanedUp bool `gorm:"column:cleaned_up;not null;default:false"`
66-
Finalized bool `gorm:"column:finalized;not null;default:false"`
65+
CleanedUp bool `gorm:"column:cleaned_up;not null;default:false"`
66+
Finalized bool `gorm:"column:finalized;not null;default:false"`
67+
OwnerSigningPublicKey string `gorm:"column:owner_signing_public_key;size:512;not null" json:"owner_signing_public_key"`
6768

6869
// FileOptions to define file restrictions on an allocation for third-parties
6970
// default 00000000 for all crud operations suggesting only owner has the below listed abilities.

code/go/0chain.net/blobbercore/allocation/file_changer_base.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ type BaseFileChanger struct {
5252
EncryptedKeyPoint string `json:"encrypted_key_point,omitempty"`
5353
CustomMeta string `json:"custom_meta,omitempty"`
5454

55-
ChunkSize int64 `json:"chunk_size,omitempty"` // the size of achunk. 64*1024 is default
56-
IsFinal bool `json:"is_final,omitempty"` // current chunk is last or not
55+
ChunkSize int64 `json:"chunk_size,omitempty"` // the size of achunk. 64*1024 is default
56+
IsFinal bool `json:"is_final,omitempty"` // current chunk is last or not
57+
SignatureVersion int `json:"signature_version,omitempty"`
5758

5859
ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks.
5960
ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of CompactMerkleTree

code/go/0chain.net/blobbercore/allocation/file_changer_update.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (nf *UpdateFileChanger) ApplyChange(ctx context.Context, rootRef *reference
106106
fileRef.ChunkSize = nf.ChunkSize
107107
fileRef.IsPrecommit = true
108108
fileRef.FilestoreVersion = filestore.VERSION
109+
fileRef.SignatureVersion = nf.SignatureVersion
109110

110111
return rootRef, nil
111112
}
@@ -173,6 +174,7 @@ func (nf *UpdateFileChanger) ApplyChangeV2(ctx context.Context, allocationRoot,
173174
PathLevel: len(strings.Split(strings.TrimRight(nf.Path, "/"), "/")),
174175
NumBlocks: int64(math.Ceil(float64(nf.Size*1.0) / float64(nf.ChunkSize))),
175176
NumUpdates: refResult.NumUpdates + 1,
177+
SignatureVersion: nf.SignatureVersion,
176178
}
177179
nf.storageVersion = 1
178180
newFile.FileMetaHash = encryption.Hash(newFile.GetFileMetaHashDataV2())

code/go/0chain.net/blobbercore/allocation/file_changer_upload.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (nf *UploadFileChanger) applyChange(ctx context.Context, rootRef *reference
130130
HashToBeComputed: true,
131131
IsPrecommit: true,
132132
FilestoreVersion: filestore.VERSION,
133+
SignatureVersion: nf.SignatureVersion,
133134
}
134135

135136
fileID, ok := fileIDMeta[newFile.Path]
@@ -197,6 +198,7 @@ func (nf *UploadFileChanger) ApplyChangeV2(ctx context.Context, allocationRoot,
197198
PathLevel: len(strings.Split(strings.TrimRight(nf.Path, "/"), "/")),
198199
NumBlocks: int64(math.Ceil(float64(nf.Size*1.0) / float64(nf.ChunkSize))),
199200
NumUpdates: 1,
201+
SignatureVersion: nf.SignatureVersion,
200202
}
201203
nf.storageVersion = 1
202204
newFile.FileMetaHash = encryption.Hash(newFile.GetFileMetaHashDataV2())

code/go/0chain.net/blobbercore/allocation/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc
110110
a.TimeUnit = sa.TimeUnit
111111
a.FileOptions = sa.FileOptions
112112
a.StartTime = sa.StartTime
113-
// Only for testing purpose
114113
a.StorageVersion = uint8(sa.StorageVersion)
114+
a.OwnerSigningPublicKey = sa.OwnerSigningPublicKey
115115

116116
m := map[string]interface{}{
117117
"allocation_id": a.ID,

code/go/0chain.net/blobbercore/allocation/workers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation, sa *transaction.St
246246
a.Tx = sa.Tx
247247
a.OwnerID = sa.OwnerID
248248
a.OwnerPublicKey = sa.OwnerPublicKey
249+
a.OwnerSigningPublicKey = sa.OwnerSigningPublicKey
249250

250251
// // update fields
251252
a.Expiration = sa.Expiration
@@ -263,6 +264,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation, sa *transaction.St
263264
updateMap["finalized"] = a.Finalized
264265
updateMap["file_options"] = a.FileOptions
265266
updateMap["blobber_size"] = a.BlobberSize
267+
updateMap["owner_signing_public_key"] = a.OwnerSigningPublicKey
266268

267269
updateOption := func(alloc *Allocation) {
268270
alloc.Tx = a.Tx
@@ -273,6 +275,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation, sa *transaction.St
273275
alloc.Finalized = a.Finalized
274276
alloc.FileOptions = a.FileOptions
275277
alloc.BlobberSize = a.BlobberSize
278+
alloc.OwnerSigningPublicKey = a.OwnerSigningPublicKey
276279
}
277280

278281
// update terms

code/go/0chain.net/blobbercore/allocation/zcn.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func SyncAllocation(allocationId string) (*Allocation, error) {
5252
alloc.Finalized = sa.Finalized
5353
alloc.TimeUnit = sa.TimeUnit
5454
alloc.FileOptions = sa.FileOptions
55+
alloc.StorageVersion = uint8(sa.StorageVersion)
56+
alloc.OwnerSigningPublicKey = sa.OwnerSigningPublicKey
5557

5658
// related terms
5759
terms := make([]*Terms, 0, len(sa.BlobberDetails))

code/go/0chain.net/blobbercore/challenge/protocol.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"errors"
8-
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
9-
coreTxn "github.com/0chain/gosdk/core/transaction"
108
"math/rand"
119
"strings"
1210
"sync"
1311
"time"
1412

13+
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
14+
coreTxn "github.com/0chain/gosdk/core/transaction"
15+
1516
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/allocation"
1617
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
1718
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/filestore"
@@ -366,6 +367,7 @@ func (cr *ChallengeEntity) getPostDataV2(ctx context.Context, allocationObj *all
366367
FixedMerkleRoot: ref.FixedMerkleRoot,
367368
Size: ref.Size,
368369
FileMetaHash: ref.FileMetaHash,
370+
SignatureVersion: ref.SignatureVersion,
369371
}
370372
postData["meta"] = metaRef
371373
}

code/go/0chain.net/blobbercore/handler/file_command_update.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,15 @@ func (cmd *UpdateFileCommand) ProcessContent(ctx context.Context, allocationObj
155155
hashData := fmt.Sprintf("%s:%s:%s:%s", cmd.fileChanger.ActualHash, cmd.fileChanger.ValidationRoot, cmd.fileChanger.FixedMerkleRoot, node.Self.ID)
156156
hash = encryption.Hash(hashData)
157157
}
158-
verify, err := encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
158+
var (
159+
err error
160+
verify bool
161+
)
162+
if cmd.fileChanger.SignatureVersion == reference.SignatureV2 {
163+
verify, err = encryption.VerifyEd25519(allocationObj.OwnerSigningPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
164+
} else {
165+
verify, err = encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
166+
}
159167
if err != nil || !verify {
160168
logging.Logger.Error("UpdateFileCommand.VerifySignature", zap.Error(err))
161169
return result, common.NewError("update_error", "Failed to verify validation root signature. ")

code/go/0chain.net/blobbercore/handler/file_command_upload.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ func (cmd *UploadFileCommand) ProcessContent(ctx context.Context, allocationObj
167167
hashData := fmt.Sprintf("%s:%s:%s:%s", cmd.fileChanger.ActualHash, cmd.fileChanger.ValidationRoot, cmd.fileChanger.FixedMerkleRoot, node.Self.ID)
168168
hash = encryption.Hash(hashData)
169169
}
170-
verify, err := encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
170+
var (
171+
err error
172+
verify bool
173+
)
174+
if cmd.fileChanger.SignatureVersion == reference.SignatureV2 {
175+
verify, err = encryption.VerifyEd25519(allocationObj.OwnerSigningPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
176+
} else {
177+
verify, err = encryption.Verify(allocationObj.OwnerPublicKey, cmd.fileChanger.ValidationRootSignature, hash)
178+
}
171179
if err != nil || !verify {
172180
logging.Logger.Error("UploadFileCommand.VerifySignature", zap.Error(err))
173181
return result, common.NewError("upload_error", "Failed to verify validation root signature. ")

0 commit comments

Comments
 (0)