Last active
February 26, 2020 02:19
-
-
Save cayblood/5d4e5cab47c77ed42efb35790fd375cb to your computer and use it in GitHub Desktop.
example of how to use hyperledger fabric permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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