XRP瑞波JAVA调用(离线签名)

具体注意点请参考:https://blog.csdn.net/liu1765686161/article/details/82492937

1.需要安装两个jar包   https://github.com/ripple-unmaintained/ripple-lib-java  中

ripple-bouncycastle 和  ripple-core .

2. 可以编译后放入maven仓库,然后引用 

例如:


            com.ripple
            ripple-bouncycastle
            1.0.0
        
        
          com.ripple
          ripple-core
          1.0.0
        

3,直接上代码 

常量配置

private String getUrl = "https://data.ripple.com";
private String postUrl = "https://s1.ripple.com:51234";
private String address = "rani9PZFVtQtAjVsvQY7AGc7xZVFLjPc1Z";
private String password = "";
private static final String gasFee = "100";
private static final String COIN_XRP = "XRP";
private Logger log = Log.get();
private final static String RESULT = "result";
private final static String SUCCESS = "success";
private final static String TES_SUCCESS = "tesSUCCESS";
private final static String METHOD_GET_TRANSACTION = "/v2/accounts/{0}/transactions";
private final static String METHOD_GET_BALANCE = "/v2/accounts/{0}/balances";
private final static String METHOD_POST_INDEX = "ledger_current";
private final static String METHOD_POST_ACCOUNT_INFO = "account_info";
private final static String METHOD_POST_SUBMIT = "submit";

获取账户序列号和区块最新序列号

public Map getAccountSequenceAndLedgerCurrentIndex(){
    	HashMap params = new HashMap();
    	params.put("account", address);
    	params.put("strict", "true");
    	params.put("ledger_index", "current");
    	params.put("queue", "true");
    	JSONObject re = doRequest(METHOD_POST_ACCOUNT_INFO,params);
        if (re != null) {
        	JSONObject  result = re.getJSONObject("result");
			if (SUCCESS.equals(result.getString("status"))) {
				Map map = new HashMap();
				map.put("accountSequence", result.getJSONObject("account_data").getString("Sequence"));
				map.put("ledgerCurrentIndex", result.getString("ledger_current_index"));
				return map;
			}
		}
        return null;
    }

签名

public String sign(String toAddress,Double value){
    	value = BigDecimalUtil.mul(value, 1000000);
    	Integer vInteger = BigDecimal.valueOf(value).intValue();
    	Map map = getAccountSequenceAndLedgerCurrentIndex();
    	Payment payment = new Payment();
        payment.as(AccountID.Account,     address);
        payment.as(AccountID.Destination, toAddress);
        payment.as(UInt32.DestinationTag, "1");
        payment.as(Amount.Amount,         vInteger.toString());
        payment.as(UInt32.Sequence,       map.get("accountSequence"));
        payment.as(UInt32.LastLedgerSequence, map.get("ledgerCurrentIndex")+4);
        payment.as(Amount.Fee,            gasFee);
        SignedTransaction signed = payment.sign(password);
        if (signed != null) {
        	return signed.tx_blob;
		}
        return null;
    }

发送交易信息

public String send(String toAddress,double value){
    	String txBlob = this.sign(toAddress, value);
    	if (StringUtils.isEmpty(txBlob)) {
			log.error("签名失败:{}",toAddress);
			return null;
		}
    	HashMap params = new HashMap();
    	params.put("tx_blob", txBlob);
    	//签名
    	JSONObject json = doRequest(METHOD_POST_SUBMIT, params);
    	if (!isError(json)) {
    		JSONObject result = json.getJSONObject(RESULT);
    		if (result != null) {
				if (TES_SUCCESS.equals(result.getString("engine_result"))) {
					String hash = result.getJSONObject("tx_json").getString("hash");
					if (!StringUtils.isEmpty(hash)) {
						log.info("转账成功:toAddress:{},value:{},hash:{}",toAddress,value,hash);
						return hash;
					}else {
						log.error("转账失败:toAddress:{},value:{},hash:{}",toAddress,value,hash);
					}
				}
			}
		}
    	return null;
    }

希望能帮到大家,欢迎大家一起分享。

觉得有用请打赏,你的鼓励就是我的动力!

有问题可以通过chat向我提问,共同进步

同时也可以加入我创建的技术交流群629042605

你可能感兴趣的:(区块链)