• fisco Java-sdk 快速入门案例


    1.安装环境(Ubantu)

    Linux IDEA下载: https://blog.csdn.net/JOJO_jiongjiong/article/details/123087307

    Linux Maven下载: https://zhuanlan.zhihu.com/p/443389963

    最好在setting.xml 把maven本地仓库也改一下。

    Linux java( 8-14都可以)下载:https://blog.csdn.net/baoqiaoben/article/details/78936955

    2.搭建区块链

    单机4节点区块链网络搭建: https://blog.csdn.net/Qhx20040819/article/details/131909957

     3.开发智能合约应用

    1. 使用 IDEA 创建一个Maven应用

    2.引入 fisco java sdk

    1. <dependency>
    2. <groupId>org.fisco-bcos.java-sdkgroupId>
    3. <artifactId>fisco-bcos-java-sdkartifactId>
    4. <version>2.9.1version>
    5. dependency>

    3. 配置SDK证书

    将节点的sdk证书拷贝到resources目录下面。

    1. # 假设SDK证书位于~/fisco/nodes/127.0.0.1/sdk/目录
    2. mkdir -p conf && cp -r ~/fisco/nodes/127.0.0.1/sdk/* conf

    4. 将合约生成对应的java 文件

    我们用拉取fisco 的控制台里默认带的HelloWorld合约来演示。

    在 console/目录下

    若控制台版本大于等于2.8.0:

    1. # 使用sol2java.sh将contracts/solidity下的所有合约编译产生bin,abi,java工具类。
    2. # 当前目录~/fisco/console
    3. $ bash sol2java.sh -p org.com.fisco
    4. # 以上命令中参数“org.com.fisco”是指定产生的java类所属的包名。
    5. # 通过命令./sol2java.sh -h可查看该脚本使用方法

    若控制台版本小于2.8.0:

    1. # 使用sol2java.sh将contracts/solidity下的所有合约编译产生bin,abi,java工具类。
    2. # 当前目录~/fisco/console
    3. $ bash sol2java.sh org.com.fisco
    4. # 以上命令中参数“org.com.fisco”是指定产生的java类所属的包名。
    5. # ./sol2java.sh [packageName] [solidityFilePath] [javaCodeOutputDir]

    查看编译结果

    1. $ ls contracts/sdk/java/org/com/fisco
    2. # 得到返回
    3. # HelloWorld.java KVTableTest.java ShaTest.java Table.java TableTest.java

    将该java 文件移动到你的java项目里。

    PS :注意,生成java文件包名要跟你项目里对应的包名要一致。

    5. 创建配置文件

    PS :我直接从文档粘贴的,详情请见:配置说明 — FISCO BCOS v2 v2.9.0 文档

    1. [cryptoMaterial]
    2. certPath = "conf" # The certification path
    3. # The following configurations take the certPath by default if commented
    4. # caCert = "conf/ca.crt" # CA cert file path
    5. # If connect to the GM node, default CA cert path is ${certPath}/gm/gmca.crt
    6. # sslCert = "conf/sdk.crt" # SSL cert file path
    7. # If connect to the GM node, the default SDK cert path is ${certPath}/gm/gmsdk.crt
    8. # sslKey = "conf/sdk.key" # SSL key file path
    9. # If connect to the GM node, the default SDK privateKey path is ${certPath}/gm/gmsdk.key
    10. # enSslCert = "conf/gm/gmensdk.crt" # GM encryption cert file path
    11. # default load the GM SSL encryption cert from ${certPath}/gm/gmensdk.crt
    12. # enSslKey = "conf/gm/gmensdk.key" # GM ssl cert file path
    13. # default load the GM SSL encryption privateKey from ${certPath}/gm/gmensdk.key
    14. [network]
    15. peers=["127.0.0.1:20200", "127.0.0.1:20201"] # The peer list to connect
    16. # AMOP configuration
    17. # You can use following two methods to configure as a private topic message sender or subscriber.
    18. # Usually, the public key and private key is generated by subscriber.
    19. # Message sender receive public key from topic subscriber then make configuration.
    20. # But, please do not config as both the message sender and the subscriber of one private topic, or you may send the message to yourself.
    21. # Configure a private topic as a topic message sender.
    22. # [[amop]]
    23. # topicName = "PrivateTopic"
    24. # publicKeys = [ "conf/amop/consumer_public_key_1.pem" ] # Public keys of the nodes that you want to send AMOP message of this topic to.
    25. # Configure a private topic as a topic subscriber.
    26. # [[amop]]
    27. # topicName = "PrivateTopic"
    28. # privateKey = "conf/amop/consumer_private_key.p12" # Your private key that used to subscriber verification.
    29. # password = "123456"
    30. [account]
    31. keyStoreDir = "account" # The directory to load/store the account file, default is "account"
    32. # accountFilePath = "" # The account file path (default load from the path specified by the keyStoreDir)
    33. accountFileFormat = "pem" # The storage format of account file (Default is "pem", "p12" as an option)
    34. # accountAddress = "" # The transactions sending account address
    35. # Default is a randomly generated account
    36. # The randomly generated account is stored in the path specified by the keyStoreDir
    37. # password = "" # The password used to load the account file
    38. [threadPool]
    39. # channelProcessorThreadSize = "16" # The size of the thread pool to process channel callback
    40. # Default is the number of cpu cores
    41. # receiptProcessorThreadSize = "16" # The size of the thread pool to process transaction receipt notification
    42. # Default is the number of cpu cores
    43. maxBlockingQueueSize = "102400" # The max blocking queue size of the thread pool

    将该配置文件也移动到conf同级目录。

     

    6. 使用测试文件部署和调用智能合约

    1. public class BcosSDKTest
    2. {
    3. // 获取配置文件路径
    4. public final String configFile = BcosSDKTest.class.getClassLoader().getResource("config.toml").getPath();
    5. public void testClient() throws ConfigException, ContractException {
    6. // 初始化BcosSDK
    7. BcosSDK sdk = BcosSDK.build(configFile);
    8. // 为群组1初始化client
    9. Client client = sdk.getClient(Integer.valueOf(1));
    10. // 获取群组1的块高
    11. BlockNumber blockNumber = client.getBlockNumber();
    12. // 向群组1部署HelloWorld合约
    13. CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();
    14. HelloWorld helloWorld = HelloWorld.deploy(client, cryptoKeyPair);
    15. // 调用HelloWorld合约的get接口
    16. String getValue = helloWorld.get();
    17. // 调用HelloWorld合约的set接口
    18. TransactionReceipt receipt = helloWorld.set("Hello, fisco");
    19. }
    20. public static void main(String[] args) throws ContractException, ConfigException {
    21. BcosSDKTest bcosSDKTest = new BcosSDKTest();
    22. bcosSDKTest.testClient();
    23. }
    24. }

     运行之后,控制台出现如此效果,代表成功:

  • 相关阅读:
    士官类学校名录
    Database之SQL:SQL在线编程、工作中常用SQL代码实践(以语法为导向的增、删、改、查,已基本涵盖所有语法案例)之详细攻略
    typescript
    图像采集 deep OCR
    C语言|图解指针变量
    72.动态插槽名称&无渲染组件与可组合
    网站交换友情链接是否对SEO优化有帮助?
    产品经理视角 | API接口知识小结
    备忘录模式(Memento Pattern)
    C++新经典 | C++ 查漏补缺(类)
  • 原文地址:https://blog.csdn.net/Qhx20040819/article/details/133718077