Skip to content

Commit

Permalink
[FABCN-381] Access localmspid
Browse files Browse the repository at this point in the history
Signed-off-by: James Taylor <jamest@uk.ibm.com>
  • Loading branch information
jt-nti authored and mbwhite committed Feb 14, 2020
1 parent ec6caa6 commit e86cde6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis/fabric-shim-api/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ declare module 'fabric-shim-api' {
getTxID(): string;
getChannelID(): string;
getCreator(): SerializedIdentity;
getMspID(): string;
getTransient(): Map<string, Uint8Array>;

getSignedProposal(): ChaincodeProposal.SignedProposal;
Expand Down
12 changes: 12 additions & 0 deletions libraries/fabric-shim/lib/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions libraries/fabric-shim/test/unit/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
5 changes: 5 additions & 0 deletions test/chaincodes/clientidentity/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 7 additions & 0 deletions test/fv/clientidentity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()');
});

});

0 comments on commit e86cde6

Please sign in to comment.