原创

java使用json-rpc 调用btc、usdt钱包节点

前言:

   搭建完钱包节点(节点搭建不作说明^_^),调用其api进行签名转账等操作。

   详细Api地址参考:

   btc:https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list

   usdt:https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md

    这里使用json-rpc进行客户端构建,以springboot项目为基础:

pom:

<dependency>  
    <groupId>com.github.briandilley.jsonrpc4j</groupId>  
    <artifactId>jsonrpc4j</artifactId>  
    <version>1.1</version>  
</dependency>

Client:

@Component
@Configuration
public class CoinRpcClient {

private Logger LOG = LoggerFactory.getLogger(getClass());

@Value("${btc.rpc.user}")
public  String user; //验证用户名
@Value("${btc.rpc.password}")
public  String password; //验证密码
@Value("${btc.rpc.allowip}")
public  String allowip; //验证ip
@Value("${btc.rpc.port}")
public  String port; //验证端口


// 比特币身份认证
public JsonRpcHttpClient getClient() {
    JsonRpcHttpClient client = null;
    try {
        String cred = Base64.encodeBytes((user + ":" + password).getBytes());
        Map<String, String> headers = new HashMap<String, String>(1);
        headers.put("Authorization", "Basic " + cred);
        client = new JsonRpcHttpClient(new URL("http://" + allowip + ":" + port), headers);
    } catch (Exception e) {
        LOG.info("===com.bscoin.bit.env.CoinRpcClient:{} btc client !===",e.getMessage(),e);
    }
    return client;
}

}
常用接口:

public interface BtcService {

    /**
     * @throws Throwable 
     * @throws Exception   
    * @Title: validateaddress 验证钱包地址是否有效  
    * @param @param address
    * @param @return    参数  
    * @return boolean    返回类型  
    * @throws  
    */  
  boolean validateAddress(String address) throws  CoinException;


    /**
     * @throws CoinException   
    * @Title: getBalance  
    * @param @param account
    * @param @return    参数  
    * @return Object    返回类型  
    * @throws  
    */  
  double getBalance(String  account) throws CoinException;


    /**
     * @throws CoinException   
    * @Title: signSendToAddress  
    * @param @param address
    * @param @param amount
    * @param @return    参数  
    * @return Object    返回类型  
    * @throws  
    * <amount>是一个实数,并四舍五入到小数点后8位。如果成功,则返回事务ID <txid>
    */  
Object signSendToAddress(String address,double amount) throws CoinException;


    /**
     * @throws CoinException   
    * @Title: multiSendToAddress  
    * @param @param fromaccount
    * @param @param target
    * @param @return    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object multiSendToAddress(String fromaccount,Object target) throws CoinException;


    /**
     * @throws CoinException   
    * @Title: getTransaction  
    * @param @param txId
    * @param @return    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object getTrawtransaction(String txId,int verbose) throws CoinException;


    /**  
    * @Title: createrawTransaction  
    * @param @param transferInfo
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object createrawTransaction(Object transferInfo,Object sendInfo) throws CoinException;


    /**  
    * @Title: signrawTransaction  
    * @param @param hexstring
    * @param @param transferInfo
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object signrawTransaction(String hexstring, Object transferInfo) throws CoinException;


    /**  
    * @Title: sendrawTransaction  
    * @param @param hexHash
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  

Object sendrawTransaction(String hexHash) throws CoinException;



    /**  
    * @Title: decoderawtransaction  
    * @param @param hex
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object decoderawtransaction(String hex) throws CoinException;
    /**  
    * @Title: getTransaction  
    * @param @param txId
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  

Object getTransaction(String txId) throws CoinException;

    /**
     * @throws CoinException   
    * @Title: setAccount  
    * @param @param address
    * @param @param account
    * @param @return    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object setAccount(String address,String account) throws CoinException;


    /**  
    * @Title: listReceivedByAddress  
    * @param @param minconf
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object listReceivedByAddress(int minconf) throws CoinException;


    /**  
    * @Title: settxfee  
    * @param @param account
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  

Object settxfee(double account) throws CoinException;


    /**  
    * @Title: encryptwallet  
    * @param @param passphrase
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  

Object encryptwallet(String passphrase) throws CoinException;


    /**  
    * @Title: walletpassphrase  
    * @param @param passphrase
    * @param @param timeout
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  

Object walletpassphrase(String passphrase,int timeout) throws CoinException;



    /**  
    * @Title: blockCount  
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object getBlockCount() throws CoinException;


    /**  
    * @Title: getBlockHash  
    * @param @param index
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object getBlockHash(int index) throws CoinException;

    /**  
    * @Title: getblock  
    * @param @param blockHash
    * @param @return
    * @param @throws CoinException    参数  
    * @return Object    返回类型  
    * @throws  
    */  
Object getblock(String blockHash) throws CoinException;

}
BTC api 调用使用:

