前言:
搭建完钱包节点(节点搭建不作说明^_^),调用其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