André Avelar
André Avelar

Reputation: 31

Same struct in two different packages Golang

I have this piece of code:

import (
    "github.com/golang/protobuf/proto"
    "github.com/hyperledger/fabric-protos-go/common"
    "github.com/hyperledger/fabric-protos-go/peer"
        "github.com/hyperledger/fabric-lib-go/bccsp/sw"
    mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
)

.....

cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    if err != nil {
        log.Fatalf("Failed to create bccsp: %v", err)
    }

    bccsp := mspmgmt.GetIdentityDeserializer(channelId, cryptoProvider)
    if bccsp == nil {
        log.Fatalf("could not get msp for channel [%s]", channelId)
    }

And I am getting the following error:

> ./signatureValidator.go:30:54: cannot use cryptoProvider (variable of
> type "github.com/hyperledger/fabric-lib-go/bccsp".BCCSP) as
> "github.com/hyperledger/fabric/bccsp".BCCSP value in argument to
> mspmgmt.GetIdentityDeserializer:
> "github.com/hyperledger/fabric-lib-go/bccsp".BCCSP does not implement
> "github.com/hyperledger/fabric/bccsp".BCCSP (wrong type for method
> Decrypt)have Decrypt("github.com/hyperledger/fabric-lib-go/bccsp".Key,
> []byte, "github.com/hyperledger/fabric-lib-go/bccsp".DecrypterOpts)
> ([]byte, error)want Decrypt("github.com/hyperledger/fabric/bccsp".Key,
> []byte, "github.com/hyperledger/fabric/bccsp".DecrypterOpts) ([]byte,
> error)

----------------

Apparently cryptoProvider is an instance of a struct defined in github.com/hyperledger/fabric-lib-go/bccsp but GetIdentityDeserializer is asking for the same struct, but from github.com/hyperledger/fabric/bccsp.

I have tried removing all instances of github.com/hyperledger/fabric so that the program is forced to use the definition in the other repo. However, because I need to use github.com/hyperledger/fabric/msp/mgmt it imports all of /fabric in the go.mod.

I have no idea why the struct is defined twice with different names. Ideally both should use the definition in github.com/hyperledger/fabric-lib-go/bccsp. Does anyone have experience with this type of issue?

Upvotes: 0

Views: 79

Answers (3)

bestbeforetoday
bestbeforetoday

Reputation: 1684

The github.com/hyperledger/fabric/bccsp package was moved to fabric-lib-go in these pull requests:

If you are seeing bccsp in both fabric and fabric-lib-go then you are likely using mismatched versions of those modules.

Please also note that github.com/hyperledger/fabric is not intended to be used as a library. "Breaking" changes (like the package move above) may occur at any time.

Upvotes: 1

Mosharaf Hossain
Mosharaf Hossain

Reputation: 1449

import (
    "log"

    "github.com/hyperledger/fabric/bccsp/sw"
    "github.com/hyperledger/fabric/msp/mgmt"
)

func Validate() {
    cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    if err != nil {
        log.Fatalf("Failed to create bccsp: %v", err)
    }

    channelId := "1"
    deserializer := mgmt.GetIdentityDeserializer(channelId, cryptoProvider)
    if deserializer == nil {
        log.Fatalf("could not get msp for channel [%s]", channelId)
    }
}

This works - use the correct packages and run the following:

go get github.com/hyperledger/fabric/bccsp/sw
go get github.com/hyperledger/fabric/msp/mgmt

Upvotes: 1

André Avelar
André Avelar

Reputation: 31

I figured it out. "github.com/hyperledger/fabric/bccsp/sw" is also a thing and contains the same function (NewDefaultSecurityLevelWithKeystore). By not using the fabric-lib-go package I am not mixing different definitions of the BCCSP interface. I probably should not have been using those two packages at the same time in the first place.

Upvotes: 2

Related Questions