Deploy Composer and Generate REST API
Start Playground
sudo docker run --name composer-playground --publish 8080:8080 --detach hyperledger/composer-playground
以下内容为使用Command将Composer的Bussiness Networks定义的文件打包发布到Fabric Networks上。
1.Generate a Bussiness Network Definition
yo hyperledger-composer
1. select Skeleton Bussiness Network
2. Answer all of the questions
2.Generate .bna file
composer archive create --sourceType dir --sourceName . -a business-network.bna
3.Deploy .bna to Fabric
#-s 后面为任意字符串。
composer network deploy -a business-network.bna -p hlfv1 -i PeerAdmin -s 1
4.Verify the network has been deployed
composer network ping -n business-network -p hlfv1 -i admin -s adminpw
5. Retrieve metadata and assets infomation
composer network list -n business-network -p hlfv1 -i admin -s adminpw
6.Generate REST API
#所有client的交易由Blockchain identity admin签名。
composer-rest-server -p hlfv1 -n business-network -i admin -s adminpw
#经过认证的user可以使用REST API。
composer-rest-server -p hlfv1 -n business-network -i admin -s adminpw -S true
#接收所有client的请求,而不签名交易。
composer-rest-server
以下内容为对REST server设置验证策略。
Configuring the REST server to use an authentication strategy
使用Passport作为验证策略,对用户的Github ID进行验证
npm install -g passport-github
Register an OAuth application on GItHub
登录GitHub。
进入用户Settings。
点击Developer settings下的OAuth application。
点击Register a new application。
进行如下配置。
Application name: Composer Homepage: http://localhost:3000/ Application description: OAuth application for Composer Authorization callback URL: http://localhost:3000/auth/github/callback
点击Register application。
记录下Client ID和Client Secret。
设置环境变量COMPOSER_PROVIDERS。
export COMPOSER_PROVIDERS='{
"github": {
"provider": "github",
"module": "passport-github",
"clientID": "4a58efd3ffc7fd31e428",
"clientSecret": "5d9c0232fb286fc070e363178c260ccf59c7d7e9",
"authPath": "/auth/github",
"callbackURL": "/auth/github/callback",
"successRedirect": "/",
"failureRedirect": "/"
}
}'
Starting the REST server with REST API authentication enabled
composer-rest-server -p hlfv1 -n business-network -i admin -s adminpw -a true
Authenticating to the REST server
- 访问 http://localhost:3000/auth/github.
- 跳转到GitHub执行OAuth web server的验证流程,点击Authorize。
- 如果成功,跳转回REST server。
经过上面3步之后去使用REST API,此时会报No enrollment ID or enrollment secret has been provided的错误信息。
Adding a Blockchain identity to the default wallet
如果之前你没有将一个Blockchain Identity绑定到一个participant,请先执行Adding participants和Issuing a new identity to a participant这两步。
Adding participants
# -d 代表的是自定义的participant。
composer participant add -p hlfv1 -n 'business-network' -i admin -s adminpw -d '{"$class": "org.example.detail.User", "firstName": "0", "lastName": "a", "balance": 10000, "email": "a", "username": "a"}'
Issuing a new identity to a participant
composer identity issue -p hlfv1 -n 'business-network' -i admin -s adminpw -u userAdmin -a "resource:org.example.detail.User#a"
# -s 上一条命令返回的userAdmin的密码。
composer network ping -p hlfv1 -n 'business-network' -i userAdmin -s fgsoXpLaPHsR
执行GET /walles操作,列出认证客户钱包。
[ { "description": "Default wallet", "id": 1 } ]
每一个wallet都有一个唯一的ID,此示例的为1.
执行GET /wallets/{id}/identities操作,列出wallet里的所有blockchain identity。
[]
将Blockchain identity加入到wallet,执行POST /wallets/{id}/identities操作。
{ "enrollmentID": "userAdmin", "enrollmentSecret": "1" }
Response
{ "enrollmentID": "userAdmin", "enrollmentSecret": "fgsoXpLaPHsR", "id": 2 }
将Blockchain identity作为wallet的默认身份,执行POST /wallets/{id}/identities/{fk}/setDefault操作。
no content