使用 fabric-tools 开发
在教程编写第一个应用中,自带了一个 fabric 环境,现在改造成使用 fabric-tools,以便与 composer 保持一致并读取 fabric-tools 环境中的信息。
fabcar 自带环境的 CA 域名是 ca.example.com,而 fabric-tools 的 CA 域名是 ca.org1.example.com。所以要修改 enrollAdmin.js:
fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', tlsOptions , 'ca.org1.example.com', crypto_suite);
然后执行node enrollAdmin.js
,脚本会创建hfc-key-store
目录,并在里面存放admin的共私钥。
执行node registerUser.js
时碰到错误,说缺乏identity type,解决办法在这里
下面执行query.js,之前也需要做简单修改:
var channel = fabric_client.newChannel('composerchannel');
向 fabric-tools 安装链码 fabcar
向 fabric-tools 中增加 cli 容器
fabric-tools没有带cli容器,根据fabcar自带的容器环境的配置文件为原型,改造fabric-tools的docker-compose配置文件。该配置文件是/home/vagrant/fabric-tools/fabric-scripts/hlfv1/composer/docker-compose.yml
,增加下列内容:
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
- CORE_CHAINCODE_KEEPALIVE=10
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- /opt/fabric-samples/chaincode/:/opt/gopath/src/github.com/
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
然后重启整个fabric-tools环境:
cd ~/fabric-tools && ./stopFabric.sh
./startFabric.sh
安装 fabcar 链码
fabcar环境是依靠/opt/fabric-samples/fabcar/startFabric.sh
启动的。该脚本中有安装fabcar链码的命令,现在改造这个脚本:
cd /opt/fabric-samples/fabcar
cp startFabric.sh startFabric2.sh
vi startFabric2.sh
将startFabric2.sh修改成下列的内容:
LANGUAGE=${1:-"golang"}
CC_SRC_PATH=github.com/fabcar/go
if [ "$LANGUAGE" = "node" -o "$LANGUAGE" = "NODE" ]; then
CC_SRC_PATH=/opt/gopath/src/github.com/fabcar/node
fi
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n fabcar -v 1.0 -p "$CC_SRC_PATH" -l "$LANGUAGE"
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C composerchannel -n fabcar -l "$LANGUAGE" -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member')"
sleep 10
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C composerchannel -n fabcar -c '{"function":"initLedger","Args":[""]}'
主要变化一是通道名原来是mychannel
,现在是composerchannel
;背书策略原来有Org2MSP.member
,现在只剩下了Org1MSP.member。
执行刚刚创建的脚本来安装链码并测试:
./startFabric2.sh
docker ps
用docker ps
命令可以看到刚刚实例化的fabcar
链码。
继续执行query.js
原来的query.js中的通道名称是mychannel
需要改成composerchannel
:
var channel = fabric_client.newChannel('composerchannel');
执行链码查询:
node query.js
Store path:/opt/fabric-samples/fabcar/hfc-key-store
Successfully loaded user1 from persistence
Query has completed, checking results
Response is [{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko" } },{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad" } },{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo" } },{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max" } },{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana" } },{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel" } },{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav" } },{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari" } },{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria" } },{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro" } }]
用 playground 创建 tutorial-network
fabcar是个fabric链码,现在创建一个composer链码(composer叫业务网络),叫做tutorial-network
。创建tutorial-network的过程可以参考composer官方文档Playground教程。
启动Composer Playground:
composer-playground
用浏览器访问localhost:8080,点击新建一个业务网络。
使用空白模板,业务网络名称是tn(tutorial-network),业务网络管理员卡片是admin@tn,网络管理员凭据选择ID and Secret,默认管理员和密码是admin:adminpw
。
安装教程添加模型、交易处理函数、基础数据等。使用的命名空间是org.acme.mynetwork
。执行教程的所有操作,直到把资产ABC的拥有者改成TRADER2。
composer-rest-server
安装 openldap。DIT 后缀是 dc=example,dc=com
。添加一个叫john的用户,密码是johnldap。这个用户需要一个mail的属性[2]。
设置环境变量 COMPOSER_PROVIDERS
并启动composer-rest-server,选项是启用认证。
获取令牌并请求商品ABC
:
$ curl -i -X POST http://localhost:3000/auth/ldap -H "Content-Type:application/json" -d '{"username": "john", "password":"johnldap"}
set-cookie: access_token=(略); Max-Age=1209600; Path=/; Expires=Mon, 05 Feb 2018 06:35:24 GMT
$ curl -X GET --cookie "access_token=(略)" --header 'Accept: application/json' 'http://localhost:3000/api/Commodity/ABC'
{"$class":"org.acme.mynetwork.Commodity","tradingSymbol":"ABC","description":"Test commodity","mainExchange":"Euronext","quantity":72.297,"owner":"resource:org.acme.mynetwork.Trader#TRADER2"}v
上述获取商品的API是用浏览器通过 localhost:3000 查询到的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论