HyperLedger Fabric + Composer - How to make Blockchain App - KUNITO Atsunori
HyperLedger FabricHyperLedger Composer Playground Client API REST Server Other Lang Application Node.js Application Composer CLI Business Network Archive Model Query Transaction Acl Card Phase.1 Develop & Test BNA Phase.2 Deploy BNA Phase.3 Develop Your App export
What is BNA? • BNA = Business Network Archive • Literally , It’s a Archive file. It involve files described below. Purpose Similar part of Traditional DB Model Define assets Create table statement(DDL) Query Define Query with subset of assets, sort order, and condition. View or Select statement. Transaction Chaincode.(a.k.a smart contract) Update Assets and execute check or related process. Stored Procedure ACL Access control Grant
Work with Playground. • https://composer- playground.mybluemix.net/login • Develop your BNA. • Test your BNA. – Create asset , submit transaction. – Those action are virtual. It works with browser’s local storage. – [TIPS] console.log() is available in transaction. It outputs to browser console. • If you done testing, export BNA.
Sample of Model. /** * A Task */ asset Task identified by taskId { o String taskId o String title o String envId o DateTime start o DateTime end o TaskStatus status o String commander o String approver } enum TaskStatus { o REGISTERED o STARTED o FINISHED } It’s very similar to DDL. You can define enumration.
Sample of Query. query getTaskByStatus{ description: "Select all tasks with supplied status parameter" statement: SELECT xxxx.cossh.model.Task WHERE (status == _$statusParam) ORDER BY [start ASC] } It’s very similar to SQL. Query Parameter.
Sample of Transaction. function startTask(tx) { console.log('startTask[' + tx.task + "]"); var targetTask = {}; // get task return getAssetRegistry(‘xxxx.cossh.model.Task') .then(function (registry) { var ret = registry.get(tx.task.taskId); return ret; }).then(function (task) { targetTask = task; targetTask.status = 'STARTED'; return getAssetRegistry(‘xxxx.cossh.model.Task'); }).then(function (registry) { return registry.update(targetTask); }).catch(function(e){ console.log('not found?:' + e); }); } You can implement checks , Related process. Update asset. This function can be called in Transaction only.
Sample of ACL. permissions.aclrule NetworkAdminUser { description: "Grant business network administrators full access to user resources" participant: "org.hyperledger.composer.system.NetworkAdmin" operation: ALL resource: "**" action: ALLOW } rule NetworkAdminSystem { description: "Grant business network administrators full access to system resources" participant: "org.hyperledger.composer.system.NetworkAdmin" operation: ALL resource: "org.hyperledger.composer.system.**" action: ALLOW } It grants all permissions to NetworkAdmin. It’s pretty enough while developing.
Deploy BNA. • Create admin card. – Card is a another archive file. It contains … – If you used fabric-dev-server , simply run createPeerAdminCard.sh. • Install & Start BNA. – [TIPS] network start command create new card file. Application will connect to Fabric with this new card. So import new card in this step. Connection.json Fabric Information that connect to. certificate Public key. privateKey Secret key. composer network install -a cossh-network.bna -c PeerAdmin@hlfv1 composer network start --card PeerAdmin@hlfv1 -n cossh-network -V 0.0.2-deploy.6 --networkAdmin admin --networkAdminEnrollSecret ******* --file networkadmin.card -l INFO composer card import --file networkadmin.card
Connect to Fabric from App. • Option 1. Client API. – API for Node.js – If Your App were Node.js , This is a straight forward approach. – [TIPS] When Your App start up , load card which created by network start command. You can use AdminConnection.importCard() for this. – [TIPS] API version is very sensitive because Composer improve day by day. Then You should carefully choose package version. Basically , Client API , Composer CLI , Playground should have same version number. • Option 2. Composer REST Server. – REST Server is a package for Node.js. – It’s generate REST API for creating assets , submitting transaction , running queries. – It expose those REST API on http://localhost:3000.
Thank you !!!

How to make App with Hyperledger fabric and composer

  • 1.
    HyperLedger Fabric +Composer - How to make Blockchain App - KUNITO Atsunori
  • 2.
    HyperLedger FabricHyperLedger ComposerPlayground Client API REST Server Other Lang Application Node.js Application Composer CLI Business Network Archive Model Query Transaction Acl Card Phase.1 Develop & Test BNA Phase.2 Deploy BNA Phase.3 Develop Your App export
  • 3.
    What is BNA? •BNA = Business Network Archive • Literally , It’s a Archive file. It involve files described below. Purpose Similar part of Traditional DB Model Define assets Create table statement(DDL) Query Define Query with subset of assets, sort order, and condition. View or Select statement. Transaction Chaincode.(a.k.a smart contract) Update Assets and execute check or related process. Stored Procedure ACL Access control Grant
  • 4.
    Work with Playground. •https://composer- playground.mybluemix.net/login • Develop your BNA. • Test your BNA. – Create asset , submit transaction. – Those action are virtual. It works with browser’s local storage. – [TIPS] console.log() is available in transaction. It outputs to browser console. • If you done testing, export BNA.
  • 5.
    Sample of Model. /** *A Task */ asset Task identified by taskId { o String taskId o String title o String envId o DateTime start o DateTime end o TaskStatus status o String commander o String approver } enum TaskStatus { o REGISTERED o STARTED o FINISHED } It’s very similar to DDL. You can define enumration.
  • 6.
    Sample of Query. querygetTaskByStatus{ description: "Select all tasks with supplied status parameter" statement: SELECT xxxx.cossh.model.Task WHERE (status == _$statusParam) ORDER BY [start ASC] } It’s very similar to SQL. Query Parameter.
  • 7.
    Sample of Transaction. functionstartTask(tx) { console.log('startTask[' + tx.task + "]"); var targetTask = {}; // get task return getAssetRegistry(‘xxxx.cossh.model.Task') .then(function (registry) { var ret = registry.get(tx.task.taskId); return ret; }).then(function (task) { targetTask = task; targetTask.status = 'STARTED'; return getAssetRegistry(‘xxxx.cossh.model.Task'); }).then(function (registry) { return registry.update(targetTask); }).catch(function(e){ console.log('not found?:' + e); }); } You can implement checks , Related process. Update asset. This function can be called in Transaction only.
  • 8.
    Sample of ACL. permissions.aclruleNetworkAdminUser { description: "Grant business network administrators full access to user resources" participant: "org.hyperledger.composer.system.NetworkAdmin" operation: ALL resource: "**" action: ALLOW } rule NetworkAdminSystem { description: "Grant business network administrators full access to system resources" participant: "org.hyperledger.composer.system.NetworkAdmin" operation: ALL resource: "org.hyperledger.composer.system.**" action: ALLOW } It grants all permissions to NetworkAdmin. It’s pretty enough while developing.
  • 9.
    Deploy BNA. • Createadmin card. – Card is a another archive file. It contains … – If you used fabric-dev-server , simply run createPeerAdminCard.sh. • Install & Start BNA. – [TIPS] network start command create new card file. Application will connect to Fabric with this new card. So import new card in this step. Connection.json Fabric Information that connect to. certificate Public key. privateKey Secret key. composer network install -a cossh-network.bna -c PeerAdmin@hlfv1 composer network start --card PeerAdmin@hlfv1 -n cossh-network -V 0.0.2-deploy.6 --networkAdmin admin --networkAdminEnrollSecret ******* --file networkadmin.card -l INFO composer card import --file networkadmin.card
  • 10.
    Connect to Fabricfrom App. • Option 1. Client API. – API for Node.js – If Your App were Node.js , This is a straight forward approach. – [TIPS] When Your App start up , load card which created by network start command. You can use AdminConnection.importCard() for this. – [TIPS] API version is very sensitive because Composer improve day by day. Then You should carefully choose package version. Basically , Client API , Composer CLI , Playground should have same version number. • Option 2. Composer REST Server. – REST Server is a package for Node.js. – It’s generate REST API for creating assets , submitting transaction , running queries. – It expose those REST API on http://localhost:3000.
  • 11.