https://blog.csdn.net/CwFinder/article/details/88819973
工程操作节点的配置主要依据的配置文件bitcoin.conf内容如下
server=1
rpcuser=123456
rpcpassword=abcdef
rpcallowip=0.0.0.0
rpcport=8888
txindex=1
(LTC无论节点安装还有操作工程都和BTC没有差异)
com.github.briandilley.jsonrpc4j
jsonrpc4j
1.1
org.projectlombok
lombok
1.16.20
provided
com.alibaba
fastjson
1.2.7
Constants.java
package constants;
public class Constants {
public static final String RPC_USER = "123456";
public static final String RPC_PASSWORD = "abcdef";
public static final String RPC_ALLOWIP = "http://127.0.0.1";
public static final String RPC_PORT = "8888";
}
里面的属性值主要是对应bitcoin.conf内的值,如果您自己有修改请自行对应。
Test.java
package test;
import util.RpcClient;
public class Test {
public static void main(String[] args) {
RpcClient client = new RpcClient();
String address = client.engine("getnewaddress");
System.out.println(address);
String toaddress = "muzo9k9HmPdbCQSuUJWXfiEyrUHxDf1hRU";
String amount = "0.01";
System.out.println(client.engine("sendtoaddress",String.class, toaddress, String.valueOf(amount)));
}
}
BaseException.java
package util;
public class BaseException extends RuntimeException {
enum CoreRetCodeEnum {
RET_CODE_000000("A001", "成功"),
RET_CODE_999999("A999", "异常");
private String retErrCode;
private String retErrMessage;
private CoreRetCodeEnum(String errCode, String errMessage) {
this.retErrCode = errCode;
this.retErrMessage = errMessage;
}
public String getRetErrCode() {
return this.retErrCode;
}
public void setRetErrCode(String retErrCode) {
this.retErrCode = retErrCode;
}
public String getRetErrMessage() {
return this.retErrMessage;
}
public void setRetErrMessage(String retErrMessage) {
this.retErrMessage = retErrMessage;
}
}
private static final long serialVersionUID = 7833283455112352655L;
private String errCode;
public BaseException(String errCode, String message) {
super(message);
this.errCode = errCode;
}
public BaseException() {
}
public BaseException(String message) {
super(message);
}
public BaseException(String message, Throwable cause) {
super(message, cause);
}
public BaseException(Throwable cause) {
super(cause);
}
protected BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public String getErrCode() {
return this.errCode;
}
public static BaseException error(String errCode, String errMsg) {
return new BaseException(errCode, errMsg);
}
public static BaseException error(String errMsg) {
return new BaseException(CoreRetCodeEnum.RET_CODE_999999.getRetErrCode(), errMsg);
}
}
BaseRPCresponse.java
package util;
import lombok.Data;
@Data
public class BaseRPCresponse {
private T result;
private ErrorBean error;
private String id;
@Data
public static class ErrorBean {
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
* code : -32601
* message : Method not found
*/
private int code;
private String message;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
public ErrorBean getError() {
return error;
}
public void setError(ErrorBean error) {
this.error = error;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
RpcClient.java
package util;
import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import constants.Constants;
import javax.validation.constraints.NotNull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class RpcClient {
Logger log = LoggerFactory.getLogger(getClass());
private static final String URL = Constants.RPC_ALLOWIP + ":" + Constants.RPC_PORT;
private JsonRpcHttpClient mClient;
public RpcClient() {
String cred = Base64.encodeBase64String((Constants.RPC_USER + ":" + Constants.RPC_PASSWORD).getBytes());
Map headers = new HashMap<>(1);
headers.put("Authorization", "Basic " + cred);
try {
this.mClient = new JsonRpcHttpClient(new URL(URL), headers);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
/**
*
* @param method 方法名
* @param var 参数
* @return 结果
*/
public String engine(@NotNull String method, Object... var) {
return engine(method, String.class, var);
}
/**
*
* @param method 方法名
* @param clazz 返回类型
* @param var 参数
* @return 结果
*/
public T engine(@NotNull String method, Class clazz, Object... var) {
try {
return mClient.invoke(method, var, clazz);
} catch (Throwable throwable) {
String message = throwable.getMessage();
Throwable cause = throwable.getCause();
log.error("error cause=" + cause);
log.error("error message=" + throwable);
try {
BaseRPCresponse res = JSON.parseObject(message, BaseRPCresponse.class);
BaseRPCresponse.ErrorBean error = res.getError();
throw new BaseException(String.valueOf(error.getCode()), error.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new BaseException("W000", e.getMessage());
}
}
}
}
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.bitwin
rpc-test
0.0.1-SNAPSHOT
rpc-test
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-mongodb
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.github.briandilley.jsonrpc4j
jsonrpc4j
1.1
org.projectlombok
lombok
1.16.20
provided
com.alibaba
fastjson
1.2.7
org.springframework.boot
spring-boot-maven-plugin
application.properties内没有任何配置