API to get local MSPID in chaincode? #fabric #fabric-chaincode #fabric-questions

Yueming Xu

when my chaincode wants to write to the implicit private data collection, I need to get the local MSPID where the chaincode is deployed.  What is a good way to get it? check the ENV of CORE_PEER_LOCALMSPID?  Or should I use the CID's MSPID, assuming the client must match the peer's MSPID for private data?  or send the designated MSPID as part of the request parameter?

Tsvetan Georgiev

If I get your questions right, you want to fetch the MSPID of the peer that starts/endorses the chaincode. For that purpose you can use the chaincode shim API. In case it is java based chaincode you can look at the method String getMspId(); of the shim: https://github.com/hyperledger/fabric-chaincode-java/blob/master/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java
You have similar method in NodeJS shim: https://hyperledger.github.io/fabric-chaincode-node/release-2.1/api/fabric-shim.ChaincodeStub.html#getMspID__anchor

If you want to fetch the MSPID (as you mention above) of the user who requests for endorsement you can do that through the caller identity (CID) - see method getCreator() of the ChaincodeStub.

Yueming Xu

Thanks.  chaincode-go has the same API, shim.GetMSPID(), which returns the value of env CORE_PEER_LOCALMSPID.