[Webfunds-commits] java/webfunds/sox DepositRequest.java

Ian Grigg iang@cypherpunks.ai
Thu, 30 Nov 2000 09:57:31 -0400 (AST)


iang        00/11/30 09:57:31

  Modified:    webfunds/sox DepositRequest.java
  Log:
  1. added the proto payment for withdrawal use, this will break the
  existing DR's out there for withdrawal use as there are now two
  variants with the same number.  But this is unused anyway.
  2. comments
  3. finished and got going the self-test (why uncomplete, dammit!?!)

Revision  Changes    Path
1.27      +227 -68   java/webfunds/sox/DepositRequest.java

Index: DepositRequest.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/DepositRequest.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- DepositRequest.java	2000/11/09 13:13:50	1.26
+++ DepositRequest.java	2000/11/30 13:57:30	1.27
@@ -1,5 +1,5 @@
 /*
- * $Id: DepositRequest.java,v 1.26 2000/11/09 13:13:50 iang Exp $
+ * $Id: DepositRequest.java,v 1.27 2000/11/30 13:57:30 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -15,7 +15,7 @@
  * and a description string, and then signed.
  */
 public class DepositRequest
-    extends Request // implements Serializable
+    extends Request
 {
     /**
      * The version of the encoded deposit request.
@@ -34,7 +34,7 @@
                             DEPOSIT_WITHDRAW     = 3,   // can exchange types
                             DEPOSIT_not_used_yet = 4;   // use me next...
 
-    protected int version = DEPOSIT_LOCAL_VERS;  // change this if an old packet
+    protected int version;
 
     public int getVersion()         { return version; }
 
@@ -42,65 +42,78 @@
     public              String getName() { return name ; }
 
     /**
-     * The payment to be deposited
-     * (as originally created by the payer)
-     * (was Payment).
+     *  The payment to be deposited
+     *  (as originally created by the payer)
+     *  (was Payment).
      */
     protected AbstractPayment payment;
     protected byte[]          payBuf;
 
     /**
-     * An identifier for this deposit request
+     *  The payment to be withdrawn, paid for with above payment
+     *  (as originally created by the payer, added at DEPOSIT_WITHDRAW-bis).
+     */
+    protected AbstractPayment proto;
+    protected byte[]          protoBuf;
+
+    /**
+     *  An identifier for this deposit request
      *
-     * Note: although a java String is used, this identifier
-     * should only contain 8-bit ascii characters, following
-     * the same rules as that of the payment id (pid).
+     *  Note: although a java String is used, this identifier
+     *  should only contain 8-bit ascii characters, following
+     *  the same rules as that of the payment id (pid).
      */
     protected String depositId;
 
     /**
-     * A description (can be anything, although very long lengths
-     * may be refused by some issuers and clients) of what the
-     * payment is for.  This field is provided by the payee when
-     * the payment is deposited.  One interesting use of this
-     * field is for providing the payee with a key that "unlocks"
-     * a protected software package.
+     *  A description (can be anything, although very long lengths
+     *  may be refused by some issuers and clients) of what the
+     *  payment is for.  This field is provided by the payee when
+     *  the payment is deposited.  One interesting use of this
+     *  field is for providing the payee with a key that "unlocks"
+     *  a protected software package.
      */
     protected byte[] depositDesc;
 
 
     /**
-     * Get the payment to be deposited
+     *  Get the payment to be deposited.
      */
     public Payment         getPayment()  { return (Payment)payment; } // old
     public AbstractPayment getPay()      { return payment; }          // v >= 3
 
     /**
-     * Get the identifier on the payment, as originally created by the payer
+     *  Get the proto payment to be signed, if a withdrawal.
+     */
+    public AbstractPayment getProto()    { return proto; }            // v >= 3
+
+    /**
+     *  Get the identifier on the payment, as originally created by the payer.
      *
-     * Note: although a java String is used, this identifier
-     * should only contain 8-bit ascii characters.
+     *  Note: although a java String is used, this identifier
+     *  should only contain 8-bit ascii characters.
      */
     public String getPaymentId() { return payment.getId(); }
 
     /**
-     * Get the account from which the transaction was drawn
+     *  Get the account from which the transaction was drawn.
      */
     public AccountId getTarget() { return payment.getTarget(); }
 
     /**
-     * Get the account to which the payment is made
+     *  Get the account to which the payment is made.
      */
     public AccountId getSource() { return payment.getSource(); }
 
     /**
-     * Get the type of item (or "currency") on the payment
-     * (e.g. airmiles)
+     *  Get the type of item (or "currency") on the payment
+     *  (this is [almost certainly] the message digest of a
+     *  ricardian contract).
      */
     public ItemId getItem() { return payment.getItem(); }
 
     /**
-     * Get the quantity of the item on the payment
+     *  Get the quantity of the item on the payment
      */
     public long getQty() { return payment.getQty(); }
 
@@ -115,20 +128,20 @@
      */
 
     /**
-     * Get the description on the payment
+     *  Get the description on the payment
      */
     public byte[] getPaymentDesc() { return payment.getDesc(); }
 
     /**
-     * Get the identifier on this deposit request
+     *  Get the identifier on this deposit request
      *
-     * Note: although a java String is used, this identifier
-     * should only contain 8-bit ascii characters.
+     *  Note: although a java String is used, this identifier
+     *  should only contain 8-bit ascii characters.
      */
     public String getDepositId() { return depositId; }
 
     /**
-     * Get the description for this deposit request
+     *  Get the description for this deposit request
      */
     public byte[] getDepositDesc() { return depositDesc; }
 
@@ -140,7 +153,7 @@
      *
      *  Added for version DEPOSIT_WITHDRAW.
      */
-    protected int typeOfPayment = PaymentFactory.NONE;
+    protected int typeOfPayment;                    // unigned byte
     public int getTypeOfPayment()           { return typeOfPayment; }
     public void setTypeOfPayment(int type)  { typeOfPayment = type; }
        
@@ -156,14 +169,14 @@
      *
      *  Added for version DEPOSIT_WITHDRAW.
      */
-    protected int protocolState = 0;
+    protected int protocolState;                   // unigned byte
     public int getProtocolState()           { return protocolState; }
     public void setProtocolState(int state) { protocolState = state; }
 
 
 
     /**
-     * Get a Reply with Errors set.
+     *  Get a Reply with Errors set.
      */
     public Reply errorReply(int e)
                            { return new DepositReply(this, e); }
@@ -173,21 +186,62 @@
 
 
     /**
-     * Create an unsigned deposit request
-     * This constructor is the one usually called by SOX clients.
+     *  Create an unsigned deposit request
+     *  This constructor is the one usually called by SOX clients.
      *
-     * @param requestId the request identifier (must be unique)
-     * @param acc the despositor's account
-     * @param payment the payment to be deposited
-     * @param depositId the deposit request identifier (must be unique)
-     * @param desc depositor's description of what this transaction is for
+     *  @param requestId the request identifier (must be unique)
+     *  @param acc the despositor's account
+     *  @param payment the payment to be deposited
+     *  @param depositId the deposit request identifier (must be unique)
+     *  @param desc depositor's description of what this transaction is for
      */
     public DepositRequest(String requestId, AccountId acc,
                           AbstractPayment payment,
                           String depositId, byte[] desc)
     {
         super(requestId, acc);
+
+        this.version = DEPOSIT_LOCAL_VERS;
+
+        this.payment = payment;
+        this.depositId = depositId;
+        depositDesc = desc;
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            payment.encode(baos);
+        } catch (IOException ex) {
+            throw new IllegalArgumentException("DR pay: " + ex);
+        }
+        this.payBuf = baos.toByteArray();
+
+        this.protoBuf = new byte[0];
+        this.proto = null;
+
+        protocolState = 0;
+        typeOfPayment = PaymentFactory.NONE;
+    }
+
+    /**
+     *  Create an unsigned withdrawal request, to get a token payment signed.
+     *
+     *  @param requestId the request identifier (must be unique)
+     *  @param acc the despositor's account
+     *  @param payment the payment to be deposited, pays for proto
+     *  @param proto the payment to be withdrawn, paid for by payment
+     *  @param depositId the deposit request identifier (must be unique)
+     *  @param desc depositor's description of what this transaction is for
+     */
+    public DepositRequest(String requestId, AccountId acc,
+                          AbstractPayment payment, AbstractPayment proto,
+                          String depositId, byte[] desc)
+    {
+        super(requestId, acc);
+
+        this.version = DEPOSIT_WITHDRAW;
+
         this.payment = payment;
+        this.proto = proto;
         this.depositId = depositId;
         depositDesc = desc;
 
@@ -198,18 +252,29 @@
             throw new IllegalArgumentException("DR pay: " + ex);
         }
         this.payBuf = baos.toByteArray();
