From 2c20fa950dcb0f92102b5c811a571caa13817ae7 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Tue, 28 Sep 2021 19:55:30 +0200 Subject: [PATCH] Add node ID field to file structure --- Block Encoding.go | 3 +++ Block Records.go | 9 +++++---- Blockchain.go | 4 ++-- webapi/Blockchain.go | 5 +++-- webapi/Search Temp.go | 3 +++ webapi/readme.md | 8 ++++++-- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Block Encoding.go b/Block Encoding.go index ca695a3..d1b1d82 100644 --- a/Block Encoding.go +++ b/Block Encoding.go @@ -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]) diff --git a/Block Records.go b/Block Records.go index 77e650b..a7a4edc 100644 --- a/Block Records.go +++ b/Block Records.go @@ -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 } diff --git a/Blockchain.go b/Blockchain.go index e6c173f..737d809 100644 --- a/Blockchain.go +++ b/Blockchain.go @@ -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 } diff --git a/webapi/Blockchain.go b/webapi/Blockchain.go index 1d5479a..09dc73e 100644 --- a/webapi/Blockchain.go +++ b/webapi/Blockchain.go @@ -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 { diff --git a/webapi/Search Temp.go b/webapi/Search Temp.go index d7f7106..cd2ec8e 100644 --- a/webapi/Search Temp.go +++ b/webapi/Search Temp.go @@ -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: diff --git a/webapi/readme.md b/webapi/readme.md index e2b2de6..4f345df 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -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": [] }] }