Skip to content

Instantly share code, notes, and snippets.

@cayblood
Last active February 26, 2020 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cayblood/5d4e5cab47c77ed42efb35790fd375cb to your computer and use it in GitHub Desktop.
Save cayblood/5d4e5cab47c77ed42efb35790fd375cb to your computer and use it in GitHub Desktop.
example of how to use hyperledger fabric permissions
'use strict';
const shim = require('fabric-shim');
async function requireRole(stub, role) {
const ClientIdentity = shim.ClientIdentity;
let cid = new ClientIdentity(stub);
if (!cid.assertAttributeValue('role', role))
throw new Error(`Unauthorized access: ${role} required`);
}
let Chaincode = class {
async Init(stub) {
console.info('============= Init called =============');
return shim.success();
}
async Invoke(stub) {
console.info('============= Invoke called =============');
try {
await requireRole(stub, 'admin');
} catch(err) {
console.error("Error during Invoke: ", err);
return shim.error(err.message || err);
}
console.info('SUCCESS!')
return shim.success();
}
}
shim.start(new Chaincode());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment