当前位置:   article > 正文

java使用JSON-RPC进行BTC、LTC钱包开发_java bitcoinrpc

java bitcoinrpc

1、首先在本地安装BTC(LTC)钱包,安装包下载地址

2、在数据文件夹中新建bitcoin.conf(litecoin.conf),默认数据文件夹位置在 C:\Users\Administrator.PC-20170606QNRN\AppData\Roaming\BitCoin

3、打开新建的配置文件,输入以下内容

  1. rpcuser=userrpc
  2. rpcpassword=userpassword
  3. rpcallowip=127.0.0.1
  4. rpcport=61315
  5. server=1

4、打开钱包应用程序,通过cmd命令:netstat -ano|findstr "61315" 


由此可见此端口已打开。

5、新建JAVA工程,创建两个类:CoinUtils.java,Constants.java

     添加MAVEN依赖

  1. <dependency>
  2. <groupId>com.github.briandilley.jsonrpc4j</groupId>
  3. <artifactId>jsonrpc4j</artifactId>
  4. <version>1.1</version>
  5. </dependency>

6、代码如下

  1. package com.ant.wallet;
  2. public class Constants {
  3. public static String RPC_USER = "userrpc";
  4. public static String RPC_PASSWORD = "userpassword";
  5. public static String RPC_ALLOWIP = "127.0.0.1";
  6. public static String RPC_PORT = "61315";
  7. }
  1. package com.ant.wallet;
  2. import java.net.URL;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import com.googlecode.jsonrpc4j.Base64;
  6. import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
  7. public class CoinUtils {
  8. private static CoinUtils instance;
  9. private static void init() throws Throwable {
  10. if(null == instance){
  11. instance = new CoinUtils();
  12. }
  13. }
  14. private JsonRpcHttpClient client;
  15. public CoinUtils() throws Throwable{
  16. // 身份认证
  17. String cred = Base64.encodeBytes((Constants.RPC_USER + ":" + Constants.RPC_PASSWORD).getBytes());
  18. Map<String, String> headers = new HashMap<String, String>(1);
  19. headers.put("Authorization", "Basic " + cred);
  20. client = new JsonRpcHttpClient(new URL("http://"+Constants.RPC_ALLOWIP+":"+Constants.RPC_PORT), headers);
  21. }
  22. public static CoinUtils getInstance() throws Throwable {
  23. init();
  24. return instance;
  25. }
  26. /**
  27. * 验证地址是否存在
  28. * @param address
  29. * @return
  30. * @throws Throwable
  31. */
  32. public String validateaddress(String address)throws Throwable{
  33. return (String) client.invoke("validateaddress", new Object[] {address}, Object.class).toString();
  34. }
  35. /**
  36. * 如果钱包加密需要临时解锁钱包
  37. * @param password
  38. * @param time
  39. * @return
  40. * @throws Throwable
  41. */
  42. public String walletpassphase(String password,int time)throws Throwable{
  43. return (String) client.invoke("walletpassphase", new Object[] {password,time}, Object.class).toString();
  44. }
  45. /**
  46. * 转账到制定的账户中
  47. * @param address
  48. * @param amount
  49. * @return
  50. * @throws Throwable
  51. */
  52. public String sendtoaddress(String address,double amount)throws Throwable{
  53. return (String) client.invoke("sendtoaddress", new Object[] {address,amount}, Object.class).toString();
  54. }
  55. /**
  56. * 查询账户下的交易记录
  57. * @param account
  58. * @param count
  59. * @param offset
  60. * @return
  61. * @throws Throwable
  62. */
  63. public String listtransactions(String account, int count ,int offset )throws Throwable{
  64. return (String) client.invoke("listtransactions", new Object[] {account,count,offset}, Object.class).toString();
  65. }
  66. /**
  67. * 获取地址下未花费的币量
  68. * @param account
  69. * @param count
  70. * @param offset
  71. * @return
  72. * @throws Throwable
  73. */
  74. public String listunspent( int minconf ,int maxconf ,String address)throws Throwable{
  75. String[] addresss= new String[]{address};
  76. return (String) client.invoke("listunspent", new Object[] {minconf,maxconf,addresss}, Object.class).toString();
  77. }
  78. /**
  79. * 生成新的接收地址
  80. * @return
  81. * @throws Throwable
  82. */
  83. public String getNewaddress() throws Throwable{
  84. return (String) client.invoke("getnewaddress", new Object[] {}, Object.class).toString();
  85. }
  86. /**
  87. * 获取钱包信息
  88. * @return
  89. * @throws Throwable
  90. */
  91. public String getInfo() throws Throwable{
  92. return client.invoke("getinfo", new Object[] {}, Object.class).toString();
  93. }
  94. }

这样就实现了使用java代码与钱包进行通信。

以上列举了几个常用的方法,其他方法详见:这里


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/831886
推荐阅读
相关标签
  

闽ICP备14008679号