ETH官方钱包

切換
舊版
前往
大廳
主題

【區(qū)塊鏈】Hyperledger Composer with Angular (web) APP:投票

Hiteku | 2019-08-13 19:14:52 | 巴幣 2 | 人氣 449


Getting Started with Hyperledger Composer
這篇文章寫得很清楚
如何用 Hyperledger Composer 生成一個(gè)應(yīng)用
並運(yùn)行在 REST Server API 上

接下來(lái)透過(guò)此篇
把網(wǎng)頁(yè)建起來(lái)


我這邊記錄一下
中途可能會(huì)遇到的問題

401 error
首先,envvars.txt 這個(gè)檔案。
其中 COMPOSER_PROVIDERS 的"clientID"與"clientSecret"
要先至該 github網(wǎng)站 register a new OAuth application
填完就會(huì)各有一個(gè)"clientID"與"clientSecret",然後將其填入 envvars.txt。

符號(hào)問題
‘ 改成 '
改成 "


投票應(yīng)用

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 頁(yè)面新增資料 利用 Angular APP 網(wǎng)頁(yè)新增資料
voter-POST 加入投票者資料。
Participant-voter 加入投票者資料。
成功畫面如此,code 200。
成功畫面如此,簡(jiǎn)單明瞭。
在 candidateVote-POST 加入候選政黨
Asset-candidateVote 加入候選政黨。
ifVote-POST 加入判斷,數(shù)量與voter一致。
Asset-ifVote 加入判斷,數(shù)量與voter一致。
(false = 先勾選再取消勾選)
在 vote-POST 執(zhí)行投票。
Transaction-vote 執(zhí)行投票。
candidateVote-GET 查看票數(shù)結(jié)果。
Asset-candidateVote 查看票數(shù)結(jié)果。


送禮物贊助創(chuàng)作者 !
0
留言

創(chuàng)作回應(yīng)

相關(guān)創(chuàng)作

更多創(chuàng)作