Android Open Source - bitcoin-wallet Address






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.core;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
//from w w  w.  j  a va2  s  . c  om
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

public class Address implements ByteArrayable {
  private long services;
  private InetAddress ip;
  private int port;
  private ByteBuffer dataBuffer;
  private int LENGTH=26;
  private Date lastSeen=new Date(100000000000L);
  private Date lastTry=new Date(0);
  private Date lastConnected=new Date(0);
  public Address(){
    dataBuffer=ByteBuffer.allocate(LENGTH);
    dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
    setServices(1);
  }
  
  public String toString(){
    return ip.toString()+":"+port;
  }
  
  
  public Address(InetAddress ip, int port){
    dataBuffer=ByteBuffer.allocate(LENGTH);
    dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
    setServices(1);
    setIp(ip);
    setPort(port);
  }
  
  public Address(String ip, int port) throws UnknownHostException{
    setServices(1);
    setIp(InetAddress.getByName(ip));
    setPort(port);
  }
  
  public Address(DataInputStream in, boolean timestamp) throws IOException {
    if(timestamp){
      lastSeen=new Date(((long) Integer.reverseBytes(in.readInt()) & 0xffffffffL)*1000);
    }
    services=Long.reverseBytes(in.readLong());
    byte[] ip=new byte[4];
    in.skip(12); // reserved
    in.read(ip);
    setIp(InetAddress.getByAddress(ip));
    port=(int) Short.reverseBytes(in.readShort()) & 0xff;
    
  }

  public void setServices(long services) {
    this.services = services;
  }

  public long getServices() {
    return services;
  }

  public void setIp(InetAddress ip) {
    this.ip = ip;
  }

  public InetAddress getIp() {
    return ip;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public int getPort() {
    return port;
  }
  
  
  public byte[] toByteArray(boolean timestamp){
    byte[] reserved={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    byte[] addr;
    if(timestamp){
      dataBuffer=ByteBuffer.allocate(LENGTH+4);
      dataBuffer.putInt((int) (lastSeen.getTime() * .001));
    }
    else{
      dataBuffer=ByteBuffer.allocate(LENGTH);
    }
    dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
    dataBuffer.rewind();
    dataBuffer.putLong(services);
    dataBuffer.put(reserved);
    dataBuffer.put(ip.getAddress());
    dataBuffer.putShort((short) port);
    addr=dataBuffer.array();
    return addr;
  }

  public void setLastSeen(Date lastSeen) {
    this.lastSeen = lastSeen;
  }

  public Date getLastSeen() {
    return lastSeen;
  }
  
  public int hashCode() {
    return ip.hashCode() ^ port;
  }
  
  public boolean equals(Address a){
    if(a==null){
      return false;
    }
    return this.getIp().equals(a.getIp()) && this.getPort()==a.getPort();
  }

  public void setLastTry(Date lastTry) {
    this.lastTry = lastTry;
  }

  public Date getLastTry() {
    return lastTry;
  }

  public void setLastConnected(Date lastConnected) {
    this.lastConnected = lastConnected;
  }

  public Date getLastConnected() {
    return lastConnected;
  }

  public byte[] toByteArray() {
    return toByteArray(false);
  }
  
  
}




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