+
+        baos = new ByteArrayOutputStream();
+        try {
+            proto.encode(baos);
+        } catch (IOException ex) {
+            throw new IllegalArgumentException("DR proto: " + ex);
+        }
+        this.protoBuf = baos.toByteArray();
+
+        protocolState = 0;
+        typeOfPayment = proto.getType();
     }
 
     /**
-     * Construct a deposit request object from a byte array
-     * that was previously returned from the encode()
-     * method of a deposit request object.
+     *  Construct a deposit request object from a byte array
+     *  that was previously returned from the encode()
+     *  method of a deposit request object.
      *
-     * If the signature is not present in the encoded data,
-     * the created request will be unsigned.
+     *  If the signature is not present in the encoded data,
+     *  the created request will be unsigned.
      *
-     * @param depositReqData the previously encoded request
-     * @excep SOXPacketException The input data was badly formatted
+     *  @param depositReqData the previously encoded request
+     *  @excep SOXPacketException The input data was badly formatted
      */
     public DepositRequest(byte[] depositReqData)
         throws SOXPacketException
@@ -218,15 +283,15 @@
     }
 
     /**
-     * Construct a deposit request object from data on an input stream
-     * that was previously returned from the encode()
-     * method of a deposit request object.
+     *  Construct a deposit request object from data on an input stream
+     *  that was previously returned from the encode()
+     *  method of a deposit request object.
      *
-     * If the signature is not present in the encoded data,
-     * the created request will be unsigned.
+     *  If the signature is not present in the encoded data,
+     *  the created request will be unsigned.
      *
-     * @excep SOXPacketException The input data was badly formatted
-     * @excep IOException         The input stream returned EOF?
+     *  @excep SOXPacketException The input data was badly formatted
+     *  @excep IOException         The input stream returned EOF?
      */
     public DepositRequest(InputStream is)
         throws SOXPacketException
