/**
* This method takes in a transaction hash and returns a bitcoinj transaction object.
*/
synchronized org.bitcoinj.core.Transaction getTransaction(String transactionHash) throws IOException {
org.bitcoinj.core.Transaction tx = null;
String requestBody = "{\"jsonrpc\":\"2.0\",\"id\":\"null\",\"method\":\"getrawtransaction\", \"params\":[\"" + transactionHash + "\"]}";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
Base64 b = new Base64();
String authString = rpcuser + ":" + rpcpass;
String encoding = b.encodeAsString(authString.getBytes());
connection.setRequestProperty("Authorization", "Basic " + encoding);
connection.setRequestProperty("Content-Length", Integer.toString(requestBody.getBytes().length));
connection.setDoInput(true);
OutputStream out = connection.getOutputStream();
out.write(requestBody.getBytes());
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
JSONObject json = new JSONObject(response.toString());
String hexTx = (String) json.get("result");
HexBinaryAdapter adapter = new HexBinaryAdapter();
byte[] bytearray = adapter.unmarshal(hexTx);
Context context = Context.getOrCreate(netParams);
tx = new org.bitcoinj.core.Transaction(netParams, bytearray);
}
out.flush();
out.close();
return tx;
}
Btcd.java 文件源码
java
阅读 21
收藏 0
点赞 0
评论 0
项目:Shufflepuff
作者:
评论列表
文章目录