Package com.bitsofproof.supernode.api

Examples of com.bitsofproof.supernode.api.Transaction


  {
    if ( fee < 0 || fee > MAXIMUM_FEE )
    {
      throw new ValidationException ("You unlikely want to do that");
    }
    Transaction transaction = new Transaction ();
    transaction.setInputs (new ArrayList<TransactionInput> ());
    transaction.setOutputs (new ArrayList<TransactionOutput> ());

    long sumOut = 0;
    for ( TransactionSink s : sinks )
    {
      TransactionOutput o = new TransactionOutput ();
      o.setValue (s.getValue ());
      sumOut += s.getValue ();
      o.setScript (s.getAddress ().getAddressScript ());

      transaction.getOutputs ().add (o);
    }

    long sumInput = 0;
    for ( TransactionOutput o : sources )
    {
      TransactionInput i = new TransactionInput ();
      i.setSourceHash (o.getTxHash ());
      i.setIx (o.getIx ());
      sumInput += o.getValue ();

      transaction.getInputs ().add (i);
    }
    if ( sumInput != (sumOut + fee) )
    {
      throw new ValidationException ("Sum of sinks (+fee) does not match sum of sources");
    }

    int j = 0;
    for ( TransactionOutput s : sources )
    {
      TransactionInput i = transaction.getInputs ().get (j);
      ScriptFormat.Writer sw = new ScriptFormat.Writer ();
      if ( ScriptFormat.isPayToAddress (s.getScript ()) )
      {
        Address address = s.getOutputAddress ();
        Key key = getKeyForAddress (address);
        if ( key == null )
        {
          throw new ValidationException ("Have no key to spend this output");
        }
        byte[] sig = key.sign (hashTransaction (transaction, j, ScriptFormat.SIGHASH_ALL, s.getScript ()));
        byte[] sigPlusType = new byte[sig.length + 1];
        System.arraycopy (sig, 0, sigPlusType, 0, sig.length);
        sigPlusType[sigPlusType.length - 1] = (byte) (ScriptFormat.SIGHASH_ALL & 0xff);
        sw.writeData (sigPlusType);
        sw.writeData (key.getPublic ());
      }
      else
      {
        spendNonAddressOutput (j, s, sw, transaction);
      }
      i.setScript (sw.toByteArray ());
      ++j;
    }

    transaction.computeHash ();
    return transaction;
  }
View Full Code Here


    throw new ValidationException ("Can not spend this output type");
  }

  public static byte[] hashTransaction (Transaction transaction, int inr, int hashType, byte[] script) throws ValidationException
  {
    Transaction copy = null;
    try
    {
      copy = transaction.clone ();
    }
    catch ( CloneNotSupportedException e1 )
    {
      return null;
    }

    // implicit SIGHASH_ALL
    int i = 0;
    for ( TransactionInput in : copy.getInputs () )
    {
      if ( i == inr )
      {
        in.setScript (script);
      }
      else
      {
        in.setScript (new byte[0]);
      }
      ++i;
    }

    if ( (hashType & 0x1f) == ScriptFormat.SIGHASH_NONE )
    {
      copy.getOutputs ().clear ();
      i = 0;
      for ( TransactionInput in : copy.getInputs () )
      {
        if ( i != inr )
        {
          in.setSequence (0);
        }
        ++i;
      }
    }
    else if ( (hashType & 0x1f) == ScriptFormat.SIGHASH_SINGLE )
    {
      int onr = inr;
      if ( onr >= copy.getOutputs ().size () )
      {
        // this is a Satoshi client bug.
        // This case should throw an error but it instead retuns 1 that is not checked and interpreted as below
        return ByteUtils.fromHex ("0100000000000000000000000000000000000000000000000000000000000000");
      }
      for ( i = copy.getOutputs ().size () - 1; i > onr; --i )
      {
        copy.getOutputs ().remove (i);
      }
      for ( i = 0; i < onr; ++i )
      {
        copy.getOutputs ().get (i).setScript (new byte[0]);
        copy.getOutputs ().get (i).setValue (-1L);
      }
      i = 0;
      for ( TransactionInput in : copy.getInputs () )
      {
        if ( i != inr )
        {
          in.setSequence (0);
        }
        ++i;
      }
    }
    if ( (hashType & ScriptFormat.SIGHASH_ANYONECANPAY) != 0 )
    {
      List<TransactionInput> oneIn = new ArrayList<TransactionInput> ();
      oneIn.add (copy.getInputs ().get (inr));
      copy.setInputs (oneIn);
    }

    WireFormat.Writer writer = new WireFormat.Writer ();
    copy.toWire (writer);

    byte[] txwire = writer.toByteArray ();
    byte[] hash = null;
    try
    {
View Full Code Here

  @Override
  public Transaction pay (List<Address> receiver, List<Long> amounts, boolean senderPaysFee) throws ValidationException
  {
    long fee = MINIMUM_FEE;
    long estimate = 0;
    Transaction t = null;

    do
    {
      fee = Math.max (fee, estimate);
      t = pay (receiver, amounts, fee, senderPaysFee);
View Full Code Here

TOP

Related Classes of com.bitsofproof.supernode.api.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.