网站外链价格,株洲网络学院登录,软件开发工程师怎么考,免费做微信链接的网站Fisco-Bcos-java-SDK 利用java与fisco-Bcos区块链上的智能合约交互#xff08;以HelloWorld为例#xff09; 
一、部署智能合约 
1、编写智能合约 
此处用最简单的HelloWorld合约作为例子 包含两个方法和一个构造函数 
构造函数#xff1a;当合约部署的时候 执行构造函数 将…Fisco-Bcos-java-SDK 利用java与fisco-Bcos区块链上的智能合约交互以HelloWorld为例 
一、部署智能合约 
1、编写智能合约 
此处用最简单的HelloWorld合约作为例子 包含两个方法和一个构造函数 
构造函数当合约部署的时候 执行构造函数 将name赋值为Hello, World!get()方法获取当前name的值set()方法设置当前name的值 参数类型为string 
pragma solidity 0.4.25 0.6.11;contract HelloWorld {string name;constructor() public{name  Hello, World!;}function get() public view returns (string memory){return name;}function set(string memory n) public {name  n;}
}2、通过WeBASE-Front平台将合约进行部署 
将写好的合约复制到WeBASE-Front节点控制台中 将合约进行编译后 创建测试用户进行部署 可以生成出当前合约的合约地址 这个很重要  可以在这里进行图形化的合约调用进行测试 调用get方法 可以看到我们部署合约后执行的构造函数所赋的值  同样调用set方法进行赋值再调用get方法 可以看到我们所赋的值 返回交易回执 
{
transactionHash: 0x488efbe3507e36c703575ebf678d05664e7ad2d0529e463f1ba2ec7606cb5828
transactionIndex: 0x0
root: 0xd318298686d387c9f6e3122e836b3c4ab2c0b093fc701c083a263e60d696b821
blockNumber: 33
blockHash: 0xbcc1f9f372849b0f7a78fed8c612032605f0a8696d7aea654fa330d909c290f8
from: 0x96bf2ac80e9428068db9a544ff974bc32a1a2e4f
to: 0x8002b168b1f81d6c6d1148f579370648cd28dde8
gasUsed: 29813
remainGas: 0x0
contractAddress: 0x0000000000000000000000000000000000000000
logs: []
logsBloom: 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
status: 0x0
statusMsg: None
input: 
function set( string n)
data: 
n
stringhello,Fisco-Bcos!
output: 0x
txProof: null
receiptProof: null
message: Success
statusOK: true
}可以看到 交易成功 并且 值赋值为hello,Fisco-Bcos! 
再次调用get方法获取当前合约name的值 发现name的值已经发生了变化 
那我如何利用java语言来进行这样的操作呢接着往下看 
二、导出HelloWorld合约的java文件和SDK证书文件 
Fisco-Bcos提供了合约转义JAVA的功能 我们可以直接通过下载 
**同样提供了SDK证书文件的下载 ** 这里可以随便填写 我们到idea用的时候 进行修改就行 下载完成后是一个java文件和一个zip文件  包含生成的转义后的java文件和SDK证书 
三、Springboot集成HelloWorld.sol–java文件 
1、添加Fisco-Bcos依赖 dependencygroupIdorg.fisco-bcos.java-sdk/groupIdartifactIdfisco-bcos-java-sdk/artifactIdversion2.9.1/version/dependency这里直接用2.9.1版本的就行 反正博主用了很好用[doge] 
2、对Fisco-Bcos-java-sdk进行配置将生成的文件放入项目中 
1.导入文件 在resources目录下创建conf目录 将我们前面下载好的那个压缩包中的三个证书文件放到这个目录下 
再将生成的HelloWorld.java放入自己的包下 
这里我的包名是com.webase 再将java文件中的包名进行修改就行 修改成自己的包名 2.配置证书 
同样在resources目录下创建config-example.toml文件 
修改的内容为 文件内容如下 
[cryptoMaterial]certPath  conf                           # The certification path# The following configurations take the certPath by default if commented
# caCert  conf/ca.crt                    # CA cert file path
# If connect to the GM node, default CA cert path is ${certPath}/gm/gmca.crt# sslCert  conf/sdk.crt                  # SSL cert file path
# If connect to the GM node, the default SDK cert path is ${certPath}/gm/gmsdk.crt# sslKey  conf/sdk.key                   # SSL key file path
# If connect to the GM node, the default SDK privateKey path is ${certPath}/gm/gmsdk.key# enSslCert  conf/gm/gmensdk.crt         # GM encryption cert file path
# default load the GM SSL encryption cert from ${certPath}/gm/gmensdk.crt# enSslKey  conf/gm/gmensdk.key          # GM ssl cert file path
# default load the GM SSL encryption privateKey from ${certPath}/gm/gmensdk.key[network]
#peers[127.0.0.1:20200, 127.0.0.1:20201]    # The peer list to connect
#修改为服务器IP地址
peers[这里是你的区块链服务器的ip地址:20200, 这里是你的区块链服务器的ip地址:20201]    # The peer list to connect
# Configure a private topic as a topic message sender.
# [[amop]]
# topicName  PrivateTopic1
# publicKeys  [ conf/amop/consumer_public_key_1.pem ]    # Public keys of the nodes that you want to send AMOP message of this topic to.# Configure a private topic as a topic subscriber.
# [[amop]]
# topicName  PrivateTopic2
# privateKey  conf/amop/consumer_private_key.p12         # Your private key that used to subscriber verification.
# password  123456[account]
keyStoreDir  account         # The directory to load/store the account file, default is account
# accountFilePath            # The account file path (default load from the path specified by the keyStoreDir)
accountFileFormat  pem       # The storage format of account file (Default is pem, p12 as an option)# accountAddress             # The transactions sending account address
# Default is a randomly generated account
# The randomly generated account is stored in the path specified by the keyStoreDir# password                   # The password used to load the account file[threadPool]
# channelProcessorThreadSize  16         # The size of the thread pool to process channel callback
# Default is the number of cpu cores# receiptProcessorThreadSize  16         # The size of the thread pool to process transaction receipt notification
# Default is the number of cpu coresmaxBlockingQueueSize  102400             # The max blocking queue size of the thread pool 
这是利用toml文件的配置形式进行配置 官网提供了其他的配置文件格式 如果有修改 跳转官网文档查看 
Fisco-Bcos配置文件详解 
3、编写测试方法和读取配置文件 
1.编写SpringBoot配置类 代码如下 
package com.webase.config;import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Project: emr* Package: com.webase.config* Description:* author: xiaoMa* Email:17320080198163.com* date: 2024年10月25日 16:30*/
Configuration
public class BcosClientConfig {Beanpublic Client client() throws Exception {String path  getClass().getClassLoader().getResource(config-example.toml).getPath();if (path  null) {throw new IllegalArgumentException(找不到配置文件。);}BcosSDK bcosSDK  BcosSDK.build(path);// groupId 设为 1return bcosSDK.getClient(1);}
} 
这里是一个Spring配置类用于配置BcosSDK客户端。这个类通过读取config-example.toml配置文件来初始化BcosSDK并返回一个Client实例。后面我们测试类会用到 
2.编写测试方法 测试合约方法 在test下创建测试类 注意包名 这里的测试集成了springboot测试 
代码如下 
package com.webase;import com.webase.config.BcosClientConfig;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;/*** Project: emr* Package: com.webase* Description:* author: xiaoMa* Email:17320080198163.com* date: 2024年10月25日 16:09*/RunWith(SpringRunner.class)
SpringBootTest
ContextConfiguration(classes  BcosClientConfig.class)
public class HelloWorldDemoTest {Autowiredprivate Client client1;Testpublic void testHello() throws Exception {// 合约地址String contractAddress  0x8002b168b1f81d6c6d1148f579370648cd28dde8;// 创建一个私钥对CryptoKeyPair keyPair  client1.getCryptoSuite().createKeyPair();// 加载HelloWorld合约HelloWorld helloWorld  HelloWorld.load(contractAddress, client1, keyPair);// 调用HelloWorld合约set方法//TransactionReceipt receipt  helloWorld.set(Hello sent from Java!——10.26);// 打印交易回执的交易哈希//System.out.println(receipt.getTransactionHash());// 调用HelloWorld合约get方法String s  helloWorld.get();// 打印get方法的返回值System.out.println(s);}
} 
这里的contractAddress 必须要和前面部署的时候生成的合约地址保存一致 1·测试合约的get方法 可以看出 这个值是咱们之前在WeBASE-Front平台中通过图形化方式进行合约调用所设置的值 没错 
2·测试合约的set方法 测试跑通了 并且返回了交易哈希 
我们可以再次调用get方法测试是不是真的成功赋值  
可以看到没有问题 是我们赋的值没错 
更多功能接口请移步Fisco-Bcos官网开发文档 Fisco-Bcos开发文档 文章转载自: http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.ndyrb.com.gov.cn.ndyrb.com http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.pkrb.cn.gov.cn.pkrb.cn http://www.morning.vjwkb.cn.gov.cn.vjwkb.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.zzgtdz.cn.gov.cn.zzgtdz.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.hsrpc.cn.gov.cn.hsrpc.cn