web3sdk 是什么 Spring Boot Starter怎么用

2024-09-03 08:58

本文主要是介绍web3sdk 是什么 Spring Boot Starter怎么用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、Web3SDK是什么

Web3SDK为FISCO BCOS提供Java API。

利用FISCO BCOS JAVA SDK可以简单快捷的基于FISCO-BCOS进行区块链应用开发。

此版本只支持FISCO BCOS 2.0+。

 

 

 

关键特性

  • 实现FISCO BCOS的JSON-RPC的Java API。
  • 支持预编译合约管理区块链。
  • 支持链上信使协议为联盟链提供安全高效的消息信道。
  • 支持使用国密算法发送交易。

 

源码编译

环境要求:

  • JDK8及以上

一定要安装java8

https://shijianfeng.blog.csdn.net/article/details/116152884

 

  • Gradle 5.0及以上

 

编译运行如下命令:

$ cd web3sdk
$ ./gradlew build

编译结果: 编译的web3sdk jar位于:

web3sdk/dist/apps/web3sdk.jar

文档

  • 英文
  • 中文

快速入门

提供基于SDK的spring boot starter示例项目,示例项目使用了SDK的核心特性, 包括:

  • 如何连接FISCO BCOS区块链节点。
  • 在区块链网络中部署合约。
  • 读取部署合约的变量值。
  • 更新部署合约的变量值。
  • 提供SDK API接口的测试案例。

 

https://gitee.com/FISCO-BCOS/web3sdk/

 

 

二、怎么用

Spring Boot Starter

  该项目是基于Web3SDK的spring boot版本的示例项目。提供FISCO BCOS区块链应用开发的基本框架和基本的测试案例,帮助开发者基于FISCO BCOS区块链快速进行应用开发。此版本只支持FISCO BCOS 2.0+。

 

快速启动

前置条件

搭建FISCO BCOS区块链,具体步骤参考这里。

 

获取源码

git clone https://github.com/FISCO-BCOS/spring-boot-starter.git

 

节点证书配置

将节点所在目录nodes/${ip}/sdk下的ca.crtsdk.crtsdk.key文件拷贝到项目的src/main/resources录下供SDK使用

