Android Open Source - bitcoin-wallet Base58






From Project

Back to project page bitcoin-wallet.

License

The source code is released under:

Copyright (C) 2011 by Caleb Anderson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the ...

If you think the Android project bitcoin-wallet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.dirtyfilthy.bitcoin.util;
/* w  w  w .j a  va  2  s  .  co  m*/
import java.math.BigInteger;
import java.text.ParseException;


public class Base58 {
  public final static String BASE58CHARS="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  public final static BigInteger FIFTY_EIGHT=new BigInteger("58");
  
  /**
   * Takes a series of bytes and return a bitcoin base58 encoded string
   * @param bytes
   * Array of bytes to include
   * @return
   * Base58 encoded string
   */
  
  public static String encode(byte[] bytes){
    byte[] extra_zero=new byte[bytes.length+1];
    String result="";
    extra_zero[0]=0;
    
    System.arraycopy(bytes,0,extra_zero,1,bytes.length);
    BigInteger bn=new BigInteger(extra_zero);
    while(bn.compareTo(BigInteger.ZERO)==1){
      BigInteger div[]=bn.divideAndRemainder(FIFTY_EIGHT);
      bn=div[0];
      char c=BASE58CHARS.charAt(div[1].intValue());
      result=result+c;
    }
    
    // pad leading zeros
    result=new String(new StringBuffer(result).reverse());
    for(int i=0;bytes[i]==0;i++){
      result=BASE58CHARS.charAt(0)+result;
    }
    return result;
  }
  
  /**
   * Decode a bitcoin encoded base58 string
   * @param encoded
   * Base58 encoded string
   * @return
   * The decoded byte array
   * @throws ParseException
   */
  
  public static byte[] decode(String encoded) throws ParseException{
    BigInteger bn=BigInteger.ZERO;
    BigInteger mult;
    String reversed=new String(new StringBuffer(encoded).reverse());
    byte[] raw;
    for(int pos=0;pos<reversed.length();pos++){
      int val=BASE58CHARS.indexOf(reversed.charAt(pos));
      
      if(val==-1){
        throw new ParseException(reversed, pos);
      }
      mult=FIFTY_EIGHT.pow(pos);
      bn=bn.add(mult.multiply(BigInteger.valueOf(val)));
    }
    
    raw=bn.toByteArray();
    if(raw[0]==0){
      byte[] raw2=new byte[raw.length-1];
      System.arraycopy(raw, 1, raw2, 0, raw.length-1);
      raw=raw2;
    }
    int leadingZeroes=0;
    for(int pos=0;((encoded.charAt(pos)=='1') && pos<encoded.length());pos++){
      leadingZeroes++;
    }
    byte[] fin=new byte[raw.length+leadingZeroes];
    System.arraycopy(raw, 0, fin, leadingZeroes, raw.length);
    return fin;
  }
  
  /**
   * Decodes a base58 encoded string with a checksum
   * @param encoded
   * The base58 encoded string with checksum
   * @return
   * The decoded byte array
   * @throws ParseException
   */
  
  public static byte[] decodeCheck(String encoded) throws ParseException{
    byte[] raw=decode(encoded);
    if(raw.length<4){
      throw new ParseException("Decoded bytes too small for checksum",0);
    }
    byte[] decoded=new byte[raw.length-4];
    System.arraycopy(raw,0,decoded,0,raw.length-4);
    byte[] hash=HashTools.doubleSha256(decoded);
    for(int i=0;i<4;i++){
      if(hash[i]!=raw[decoded.length+i]){
        throw new ParseException("Incorrect checksum",decoded.length);
      }
    }
    return decoded;
  }
  
  /**
   * Encodes a byte array into a base58 string with a checksum
   * @param bytes
   * The byte array to encode
   * @return
   * The base58 encoded string with a checksum
   */
  
  public static String encodeCheck(byte[] bytes){
    byte[] hash=HashTools.doubleSha256(bytes);
    byte[] toHash=new byte[bytes.length+4];
    System.arraycopy(bytes, 0, toHash, 0, bytes.length);
    System.arraycopy(hash, 0, toHash, bytes.length, 4);
    return encode(toHash);
  }

}




Java Source Code List

net.dirtyfilthy.bitcoin.core.Address.java
net.dirtyfilthy.bitcoin.core.Base58Hash160.java
net.dirtyfilthy.bitcoin.core.BlockChain.java
net.dirtyfilthy.bitcoin.core.BlockExistsException.java
net.dirtyfilthy.bitcoin.core.BlockStore.java
net.dirtyfilthy.bitcoin.core.Block.java
net.dirtyfilthy.bitcoin.core.BtcValue.java
net.dirtyfilthy.bitcoin.core.ByteArrayable.java
net.dirtyfilthy.bitcoin.core.InvalidBlockException.java
net.dirtyfilthy.bitcoin.core.OpCode.java
net.dirtyfilthy.bitcoin.core.OpData.java
net.dirtyfilthy.bitcoin.core.OrphanBlockException.java
net.dirtyfilthy.bitcoin.core.Script.java
net.dirtyfilthy.bitcoin.core.TxIn.java
net.dirtyfilthy.bitcoin.core.TxOut.java
net.dirtyfilthy.bitcoin.core.Tx.java
net.dirtyfilthy.bitcoin.protocol.AddressBook.java
net.dirtyfilthy.bitcoin.protocol.AddressPacket.java
net.dirtyfilthy.bitcoin.protocol.BlockPacket.java
net.dirtyfilthy.bitcoin.protocol.ConnectionHandler.java
net.dirtyfilthy.bitcoin.protocol.Connection.java
net.dirtyfilthy.bitcoin.protocol.GetAddressPacket.java
net.dirtyfilthy.bitcoin.protocol.GetBlocksPacket.java
net.dirtyfilthy.bitcoin.protocol.GetDataPacket.java
net.dirtyfilthy.bitcoin.protocol.GetHeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.HeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryVector.java
net.dirtyfilthy.bitcoin.protocol.IrcBootStrap.java
net.dirtyfilthy.bitcoin.protocol.MalformedPacketException.java
net.dirtyfilthy.bitcoin.protocol.PacketFactory.java
net.dirtyfilthy.bitcoin.protocol.PacketType.java
net.dirtyfilthy.bitcoin.protocol.Packet.java
net.dirtyfilthy.bitcoin.protocol.PingPacket.java
net.dirtyfilthy.bitcoin.protocol.ProtocolVersion.java
net.dirtyfilthy.bitcoin.protocol.ReplyPacket.java
net.dirtyfilthy.bitcoin.protocol.TxPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionAckPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionPacket.java
net.dirtyfilthy.bitcoin.util.Base58.java
net.dirtyfilthy.bitcoin.util.BigIntegerTools.java
net.dirtyfilthy.bitcoin.util.HashTools.java
net.dirtyfilthy.bitcoin.util.KeyTools.java
net.dirtyfilthy.bitcoin.util.MyHex.java
net.dirtyfilthy.bitcoin.wallet.ExposedSQLiteCursor.java
net.dirtyfilthy.bitcoin.wallet.InvalidPasswordException.java
net.dirtyfilthy.bitcoin.wallet.KeyRing.java
net.dirtyfilthy.bitcoin.wallet.SqlBlockStore.java
net.dirtyfilthy.bitcoin.wallet.Wallet.java