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

Ian Grigg iang@cypherpunks.ai
Fri, 6 Apr 2001 19:57:06 -0400 (AST)


iang        01/04/06 19:57:06

  Modified:    webfunds/sox TokenPayment.java
  Log:
  lots more comments, proper isSigned method, better diags & examples.

Revision  Changes    Path
1.5       +68 -31    java/webfunds/sox/TokenPayment.java

Index: TokenPayment.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/TokenPayment.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TokenPayment.java	2001/03/23 15:06:04	1.4
+++ TokenPayment.java	2001/04/06 23:57:05	1.5
@@ -1,5 +1,5 @@
 /*
- * $Id: TokenPayment.java,v 1.4 2001/03/23 15:06:04 iang Exp $
+ * $Id: TokenPayment.java,v 1.5 2001/04/06 23:57:05 iang Exp $
  *
  * Copyright (c) Systemics Inc 1995-2000 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -19,8 +19,8 @@
 /**
  *  This class represents a token payment.
  *  A token payment consists of tokens, sometimes
- *  called coins, which at this level are simply
- *  big unguessable numbers.
+ *  called coins, which at this level can be thought
+ *  of as big unguessable numbers.
  *
  *  These are created by collecting the tokens out
  *  of a database, or by acquiring them from a mint-
@@ -30,7 +30,7 @@
  *  this one, in order to have different functionality.
  *  For example, it may be easier to derive from this
  *  class for the various blinded methods than to have
- *  another class.
+ *  another class.  Or maybe not.
  */
 public class TokenPayment
     extends AbstractPayment
@@ -43,10 +43,10 @@
     public int getType()                  { return TOKEN_TYPE; }
 
     /**
-     *  The version number for this payment structure
+     *  The sub version number for this payment structure
      *  (*NOT* the super packet type number).
      */
-    public static final int TOKEN_VERSION = 0;
+    public static final int TOKEN_SUBVERSION = 0;
 
     /**
      *  The account from which the payment was drawn.
@@ -82,15 +82,17 @@
     /**
      *  Create a token payment from an array of tokens.
      *
-     *  An empty array can be passed to simulate a zero payment,
-     *  in the time-honoured SOX tradition.  We may want to support
-     *  Zero coins, dunno.
+     *  An empty or null array is not supported, it generates an error
+     *  from the Issuer.
+     *  In the time-honoured SOX tradition, a zero payment can be made
+     *  with a zero-valued token.
      *
-     *  The items is indicated so that a pointer to a valid SOXServer
-     *  can be determined at the receiving end.
+     *  The item is indicated so that a pointer to a valid SOXServer
+     *  can be determined at the receiving end.  There can only be
+     *  one item in each TP.
      *
-     *  Validity times are ignored here, although real token systems
-     *  do have times involved.
+     *  Validity times are ignored ofr now, although token systems
+     *  do have times involved to slice the coin float into batches.
      *
      *  @param source the source account, can be empty / null
      *  @param item the item for which the payment is for
@@ -107,7 +109,7 @@
                 byte[] desc
                 )
     {
-        super(PF_VERSION, TOKEN_TYPE, TOKEN_VERSION, item, 0, desc);
+        super(PF_VERSION, TOKEN_TYPE, TOKEN_SUBVERSION, item, 0, desc);
 
         if (item == null)
             throw new Panic("item is null, must supply valid item");
@@ -149,6 +151,29 @@
     }
 
     /**
+     *  Check to see if this payment is signed, always false on a token
+     *  payment?  This may not be the case, the tokens themselves may
+     *  may be signed.
+     *
+     *  @return boolean false (no signature on a token payment)
+     */
+    public boolean isSigned()
+    {
+        for (int i = 0; i < tokens.length; i++)
+        {
+            Token tok = tokens[i];
+            if (tok == null)
+                throw new Panic("null token in array at position " + i);
+            if (!tok.isSigned())
+            {
+                System.err.println("Token " + i + " Unsigned!: " + tok);
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      *  Construct a payment object from a byte array that was previously
      *  returned from the encode() method of a payment object.
      *
@@ -174,15 +199,6 @@
         super(is);
     }
 
-    /**
-     *  Check to see if this payment is signed, always false on a token
-     *  payment?  This may not be the case, the tokens themselves may
-     *  may be signed.
-     *
-     *  @return boolean false (no signature on a token payment)
-     */
-    public boolean isSigned()          { return false; }
-
 
 
     /**
@@ -197,9 +213,16 @@
     {
         DataInputStream dis = new DataInputStream(is);
 
+        /*
+         *  Three leading bytes are:
+         *
+         *        PaymentFactory version,
+         *        Payment Type in PaymentFactory,
+         *        Payment subversion here.
+         */
         int v = dis.readUnsignedByte();
         if (v != PF_VERSION)
-            throw new SOXPacketException("Invalid super version in TP: " +
+            throw new SOXPacketException("Invalid TP super factory version :" +
                                          v + " != " + PF_VERSION);
         version = v;
 
@@ -218,9 +241,12 @@
                                          v + " != " + getSubVersion());
         subversion = v;
 
+// System.err.println("ok, read: " + version + ", " + type + ", " + subversion);
+
         source = new AccountId(dis);     // may be empty
         qty = dis.readLong();
         item = new ItemId(dis);
+// System.err.println("item read: " + item);
         desc = readByteArray(dis);
 
         int number = dis.readUnsignedByte();
@@ -228,6 +254,9 @@
         if (number < 0)
             throw new SOXPacketException("Negative number of tokens");
 
+        /*
+         *  This has to change, we need a Token Factory!
+         */
         tokens = (Token[]) new RandomToken[number];
         for (int i = 0; i < number; i++)
              tokens[i] = new RandomToken(dis);
@@ -240,8 +269,6 @@
     /**
      * Encode a payment as a byte array, suitable for
      * sending to third parties for depositing.
-     * If the signature is not present, an unsigned
-     * payment will be encoded.
      *
      * @return byte[] the payment in encoded form
      */
@@ -270,12 +297,14 @@
 
     public String toString()
     {
-        String s = "TokenPayment " + vString() + "  " + super.toString();
+        String s = "TokenPayment " + vString() +
+                   "  " + tokens.length + " tokens";
 
-        s += "  " + tokens.length + " tokens";
+        s += "\n\t\t" + super.toString();
 
         if (!source.isOpen())
             s += "\n   from: " + source.fp();
+        s += "\n\t\t . . . . . . . . . . . end TP\n";
 
         return s;
     }
@@ -316,7 +345,7 @@
         return true;
     }
 
-    public static TokenPayment example()
+    public static TokenPayment example(Token[] tokens)
     {
         int b = Utils.exampleByte();
         byte[] desc = Utils.exampleData();
@@ -329,14 +358,22 @@
             ac = null;
 
         ItemId item = ItemId.example();
+
+        TokenPayment obj;
+        obj = new TokenPayment(pid, ac, item, tokens, desc);
 
-        int i = b & 0x0F;      // up to 16 tokens
+        return obj;
+    }
+
+    public static TokenPayment example()
+    {
+        int i = Utils.exampleByte() & 0x0F;      // up to 16 tokens
         Token[] tokens = new Token[i];
         while (i-- > 0)
             tokens[i] = RandomToken.example();
 
         TokenPayment obj;
-        obj = new TokenPayment(pid, ac, item, tokens, desc);
+        obj = example(tokens);
 
         return obj;
     }