com.google.bitcoin.core
Class Utils

java.lang.Object
  extended by com.google.bitcoin.core.Utils

public class Utils
extends Object

A collection of various utility methods that are helpful for working with the BitCoin protocol. To enable debug logging from the library, run with -Dbitcoinj.logging=true on your command line.


Field Summary
static BigInteger CENT
          How many "nanocoins" there are in 0.01 BitCoins.
static BigInteger COIN
          How many "nanocoins" there are in a BitCoin.
 
Constructor Summary
Utils()
           
 
Method Summary
static String bitcoinValueToFriendlyString(BigInteger value)
          Returns the given value in nanocoins as a 0.12 type string.
static String bytesToHexString(byte[] bytes)
          Returns the given byte array hex encoded.
static byte[] doubleDigest(byte[] input)
          See doubleDigest(byte[],int,int).
static byte[] doubleDigest(byte[] input, int offset, int length)
          Calculates the SHA-256 hash of the given byte range, and then hashes the resulting hash again.
static byte[] doubleDigestTwoBuffers(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2)
          Calculates SHA256(SHA256(byte range 1 + byte range 2)).
static boolean isLessThanUnsigned(long n1, long n2)
          Work around lack of unsigned types in Java.
static int readUint16BE(byte[] bytes, int offset)
           
static long readUint32(byte[] bytes, int offset)
           
static long readUint32BE(byte[] bytes, int offset)
           
static byte[] reverseBytes(byte[] bytes)
          Returns a copy of the given byte array in reverse order.
static byte[] sha256hash160(byte[] input)
          Calculates RIPEMD160(SHA256(input)).
static BigInteger toNanoCoins(int coins, int cents)
          Convert an amount expressed in the way humans are used to into nanocoins.
static BigInteger toNanoCoins(String coins)
          Convert an amount expressed in the way humans are used to into nanocoins.
static void uint32ToByteArrayBE(long val, byte[] out, int offset)
           
static void uint32ToByteArrayLE(long val, byte[] out, int offset)
           
static void uint32ToByteStreamLE(long val, OutputStream stream)
           
static void uint64ToByteStreamLE(BigInteger val, OutputStream stream)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COIN

public static final BigInteger COIN
How many "nanocoins" there are in a BitCoin. A nanocoin is the smallest unit that can be transferred using BitCoin. The term nanocoin is very misleading, though, because there are only 100 million of them in a coin (whereas one would expect 1 billion.


CENT

public static final BigInteger CENT
How many "nanocoins" there are in 0.01 BitCoins. A nanocoin is the smallest unit that can be transferred using BitCoin. The term nanocoin is very misleading, though, because there are only 100 million of them in a coin (whereas one would expect 1 billion).

Constructor Detail

Utils

public Utils()
Method Detail

toNanoCoins

public static BigInteger toNanoCoins(int coins,
                                     int cents)
Convert an amount expressed in the way humans are used to into nanocoins.


toNanoCoins

public static BigInteger toNanoCoins(String coins)
Convert an amount expressed in the way humans are used to into nanocoins.

This takes string in a format understood by BigDecimal.BigDecimal(String), for example "0", "1", "0.10", "1.23E3", "1234.5E-5".

Throws:
ArithmeticException - if you try to specify fractional nanocoins

uint32ToByteArrayBE

public static void uint32ToByteArrayBE(long val,
                                       byte[] out,
                                       int offset)

uint32ToByteArrayLE

public static void uint32ToByteArrayLE(long val,
                                       byte[] out,
                                       int offset)

uint32ToByteStreamLE

public static void uint32ToByteStreamLE(long val,
                                        OutputStream stream)
                                 throws IOException
Throws:
IOException

uint64ToByteStreamLE

public static void uint64ToByteStreamLE(BigInteger val,
                                        OutputStream stream)
                                 throws IOException
Throws:
IOException

doubleDigest

public static byte[] doubleDigest(byte[] input)
See doubleDigest(byte[],int,int).


doubleDigest

public static byte[] doubleDigest(byte[] input,
                                  int offset,
                                  int length)
Calculates the SHA-256 hash of the given byte range, and then hashes the resulting hash again. This is standard procedure in BitCoin. The resulting hash is in big endian form.


doubleDigestTwoBuffers

public static byte[] doubleDigestTwoBuffers(byte[] input1,
                                            int offset1,
                                            int length1,
                                            byte[] input2,
                                            int offset2,
                                            int length2)
Calculates SHA256(SHA256(byte range 1 + byte range 2)).


isLessThanUnsigned

public static boolean isLessThanUnsigned(long n1,
                                         long n2)
Work around lack of unsigned types in Java.


bytesToHexString

public static String bytesToHexString(byte[] bytes)
Returns the given byte array hex encoded.


reverseBytes

public static byte[] reverseBytes(byte[] bytes)
Returns a copy of the given byte array in reverse order.


readUint32

public static long readUint32(byte[] bytes,
                              int offset)

readUint32BE

public static long readUint32BE(byte[] bytes,
                                int offset)

readUint16BE

public static int readUint16BE(byte[] bytes,
                               int offset)

sha256hash160

public static byte[] sha256hash160(byte[] input)
Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.


bitcoinValueToFriendlyString

public static String bitcoinValueToFriendlyString(BigInteger value)
Returns the given value in nanocoins as a 0.12 type string.



Copyright © 2011. All Rights Reserved.