@@ -235,13 +300,13 @@
     }
 
     /**
-     * Encode a deposit request to an output stream,
-     * suitable for using with RequestPacket to send
-     * to remote servers (which use the decode() method
-     * to re-construct the object).
+     *  Encode a deposit request to an output stream,
+     *  suitable for using with RequestPacket to send
+     *  to remote servers (which use the decode() method
+     *  to re-construct the object).
      *
-     * @param os the stream on which to send the output
-     * @excep an I/O error occurred writing to the output stream
+     *  @param os the stream on which to send the output
+     *  @excep an I/O error occurred writing to the output stream
      */
     public void encode(OutputStream os)
         throws IOException
@@ -255,9 +320,13 @@
  
         if (version >= DEPOSIT_WITHDRAW)  // New!  I can jump technologies...
         {
-            dos.writeByte(typeOfPayment);
-            dos.writeByte(protocolState);
+            dos.writeByte(typeOfPayment & 0x7F);
+            dos.writeByte(protocolState & 0x7F);
             writeByteArray(dos, payBuf);
+            if (typeOfPayment != PaymentFactory.NONE)
+            {
+                writeByteArray(dos, protoBuf);
+            }
         }
         else
             payment.encode(dos);
@@ -287,10 +356,11 @@
          *  deposit, which will need to be recovered, as long as
          *  the receipt is to remain usable!
          */
