使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)

作者:陳兆麟 Email:[email protected]

在开发区块链 (blockchain)之智能合约 (chaincode) 时,可以使用在 local machine 所建立或是Bluemix Blockchain service所提供的 Blockchain Network ,为了避免在建立local blockchain network 时所引发的挫折感,可先由IBM 所提供的 Blockchain service 切入(Blockchain Network 已事先建好) ;在测试 chaincode 都是使用 REST API; IBM blockchain service也提供了测试chaincode的工具(Swagger API),若觉得不好用,可以使用 Google 所提供的Postman;以下范例是在 Postman 使用REST API 测试 chaincode。

开发环境:Win7 (64 bit) ,Bluemix , Blockchain, Google Postman

1. 登录IBM Bluemix:https://console.ng.bluemix.net/ 点选 Sign Up; 注册后登 录 (30天试用期免费) 将 username及 password 记录。

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第1张图片

建立 Organization 名称

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第2张图片

建立 Space 名称 Organization, Space Region (US South)

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第3张图片

2. 点选型录 (CATALOG) =>选择 [Blockchain IBM BETA] 服务

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第4张图片

3. Service name: myblockchain => CREATE

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第5张图片

4. 点选 Service Crendentials (记录 Service Crendentials 内容如附录 1)

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第6张图片

5. 建立 github账号

登入github 建立账号 (https://github.com/ 纪录 username 及 password) 安装范例 learn-chaincode

浏览器:https://github.com/IBM-Blockchain/learn-chaincode (点选 fork )

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第7张图片

6. 确认learn-chaincode 已 clone 至你的 repository, 在Bluemix blockchainservice 环境必须使用安装在 github 内之 chaincode.

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第8张图片

7. 在 win7下载安装 Postman (Postman-win64-4.7.1-Setup.exe) https://www.getpostman.com/apps

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第9张图片

8. 安装文件执行后桌面显示 [Postman] 图示并启动注册

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第10张图片

9. [Postman]启动后点选 [import]

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第11张图片

10. 选择汇入档案Blockchain.postman_collection_chen.json (blockchain样板,附录 2)

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第12张图片

汇入后内容, User 可根据步骤 4自建的Service Crendentials 修正内容

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第13张图片

11. 点选 post /register (login) [User 至 vp1 (validating peer 1) 注册登录]:填写 REST API (以下为样板)

 {
   "enrollId": "user_type1_274d12e38f",
   "enrollSecret": "85af9da5d9"
 }

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第14张图片

回复 login success

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第15张图片

12.显示 blockchain network VP (validating peer) 内容

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第16张图片

13. 点选 post /blockchain (deploy) 填写 REST API 将 github之 chaincode_finished.go deploy至 VP1,执行init function args=”hello rhere”

path: https://github.com/xxxxxxx/learn-chaincode/finished (根据步骤6修改)

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第17张图片

14. 传回 chaincode ID (128 character)

若要执行已安装在 peer之 chaincode 则参考 chaincode ID 即可

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第18张图片

15. 点选 post /blockchain (query) 填写 REST API 呼叫chaincode_finished.go read function args=”hello_world”

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第19张图片

16. 回应”hello there”是先前chaincode deploy的结果,即[key value of hello_world]设定为 “hello there”

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第20张图片

17. 点选 post /blockchain (invoke) 填写 REST API 呼叫chaincode_finished.go,invoke function args=”hello_world”,”go away”

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第21张图片

回应如下

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第22张图片

18. 重复步骤 15点选 post /blockchain (query) 填写 REST API 呼叫 chaincode_finished.go query function args=”hello_world”; 点选 [send] ,此时 [key value of hello_world] 设定已改为 “go way”

使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)_第23张图片

19. postman 具有user friendly的使用接口,比Bluemix 内建的Swagger API 好用。

附录:

