diff --git a/apis/fabric-shim-api/types/index.d.ts b/apis/fabric-shim-api/types/index.d.ts index d1d2fa7c..5c076296 100644 --- a/apis/fabric-shim-api/types/index.d.ts +++ b/apis/fabric-shim-api/types/index.d.ts @@ -54,6 +54,7 @@ declare module 'fabric-shim-api' { getTxID(): string; getChannelID(): string; getCreator(): SerializedIdentity; + getMspID(): string; getTransient(): Map; getSignedProposal(): ChaincodeProposal.SignedProposal; diff --git a/libraries/fabric-shim/lib/stub.js b/libraries/fabric-shim/lib/stub.js index 3d673eba..324cbe08 100644 --- a/libraries/fabric-shim/lib/stub.js +++ b/libraries/fabric-shim/lib/stub.js @@ -296,6 +296,18 @@ class ChaincodeStub { return this.creator; } + /** + * Returns the MSPID of the peer that started this chaincode + * @returns {string} MSPID + */ + getMspID() { + if ('CORE_PEER_LOCALMSPID' in process.env) { + return process.env.CORE_PEER_LOCALMSPID; + } else { + throw new Error('CORE_PEER_LOCALMSPID is unset in chaincode process'); + } + } + /** * Returns the transient map that can be used by the chaincode but not * saved in the ledger, such as cryptographic information for encryption and decryption diff --git a/libraries/fabric-shim/test/unit/stub.js b/libraries/fabric-shim/test/unit/stub.js index cfdbec22..ccba16ed 100644 --- a/libraries/fabric-shim/test/unit/stub.js +++ b/libraries/fabric-shim/test/unit/stub.js @@ -527,6 +527,43 @@ describe('Stub', () => { }); }); + describe('getMspID', () => { + let mspID; + + beforeEach(() => { + if ('CORE_PEER_LOCALMSPID' in process.env) { + mspID = process.env.CORE_PEER_LOCALMSPID; + } + }); + + afterEach(() => { + delete process.env.CORE_PEER_LOCALMSPID; + if (mspID) { + process.env.CORE_PEER_LOCALMSPID = mspID; + } + }); + + it ('should return MSPID', () => { + process.env.CORE_PEER_LOCALMSPID = 'some MSPID'; + + const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', { + args: [] + }); + + expect(stub.getMspID()).to.deep.equal('some MSPID'); + }); + + it ('should throw Error if MSPID is not available', () => { + const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', { + args: [] + }); + + expect(() => { + stub.getMspID() + }).to.throw('CORE_PEER_LOCALMSPID is unset in chaincode process'); + }); + }); + describe('getTransient', () => { it ('should return transient map', () => { const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', { diff --git a/test/chaincodes/clientidentity/chaincode.js b/test/chaincodes/clientidentity/chaincode.js index cdf2a8e5..0382b7fb 100644 --- a/test/chaincodes/clientidentity/chaincode.js +++ b/test/chaincodes/clientidentity/chaincode.js @@ -37,5 +37,10 @@ class ClientIdentityChaincode extends Contract { return {mspId: cid.mspId, id: cid.id}; } + async localMspID({stub}) { + const localMspID = stub.getMspID(); + return {localMspID}; + } + } module.exports = ClientIdentityChaincode; \ No newline at end of file diff --git a/test/fv/clientidentity.js b/test/fv/clientidentity.js index 768ac612..ddbe18a6 100644 --- a/test/fv/clientidentity.js +++ b/test/fv/clientidentity.js @@ -28,4 +28,11 @@ describe('Chaincode clientidentity', () => { expect(payload.id).to.equal('x509::/C=US/ST=California/L=San Francisco/CN=Admin@org2.example.com::/C=US/ST=California/L=San Francisco/O=org2.example.com/CN=ca.org2.example.com', 'Test getID()'); }); + it('should be able to check the peer MSPID', async function () { + this.timeout(MED_STEP); + + const payload = JSON.parse(await utils.query(suite, 'org.mynamespace.clientidentity:localMspID', [])); + expect(payload.localMspID).to.equal('Org2MSP', 'Test stub.getMspID()'); + }); + }); \ No newline at end of file