Search code examples
node.jshyperledger-fabrichyperledgerhyperledger-chaincodechaincode

Get MSPID from a command line request in chaincode


I'm currently trying to evaluate the requester MSPID to authorize a specific list of Members being able to request a function on chaincode, but when I ask for "stub.getCreator().mspId" it always give me "undefined"

I'm currently calling the function with command like via "docker exec". I checked that my transaction should be signed before to "getCreator" to work, but I don't know if it's possible to sign a transaction with a command line call.

My command line request is:

docker exec cli.example.com peer chaincode invoke -o orderer.example.com:7050 -C examplechannel -c '{"Args":["createVcc", "{ \"date\": 12345, \"reference\": \"anything\", \"serialNumber\": \"BR-12345\", \"presentedTo\": \"Example project\", \"quantity\": 22279 }"]}' -n example

The validation function:

const isAdmin = (func, creator) => {
    if (!adminList.includes(creator)) {
        throw new Error(`Organization ${creator} does not have access to perform function ${func}`);
    }
}

Using validation function in chaincode:

async (stub, data) => {
...
        isAdmin('createVcc', stub.getCreator().mspId);
...
}

And I'm receiving:

Error: endorsement failure during invoke. response: status:500 message:"transaction returned with failure: Organization undefined does not have access to perform function createVcc"

I expect to "getCreator().mspid" not to be undefined, anyone knows what could solve my question ?


Solution

  • Try below my code snippet

        const ClientIdentity = require('fabric-shim').ClientIdentity;
    
        let cid = new ClientIdentity(stub);
    
        let mspID = cid.getMSPID()
    
        let isAuthorized = false;
    
        if (cid.assertAttributeValue('createVcc', mspID){
            isAuthorized = true;
        }