Android Open Source - bitcoin-wallet Tx






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;
//from   w w w  .j  av  a 2  s .  c o  m
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Vector;

import net.dirtyfilthy.bitcoin.protocol.Packet;
import net.dirtyfilthy.bitcoin.util.HashTools;


public class Tx  implements ByteArrayable {
  private long transactionVersion;
  private long lockTime;
  private Vector<TxIn> txInputs=new Vector<TxIn>();
  private Vector<TxOut> txOutputs=new Vector<TxOut>();
  public static final int SIGHASH_ALL = 1;
  public static final int SIGHASH_NONE = 2;
  public static final int SIGHASH_SINGLE = 3;
  public static final int SIGHASH_ANYONECANPAY = 0x80;
  
  public Tx(){
    this.transactionVersion=1;
    this.lockTime=0;
  }
  
  public Tx(Tx t){
    this.lockTime=t.lockTime;
    this.transactionVersion=t.transactionVersion;
    int index=0;
    for(TxIn in : t.txInputs){
      this.txInputs.add(in.clone());
    }
    for(TxOut out : t.txOutputs){
      this.txOutputs.add(out.clone());
    }
    
  }
  
  public String toString(){
    String s="transactionVersion "+this.transactionVersion+"\n";
    s+="lockTime "+this.lockTime+"\n";
    s+="INPUTS\n";
    for(TxIn in : txInputs ){
      s+=in.toString();
      s+="\n";
    }
    s+="OUTPUTS\n";
    for(TxOut out : txOutputs){
      s+=out.toString();
      s+="\n";
    }
    return s;
    
  }
  
  public static boolean verifySignature(Tx from, Tx to, int index, int hashType){
    if(index>=to.getTxInputs().size()){
      System.out.println("Input index greater than length");
      return false;
    }
    TxIn input=to.getTxInputs().get(index);
    if (input.getOutpointIndex() >= from.getTxOutputs().size()){
      System.out.println("Output index greater than length");
      return false;
    }
    TxOut output=from.getTxOutputs().get((int) input.getOutpointIndex());
    if(!Script.verifyScript(input.getScript(), output.getScript(), to, index, hashType)){
      System.out.println("Verify script failed");
      return false;
    }
    return true;
  }
  
  public Tx(DataInputStream in) throws IOException {
    int items;
    this.transactionVersion=((long) Integer.reverseBytes(in.readInt())) & 0xffffffff;
    items=(int) Packet.readUnsignedVarInt(in);
    for(int i=0;i<items;i++){
      this.txInputs.add(new TxIn(in));
    }
    items=(int) Packet.readUnsignedVarInt(in);
    for(int i=0;i<items;i++){
      this.txOutputs.add(new TxOut(in));
    }
    this.lockTime=((long) Integer.reverseBytes(in.readInt())) & 0xffffffff;
  }
  
  public byte[] toByteArray() {
    ByteBuffer dataBuffer=ByteBuffer.allocate(500000);
    dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
    dataBuffer.putInt((int) this.transactionVersion);
    dataBuffer.put(Packet.createUnsignedVarInt(this.txInputs.size()));
    for(TxIn in : txInputs){
      dataBuffer.put(in.toByteArray());
    }
    dataBuffer.put(Packet.createUnsignedVarInt(this.txOutputs.size()));
    for(TxOut out : txOutputs){
      dataBuffer.put(out.toByteArray());
    }
    dataBuffer.putInt((int) this.lockTime);
    byte[] dataContents=new byte[dataBuffer.position()];
    ByteBuffer slicedBuffer=(ByteBuffer) dataBuffer.duplicate();
    slicedBuffer.rewind();
    slicedBuffer.limit(dataBuffer.position());
    slicedBuffer.get(dataContents);
    return dataContents;
  }
  
  public void setTransactionVersion(long transactionVersion) {
    this.transactionVersion = transactionVersion;
  }

  public long getTransactionVersion() {
    return transactionVersion;
  }

  public void setLockTime(long lockTime) {
    this.lockTime = lockTime;
  }

  public long getLockTime() {
    return lockTime;
  }

  public void setTxInputs(Vector<TxIn> txInputs) {
    this.txInputs = txInputs;
  }

  public Vector<TxIn> getTxInputs() {
    return txInputs;
  }

  public void setTxOutputs(Vector<TxOut> txOutputs) {
    this.txOutputs = txOutputs;
  }

  public Vector<TxOut> getTxOutputs() {
    return txOutputs;
  }
  
  public byte[] hash(){
    return HashTools.doubleSha256(this.toByteArray());
  }

}




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