[Webfunds-commits] java/webfunds/client/sox SOXWallet.java

Ian Grigg iang@cypherpunks.ai
Fri, 13 Apr 2001 13:42:50 -0400 (AST)


iang        01/04/13 13:42:50

  Modified:    webfunds/client/sox SOXWallet.java
  Log:
  1.  Uses Log all through.
  2.  Uses CommsManager all through.
  3.  uses new SSD Store from webfunds.sox.value;
  4.  deprecated automatic addition of contracts
  5.  protects better against bad AccountInfo being not an AccountId...
  6.  ...and handles faulty payment error returned from Issuer.

Revision  Changes    Path
1.153     +228 -121  java/webfunds/client/sox/SOXWallet.java

Index: SOXWallet.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/sox/SOXWallet.java,v
retrieving revision 1.152
retrieving revision 1.153
diff -u -r1.152 -r1.153
--- SOXWallet.java	2001/04/12 19:02:49	1.152
+++ SOXWallet.java	2001/04/13 17:42:49	1.153
@@ -1,4 +1,4 @@
-/* $Id: SOXWallet.java,v 1.152 2001/04/12 19:02:49 iang Exp $
+/* $Id: SOXWallet.java,v 1.153 2001/04/13 17:42:49 iang Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All Rights Reserved.
@@ -18,24 +18,31 @@
 // SOX toolkit
 import webfunds.ricardian.Contract;
 import webfunds.ricardian.ContractStore;
-import webfunds.ricardian.SOXServerException;
+// import webfunds.ricardian.SOXServerException;
 
+
+import webfunds.util.Hex;
+import webfunds.util.Panic;
+
 import webfunds.utils.Debug;
-import webfunds.utils.Hex;
-import webfunds.utils.Panic;
+
+import webfunds.comms.CommsManager;
 
 import webfunds.store.Store;
 import webfunds.store.StoreException;
 import webfunds.store.SepFileStore;
 
+import webfunds.sox.value.SSDStore;
+
 import webfunds.sox.Account;
 import webfunds.sox.AccountId;
 import webfunds.sox.ArmouredPayment;
 import webfunds.sox.Crypto;
-import webfunds.sox.IssuerFinder;
+import webfunds.sox.Errors;
 import webfunds.sox.ItemId;
 import webfunds.sox.MailId;
 import webfunds.sox.MailItem;
+import webfunds.sox.ServerFinder;
 
 import webfunds.sox.AbstractPayment;
 import webfunds.sox.PaymentFactory;
@@ -80,7 +87,7 @@
  *
  *  Check the above search strings for separation so far.
  *
- * @version $Revision: 1.152 $
+ * @version $Revision: 1.153 $
  */
 public class SOXWallet
     extends Debug
@@ -90,7 +97,7 @@
     protected   WalletContext context;
 
 
-    // these should probably be protected
+    // these should be protected but are used by sox/gui/PaymentFrame.java
     public void error(String e)
     {
         logmsg("[ " + e + " ]");
@@ -113,8 +120,9 @@
         return context.getUInterface(this).getYesNo(q) == UInterface.YES ;
     }
 
+    protected   CommsManager            comms;
     protected   AccountStore            accountStore;
-    protected   SOXServerStore          soxes;
+    protected   SSDStore                soxes;
     protected   ReceiptsStore           receiptStore;
     protected   Store                   store;
     protected   ContractStore           contracts = null;
@@ -123,7 +131,8 @@
 
     protected static final String name = "SOX";
     protected String shortname         = name;
-    protected static final String fix  = "  SW: ";
+    protected static final String TAB  = "  ",
+                                  fix  = "SW: ";
 
     protected static final String
         try_again_later = "Try again later - cannot get through.",
@@ -140,11 +149,11 @@
 /////////  Initialisation  //////////////////////////////
 
     // bug gets set directly by WalletManager
