Search code examples
hyperledger-fabricchaincode

how to query chaincode metada with hyperledger fabric network SDK


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??


Solution

  • 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
    }