Composite Key functions in Hyperledger

前端 未结 3 1660
梦如初夏
梦如初夏 2021-01-15 01:02

I need to implement composite keys in hyperledger so that I could have a unique key based on the attributes put into the ledger. The function CreateCompositeKey(object

相关标签:
3条回答
  • 2021-01-15 01:42

    In Hyperledger Fabric there is an example chaincode which shows how to use composite keys, check it out: Marbles

    Basically it almost as you said:

    key, err := stub.CreateCompositeKey(index, []string{key1, key2, key3})
    
    // Skiped
    
    stub.PutState(key, value)
    
    0 讨论(0)
  • 2021-01-15 01:48

    The function simply creates a key by combining the attributes into a single string. Its application is where we need to store multiple instances of one type on the ledger. The keys of those instances will be constructed from a combination of attributes— for example, "Order" + ID, yielding ["Order1","Order2", ...]. This function comes in hand when you intend to search for assets based on components of the key in range queries.

    The 'CreateCompositeKey' in SHIM construct a composite key (indeed, a unique key) based on a combination of several attributes.

    The inverse function is SplitCompositeKey which splits the compositeKey into attributes.

    func SplitCompositeKey(compositeKey string) (string, []string, error)

    The 'TestTradeWorkflow_Agreement' function is this code is also useful in understanding the whole process:

    0 讨论(0)
  • 2021-01-15 01:53

    For Java implementation it did not clearly come out in the documentation/ examples, did a little digging around and you can use 'compositeKey.toString()' as the composite key.

    Example below:

    final String compositeKey = stub.createCompositeKey("my-key-part-1", "my-key-part-2").toString();
    stub.putStringState(compositeKey, myJSONString); // use this
    stub.putState(compositeKey, myJSONString.getBytes()); // or this
    
    0 讨论(0)
提交回复
热议问题