hyperledger-fabricsha256hyperledger-chaincode

Is there a reason I can't see/store a SHA256 in a Hyperledger Fabric worldstate?


I have been working on testing hyperledger fabric to store details on a selection of files and folders including sha256 keys to track updates made to a file. I have installed the test network, I am able to get the network running correctly and have installed and defined the chaincode to work. Everything is working perfectly, except when I query the assets the only field missing is the sha256 key.

Is there a reason for this?

This is how the ledger should be initialised:

// InitLedger adds a base set of assets to the ledger func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error { assets := []Asset{ {ID: "asset1", sha256: "3654dda35f2604fe43af8b49fae361b0ab77850fc4b9b2d21f098bed8bfdf376", Size: 56, ModifiedOn: "06.04.2022 09:29"}, {ID: "asset2", sha256: "fe8979c8c7b6f69b393ff1bbd688d3cafa3fd3e8efc1955682cc685517bd64b7", Size: 9756, ModifiedOn: "28.05.2020 10:26"}, {ID: "asset3", sha256: "c023e9f6c1fb3d0ba9894123e0c58c46039863dea617e9007569f76014334206", Size: 5992, ModifiedOn: "20.03.2022 08:53"}, {ID: "asset4", sha256: "5f87db405f88f6788710a6f21c4dd74221716b4832a17d3cbd4701741c716a9b", Size: 45336, ModifiedOn: "21.09.2016 11:21"}, {ID: "asset5", sha256: "367c38d5833e63e19a2053c2787b9913ed45e406d947a55fc08b21d03c60a9d7", Size: 108, ModifiedOn: "13.08.2013 10:40"}, {ID: "asset6", sha256: "d1652041e169ddfe0ee32cd0f8b4ef6f968dbd2dd119a3769dd44081661bf202", Size: 5308, ModifiedOn: "21.02.2019 17:00"}, {ID: "asset7", sha256: "2f9684e36bace94d4791160a31e0595e4cae7a9f17003d460f01c7e1ed1e8b04", Size: 8704, ModifiedOn: "12.04.2022 17:02"}, {ID: "asset8", sha256: "702b9d6ddd7d850ca7ae06d9521ef5400ea5d8a2e6ffa75737560ef54b3a1ba1", Size: 13436, ModifiedOn: "27.02.2022 09:22"}, {ID: "asset9", sha256: "a81f09ae679dbf4c597060d6de14283b69c7c1a0c0fbc6e484c5762d0c19ab26", Size: 159732, ModifiedOn: "27.04.2022 13:35"}, {ID: "asset10", sha256: "6e166d4f7c464cff392f3d4e3e13bd780243a9793d19b95a1eb1df896ab9162a", Size: 322120, ModifiedOn: "25.05.2022 09:41"}, }

This is the result of a query:

peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}' [{"ID":"asset1","ModifiedOn":"06.04.2022 09:29","Size":56},{"ID":"asset10","ModifiedOn":"25.05.2022 09:41","Size":322120},{"ID":"asset2","ModifiedOn":"28.05.2020 10:26","Size":9756},{"ID":"asset3","ModifiedOn":"20.03.2022 08:53","Size":5992},{"ID":"asset4","ModifiedOn":"21.09.2016 11:21","Size":45336},{"ID":"asset5","ModifiedOn":"13.08.2013 10:40","Size":108}, {"ID":"asset6","ModifiedOn":"21.02.2019 17:00","Size":5308},{"ID":"asset7","ModifiedOn":"12.04.2022 17:02","Size":8704},{"ID":"asset8","ModifiedOn":"27.02.2022 09:22","Size":13436},{"ID":"asset9","ModifiedOn":"27.04.2022 13:35","Size":159732}]

The sha256 is just missing. Any help on this would be greatly appreciated.


Solution

  • You are marshalling the asset to JSON to store in the ledger. The Go JSON marshaller only includes exported fields. For example:

    type Asset struct {
        sha256 string `json:"privateSHA256"`
        SHA256 string `json:"publicSHA256"`
    }
    
    func main() {
    asset := &Asset{
        sha256: "privateSHA256Value",
        SHA256: "publicSHA256Value",
    }
    
        assetJSON, err := json.MarshalIndent(asset, "", "    ")
        if err != nil {
            panic(err)
        }
    
        fmt.Printf("Asset: %#v\n", asset)
        fmt.Printf("JSON: %s\n", assetJSON)
    }
    

    Produces the following output:

    Asset: &main.Asset{sha256:"privateSHA256Value", SHA256:"publicSHA256Value"}
    JSON: {
        "publicSHA256": "publicSHA256Value"
    }