1.blockchain service 之 Service Crendentials 之内容 (样板格式)
{
“credentials”: {
“peers”: [
{
“discovery_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp0.us.blockchain.ibm.com”,
“discovery_port”: 30303,
“api_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp0.us.blockchain.ibm.com”,
“api_port_tls”: 443,
“api_port”: 443,
“type”: “peer”,
“network_id”: “a42168d2-2d4b-4a09-af38-af73efbfc091”,
“container_id”: “86fb78ab9aa4fb11d9d1b8bd92c6d160afe6661d228430202a5ec675cbcfa41a”,
“id”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp0”,
“api_url”: “http://a42168d2-2d4b-4a09-af38-af73efbfc091_vp0.us.blockchain.ibm.com:443”
},
{
“discovery_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp1.us.blockchain.ibm.com”,
“discovery_port”: 30303,
“api_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp1.us.blockchain.ibm.com”,
“api_port_tls”: 443,
“api_port”: 443,
“type”: “peer”,
“network_id”: “a42168d2-2d4b-4a09-af38-af73efbfc091”,
“container_id”: “8d05a97ec08b7556d5716dc176acb611abdf0cb56d58c8307fd320ce81c4cc51”,
“id”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp1”,
“api_url”: “http://a42168d2-2d4b-4a09-af38-af73efbfc091_vp1.us.blockchain.ibm.com:443”
},
{
“discovery_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp2.us.blockchain.ibm.com”,
“discovery_port”: 30303,
“api_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp2.us.blockchain.ibm.com”,
“api_port_tls”: 443,
“api_port”: 443,
“type”: “peer”,
“network_id”: “a42168d2-2d4b-4a09-af38-af73efbfc091”,
“container_id”: “feb00576275c0875dfffdfbb6467d06e716a60939883095ea190955405741d1a”,
“id”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp2”,
“api_url”: “http://a42168d2-2d4b-4a09-af38-af73efbfc091_vp2.us.blockchain.ibm.com:443”
},
{
“discovery_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp3.us.blockchain.ibm.com”,
“discovery_port”: 30303,
“api_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp3.us.blockchain.ibm.com”,
“api_port_tls”: 443,
“api_port”: 443,
“type”: “peer”,
“network_id”: “a42168d2-2d4b-4a09-af38-af73efbfc091”,
“container_id”: “6dc096f0443c5f6306bb69c1e033617a5de034c0c2296336fe33fa06adb77272”,
“id”: “a42168d2-2d4b-4a09-af38-af73efbfc091_vp3”,
“api_url”: “http://a42168d2-2d4b-4a09-af38-af73efbfc091_vp3.us.blockchain.ibm.com:443”
}
],
“ca”: {
“a42168d2-2d4b-4a09-af38-af73efbfc091_ca”: {
“url”: “a42168d2-2d4b-4a09-af38-af73efbfc091_ca.us.blockchain.ibm.com:30303”,
“discovery_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_ca.us.blockchain.ibm.com”,
“discovery_port”: 30303,
“api_host”: “a42168d2-2d4b-4a09-af38-af73efbfc091_ca.us.blockchain.ibm.com”,
“api_port_tls”: 30303,
“api_port”: 443,
“type”: “ca”,
“network_id”: “a42168d2-2d4b-4a09-af38-af73efbfc091”,
“container_id”: “74f8a8e89da5e3241c1042f9eab92c4bc2a83da9415e3412894dda984ce02315”
}
},
“users”: [
{
“username”: “admin”,
“secret”: “67415ec617”,
“enrollId”: “admin”,
“enrollSecret”: “67415ec617”
},
{
“username”: “WebAppAdmin”,
“secret”: “2de6e569fb”,
“enrollId”: “WebAppAdmin”,
“enrollSecret”: “2de6e569fb”
},
{
“username”: “user_type1_274d12e38f”,
“secret”: “85af9da5d9”,
“enrollId”: “user_type1_274d12e38f”,
“enrollSecret”: “85af9da5d9”
},
{
“username”: “user_type1_d22e381b83”,
“secret”: “4c5c6599ac”,
“enrollId”: “user_type1_d22e381b83”,
“enrollSecret”: “4c5c6599ac”
},
{
“username”: “user_type1_1c0f15ca44”,
“secret”: “7d6bb4aab5”,
“enrollId”: “user_type1_1c0f15ca44”,
“enrollSecret”: “7d6bb4aab5”
},
{
“username”: “user_type1_0e5e244982”,
“secret”: “f3c8a447d5”,
“enrollId”: “user_type1_0e5e244982”,
“enrollSecret”: “f3c8a447d5”
},
{
“username”: “user_type1_b122fc510b”,
“secret”: “7a75368a80”,
“enrollId”: “user_type1_b122fc510b”,
“enrollSecret”: “7a75368a80”
},
{
“username”: “user_type2_e9016322bc”,
“secret”: “8d274e1609”,
“enrollId”: “user_type2_e9016322bc”,
“enrollSecret”: “8d274e1609”
},
{
“username”: “user_type2_31099e3ee5”,
“secret”: “b47332934e”,
“enrollId”: “user_type2_31099e3ee5”,
“enrollSecret”: “b47332934e”
},
{
“username”: “user_type2_45825d2b2a”,
“secret”: “930a499b56”,
“enrollId”: “user_type2_45825d2b2a”,
“enrollSecret”: “930a499b56”
},
{
“username”: “user_type2_6f2ae3c26f”,
“secret”: “d88055d3ef”,
“enrollId”: “user_type2_6f2ae3c26f”,
“enrollSecret”: “d88055d3ef”
},
{
“username”: “user_type2_3342cc39bf”,
“secret”: “5865bcbcf8”,
“enrollId”: “user_type2_3342cc39bf”,
“enrollSecret”: “5865bcbcf8”
},
{
“username”: “user_type4_5848eeb539”,
“secret”: “962f57f521”,
“enrollId”: “user_type4_5848eeb539”,
“enrollSecret”: “962f57f521”
},
{
“username”: “user_type4_6fc69c0d81”,
“secret”: “405275781c”,
“enrollId”: “user_type4_6fc69c0d81”,
“enrollSecret”: “405275781c”
},
{
“username”: “user_type4_a9d38e8e82”,
“secret”: “1d6da1c441”,
“enrollId”: “user_type4_a9d38e8e82”,
“enrollSecret”: “1d6da1c441”
},
{
“username”: “user_type4_aad9112619”,
“secret”: “1b2c2c9790”,
“enrollId”: “user_type4_aad9112619”,
“enrollSecret”: “1b2c2c9790”
},
{
“username”: “user_type4_2c87866824”,
“secret”: “bde437fce6”,
“enrollId”: “user_type4_2c87866824”,
“enrollSecret”: “bde437fce6”
},
{
“username”: “user_type8_43aca18a98”,
“secret”: “8e7874e6ef”,
“enrollId”: “user_type8_43aca18a98”,
“enrollSecret”: “8e7874e6ef”
},
{
“username”: “user_type8_f9ae482f40”,
“secret”: “85fc476f56”,
“enrollId”: “user_type8_f9ae482f40”,
“enrollSecret”: “85fc476f56”
},
{
“username”: “user_type8_d0218d5be8”,
“secret”: “e8ab9d3d32”,
“enrollId”: “user_type8_d0218d5be8”,
“enrollSecret”: “e8ab9d3d32”
},
{
“username”: “user_type8_8c4c2accb8”,
“secret”: “b0411b5e58”,
“enrollId”: “user_type8_8c4c2accb8”,
“enrollSecret”: “b0411b5e58”
},
{
“username”: “user_type8_fcfc0208a8”,
“secret”: “a7f6c12607”,
“enrollId”: “user_type8_fcfc0208a8”,
“enrollSecret”: “a7f6c12607”
}
],
“cert”: “https://blockchain-certs.mybluemix.net/us.blockchain.ibm.com.cert”
}
}