-    public SOXWallet()                          { super(); debug(fix); }
+    public SOXWallet()                          { super(); debug(TAB + fix); }
 
     // these are therefore no use...
-    public SOXWallet(PrintWriter bug)           { super(); debug(bug, fix); }
-    public SOXWallet(PrintWriter bug, String f) { super(); debug(bug, f); }
+    // public SOXWallet(PrintWriter bug)           { super(); debug(bug, fix); }
+    // public SOXWallet(PrintWriter bug, String f) { super(); debug(bug, f); }
 
 
     /**
@@ -163,6 +172,8 @@
             throw new InternalError("PANIC: context already set!");
 
         this.context = aContext;
+        contracts = context.getContractStore(this);
+        comms = context.getCommsManager(this);
 
         if (Crypto.sr == null)
             Crypto.setSecureRandom(context.getSecureRandom());
@@ -170,8 +181,6 @@
         try
         {
             setStore(context.getStore(this));
-            contracts = context.getContractStore(this);
-            soxes.setContractStore(contracts);
         }
         catch (StoreException ex)
         {
@@ -180,6 +189,8 @@
             throw new InternalError(e);
         }
 
+        // soxes.setContractStore(contracts);
+
         properties = context.getProperties(this);
 
         if (isTestMode())
@@ -294,26 +305,26 @@
      *  This is the only access we have to persistant storage.
      *  Unfortunately, it's quite limiting.
      */
-    public void setStore(Store store)
+    protected void setStore(Store store)
         throws StoreException
     {
         this.store   = store;
 
         //
-        //  The SOX server file Store.
+        //  The SSD Store.
         //
-        Store soxstore = null;
+        Store ssdstore = null;
         try {
-            soxstore = store.getStore("SOXServers", SepFileStore.APPEND);
+            ssdstore = store.getStore("SOXServers", SepFileStore.APPEND);
         } catch (StoreException ex) {  // should delete and start again
             // ex.printStackTrace(err());
             logmsg("Failed to get SOXServers store: " + ex);
             // System.exit(1);
             throw ex ;
         }
-        soxes = new SOXServerStore(soxstore, getDebug());
-        IssuerFinder finder = null;
-        finder = (IssuerFinder)soxes;
+        soxes = new SSDStore(ssdstore, contracts, comms, getLog());
+        ServerFinder finder = null;
+        finder = (ServerFinder)soxes;
 
         //
         //  Account store.
@@ -355,10 +366,6 @@
             // System.exit(1);
         }
 
-        //
-        //  An old store is Issuers.  We should remove that some time.
-        //
-
     }
 
 