@Service
public class BtcServiceImpl implements BtcService {

private Logger LOG = LoggerFactory.getLogger("btc");

@Autowired
private CoinRpcClient client;

@Override
public boolean validateAddress(String address) throws CoinException {
    try {
        String res = (String) client.getClient().invoke("validateaddress", new Object[] { address }, Object.class);
        if (!StringUtils.isEmpty(res)) {
            JSONObject obj = JSON.parseObject(res);
            if (obj.getBoolean("isvalid") == true) {
                return true;
            }
        }
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.validateAddress(String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: address=%s", address));
    }
    return false;
}

@Override
public double getBalance(String account) throws CoinException {
    double balance;
    try {
        if (!StringUtils.isEmpty(account)) {
            balance = (double) client.getClient().invoke("getbalance", new Object[] { account }, Object.class);
        } else {
            balance = (double) client.getClient().invoke("getbalance", new Object[] {}, Object.class);
        }
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.getBalance(String...):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: account=%s", account));
    }
    return balance;
}

@Override
public Object signSendToAddress(String address, double amount) throws CoinException {
    try {
        String txId = client.getClient().invoke("sendtoaddress", new Object[] { address, amount }, Object.class).toString();
        return txId;
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.signSendToAddress(String, double):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: address=%s,amount=%s", address, amount));
    }
}

@Override
public Object multiSendToAddress(String fromaccount, Object target) throws CoinException {
    try {
        String txId = client.getClient().invoke("sendmany", new Object[] { fromaccount, target }, Object.class).toString();
        return txId;
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.signSendToAddress(String, double):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: fromaccount=%s,target=%s", fromaccount, target));
    }
}

@Override
public Object getTrawtransaction(String txId, int verbose) throws CoinException {
    try {
        return client.getClient().invoke("getrawtransaction", new Object[] { txId, verbose }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.getTrawtransaction(String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: txId=%s", txId));
    }
}

@Override
public Object getTransaction(String txId) throws CoinException {
    try {
        return client.getClient().invoke("gettransaction", new Object[] { txId }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.getTransaction(String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: txId=%s", txId));
    }
}

@Override
public Object setAccount(String address, String account) throws CoinException {
    try {
        return client.getClient().invoke("setaccount", new Object[] { address, account }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.setAccount(String, String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: address=%s,account=%s", address, account));
    }
}

@Override
public Object listReceivedByAddress(int minconf) throws CoinException {
    try {
        if (1 != minconf) {
            return client.getClient().invoke("listreceivedbyaccount", new Object[] { minconf }, Object.class);
        } else {
            return client.getClient().invoke("listreceivedbyaccount", new Object[] {}, Object.class);
        }
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.BtcServiceImpl.listReceivedByAddress(int minconf):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: minconf=%s", minconf));
    }
}

@Override
public Object settxfee(double account) throws CoinException {
    try {
        return client.getClient().invoke("settxfee", new Object[] { account }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.settxfee(double):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: account=%s", account));
    }
}

@Override
public Object encryptwallet(String passphrase) throws CoinException {
    try {
        return client.getClient().invoke("encryptwallet", new Object[] { passphrase }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.encryptwallet(String) ===", e.getMessage(), e);
        throw new CoinException(e.getMessage());
    }
}

@Override
public Object walletpassphrase(String passphrase, int timeout) throws CoinException {
    try {
        return client.getClient().invoke("walletpassphrase", new Object[] { passphrase, timeout }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.walletpassphrase(String, int):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: passphrase=%s,timeout=%s", passphrase, timeout));
    }
}

@Override
public Object createrawTransaction(Object transferInfo, Object sendInfo) throws CoinException {
    try {
        return client.getClient().invoke("createrawtransaction", new Object[] { transferInfo, sendInfo }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.createrawTransaction(Object):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: transferInfo=%s,sendInfo=%s", transferInfo, sendInfo));
    }
}

@Override
public Object signrawTransaction(String hexstring, Object transferInfo) throws CoinException {
    try {
        return client.getClient().invoke("signrawtransaction", new Object[] { hexstring, transferInfo }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.signrawTransaction(String, Object):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: hexstring=%s,transferInfo=%s", hexstring, transferInfo));
    }
}

@Override
public Object sendrawTransaction(String hexHash) throws CoinException {
    try {
        return client.getClient().invoke("sendrawtransaction", new Object[] { hexHash }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.sendrawTransaction(String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: hexHash=%s", hexHash));
    }
}


@Override
public Object decoderawtransaction(String hex) throws CoinException {
    try {
        return client.getClient().invoke("decoderawtransaction", new Object[] { hex }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.wallet.bit.service.btc.impl.BtcServiceImpl.decoderawtransaction(String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: hex=%s", hex));
    }
}


@Override
public Object getBlockCount() throws CoinException {
    try {
        return client.getClient().invoke("getblockcount", new Object[] {}, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.wallet.bit.service.btc.impl.BtcServiceImpl.getBlockCount():{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage());
    }
}

@Override
public Object getBlockHash(int index) throws CoinException {
    try {
        return client.getClient().invoke("getblockhash", new Object[]{index}, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.wallet.bit.service.btc.impl.BtcServiceImpl.getBlockHash(int):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: index=%s", index));
    }
}


@Override
public Object getblock(String blockHash) throws CoinException {
    try {
        return client.getClient().invoke("getblock", new Object[] { blockHash }, Object.class);
    } catch (Throwable e) {
        LOG.info("=== com.bscoin.bit.service.btc.impl.BtcServiceImpl.getblock(String):{} ===", e.getMessage(), e);
        throw new CoinException(e.getMessage() + String.format("[params]: blockHash=%s", blockHash));
    }
}

}
USDT api调用:只需在调用接口方法加上 omni_ 例如:

return client.getClient().invoke("omni_getbalance", new Object[] {address,propertyid}, Object.class);

/创建普通发送/
return client.getUsdtClient().invoke("omni_createpayload_simplesend", new Object[] {propertyid,amount}, Object.class);

/创建原始交易返回/
public Object omniCreaterawtxOpreturn(String rawtx, String playload) throws CoinException {
try {
if(null == rawtx){
return client.getUsdtClient().invoke("omni_createrawtx_opreturn", new Object[] {null,playload}, Object.class);
}else{
return client.getUsdtClient().invoke("omni_createrawtx_opreturn", new Object[] {rawtx,playload}, Object.class);
}
}
}

以上是节点调用的简单api调用啦。

正文到此结束
本文目录