赞
踩
1、首先在本地安装BTC(LTC)钱包,安装包下载地址。
2、在数据文件夹中新建bitcoin.conf(litecoin.conf),默认数据文件夹位置在 C:\Users\Administrator.PC-20170606QNRN\AppData\Roaming\BitCoin
3、打开新建的配置文件,输入以下内容
- rpcuser=userrpc
- rpcpassword=userpassword
- rpcallowip=127.0.0.1
- rpcport=61315
- server=1
4、打开钱包应用程序,通过cmd命令:netstat -ano|findstr "61315"
由此可见此端口已打开。
5、新建JAVA工程,创建两个类:CoinUtils.java,Constants.java
添加MAVEN依赖
- <dependency>
- <groupId>com.github.briandilley.jsonrpc4j</groupId>
- <artifactId>jsonrpc4j</artifactId>
- <version>1.1</version>
- </dependency>
6、代码如下
- package com.ant.wallet;
-
- public class Constants {
- public static String RPC_USER = "userrpc";
- public static String RPC_PASSWORD = "userpassword";
- public static String RPC_ALLOWIP = "127.0.0.1";
- public static String RPC_PORT = "61315";
- }
- package com.ant.wallet;
-
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
-
-
- import com.googlecode.jsonrpc4j.Base64;
- import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
-
- public class CoinUtils {
-
-
- private static CoinUtils instance;
- private static void init() throws Throwable {
- if(null == instance){
- instance = new CoinUtils();
- }
- }
-
- private JsonRpcHttpClient client;
- public CoinUtils() throws Throwable{
- // 身份认证
- String cred = Base64.encodeBytes((Constants.RPC_USER + ":" + Constants.RPC_PASSWORD).getBytes());
- Map<String, String> headers = new HashMap<String, String>(1);
- headers.put("Authorization", "Basic " + cred);
- client = new JsonRpcHttpClient(new URL("http://"+Constants.RPC_ALLOWIP+":"+Constants.RPC_PORT), headers);
- }
-
-
- public static CoinUtils getInstance() throws Throwable {
- init();
- return instance;
- }
-
-
- /**
- * 验证地址是否存在
- * @param address
- * @return
- * @throws Throwable
- */
- public String validateaddress(String address)throws Throwable{
- return (String) client.invoke("validateaddress", new Object[] {address}, Object.class).toString();
- }
-
-
- /**
- * 如果钱包加密需要临时解锁钱包
- * @param password
- * @param time
- * @return
- * @throws Throwable
- */
- public String walletpassphase(String password,int time)throws Throwable{
- return (String) client.invoke("walletpassphase", new Object[] {password,time}, Object.class).toString();
- }
-
- /**
- * 转账到制定的账户中
- * @param address
- * @param amount
- * @return
- * @throws Throwable
- */
- public String sendtoaddress(String address,double amount)throws Throwable{
- return (String) client.invoke("sendtoaddress", new Object[] {address,amount}, Object.class).toString();
- }
-
- /**
- * 查询账户下的交易记录
- * @param account
- * @param count
- * @param offset
- * @return
- * @throws Throwable
- */
- public String listtransactions(String account, int count ,int offset )throws Throwable{
- return (String) client.invoke("listtransactions", new Object[] {account,count,offset}, Object.class).toString();
- }
-
- /**
- * 获取地址下未花费的币量
- * @param account
- * @param count
- * @param offset
- * @return
- * @throws Throwable
- */
- public String listunspent( int minconf ,int maxconf ,String address)throws Throwable{
- String[] addresss= new String[]{address};
- return (String) client.invoke("listunspent", new Object[] {minconf,maxconf,addresss}, Object.class).toString();
- }
-
- /**
- * 生成新的接收地址
- * @return
- * @throws Throwable
- */
- public String getNewaddress() throws Throwable{
- return (String) client.invoke("getnewaddress", new Object[] {}, Object.class).toString();
- }
-
- /**
- * 获取钱包信息
- * @return
- * @throws Throwable
- */
- public String getInfo() throws Throwable{
- return client.invoke("getinfo", new Object[] {}, Object.class).toString();
- }
-
- }
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png)
这样就实现了使用java代码与钱包进行通信。
以上列举了几个常用的方法,其他方法详见:这里
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。