這篇文章寫得很清楚
如何用 Hyperledger Composer 生成一個應用
並運行在 REST Server API 上
接下來透過此篇
把網頁建起來
我這邊記錄一下
中途可能會遇到的問題
401 error
首先,envvars.txt 這個檔案。
其中 COMPOSER_PROVIDERS 的"clientID"與"clientSecret"
要先至該 github網站 register a new OAuth application
填完就會各有一個"clientID"與"clientSecret",然後將其填入 envvars.txt。
符號問題
‘ 改成 '
“ 改成 "
投票應用
models
/**
* Write your model definitions here
*/
namespace org.basic.vote
participant voter identified by voterID {
o String voterID
o String fullName
}
asset ifVoted identified by voterID {
o String voterID
o Boolean votedOrNot
}
asset candidateVote identified by politicalParty {
o String politicalParty
o Integer totalVote
}
transaction vote {
--> candidateVote candidateVoteAsset
--> ifVoted ifVotedAsset
}
logic.js
'use strict';
/**
* Write your transaction processor functions here
*/
/**
* Sample transaction
* @param {org.basic.vote.vote} vote
* @transaction
*/
function vote(tx) {
if (!tx.ifVotedAsset.votedOrNot) {
tx.candidateVoteAsset.totalVote = tx.candidateVoteAsset.totalVote + 1;
return getAssetRegistry('org.basic.vote.candidateVote')
.then(function (assetRegistry) {
return assetRegistry.update(tx.candidateVoteAsset);
})
.then(function () {
return getAssetRegistry('org.basic.vote.ifVoted')
.then(function (assetRegistry) {
tx.ifVotedAsset.votedOrNot = true;
return assetRegistry.update(tx.ifVotedAsset);
})
});
}
}
permissions.acl
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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
}
利用 REST Server 頁面新增資料 | 利用 Angular APP 網頁新增資料 |
在 voter-POST 加入投票者資料。 |
在 Participant-voter 加入投票者資料。 |
成功畫面如此,code 200。 |
成功畫面如此,簡單明瞭。 |
在 candidateVote-POST 加入候選政黨。 |
在 Asset-candidateVote 加入候選政黨。 |
ifVote-POST 加入判斷,數量與voter一致。 |
Asset-ifVote 加入判斷,數量與voter一致。 (false = 先勾選再取消勾選) |
在 vote-POST 執行投票。 |
在 Transaction-vote 執行投票。 |
於 candidateVote-GET 查看票數結果。 |
於 Asset-candidateVote 查看票數結果。 |