2.Blockchain.postman_collection_chen.json 下载网址
https://1drv.ms/u/s!AkBmzWP0h-Vxggnv3TRRC8uZync9
3.Postman使用手册
 http://www.jianshu.com/p/13c8017bb5c8 
4.Bluemix 全球系统维护现况
https://developer.ibm.com/bluemix/support/#status
5.Bluemix 可选择 Region
api.ng.bluemix.net (US South)
api.eu-gb.bluemix.net (United Kingdom)
api.au-syd.bluemix.net (Sydney)
6.陈兆麟 facebook
https://zh-cn.facebook.com/chaolin.chen.18

參考文獻:

1.https://github.com/IBM-Blockchain/learn-chaincode
2.http://www.remkohde.com/2016/07/04/running-your-own-blockchain/
3.http://www.jianshu.com/p/13c8017bb5c8
4.http://hyperledger-fabric.readthedocs.io/en/latest/Setup/Network-setup/
5.http://hyperledger-fabric.readthedocs.io/en/latest/Setup/Chaincode-setup/
6.https://github.com/hyperledger/fabric/blob/master/docs/protocol-spec.md
7.http://www.huhuhe.top/index.php/fabric-environment-build-onmac/
8.https://www.getpostman.com/collections/137770d52c5f070b03f4
9.https://1drv.ms/f/s!AkBmzWP0h-Vxbr7h1uTNsTMcPWI
10.https://1drv.ms/b/s!AkBmzWP0h-VxazOBOsVz8nGMLpM
11.https://1drv.ms/b/s!AkBmzWP0h-VxbLK7FOjH9IL5HHc
12.https://www.youtube.com/watch?v=24JAF9Eu1mk&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=74
13.https://www.youtube.com/watch?v=wSmJ-rwUo8I&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=73
14.https://www.youtube.com/watch?v=SOsn6MvGH60&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=72

你可能感兴趣的:(bluemix,blockchain)