chia/xch rpc形式充提生成地址

这里暂时用的都是rpc,只用到了 wallet模块,个人觉得比较复杂的是节点的搭建与维护,官方也经常更新,我代码后续也会进行优化改进,欢迎技术交流 qq:284466315 vx:a284466315

1.生成地址: 

 JSONObject post = post("https://localhost:9256/get_next_address", "{\"wallet_id\": 1, \"new_address\":true}", AUTH_WALLET_FILE_URL, AUTH_KEYWORDS);
            if (post != null && post.getBoolean("success")) {
                return post.getString("address");
            }

2.查询充值,这里暂时没有用扫块,后续会更新

 JSONObject transactions = post("https://localhost:9256/get_transactions", "{\"wallet_id\": 1}", AUTH_WALLET_FILE_URL, AUTH_KEYWORDS);
            Boolean success = transactions.getBoolean("success");
            if (transactions != null && success) {
                AddressUtil addressUtil = AddressUtil.getInstance();
                JSONArray transactionsList = transactions.getJSONArray("transactions");
                for (Object transaction :
                        transactionsList) {
                    JSONObject jsonObject = JSONObject.parseObject(transaction.toString());
                    // 0接收记录 1 提币记录
                    Integer sent = jsonObject.getInteger("sent");
                    // 区块确认高度
                    Integer confirmedAtHeight = jsonObject.getInteger("confirmed_at_height");
                    //是否被确认
                    Boolean confirmed = jsonObject.getBoolean("confirmed");
                    // hash值
                    JSONObject coinIdVo = jsonObject.getJSONArray("additions").getJSONObject(0);
                        String hash = getHash(coinIdVo.getBigInteger("amount"), coinIdVo.getString("parent_coin_info"), coinIdVo.getString("puzzle_hash"));
                    // 接收地址
                    String toAddress = jsonObject.getString("to_address");
                    BigDecimal amount = jsonObject.getBigDecimal("amount").divide(BigDecimal.TEN.pow(12), 8, BigDecimal.ROUND_DOWN);

3.提币 

JSONObject requestBody = new JSONObject();
        requestBody.put("wallet_id", 1);
        requestBody.put("amount", amount.multiply(BigDecimal.TEN.pow(12)).toBigInteger());
        requestBody.put("fee", 0);
        requestBody.put("address", toAddress);

        JSONObject post = post("https://localhost:9256/send_transaction", requestBody.toJSONString(), AUTH_WALLET_FILE_URL, AUTH_KEYWORDS);
        Boolean success = post.getBoolean("success");

 

你可能感兴趣的:(java,区块链,钱包,区块链,java)