@@ -1439,7 +1446,6 @@
             if (pt.isOpen())
             {
                 logmsg("pt.isOpen() says 'true'");
-
                 // Ok, so it's open, and we can deposit it anywhere.
                 ac = getAccount(callerAc);
 
@@ -1532,47 +1538,115 @@
         ValueAccount sub = (ValueAccount) ac.getSub(item);
         if (sub == null)
         {
-            makeNewSub = true;              // only if our account...
-            sub = new ValueAccount(item);
-            try {
-                ac.newSub(sub);
-            } catch (SOXSubAccountException ex) {
-                if (ex.isNoContract())
-                {
-                    error(
-                      "It is not possible to deposit this payment because\n"+
-                      "the SOX Issuer server cannot be identified or found\n"+
-                      "(normally pointed to by the Ricardian Contract)!\n"+
-                      "\n"+
-                      "In order to deposit this payment, you must find the\n"+
-                      "the contract that has identifier:\n"+
-                      "\n"+
-                      "               " + item + "\n"+
-                      "\n"+
-                      "and add it to the Contract Store using the Plugins /\n"+
-                      "Contract Browser button.\n"+
-                      "");
-                      return ;
-                }
-                ex.printStackTrace();
-                error("cannot create SubAccount: " + ex);
-                return ;
+            Contract contract = this.contracts.getContract(item);
+            if (contract != null)
+            {
+                error(
+                  "This contract has not been added to the account.\n"+
+                  "\n"+
+                  "In order to deposit this payment, you must first add\n"+
+                  "the contract that has name and identifier:\n"+
+                  "\n"+
+                  "               " + contract.getName() + "   " + item + "\n"+
+                  "\n"+
+                  "and then deposit the payment.\n");
+                return;
+            }
+
+            error(
+              "There is no Ricardian Contract available for this payment.\n"+
+              "\n"+
+              "In order to deposit this payment, you must first add\n"+
+              "the Ricardian Contract that has identifier:\n"+
+              "\n"+
+              "               " + item + "\n"+
+              "\n"+
+              "to the Contract Store within the Plugins.\n"+
+              "\n"+
+              "Then, add that contract to the account, and deposit\n"+
+              "the payment.\n");
 
-            } catch (SOXAccountException ex) {
-                ex.printStackTrace();
-                error("Account error: " + ex);
-                return ;
-            }
+            return;
         }
 
+//            makeNewSub = true;              // only if our account...
+//            sub = new ValueAccount(item);
+//            try {
+//                ac.newSub(sub);
+//            } catch (SOXSubAccountException ex) {
+//                if (ex.isNoContract())
+//                {
+//                    error(
+//                      "It is not possible to deposit this payment because\n"+
+//                      "the SOX Issuer server cannot be identified or found\n"+
+//                      "(normally pointed to by the Ricardian Contract)!\n"+
+//                      "\n"+
+//                      "In order to deposit this payment, you must find the\n"+
+//                      "the contract that has identifier:\n"+
+//                      "\n"+
+//                      "               " + item + "\n"+
+//                      "\n"+
+//                      "and add it to the Contract Store using the Plugins /\n"+
+//                      "Contract Browser button.\n"+
+//                      "");
+//                      return ;
+//                }
+//                ex.printStackTrace();
+//                error("cannot create SubAccount: " + ex);
+//                return ;
+//
+//            } catch (SOXAccountException ex) {
+//                ex.printStackTrace();
+//                error("Account error: " + ex);
+//                return ;
+//            }
+//        }
+
         MailItem[] mails;
         try {
             mails = sub.deposit(pay, new String(desc), null);
         } catch (SOXLaterException ex) {
             error(try_again_later);
             return ;
-        } catch (SOXSubAccountException e) {
-            error("deposit failed (SubAccount broken?) :\n    " + e);
+        } catch (SOXSubAccountException ex) {
+            int err = ex.getNumber();
+            String error = ex.getMessage();
+
+            /*
+             *  Anything that is to do with the payment is not
+             *  our problem, caller supplied junk, and should
+             *  have checked more carefully.
+             *  For example,  a junk target is probably a coding
+             *  in the original payment dialog.
+             *  What it means here is that there is no need to
+             *  print a stacktrace at this level, but caller
+             *  should treat this payment with suspicion.
+             */
+            if (
+                  (err == Errors.ERROR_NOT_A_PAYMENT)                     ||
+                  (err == Errors.ERROR_PAYMENT_NOT_SIGNED)                ||
+                  (err == Errors.ERROR_NOT_AN_ITEM)                       ||
+                  (err == Errors.ERROR_NOT_A_QUANTITY)                    ||
+                  ((err==Errors.ERROR_NOT_TARGET_KHID) && !pay.isOpen())  ||
+                  (err == Errors.ERROR_ILLEGAL_PID)                       ||
+                  (err == Errors.ERROR_NOT_PROTO)                         ||
+                  (err == Errors.ERROR_NOT_TOKEN)                         ||
+                  false
+               )
+            {
+                error(
+                  "The Issuer has rejected this payment as faulty:\n"+
+                  "\n"+
+                  "\n"+
+                  "               (" + err + ") " + error + "\n"+
+                  "\n"+
+                  "As the payment is faulty, you may wish to contact the\n"+
+                  "source of the payment or your software vendor.");
+                return;
+            }
+
+            ex.printStackTrace();
+            error("deposit failed (SubAccount broken?) :\n    " + ex);
             return ;
         } catch (SOXException ex) {
             ex.printStackTrace();
@@ -1609,38 +1683,41 @@
         }
 
         String statString = "";
-        //
-        // Some admin
-        //       - make a new sub by re-adding the account if needed.
-        //       - get a name for a new source account.
-        //
-        if (makeNewSub)
-        {
-            //
-            // The getSub() did all the account work,
-            // now just need to store it.
-            //
-            try {
-                accountStore.addAccount(ac);
-            } catch (StoreException ex) {
-                ex.printStackTrace(System.err);
-                logmsg("account " + ac + " sub " + sub);
-                logmsg("cannot add subaccount? " + ex);
-                System.exit(1);
-            }
-            aviso("Hmmm.  That worked, and created a new subaccount...\n" +
-                  "which you can't see :(  What you need is a workaround:" +
-                  "\n\nTo see the contract, hit [Add Contract] on this:" +
-                  "\n\n" + item + "\n\n" +
-                  "(as there is no way to tell the GUI what to do here!)");
-            statString += "\n    (subAccount made in " + ac + ")";
-        }
+//        //
+//        // Some admin
+//        //       - make a new sub by re-adding the account if needed.
+//        //
+//        if (makeNewSub)
+//        {
+//            //
+//            // The getSub() did all the account work,
+//            // now just need to store it.
+//            //
+//            try {
+//                accountStore.addAccount(ac);
+//            } catch (StoreException ex) {
+//                ex.printStackTrace(System.err);
+//                logmsg("account " + ac + " sub " + sub);
+//                logmsg("cannot add subaccount? " + ex);
+//                System.exit(1);
+//            }
+//            aviso("Hmmm.  That worked, and created a new subaccount...\n" +
+//                  "which you can't see :(  What you need is a workaround:" +
+//                  "\n\nTo see the contract, hit [Add Contract] on this:" +
+//                  "\n\n" + item + "\n\n" +
+//                  "(as there is no way to tell the GUI what to do here!)");
+//            statString += "\n    (subAccount made in " + ac + ")";
+//        }
 
 
         AccountInfo source;
         source = context.getAddressbook(this).getInfo(pss);
         String name = source.getName();
 
+        //
+        // Some admin
+        //       - get a name for a new source account.
+        //
         if (name == null)
         {
             String q = "You have received a good payment from an account:\n" +
@@ -1685,7 +1762,7 @@
      */
     public Receipt doDeposit(AbstractPayment pay, AccountId account, 
                              byte[] desc, String idempotentId)
-        throws DepositException
+        throws DepositException, PaymentException
     {
         if (isClosed())
             throw new DepositException(WalletException.CLOSED, closeReason());
@@ -1724,31 +1801,61 @@
         ValueAccount sub = (ValueAccount) ac.getSub(item);
         if (sub == null)
         {
-            makeNewSub = true;
-            logmsg("no sub " + item.fp() + " in " + ac.fp() + ", making...");
-            sub = new ValueAccount(item);
-            try {
-                ac.newSub(sub);
-            } catch (SOXSubAccountException ex) {
-                ex.printStackTrace();
-                throw new Panic("cannot create subAccount: " + ex);
-            } catch (SOXAccountException ex) {
-                ex.printStackTrace();
-                throw new Panic("Account error: " + ex);
-            }
+            throw new DepositException(DepositException.UNKNOWN_SUB,
+                                       "must add contract before depositing");
+
+//            makeNewSub = true;
+//            logmsg("no sub " + item.fp() + " in " + ac.fp() + ", making...");
+//            sub = new ValueAccount(item);
+//            try {
+//                ac.newSub(sub);
+//            } catch (SOXSubAccountException ex) {
+//                ex.printStackTrace();
+//                throw new Panic("cannot create subAccount: " + ex);
+//            } catch (SOXAccountException ex) {
+//                ex.printStackTrace();
+//                throw new Panic("Account error: " + ex);
+//            }
         }
 
         MailItem[] mails;
         try {
             mails = sub.deposit(pay, new String(desc), idempotentId);
 
-        } catch (SOXDepositException ex) {      // webfunds.sox.Errors
+        } catch (SOXDepositException ex) {      // routine payment errors
             throw new DepositException(ex.getNumber(), ex.getMessage());
         } catch (SOXLaterException ex) {
             throw new DepositException(ex.getNumber(), ex.getMessage());
-        } catch (SOXSubAccountException ex) {   // negative
+
+        } catch (SOXSubAccountException ex) {   // abnormalities
+            int err = ex.getNumber();
+            String error = ex.getMessage();
+
+            /*
+             *  Anything that is to do with the payment is not
+             *  our problem, caller supplied junk, and should
+             *  have checked more carefully.
+             *  For example,  a junk target is probably a
+             *  laxness in the original payment dialog.
+             *  What it means here is that there is no need to
+             *  print a stacktrace at this level, but caller
+             *  should treat this payment with suspicion.
+             */
+            if (
+                  (err == Errors.ERROR_NOT_A_PAYMENT)                     ||
+                  (err == Errors.ERROR_PAYMENT_NOT_SIGNED)                ||
+                  (err == Errors.ERROR_NOT_AN_ITEM)                       ||
+                  (err == Errors.ERROR_NOT_A_QUANTITY)                    ||
+                  ((err==Errors.ERROR_NOT_TARGET_KHID) && !pay.isOpen())  ||
+                  (err == Errors.ERROR_ILLEGAL_PID)                       ||
+                  (err == Errors.ERROR_NOT_PROTO)                         ||
+                  (err == Errors.ERROR_NOT_TOKEN)                         ||
+                  false
+               )
+                  throw new PaymentException(err, ex.getMessage());
+
             ex.printStackTrace();
-            throw new DepositException(ex.getNumber(), ex.getMessage());
+            throw new DepositException(err, error);
 
         } catch (SOXArgsException ex) {
             ex.printStackTrace();
@@ -1778,27 +1885,27 @@
             /// hmmm this is really a SubAccount failure, should never happen
             throw new DepositException(SOXSubAccountException.NO_RECEIPT);
 
-        //
-        //  Some admin
-        //       - make a new sub by re-adding the account if needed.
-        //       - get a name for a new source account.
-        //
-        if (makeNewSub)
-        {
-            //
-            //  The getSub() did all the account work,
-            //  now just need to (re)store it.
-            //  As this is a low level call, caller already checked
-            //  whether the deposit was appropriate.
-            //
-            try {
-                accountStore.addAccount(ac);
-            } catch (StoreException ex) {
-                ex.printStackTrace(System.err);
-                throw new Panic("cannot add subaccount " + sub +
-                                " to account " + ac + "\n" + ex);
-            }
-        }
+//        //
+//        //  Some admin
+//        //       - make a new sub by re-adding the account if needed.
+//        //       - get a name for a new source account.
+//        //
+//        if (makeNewSub)
+//        {
+//            //
+//            //  The getSub() did all the account work,
+//            //  now just need to (re)store it.
+//            //  As this is a low level call, caller already checked
+//            //  whether the deposit was appropriate.
+//            //
+//            try {
+//                accountStore.addAccount(ac);
+//            } catch (StoreException ex) {
+//                ex.printStackTrace(System.err);
+//                throw new Panic("cannot add subaccount " + sub +
+//                                " to account " + ac + "\n" + ex);
+//            }
+//        }
 
 
         internalUpdate(sub, mails);