Android Open Source - My-Wallet-Android Bitcoin Address






From Project

Back to project page My-Wallet-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project My-Wallet-Android 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

/*
 * Copyright 2011-2012 the original author or authors.
 */*from  w  w  w .  j  ava2 s .co m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package piuk;

import java.io.Serializable;

import com.google.bitcoin.core.AddressFormatException;
import com.google.bitcoin.core.Base58;
import com.google.bitcoin.core.Utils;

import piuk.Hash;

public class BitcoinAddress implements Serializable {
  private static final long serialVersionUID = 1L;
  private final Hash hash160;
  private final short version;
  private transient String _toStringCache;

  public BitcoinAddress(String humanReadable) throws AddressFormatException {    
    byte[] tmp = Base58.decodeChecked(humanReadable);

    version = (short) (tmp[0] & 0xFF);

    byte[] bytes = new byte[tmp.length - 1];

    System.arraycopy(tmp, 1, bytes, 0, tmp.length - 1);

    this.hash160  = new Hash(bytes);
  }

  @Override
  public String toString() {
    if (_toStringCache == null) {
      int length = hash160.getBytes().length;

      byte[] addressBytes = new byte[1 + length + 4];

      addressBytes[0] = (byte) version;

      System.arraycopy(hash160.getBytes(), 0, addressBytes, 1, length);
      byte[] check = Utils.doubleDigest(addressBytes, 0, length + 1);
      System.arraycopy(check, 0, addressBytes, length + 1, 4);
      
      _toStringCache = Base58.encode(addressBytes);
    }
    
    return _toStringCache;
  }

  //from pubKey
  public BitcoinAddress(byte[] pubKey) {    
    this.hash160  = new Hash(Utils.sha256hash160(pubKey));
    this.version = 0;
  }

  public short getVersion() {
    return version;
  }

  public short getDBType() {
    if (version == 5)
      return -2;
    else
      return 0;
  }

  public BitcoinAddress(Hash hash, short version) throws AddressFormatException {
    this.hash160 = hash;
    this.version = version;
  }

  public Hash getHash160() {
    return hash160;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((hash160 == null) ? 0 : hash160.hashCode());
    result = prime * result + version;
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    BitcoinAddress other = (BitcoinAddress) obj;
    if (hash160 == null) {
      if (other.hash160 != null)
        return false;
    } else if (!hash160.equals(other.hash160))
      return false;
    if (version != other.version)
      return false;
    return true;
  }
}




Java Source Code List

piuk.BitcoinAddress.java
piuk.BitcoinScript.java
piuk.Hash.java
piuk.MyBlockChain.java
piuk.MyRemoteWallet.java
piuk.MyTransactionConfidence.java
piuk.MyTransactionInput.java
piuk.MyTransactionOutPoint.java
piuk.MyTransactionOutput.java
piuk.MyTransaction.java
piuk.MyWallet.java
piuk.blockchain.android.AddressBookProvider.java
piuk.blockchain.android.BlockchainService.java
piuk.blockchain.android.Constants.java
piuk.blockchain.android.DetermineFirstSeenThread.java
piuk.blockchain.android.ExchangeRatesProvider.java
piuk.blockchain.android.WalletApplication.java
piuk.blockchain.android.WalletBalanceWidgetProvider.java
piuk.blockchain.android.ui.AbstractWalletActivity.java
piuk.blockchain.android.ui.AmountCalculatorFragment.java
piuk.blockchain.android.ui.CurrencyAmountView.java
piuk.blockchain.android.ui.CurrencyCodeDrawable.java
piuk.blockchain.android.ui.EditAddressBookEntryFragment.java
piuk.blockchain.android.ui.ExchangeRatesFragment.java
piuk.blockchain.android.ui.NewAccountFragment.java
piuk.blockchain.android.ui.PairWalletActivity.java
piuk.blockchain.android.ui.PreferencesActivity.java
piuk.blockchain.android.ui.RequestCoinsActivity.java
piuk.blockchain.android.ui.RequestCoinsFragment.java
piuk.blockchain.android.ui.SecondPasswordFragment.java
piuk.blockchain.android.ui.SendCoinsActivity.java
piuk.blockchain.android.ui.SendCoinsFragment.java
piuk.blockchain.android.ui.SendingAddressesFragment.java
piuk.blockchain.android.ui.TransactionActivity.java
piuk.blockchain.android.ui.TransactionFragment.java
piuk.blockchain.android.ui.WalletActivity.java
piuk.blockchain.android.ui.WalletAddressesFragment.java
piuk.blockchain.android.ui.WalletBalanceFragment.java
piuk.blockchain.android.ui.WalletTransactionsFragment.java
piuk.blockchain.android.ui.WelcomeFragment.java
piuk.blockchain.android.util.ActionBarFragment.java
piuk.blockchain.android.util.Base43.java
piuk.blockchain.android.util.CircularProgressView.java
piuk.blockchain.android.util.ErrorReporter.java
piuk.blockchain.android.util.IOUtils.java
piuk.blockchain.android.util.Iso8601Format.java
piuk.blockchain.android.util.NfcTools.java
piuk.blockchain.android.util.QrDialog.java
piuk.blockchain.android.util.ViewPagerTabs.java
piuk.blockchain.android.util.WalletUtils.java