-        int v = 1;
+        // int v = 1;
+        version = DEPOSIT_ORIGINAL;
         if (req_version >= REQ_SUBS_ADD_V)    // parent, adds my version num
         {
-            v = dis.readUnsignedByte();
+            int v = dis.readUnsignedByte();
             if (! ((DEPOSIT_LOCAL_VERS <= v) && (v <= DEPOSIT_WITHDRAW)) )
                 throw new SOXPacketException("Invalid version in DepositR:" +
                     v + " != {"+DEPOSIT_LOCAL_VERS+","+DEPOSIT_WITHDRAW+"}");
@@ -303,10 +373,36 @@
             protocolState = dis.readUnsignedByte();
             payBuf = readByteArray(dis);
             payment = PaymentFactory.decode(payBuf);
+            if (typeOfPayment != PaymentFactory.NONE)
+            {
+                // added for DEPOSIT_WITHDRAW-bis, Wf 1.7.5 will fail here
+                protoBuf = readByteArray(dis);
+                proto = PaymentFactory.decode(protoBuf);
+            }
+            else
+            {
+                protoBuf = new byte[0];
+                proto = null;
+            }
         }
         else
+        {
             payment = new Payment(dis);
 
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            try {
+                payment.encode(baos);
+            } catch (IOException ex) {
+                throw new IllegalArgumentException("DR pay: " + ex);
+            }
+            this.payBuf = baos.toByteArray();
+
+            this.protoBuf = new byte[0];
+            this.proto = null;
+            typeOfPayment = PaymentFactory.NONE;
+            protocolState = 0;
+        }
+
         depositId = readString(dis);
         depositDesc = readByteArray(dis);
     }
@@ -323,12 +419,62 @@
 
 ////// Self-Test //////////////////////////////////
 
+    public boolean equals(Object object)
+    {  
+        if ( (object == null) || !(object instanceof DepositRequest) )
+            return false;
+
+        if (!super.equals((Request) object))
+            return false ;
+
+        DepositRequest other = (DepositRequest)object;
+
+//System.err.println("1: " + version + " " + other.version);
+        if (version != other.version)
+            return false;
+       
+//System.err.println("2");
+        String otherId;  
+        otherId = other.getDepositId();
+        if (depositId == null)
+        {
+            if (otherId != null)
+                return false ;
+        }
+        else
+        {
+            if (!depositId.equals(otherId))
+                return false;
+        }
+
+//System.err.println("3");
+        if (!Utils.byteEquals(depositDesc, other.depositDesc))
+            return false;
+
+//System.err.println("4");
+        if (!Utils.byteEquals(payBuf, other.payBuf))
+            return false;
+//System.err.println("5");
+        if (!Utils.byteEquals(protoBuf, other.protoBuf))
+            return false;
+
+//System.err.println("6");
+        if (typeOfPayment != other.typeOfPayment)
+            return false;
+       
+//System.err.println("7");
+        if (protocolState != other.protocolState)
+            return false;
+
+        return true ;
+    }       
+
     /**
      * Convert this object to a human readable string
      */
     public String toString()
     {
-        String s = "SOX DepositRequest " + vString() + ":";
+        String s = "SOX DepositRequest " + vString() + ": ";
         s += super.toString();
 
         s += "\tPayment: "+payment+"\n";
@@ -342,7 +488,12 @@
         {
             s += "\ttype: " + PaymentFactory.getTypeString(typeOfPayment) ;
             s += "    state: " + protocolState;
+            if (typeOfPayment != PaymentFactory.NONE)
+            {
+                s += "\n\tProto: "+proto+"\n";
+            }
         }
+
         return s;
     }
 
@@ -359,10 +510,18 @@
         String depId = Utils.exampleString();
         byte[] desc = Utils.exampleData();
 
-        DepositRequest req = new DepositRequest(reqId, ac, pay, depId, desc);
+        DepositRequest req;
+        byte bite = Utils.exampleByte();
 
-        req.setTypeOfPayment(Utils.exampleByte());
-        req.setProtocolState(Utils.exampleByte());
+        if ((bite & 0x01) == 0x01)
+            req = new DepositRequest(reqId, ac, pay, depId, desc);
+        else
+        {
+            TokenPayment proto = TokenPayment.example();
+            req = new DepositRequest(reqId, ac, pay, proto, depId, desc);
+            req.setTypeOfPayment(proto.getType());
+            req.setProtocolState(Utils.exampleByte() & 0x7F);
+        }
 
         return req ;
     }