mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-16 18:37:51 +01:00
Add node ID field to file structure
This commit is contained in:
@@ -36,6 +36,7 @@ import (
|
||||
// It has no upper size limit, although a soft limit of 64 KB - overhead is encouraged for efficiency.
|
||||
type Block struct {
|
||||
OwnerPublicKey *btcec.PublicKey // Owner Public Key, ECDSA (secp256k1) 257-bit
|
||||
NodeID []byte // Node ID of the owner (derived from the public key)
|
||||
LastBlockHash []byte // Hash of the last block. Blake3.
|
||||
BlockchainVersion uint64 // Blockchain version
|
||||
Number uint64 // Block number
|
||||
@@ -67,6 +68,8 @@ func decodeBlock(raw []byte) (block *Block, err error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block.NodeID = PublicKey2NodeID(block.OwnerPublicKey)
|
||||
|
||||
block.LastBlockHash = make([]byte, hashSize)
|
||||
copy(block.LastBlockHash, raw[65:65+hashSize])
|
||||
|
||||
|
||||
@@ -57,10 +57,11 @@ const (
|
||||
// BlockRecordFile is the metadata of a file published on the blockchain
|
||||
type BlockRecordFile struct {
|
||||
Hash []byte // Hash of the file data
|
||||
ID uuid.UUID // ID
|
||||
ID uuid.UUID // ID of the file
|
||||
Type uint8 // File Type
|
||||
Format uint16 // File Format
|
||||
Size uint64 // Size of the file data
|
||||
NodeID []byte // Node ID, owner of the file
|
||||
Tags []BlockRecordFileTag // Tags provide additional metadata
|
||||
}
|
||||
|
||||
@@ -76,7 +77,7 @@ type BlockRecordFileTag struct {
|
||||
// ---- low-level encoding ----
|
||||
|
||||
// decodeBlockRecordFiles decodes only file records. Other records are ignored.
|
||||
func decodeBlockRecordFiles(recordsRaw []BlockRecordRaw) (files []BlockRecordFile, err error) {
|
||||
func decodeBlockRecordFiles(recordsRaw []BlockRecordRaw, nodeID []byte) (files []BlockRecordFile, err error) {
|
||||
for i, record := range recordsRaw {
|
||||
switch record.Type {
|
||||
case RecordTypeFile:
|
||||
@@ -84,7 +85,7 @@ func decodeBlockRecordFiles(recordsRaw []BlockRecordRaw) (files []BlockRecordFil
|
||||
return nil, errors.New("file record invalid size")
|
||||
}
|
||||
|
||||
file := BlockRecordFile{}
|
||||
file := BlockRecordFile{NodeID: nodeID}
|
||||
file.Hash = make([]byte, hashSize)
|
||||
copy(file.Hash, record.Data[0:0+hashSize])
|
||||
copy(file.ID[:], record.Data[32:32+16])
|
||||
@@ -243,7 +244,7 @@ type BlockDecoded struct {
|
||||
func decodeBlockRecords(block *Block) (decoded *BlockDecoded, err error) {
|
||||
decoded = &BlockDecoded{Block: *block}
|
||||
|
||||
files, err := decodeBlockRecordFiles(block.RecordsRaw)
|
||||
files, err := decodeBlockRecordFiles(block.RecordsRaw, block.NodeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -348,7 +348,7 @@ func UserBlockchainAddFiles(files []BlockRecordFile) (newHeight, newVersion uint
|
||||
// If there is a corruption in the blockchain it will stop reading but return the files parsed so far.
|
||||
func UserBlockchainListFiles() (files []BlockRecordFile, status int) {
|
||||
status = blockchainIterate(func(block *Block) (statusI int) {
|
||||
filesMore, err := decodeBlockRecordFiles(block.RecordsRaw)
|
||||
filesMore, err := decodeBlockRecordFiles(block.RecordsRaw, block.NodeID)
|
||||
if err != nil {
|
||||
return BlockchainStatusCorruptBlockRecord
|
||||
}
|
||||
@@ -455,7 +455,7 @@ func UserBlockchainDeleteFiles(IDs []uuid.UUID) (newHeight, newVersion uint64, s
|
||||
return 0 // no action on record
|
||||
}
|
||||
|
||||
filesDecoded, err := decodeBlockRecordFiles([]BlockRecordRaw{*record})
|
||||
filesDecoded, err := decodeBlockRecordFiles([]BlockRecordRaw{*record}, nil)
|
||||
if err != nil || len(filesDecoded) != 1 {
|
||||
return 3 // error blockchain corrupt
|
||||
}
|
||||
|
||||
@@ -106,7 +106,8 @@ type apiBlockRecordFile struct {
|
||||
Folder string `json:"folder"` // Folder, optional
|
||||
Name string `json:"name"` // Name of the file
|
||||
Description string `json:"description"` // Description. This is expected to be multiline and contain hashtags!
|
||||
Date time.Time `json:"date"` // Date of the virtual file
|
||||
Date time.Time `json:"date"` // Date shared
|
||||
NodeID []byte `json:"nodeid"` // Node ID, owner of the file
|
||||
Metadata []apiFileMetadata `json:"metadata"` // Additional metadata.
|
||||
}
|
||||
|
||||
@@ -228,7 +229,7 @@ func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
// --- conversion from core to API data ---
|
||||
|
||||
func blockRecordFileToAPI(input core.BlockRecordFile) (output apiBlockRecordFile) {
|
||||
output = apiBlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size, Metadata: []apiFileMetadata{}}
|
||||
output = apiBlockRecordFile{ID: input.ID, Hash: input.Hash, NodeID: input.NodeID, Type: input.Type, Format: input.Format, Size: input.Size, Metadata: []apiFileMetadata{}}
|
||||
|
||||
for _, tag := range input.Tags {
|
||||
switch tag.Type {
|
||||
|
||||
@@ -159,6 +159,9 @@ func createTestResult(fileType int) (file core.BlockRecordFile) {
|
||||
file.ID = uuid.New()
|
||||
file.Size = uint64(len(randomData))
|
||||
|
||||
file.NodeID = make([]byte, 32) // node ID = blake3 hash of peer ID
|
||||
rand.Read(file.NodeID)
|
||||
|
||||
if fileType == -1 {
|
||||
switch file.Format {
|
||||
case core.FormatCSV, core.FormatEmail, core.FormatText, core.FormatHTML:
|
||||
|
||||
@@ -181,7 +181,7 @@ The array `RecordsDecoded` will contain any present record of the following:
|
||||
|
||||
## File Functions
|
||||
|
||||
These functions allow adding, deleting, and listing files stored on the users blockchain. Only metadata is actually stored on the blockchain.
|
||||
These functions allow adding, deleting, and listing files stored on the users blockchain. Only metadata is actually stored on the blockchain. To download a remote file both the file hash and the node ID are required. The node ID specifies the owner of the file.
|
||||
|
||||
```go
|
||||
type apiBlockRecordFile struct {
|
||||
@@ -193,7 +193,8 @@ type apiBlockRecordFile struct {
|
||||
Folder string `json:"folder"` // Folder, optional
|
||||
Name string `json:"name"` // Name of the file
|
||||
Description string `json:"description"` // Description. This is expected to be multiline and contain hashtags!
|
||||
Date time.Time `json:"date"` // Date of the virtual file
|
||||
Date time.Time `json:"date"` // Date shared
|
||||
NodeID []byte `json:"nodeid"` // Node ID, owner of the file
|
||||
Metadata []apiFileMetadata `json:"metadata"` // Additional metadata.
|
||||
}
|
||||
|
||||
@@ -296,6 +297,7 @@ Example response:
|
||||
"name": "Test.txt",
|
||||
"description": "",
|
||||
"date": "2021-08-27T14:59:13Z",
|
||||
"nodeid": "0Zo9QHCF06Nrbxgg9s4Q4wYpcHzsQhSMsmftQqjanVI=",
|
||||
"metadata": []
|
||||
}, {
|
||||
"id": "bc32cbae-011d-4f0b-80a8-281ca9369211",
|
||||
@@ -307,6 +309,7 @@ Example response:
|
||||
"name": "Test 2.txt",
|
||||
"description": "Example description\nThis can be any text #newfile #2021.",
|
||||
"date": "2021-09-27T23:33:37Z",
|
||||
"nodeid": "0Zo9QHCF06Nrbxgg9s4Q4wYpcHzsQhSMsmftQqjanVI=",
|
||||
"metadata": [{
|
||||
"type": 2,
|
||||
"name": "Date Created",
|
||||
@@ -566,6 +569,7 @@ Example response with dummy data:
|
||||
"name": "88d8cc57d5c2a5fea881ceea09503ee4.txt",
|
||||
"description": "",
|
||||
"date": "2021-09-23T00:00:00Z",
|
||||
"nodeid": "j4yHzmCXiXqg4DPhowj0DIOuuyJxQflo2QSNG3yhCK8=",
|
||||
"metadata": []
|
||||
}]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user