Android Open Source - bitcoin-wallet Version Packet






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.protocol;
/*w w w.  ja v  a 2 s . c  om*/
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import net.dirtyfilthy.bitcoin.core.Address;

public class VersionPacket extends Packet {
  
  private Date timeStamp;
  private String subversion;
  private long services;
  private long nonce;
  private long startingHeight;
  private Address remoteAddress;
  private Address fromAddress;
  
  public VersionPacket(long ver) {
    super(ver,"version");
    subversion="";
    services=1;
  }
  
  public VersionPacket() {
    super();
    subversion="";
    setCommand("version");
    services=1;
  }
  
  // no checksum on version
  
  //public byte[] checksum(){
  //  byte[] checksum={0,0,0,0};
  //  return checksum;
  //}
  
  public byte[] create(){
    dataBuffer.putInt((int) getVersion());
    dataBuffer.putLong(services);
    dataBuffer.putLong(getTimeStamp().getTime() / 1000);
    dataBuffer.put(remoteAddress.toByteArray(false));
    if(version>=106){
      dataBuffer.put(fromAddress.toByteArray(false));
      dataBuffer.putLong(nonce);
      writeVariableStringField(subversion);
    }
    if(version>=209){
      dataBuffer.putInt((int) startingHeight); 
    }
    return toByteArray();
  }

  public void setRemoteAddress(Address remoteAddress) {
    this.remoteAddress = remoteAddress;
  }

  public Address getRemoteAddress() {
    return remoteAddress;
  }

  public void setSubversion(String string) {
    this.subversion = string;
  }

  public String getSubversion() {
    return subversion;
  }

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

  public long getServices() {
    return services;
  }

  public void setTimeStamp(Date timeStamp) {
    this.timeStamp = timeStamp;
    
  }

  public void setTimeStamp(long timeStamp) {
    this.timeStamp = new Date(timeStamp);
    
  }

  
  public Date getTimeStamp() {
    if(timeStamp==null){
      timeStamp=Calendar.getInstance().getTime();
    }
    return timeStamp;
  }
  
  protected long readUnsignedInt(DataInputStream in) throws IOException{
    long ret=(long) Integer.reverseBytes(in.readInt()) & 0xffffffffL;
    return ret;
  }
  
  protected void readData(DataInputStream in) throws IOException {
    this.setVersion((long) Integer.reverseBytes(in.readInt()) & 0xffffffffL);
    System.out.println("version "+getVersion());
    this.setServices(Long.reverseBytes(in.readLong()));
    System.out.println("services "+getServices());
    this.setTimeStamp(Long.reverseBytes(in.readLong())*1000);
    this.setRemoteAddress(new Address(in,false));
    if(version>=106){
      this.setFromAddress(new Address(in,false));
      this.setNonce(Long.reverseBytes(in.readLong()));
      this.setSubversion(readVariableString(in));
    }
    if(version>=209){
      this.setStartingHeight(readUnsignedInt(in));
    }
  
  }

  public void setNonce(long nonce) {
    this.nonce = nonce;
  }

  public long getNonce() {
    return nonce;
  }

  public void setFromAddress(Address fromAddress) {
    this.fromAddress = fromAddress;
  }

  public Address getFromAddress() {
    return fromAddress;
  }

  public void setStartingHeight(long startingHeight) {
    this.startingHeight = startingHeight;
  }

  public long getStartingHeight() {
    return startingHeight;
  }
  
}




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