专有节点接入说明
在完成项目的创建后,系统会为每个项目创建对应框架所需要的接入信息,用户在访问区块链节点时进行接入授权验证,通过验证后即可访问链上。
Quorum
-
GoQuorum
使用GoQuorum client与节点进行交互。
示例:
$ ./geth attach https://VuF0h0y7pLwtvDqjuW:[email protected]:19602/node1 INFO [05-08|16:05:52.534] Running with private transaction manager disabled - quorum private transactions will not be supported Welcome to the Geth JavaScript console! instance: Geth/v1.9.7-stable-af752518(quorum-v20.10.0)/linux-amd64/go1.13.15 coinbase: 0xf957d0ae8a1c1b2cdcea0acb8fb0a2a750abadaa at block: 109 (Sat May 08 2021 16:05:48 GMT+0800 (CST)) datadir: /root/quorum/data modules: admin:1.0 debug:1.0 eth:1.0 istanbul:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 > web3.eth.blockNumber 11
-
JSON-RPC API
- Geth JSON-RPC文档: https://github.com/ethereum/wiki/wiki/JSON-RPC
- Quorum API文档: https://docs.goquorum.consensys.net/en/latest/Reference/APIs/PrivacyAPI/
可以使用curl和Postman进行调用。
示例:
$ curl -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":2}' https://VuF0h0y7pLwtvDqjuW:[email protected]:19602/node1 {"jsonrpc":"2.0","id":2,"result":"0x10"}
-
web3.js
- web3.js类库:https://github.com/ethereum/web3.js
示例:
const Web3 = require("web3"); const web3 = new Web3( new Web3.providers.HttpProvider("https://VuF0h0y7pLwtvDqjuW:[email protected]:19602/node1") ); web3.eth.getBlockNumber().then(console.log);
fabric
-
Tools
使用fabric-tools与节点进行交互。
示例:
启动cli:
# docker-compose-cli.yaml文件如下: $ cat docker-compose-cli.yaml version: '2' services: cli: container_name: fabric_peercli image: hyperledger/fabric-tools:2.2.0 restart: always tty: true stdin_open: true environment: - FABRIC_LOGGING_SPEC=DEBUG - CORE_PEER_TLS_ENABLED=true - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt - CORE_PEER_ADDRESS=peer1.org1.example.com:1051 - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp - CORE_PEER_LOCALMSPID=Org1MSP - ORDERER_CA=/etc/hyperledger/fabric/orderer/tlsca/tlsca.orderer.example.com-cert.pem - ORDERER_ADDRESS=orderer1.orderer.example.com:7050 working_dir: /etc/hyperledger/fabric command: /bin/bash volumes: - /var/run/:/host/var/run/ - ./certs/ordererOrganizations/orderer.example.com:/etc/hyperledger/fabric/orderer - ./certs/peerOrganizations/org1.example.com/users/[email protected]/msp:/etc/hyperledger/fabric/msp - ./certs/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls:/etc/hyperledger/fabric/tls - ./sharedfiles/chaincode:/etc/hyperledger/fabric/src extra_hosts: - "orderer1.orderer.example.com:161.189.69.75" - "peer1.org1.example.com:161.189.69.75"
进入容器进行链码部署以及调用:
#链码部署 // package peer lifecycle chaincode package basic414.tar.gz --path ./asset-transfer-basic/chaincode-javascript/ --lang node --label basic414 // install peer lifecycle chaincode install basic414.tar.gz // queryinstalled peer lifecycle chaincode queryinstalled // approveformyorg peer lifecycle chaincode approveformyorg --name basic414 --package-id basic414:16bf72ced8451fc6fd94bd139de1532adfdd190af075c2e84a87513915a97365 -o $ORDERER_ADDRESS --tls --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE --cafile $ORDERER_CA --version 1.0 --channelID netchannel --sequence 1 --connTimeout 30s // queryapproved peer lifecycle chaincode queryapproved --channelID netchannel -n basic414 // checkcommitreadiness peer lifecycle chaincode checkcommitreadiness --channelID netchannel --name basic414 --version 1.0 --sequence 1 --output json // commit peer lifecycle chaincode commit -o $ORDERER_ADDRESS --cafile $ORDERER_CA --channelID netchannel --name basic414 --version 1.0 --sequence 1 --peerAddresses $CORE_PEER_ADDRESS --tls --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE // querycommitted peer lifecycle chaincode querycommitted -o $ORDERER_ADDRESS --channelID netchannel --tls --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE --cafile $ORDERER_CA #调用链码 // InitLedger peer chaincode invoke -o $ORDERER_ADDRESS --tls --cafile $ORDERER_CA -C netchannel -n basic414 --peerAddresses $CORE_PEER_ADDRESS --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE -c '{"Args":["InitLedger"]}' // GetAllAssets peer chaincode query -C netchannel -n basic414 -c '{"Args":["GetAllAssets"]}' // CreateAsset peer chaincode invoke -o $ORDERER_ADDRESS --tls --cafile $ORDERER_CA -C netchannel -n basic414 --peerAddresses $CORE_PEER_ADDRESS --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE -c '{"Args":["CreateAsset","asset7","white", "15", "zxl", "800"]}' //UpdateAsset peer chaincode invoke -o $ORDERER_ADDRESS --tls --cafile $ORDERER_CA -C netchannel -n basic414 --peerAddresses $CORE_PEER_ADDRESS --tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE -c '{"Args":["UpdateAsset","asset7","zxl", "1218", "zxl", "1218"]}' //ReadAsset peer chaincode query -C netchannel -n basic414 -c '{"Args":["ReadAsset","asset7"]}'
chain33
-
chain33-cli
使用chain33 client与节点进行交互。
示例:
./chain33-cli --rpc_laddr "https://4PRFS2zJmhYYqzKllW:[email protected]:19602/node1" block last_header { "version": 0, "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "txHash": "0x22db70a26130f2fe4758fd65bf016949c1c46f5642b4a556f8a0ff7a41350898", "stateHash": "0xc34a7713273f1ac2256355c754b6c8ed9b9c726b84d16866ccaa4b7df29d7cc8", "height": 0, "blockTime": 1620443036658, "txCount": 1, "hash": "0xa17a19fc1b525b783130fe4ce797feb815039e5fb96e06ea4e6a600ee0970170", "difficulty": 520159231 }
-
JSON-RPC API
- Json-RPC接口地址: https://chain.33.cn/document/95
可以使用curl和Postman进行调用。
示例:
curl -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"Chain33.GetLastHeader","params":[],"id":2}' https://4PRFS2zJmhYYqzKllW:[email protected]:19602/node1 {"id":2,"result":{"version":0,"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","txHash":"0x22db70a26130f2fe4758fd65bf016949c1c46f5642b4a556f8a0ff7a41350898","stateHash":"0xc34a7713273f1ac2256355c754b6c8ed9b9c726b84d16866ccaa4b7df29d7cc8","height":0,"blockTime":1620443036658,"txCount":1,"hash":"0xa17a19fc1b525b783130fe4ce797feb815039e5fb96e06ea4e6a600ee0970170","difficulty":520159231},"error":null}
-
Java SDK
- 接口文档: https://github.com/33cn/chain33-sdk-java/blob/master/联盟链接口说明.md
示例:
package com.hll.bty; import cn.chain33.javasdk.client.RpcClient; import cn.chain33.javasdk.utils.EvmUtil; import cn.chain33.javasdk.utils.HexUtil; import org.junit.Test; import com.alibaba.fastjson.JSONArray; public class EvmTest { //格式为https://授权用户名:授权密码@peer节点RPC端点 RpcClient client = new RpcClient("https://4PRFS2zJmhYYqzKllW:[email protected]:19602/node1"); @Test public void testEvmContract() throws InterruptedException { String privateKey = "73363A3A483A0916835EB8B803D23EBE27631BE96E9FCA0CBBFB702EABC21BAC"; String code = "0x608060405234801561001057600080fd5b5060c78061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea2646970667358221220ed06cf1aefb69c19593b31e7039b83768108c2943ca726b68b351622b14a292f64736f6c63430007040033"; String abi = "[\n" + "\t{\n" + "\t\t\"inputs\": [],\n" + "\t\t\"name\": \"get\",\n" + "\t\t\"outputs\": [\n" + "\t\t\t{\n" + "\t\t\t\t\"internalType\": \"uint256\",\n" + "\t\t\t\t\"name\": \"\",\n" + "\t\t\t\t\"type\": \"uint256\"\n" + "\t\t\t}\n" + "\t\t],\n" + "\t\t\"stateMutability\": \"view\",\n" + "\t\t\"type\": \"function\"\n" + "\t},\n" + "\t{\n" + "\t\t\"inputs\": [\n" + "\t\t\t{\n" + "\t\t\t\t\"internalType\": \"uint256\",\n" + "\t\t\t\t\"name\": \"_x\",\n" + "\t\t\t\t\"type\": \"uint256\"\n" + "\t\t\t}\n" + "\t\t],\n" + "\t\t\"name\": \"set\",\n" + "\t\t\"outputs\": [],\n" + "\t\t\"stateMutability\": \"nonpayable\",\n" + "\t\t\"type\": \"function\"\n" + "\t}\n" + "]"; // 部署合约 String txEncode = EvmUtil.createEvmContract(HexUtil.fromHexString(code), "", "evm-sdk-test-para0", abi, privateKey); String submitTransaction = client.submitTransaction(txEncode); String contractName = submitTransaction; System.out.println(submitTransaction); Thread.sleep(1000); // 调用合约 txEncode = EvmUtil.callEvmContract("".getBytes(),"", 0, "set(15)", contractName, privateKey); submitTransaction = client.submitTransaction(txEncode); System.out.println(submitTransaction); Thread.sleep(1000); // 计算合约地址 String contractAddress = client.convertExectoAddr("user.evm." + contractName); System.out.println(contractAddress); // 查询 JSONArray result = client.queryEVMABIResult(contractAddress, "user.evm." + contractName, "get()"); System.out.println("查询结果: " + result.toJSONString()); // 再次调用合约 txEncode = EvmUtil.callEvmContract("".getBytes(),"", 0, "get()", contractName, privateKey); submitTransaction = client.submitTransaction(txEncode); System.out.println(submitTransaction); } }