Deploy Composer and Generate REST API

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

  1. 登录GitHub。

  2. 进入用户Settings

  3. 点击Developer settings下的OAuth application

  4. 点击Register a new application

  5. 进行如下配置。

    Application name: Composer
    Homepage: http://localhost:3000/
    Application description: OAuth application for Composer
    Authorization callback URL: http://localhost:3000/auth/github/callback
  6. 点击Register application

  7. 记录下Client IDClient Secret

  8. 设置环境变量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

  1. 访问 http://localhost:3000/auth/github.
  2. 跳转到GitHub执行OAuth web server的验证流程,点击Authorize
  3. 如果成功,跳转回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
  1. 访问 http://localhost:3000/explorer/.

  2. 执行GET /walles操作,列出认证客户钱包。

    [
     {
       "description": "Default wallet",
       "id": 1
     }
    ]

    每一个wallet都有一个唯一的ID,此示例的为1.

  3. 执行GET /wallets/{id}/identities操作,列出wallet里的所有blockchain identity。

    []
  4. 将Blockchain identity加入到wallet,执行POST /wallets/{id}/identities操作。

    {
     "enrollmentID": "userAdmin",
     "enrollmentSecret": "1"
    }

    Response

    {
     "enrollmentID": "userAdmin",
     "enrollmentSecret": "fgsoXpLaPHsR",
     "id": 2
    }
  5. 将Blockchain identity作为wallet的默认身份,执行POST /wallets/{id}/identities/{fk}/setDefault操作。

    no content

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值