はじめに

IBMのThinkJapanに参加したのですが、そこのセッションでやっていたのが
Hyperledger Composer を使ったブロックチェーンアプリの開発方法でした。

https://ibmevent.jp/thinkjapan2018devday/

Hyperledger Composer ってなに?というのは下記にまとめられているのでここでは割愛します。

Hyperledger Composer 使ってみた#1


Hyperledger Composerの基本

Hyperledger Composer はビジネスネットワークアーカイブ(BNA)というものを
定義する必要があります。

このBNAは下記3つで成り立っています

  • Model・・・Participant(ユーザ)、Asset(リソース) 
  • Logic・・・ビジネスロジック
  • ACL・・・アクセス制御のコード

開発環境

開発はブラウザ上で簡単に実施できます

https://composer-playground.mybluemix.net/

Playgroundという機能で、ブラウザ上で実際にトランザクションを動かすことが可能です

コード

ここではセミナーでやっていた、自動車のオーナーを移管するというアプリをHyperledger Composer 
上で実装してみたいと思います



Model
namespace org.example.empty

participant Maker identified by id {
  o String id
  o String name
}


participant Owner identified by id {
  o String id
  o String firstName
  o String lastName
}

asset Car identified by vin {
 o String vin
 o String type
 o String name
 /* 外部参照*/
 --> Maker maker
 --> Owner owner
}

transaction Transfer {
 --> Car car
 --> Owner newOwner
}

Logic
transaction が実行された時に引数で渡された newOwnerで carのオーナーを更新させます
確認用にconsoleに出力させています
/**
 * @param {org.example.empty.Transfer} tx
 * @transaction
*/
async function transfer(tx) {

  let car = tx.car;
  
  console.log("*********** old owner *************");
  console.log(car.owner.firstName);
  console.log(car.owner.lastName);
  
  let newOwner = tx.newOwner;
  car.owner = newOwner;
  
  console.log("*********** Transfer result *************");
  console.log(car.owner.firstName);
  console.log(car.owner.lastName);
}

ACL
rule OwnerCanSubmitTransfer {
  description: "Owner Car Submit"
  participant: "org.example.empty.Owner" 
  operation: ALL
  resource: "org.example.empty.Car"
  action: ALLOW
}

rule 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
}

Playgroundで動かしてみる

Let's BlockChain

WS2018-06-11_19_30_28000000

Get Started

WS2018-06-11_19_32_37000000

Modelの作成
WS2018-06-11_19_24_30000000

Logicの作成(左下の Add file  -> Script file)
WS2018-06-11_19_24_46000000


WS2018-06-11_19_24_34000000

ACLの作成
WS2018-06-11_19_24_36000000


左下のDeploy Change ボタンを押す
WS2018-06-11_19_24_58000000

右上にメッセージがでる
WS2018-06-11_19_25_02000000

 Testタブ

Create Participant

Maker
{
  "$class": "org.example.empty.Maker",
  "id": "1",
  "name": "maker_man"
}{

WS2018-06-11_19_25_39000000


Owner
{
  "$class": "org.example.empty.Owner",
  "id": "1",
  "firstName": "ImOld",
  "lastName": "Owner"
}
{
  "$class": "org.example.empty.Owner",
  "id": "2",
  "firstName": "ImNew",
  "lastName": "Owner"
}{


WS2018-06-11_19_25_56000000

WS2018-06-11_19_26_27000000

ASSETS

Car
{
  "$class": "org.example.empty.Car",
  "vin": "1",
  "type": "toyota",
  "name": "mirai",
  "maker": "resource:org.example.empty.Maker#1",
  "owner": "resource:org.example.empty.Owner#1"
}{
#のあとの数字がID

WS2018-06-11_19_26_46000000


トランザクションの実行

Submit Transaction

WS2018-06-11_19_45_33000000

Car ID#1 のリソースのオーナーを #2に変更する
{
  "$class": "org.example.empty.Transfer",
  "car": "resource:org.example.empty.Car#1",
  "newOwner": "resource:org.example.empty.Owner#2"
}
WS2018-06-11_19_45_41000000

右上に結果が出力

WS2018-06-11_19_45_49000000

コンソールログ( Ctrl + Shift + I)
WS2018-06-11_19_46_25000000

トランザクションの結果をみる
WS2018-06-11_19_46_39000000
WS2018-06-11_19_46_43000000


簡単ですね~