はじめに
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
Get Started

Modelの作成

Logicの作成(左下の Add file -> Script file)


ACLの作成

左下のDeploy Change ボタンを押す

右上にメッセージがでる

Testタブ
Create Participant
Maker
{
"$class": "org.example.empty.Maker",
"id": "1",
"name": "maker_man"
}{
Owner
{
"$class": "org.example.empty.Owner",
"id": "1",
"firstName": "ImOld",
"lastName": "Owner"
}{
"$class": "org.example.empty.Owner",
"id": "2",
"firstName": "ImNew",
"lastName": "Owner"
}{

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
トランザクションの実行
Submit Transaction
Car ID#1 のリソースのオーナーを #2に変更する
{
"$class": "org.example.empty.Transfer",
"car": "resource:org.example.empty.Car#1",
"newOwner": "resource:org.example.empty.Owner#2"
}
右上に結果が出力

コンソールログ( Ctrl + Shift + I)

トランザクションの結果をみる


簡単ですね~
コメント