用于创建 JsonRpcClient 对象的类,用于通过比特币测试网连接我的应用程序,已经安装了 bitcoind,并将其与测试网以及比特币主网络同步。尝试创建唯一的钱包地址但得到空指针异常,因为我可以观察到值没有被使用spring 工具套件和 maven dependencywf.bitcoin bitcoin-rpc-client 1.1.0从 application.properties 文件中使用
package bitcoin.utils;
import java.net.URL;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.naming.CommunicationException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;
import wf.bitcoin.javabitcoindrpcclient.BitcoinRPCException;
@Configuration
@Component
public class ResourceUtils {
private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class);
@Value("${node.bitcoind.rpc.protocol}")
private static String protocol;
@Value("${node.bitcoind.rpc.host}")
private static String host;
@Value("${node.bitcoind.rpc.port}")
private static String port;
@Value("${node.bitcoind.rpc.user}")
private static String user;
@Value("${node.bitcoind.rpc.password}")
private static String password;
@Value("${node.bitcoind.http.auth_scheme}")
private static String authScheme;
private static Properties nodeConfig;
@PostConstruct
void init() {
logger.info("in init {}", "in init");
nodeConfig = new Properties();
nodeConfig.setProperty("node.bitcoind.rpc.protocol", protocol);
logger.debug("protocol==============================================={}", protocol);
nodeConfig.setProperty("node.bitcoind.rpc.host", host);
logger.debug("host==============================================={}", host);
nodeConfig.setProperty("node.bitcoind.rpc.port", port);
logger.debug("port==============================================={}", port);
nodeConfig.setProperty("node.bitcoind.rpc.user", user);
logger.debug("user==============================================={}", user);
nodeConfig.setProperty("node.bitcoind.rpc.password", password);
logger.debug("password==============================================={}", password);
nodeConfig.setProperty("node.bitcoind.http.auth_scheme", authScheme);
logger.debug("authScheme==============================================={}", authScheme);
}
public ResourceUtils() {
}
public static CloseableHttpClient getHttpProvider() {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
return HttpClients.custom().setConnectionManager(connManager).build();
}
public static BitcoinJSONRPCClient getBtcdProvider() throws BitcoinRPCException, CommunicationException {
logger.info("getHttpProvider {}", getHttpProvider());
logger.info("getNodeConfig {}", getNodeConfig());
BitcoinJSONRPCClient bitcoinJsonRpcClient = null;
try {
URL url = new URL(protocol + "://" + user + ':' + password + "@" + host + ":" + port + "/");
bitcoinJsonRpcClient = new BitcoinJSONRPCClient(url);
logger.info("btcdClientt {}", bitcoinJsonRpcClient);
// bitcoinJsonRpcClient.setTxFee(BigDecimal.valueOf(0.001));
} catch (Exception e) {
return bitcoinJsonRpcClient;
}
return bitcoinJsonRpcClient;
}
public static Properties getNodeConfig() {
return nodeConfig;
}
}
配置文件
node.bitcoind.rpc.protocol = http
node.bitcoind.rpc.host = 127.0.0.1
node.bitcoind.rpc.port = 8332
node.bitcoind.rpc.user = username
node.bitcoind.rpc.password = password
node.bitcoind.http.auth_scheme = Basic
比特币配置文件
rpcuser=user
rpcpassword=password
testnet=1
rpcport=8332
rpcallowip=127.0.0.1
rpcallowip=195.154.11.93
server=1
获取比特币钱包地址的方法
public String generateWalletAddress(String id) {
try {
BitcoinJSONRPCClient client = ResourceUtils.getBtcdProvider();
return client.getNewAddress();
} catch (CommunicationException c) {
System.err.println(c);
return null;
}
}
堆栈跟踪
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-12 11:50:45.205 ERROR 10991 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceUtils': Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at bitcoin.BitcoinApplication.main(BitcoinApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.4.RELEASE.jar:2.2.4.RELEASE]
Caused by: java.lang.NullPointerException: null
at java.util.Hashtable.put(Hashtable.java:460) ~[na:1.8.0_242]
at java.util.Properties.setProperty(Properties.java:166) ~[na:1.8.0_242]
at bitcoin.utils.ResourceUtils.init(ResourceUtils.java:47) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
... 23 common frames omitted