Example usage for javax.xml.bind.annotation.adapters HexBinaryAdapter unmarshal

List of usage examples for javax.xml.bind.annotation.adapters HexBinaryAdapter unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind.annotation.adapters HexBinaryAdapter unmarshal.

Prototype

public byte[] unmarshal(String s) 

Source Link

Usage

From source file:com.shuffle.bitcoin.blockchain.BlockchainDotInfo.java

/**
 *
 * This function takes in a transaction hash and passes it to Blockchain.info's API.
 * After some formatting, it returns a bitcoinj Transaction object using this transaction hash.
 *
 *//*from w w w.  j  av a  2  s  .c o m*/
public synchronized org.bitcoinj.core.Transaction getTransaction(String transactionHash) throws IOException {

    String url = "https://blockchain.info/tr/rawtx/" + transactionHash + "?format=hex";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", userAgent);
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuilder response = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    HexBinaryAdapter adapter = new HexBinaryAdapter();
    byte[] bytearray = adapter.unmarshal(response.toString());
    // bitcoinj needs this Context variable
    Context context = Context.getOrCreate(netParams);
    return new org.bitcoinj.core.Transaction(netParams, bytearray);

}

From source file:com.shuffle.bitcoin.blockchain.BlockCypherDotCom.java

/**
 *
 * This function takes in a transaction hash and passes it to Blockchain.info's API.
 * After some formatting, it returns a bitcoinj Transaction object using this transaction hash.
 *
 *//*from   w  w  w.ja v a 2 s .  com*/
public synchronized org.bitcoinj.core.Transaction getTransaction(String transactionHash) throws IOException {
    rateLimiter.acquire();
    String url;
    if (netParams == NetworkParameters.fromID(NetworkParameters.ID_TESTNET)) {
        url = "https://api.blockcypher.com/v1/btc/test3/txs/" + transactionHash + "?includeHex=true";
    } else {
        url = "https://api.blockcypher.com/v1/btc/main/txs/" + transactionHash + "?includeHex=true";
    }
    URL obj = new URL(url);
    rateLimiter.acquire();
    JSONTokener tokener = new JSONTokener(obj.openStream());
    JSONObject root = new JSONObject(tokener);
    HexBinaryAdapter adapter = new HexBinaryAdapter();
    byte[] bytearray = adapter.unmarshal(root.get("hex").toString());
    org.bitcoinj.core.Transaction transaction = new org.bitcoinj.core.Transaction(netParams, bytearray);
    return transaction;
}

From source file:com.shuffle.bitcoin.blockchain.Btcd.java

/**
 * This method takes in a transaction hash and returns a bitcoinj transaction object.
 *//*from w ww .  ja  v  a2s .  c o m*/
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;

}

From source file:com.shuffle.bitcoin.blockchain.Btcd.java

/**
 * This method will take in an address hash and return a List of all transactions associated with
 * this address.  These transactions are in bitcoinj's Transaction format.
 */// w  ww.  j  a v  a 2s.c  om
public synchronized List<Transaction> getAddressTransactionsInner(String address) throws IOException {

    List<Transaction> txList = null;
    String requestBody = "{\"jsonrpc\":\"2.0\",\"id\":\"null\",\"method\":\"searchrawtransactions\", \"params\":[\""
            + address + "\"]}";

    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());
        JSONArray jsonarray = null;
        txList = new LinkedList<>();
        if (json.isNull("result")) {
            return txList;
        } else {
            jsonarray = json.getJSONArray("result");
        }
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject currentJson = jsonarray.getJSONObject(i);
            String txid = currentJson.get("txid").toString();
            HexBinaryAdapter adapter = new HexBinaryAdapter();
            byte[] bytearray = adapter.unmarshal(currentJson.get("hex").toString());
            Context context = Context.getOrCreate(netParams);
            int confirmations = Integer.parseInt(currentJson.get("confirmations").toString());
            boolean confirmed;
            if (confirmations == 0) {
                confirmed = false;
            } else {
                confirmed = true;
            }
            org.bitcoinj.core.Transaction bitTx = new org.bitcoinj.core.Transaction(netParams, bytearray);
            Transaction tx = new Transaction(txid, bitTx, false, confirmed);
            txList.add(tx);
        }

    }

    out.flush();
    out.close();

    return txList;

}

From source file:org.jts.eclipse.conversion.cjsidl.ConversionUtil.java

/**
 * Converts a hex string to a byte array
 * @param hexString - input hex string//  w  w w . j  a  va2 s.c  o  m
 * @return - resulting byte array
 */
public static byte[] hexStringToByteArray(String hexString) {
    HexBinaryAdapter adapter = new HexBinaryAdapter();
    if (hexString.startsWith("0x")) {
        hexString = hexString.substring(2, hexString.length());
    }
    byte[] bytes = adapter.unmarshal(hexString);
    return bytes;
}