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

Ian Grigg iang@cypherpunks.ai
Sun, 18 Mar 2001 22:00:02 -0400 (AST)


iang        01/03/18 22:00:01

  Modified:    webfunds/sox PaymentFactory.java
  Log:
  Fixed up conflicts, I had already done a PaymentFactory.example() !

Revision  Changes    Path
1.10      +64 -46    java/webfunds/sox/PaymentFactory.java

Index: PaymentFactory.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/PaymentFactory.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- PaymentFactory.java	2001/03/18 23:18:30	1.9
+++ PaymentFactory.java	2001/03/19 02:00:01	1.10
@@ -1,4 +1,4 @@
-/* $Id: PaymentFactory.java,v 1.9 2001/03/18 23:18:30 iang Exp $
+/* $Id: PaymentFactory.java,v 1.10 2001/03/19 02:00:01 iang Exp $
  *
  * Copyright (c) 1995-2001 Systemics Inc. on behalf of
  * The WebFunds Development Team. All Rights Reserved.
@@ -18,20 +18,25 @@
     public static final int        NONE = 0,
                                    SOX_CHEQUE = 1,
                                    RANDOM_TOKEN = 2,
-                                   WAGNER_TOKEN = 3;
+                                   WAGNER_TOKEN = 3,
+                                   CHAUM_TOKEN = 4;
 
+    public static boolean isTokenPayment(int i) { return (2<=i) && (i<=4); }
+
     public static final String[] typeNames = {
                                    "None?",
                                    "SOX Cheque",
                                    "Random Token", 
-                                   "Wagner Token"
+                                   "Wagner Token",
+                                   "Chaum Token"
                                   };
 
     public static final String[] base64Type = {
                                    null,
                                    null,
                                    "Random", 
-                                   "Wagner"
+                                   "Wagner",
+                                   "Chaum"
                                   };
 
     private PaymentFactory() {}
@@ -44,53 +49,33 @@
         if (buf == null)
             throw new IllegalArgumentException("PaymentFactory.decode(null)");
 
-        int pv = buf[0];            // factory version, old Pay == 0
-        int sv = buf[1];            // within factory, sub version
+        int fv = buf[0];            // factory version 1, or old Payment 0
+        int typ = buf[1];           // within factory, type of AbstractPayment
+        int sv = buf[2];            // within AbstractPayment, subversion
+        String s = "fv=" + fv + ", f-type=" + typ + ", sv=" + sv;
+System.err.println("analysing payment: " + s);
 
-//        try
-//        {
-        if (pv == AbstractPayment.SINGLETON_VERSION)
+        /*
+         *  First, check if it is the old pre-factory form of payment.
+         *  The first byte was zero for those, so the Factory version
+         *  starts at 1.
+         */
+        if (fv == AbstractPayment.SINGLETON_VERSION)
             return new Payment(buf);
-        else if (pv != AbstractPayment.PF_VERSION)
-            throw new SOXPacketException("Not an AbstractPayment: " + pv + ", " + sv);
+        else if (fv != AbstractPayment.PF_VERSION)
+            throw new SOXPacketException("Not AbstractPayment: " + s);
 
-        if (sv == SOX_CHEQUE)
-            return new Payment(buf);
-        else if (sv == RANDOM_TOKEN)
-            return new TokenPayment(buf);
         /*
-        else if (sv == WAGNER_TOKEN)
+         *  It's a Factory version.
+         *  The second byte determines which one to recover.
+         *  There are really only two, SOX cheques and Token containers.
+         */
+        if (typ == SOX_CHEQUE)
+            return new Payment(buf);
+        else if (isTokenPayment(typ))
             return new TokenPayment(buf);
-        */
-
         else
-            throw new SOXPacketException("Not a known FactoryPayment: "+pv+", "+sv);
-
-
-//        }
-//        catch (Exception e)
-//        {
-//            e.printStackTrace();
-//            throw new SOXException("Payment Decode Error");
-//        }
-    }
-
-    public static AbstractPayment example()
-    {
-        
-        int sv = Utils.exampleByte() & 0x7F;
-        sv = 1 + (sv % 2);          // 1,2
-
-        if (sv == SOX_CHEQUE)
-            return Payment.example();
-        else if (sv == RANDOM_TOKEN)
-            return TokenPayment.example();
-        /*
-        else if (sv == WAGNER_TOKEN)
-            return TokenPayment.example();
-        */
-
-        throw new RuntimeException("Example did not catch " + sv);
+            throw new SOXPacketException("Unknown FactoryPayment: " + s);
     }
 
     /**
@@ -103,7 +88,7 @@
     {
         if (type <= 0)
             return false;
-        if (type >= 4)
+        if (type >= 5)
             return false;
         return true ;
     }
@@ -263,6 +248,10 @@
         return coins;
     }
 
+
+
+////// Test Code /////////////////////////////////////
+
     public static void main(String[] args)
     {
         if (log2qty(0) != 0)
@@ -310,5 +299,34 @@
         }
     }
 
+    public static AbstractPayment example()
+    {
+        
+        int sv = Utils.exampleByte() & 0x7F;
+        sv = 1 + (sv % 2);          // 1,2
+
+        return example(sv);
+    }
+
+    public static AbstractPayment example(int type)
+    {
+        AbstractPayment p;
+        if (type == SOX_CHEQUE)
+            p = Payment.example();
+
+        else if (type == RANDOM_TOKEN)
+            p = TokenPayment.example();
+        /*
+        else if (type == WAGNER_TOKEN)
+            p = TokenPayment(buf);
+        else if (type == CHAUM_TOKEN)
+            p = TokenPayment.example();
+        */
+        else
+            throw new RuntimeException("unknown AbstractPayment: " + type);
+
+        return p;
+
+    }
 
 }