1

Is there a way to get the chaincode metadata using the NodeJS or Go fabric-network SDK?

Something similar to the peer lifecycle chaincode queryinstalled command:

{
    "installed_chaincodes": [
            {
                    "package_id": "testcc_1:75afd7c4c165c56e8b8f3bd4c53cea8b420f4d94a3d53093aa0ec0229f5c738a",
                    "label": "testcc_1",
                    "references": {
                            "mychannel": {
                                    "chaincodes": [
                                            {
                                                    "name": "testcc",
                                                    "version": "1"
                                            }
                                    ]
                            }
                    }
            }
    ]
}

If not, then how do clients ensure which version of the chaincode they are calling??

2 Answers2

1

Every SDK implements almost all the methods available in cli.

In case of go-sdk, you can implement something like this.

import (
    "fmt"
    "strings"

    "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
    "github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
    fabAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
    lcpackager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/lifecycle"
    "github.com/pkg/errors"
)


// QueryInstalledCC : query installed CC
func QueryInstalledCC(setup *OrgSetup, ccName, ccVersion, packageID string) (string, string, map[string][]resmgmt.CCReference, error) {

    label, _, _ := PackageCC(ccName, ccVersion)

    resp, err := setup.Resmgmt.LifecycleQueryInstalledCC(resmgmt.WithTargetEndpoints(peer1), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
    if err != nil {
        fmt.Printf("\n Error occurred in queryInstalledCC func and error is %s", err)
    }

    if !strings.EqualFold(packageID, resp[0].PackageID) {
        fmt.Print("Unable to match packageID in QueryInstalledCC")
        return "", "", nil, nil
    }

    if !strings.EqualFold(label, resp[0].Label) {
        fmt.Print("Unable to match labels in QueryInstalledCC")
        return "", "", nil, nil
    }

    ref := resp[0].References

    return resp[0].Label, resp[0].PackageID, ref, nil
}

metadata
  • 1,139
  • 1
  • 7
  • 28
  • Thank you for the example with go. Is it possible to do the same thing with the NodeJS SDK? – zenoparadox Aug 24 '20 at 06:45
  • yes it is possible. `Node-sdk` is first one to get all the latest updates. I never used `node-sdk` hence I didn't share any snippet. you can find all the examples here: https://github.com/hyperledger/fabric-samples – metadata Aug 24 '20 at 06:57
  • I couldn't find any pkg client module in the Node SDK, or anything related to the chaincode lifecycle queries. Maybe it's only in go? – zenoparadox Aug 24 '20 at 09:10
  • yes it is possible or node-sdk might using a different approach. you can ask it in the official chat group : https://chat.hyperledger.org/channel/fabric-sdk-node – metadata Aug 24 '20 at 10:13
0

peer chaincode query -C mychannel -n basic -c '{"Args":["org.hyperledger.fabric:GetMetadata"]}'

  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Aug 06 '23 at 13:55
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 13 '23 at 01:12