(FISCO BCOS 2.1以前,证书为ca.crtnode.crtnode.key`)。

 

 

配置文件设置

spring boot项目的配置文件application.yml如下图所示,其中加了注释的内容根据区块链节点配置做相应修改。

encrypt-type: # 0:standard, 1:guomiencrypt-type: 0group-channel-connections-config:caCert: classpath:ca.crtsslCert: classpath:sdk.crtsslKey: classpath:sdk.keyall-channel-connections:- group-id: 1 #group IDconnections-str:- 192.168.64.129:20200 # node listen_ip:channel_listen_port- 192.168.64.131:20200- 192.168.64.132:20200- 192.168.64.130:20200- group-id: 2connections-str:- 127.0.0.1:20202 # node listen_ip:channel_listen_port- 127.0.0.1:20203channel-service:group-id: 1 # The specified group to which the SDK connectsagency-name: agencyA # agency nameaccounts:pem-file: 0xcdcce60801c0a2e6bb534322c32ae528b9dec8d2.pemp12-file: 0x98333491efac02f8ce109b0c499074d47e7779a6.p12password: 123456

 

项目中关于SDK配置的详细说明请参考这里。

 

 

运行

编译并运行测试案例,在项目根目录下运行:

cd spring-boot-starter
./gradlew build
./gradlew test

当所有测试案例运行成功,则代表

  • 区块链运行正常,
  • 该项目通过SDK连接区块链正常。

可以通过这个网址看错误报告file:///home/shijianfeng/fisco/spring-boot-starter/build/reports/tests/test/index.html

开发者可以基于该项目进行具体应用开发。

注:如果在IntelliJ IDEA或Eclipse中运行该demo工程,则使用gradle wrapper模式,此外IntelliJ IDEA需要在设置中开启Annotation Processors功能。

Web3SDK - 常见的异常启动问题错误查看https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/faq/sdk.html?highlight=Web3SDK

 

测试案例介绍

该示例项目提供的测试案例,供开发者参考使用。测试案例主要分为对Web3j API,Precompiled Serveice API、Solidity合约文件转Java合约文件、部署和调用合约的测试。

 

Web3j API测试

提供Web3jApiTest测试类测试Web3j API。示例测试如下:

@Test
public void getBlockNumber() throws IOException {BigInteger blockNumber = web3j.getBlockNumber().send().getBlockNumber();System.out.println(blockNumber);assertTrue(blockNumber.compareTo(new BigInteger("0"))>= 0);
}

温馨提示: Application类初始化了web3j对象,在业务代码需要的地方可用注解的方式直接使用,使用方式如下:

@Autowired
private Web3j web3j

 

Precompiled Service API测试

提供PrecompiledServiceApiTest测试类测试Precompiled Service API。示例测试如下:

@Test
public void testSystemConfigService() throws Exception {SystemConfigSerivce systemConfigSerivce = new SystemConfigSerivce(web3j, credentials);systemConfigSerivce.setValueByKey("tx_count_limit", "2000");String value = web3j.getSystemConfigByKey("tx_count_limit").send().getSystemConfigByKey();System.out.println(value);assertTrue("2000".equals(value));
}

 

Solidity合约文件转Java合约文件测试

提供SolidityFunctionWrapperGeneratorTest测试类测试Solidity合约文件转Java合约文件。示例测试如下:

@Test
public void compileSolFilesToJavaTest() throws IOException {File solFileList = new File("src/test/resources/contract");File[] solFiles = solFileList.listFiles();for (File solFile : solFiles) {SolidityCompiler.Result res = SolidityCompiler.compile(solFile, true, ABI, BIN, INTERFACE, METADATA);System.out.println("Out: '" + res.output + "'");System.out.println("Err: '" + res.errors + "'");CompilationResult result = CompilationResult.parse(res.output);System.out.println("contractname  " + solFile.getName());Path source = Paths.get(solFile.getPath());String contractname = solFile.getName().split("\\.")[0];CompilationResult.ContractMetadata a = result.getContract(solFile.getName().split("\\.")[0]);System.out.println("abi   " + a.abi);System.out.println("bin   " + a.bin);FileUtils.writeStringToFile(new File("src/test/resources/solidity/" + contractname + ".abi"), a.abi);FileUtils.writeStringToFile(new File("src/test/resources/solidity/" + contractname + ".bin"), a.bin);String binFile;String abiFile;String tempDirPath = new File("src/test/java/").getAbsolutePath();String packageName = "org.fisco.bcos.temp";String filename = contractname;abiFile = "src/test/resources/solidity/" + filename + ".abi";binFile = "src/test/resources/solidity/" + filename + ".bin";SolidityFunctionWrapperGenerator.main(Arrays.asList("-a", abiFile,"-b", binFile,"-p", packageName,"-o", tempDirPath).toArray(new String[0]));}System.out.println("generate successfully");
}

该测试案例将src/test/resources/contract目录下的所有Solidity合约文件(默认提供HelloWorld合约)均转为相应的abi和bin文件,保存在src/test/resources/solidity目录下。然后将abi文件和对应的bin文件组合转换为Java合约文件,保存在src/test/java/org/fisco/bcos/temp目录下。SDK将利用Java合约文件进行合约部署与调用。

 

部署和调用合约测试

提供ContractTest测试类测试部署和调用合约。示例测试如下:

@Test
public void deployAndCallHelloWorld() throws Exception {//deploy contractHelloWorld helloWorld = HelloWorld.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();if (helloWorld != null) {System.out.println("HelloWorld address is: " + helloWorld.getContractAddress());//call set functionhelloWorld.set("Hello, World!").send();//call get functionString result = helloWorld.get().send();System.out.println(result);assertTrue( "Hello, World!".equals(result));}
}

https://github.com/FISCO-BCOS/spring-boot-starter/blob/master/doc/README_CN.md

这篇关于web3sdk 是什么 Spring Boot Starter怎么用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132572

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、