[Webfunds-commits] java/webfunds/sox SubAccount.java ValueAccount.java

Ian Grigg iang@cypherpunks.ai
Fri, 13 Oct 2000 21:50:45 -0400 (AST)


iang        00/10/13 21:50:45

  Modified:    webfunds/sox SubAccount.java ValueAccount.java
  Log:
  fixed up Reqeust signing to use Account.sign();
  comments;
  added nym();

Revision  Changes    Path
1.29      +142 -10   java/webfunds/sox/SubAccount.java

Index: SubAccount.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/SubAccount.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- SubAccount.java	2000/09/30 18:20:13	1.28
+++ SubAccount.java	2000/10/14 01:50:44	1.29
@@ -1,4 +1,4 @@
-/* $Id: SubAccount.java,v 1.28 2000/09/30 18:20:13 iang Exp $
+/* $Id: SubAccount.java,v 1.29 2000/10/14 01:50:44 iang Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All Rights Reserved.
@@ -139,10 +139,22 @@
         return issuer ;
     }
 
+    /**
+     * @return an issuer for use with this subaccount.
+     * @except SOXSubAccountException if the account cannot find an issuer.
+     */
+    public void   setIssuer(Issuer issuer)
+    {
+        this.issuer = issuer;
+    }
+
     private long lastDiff = 0;
     private long stdDeviation = 24 * 60 * 60 * 1000;
 
 
+
+////// Requests  //////////////////////////////////
+
     /**
      * always works, might be ultra conservative
      * deprecated?  not really used, SOX shouldn't know about Dates.
@@ -231,7 +243,7 @@
 
         req = new RegisterRequest(num, acct.getId(), pub);
 
-        req.sign(acct.getKey());
+        acct.sign(req);
         if (true) /* sanity_checking_ON */
         {
             if (!req.verify(pub))
@@ -309,14 +321,15 @@
         //  and receives new mail in one request cycle.
         //
         MailRequest req = new MailRequest(""+reqNo++, acct.getId(), mails);
-        byte[] sig;
-        try {
-            sig = Crypto.sign(acct.getKey(), req.encode());
-        } catch (java.security.KeyException kex) {
-            kex.printStackTrace(System.err);
-            throw new SOXKeyException("KeyException: " + kex);
-        }
-        req.setSignature(sig);
+        acct.sign(req);
+//        byte[] sig;
+//        try {
+//            sig = Crypto.sign(acct.getKey(), req.encode());
+//        } catch (java.security.KeyException kex) {
+//            kex.printStackTrace(System.err);
+//            throw new SOXKeyException("KeyException: " + kex);
+//        }
+//        req.setSignature(sig);
 
         byte[] replyBuf = requestExtra(req);
 
@@ -349,7 +362,125 @@
 
 
 
+////// Nym - experimental
+
     /**
+     *  Send some details to turn the registered key into a Nym.
+     *  I'm not sure where this belongs as yet...
+     *
+     *  @throws SOXArgsException if item in payment is invalid
+     *  @throws SOXLaterException if there is no ability to talk to the server
+     *  @throws SOXSubAccountException if something goes wrong with access
+     *  @throws SOXKeyException if something goes wrong with signing
+     */
+    public String[] nym(long flags, String[] names)
+          throws SOXSubAccountException, SOXLaterException, SOXKeyException,
+                 SOXArgsException, SOXRequestException
+    {
+        checkFrozen("nym");
+        checkNet();
+
+        String num = "" + reqNo++;
+        NymRequest req = new NymRequest(num, acct.getId());
+        req.setFlags(flags);
+        req.setNames(names);
+
+        acct.sign(req);
+        NymReply rep = (NymReply) doRequest(req, false);
+
+        return rep.getNames();
+    }
+
+
+
+    /**
+     *  Convert the request into a reply.
+     *  This catches a number of local snafus that are reported
+     *  by the SOX Server, corrects them, and retries.
+     *  As there are a number of SOX Server used by this account
+     *  it is hard to keep track of who has registered where
+     *  (bearing in mind that there are few issuers, and many contracts).
+     *  So, we do it by feedback for the moment.
+     *
+     *  @param retry true if this call is already recursive
+     */
+    protected Reply doRequest(Request req, boolean retry)
+        throws SOXSubAccountException, SOXRequestException, SOXLaterException,
+               SOXKeyException
+    {
+          
+        Reply reply = doRequest(req);
+     
+        int err = reply.getErrorNumber();
+        String txt = reply.getErrorText();
+        if (txt == null || txt.trim() == "")
+            txt = Errors.errorString(err);
+
+        status |= ACTIVE;
+
+        if (err == Errors.ERROR_GOOD)
+            return reply ;
+        else if (err == Errors.ERROR_LATER)
+            throw new SOXLaterException(err, txt);
+
+        else if (!retry && err == Errors.ERROR_NOT_GOOD_KHID)
+        {
+            logmsg("Hmm, I'm not registered.  Trying ...");
+            status &= ~REGISTERED;             // turn off that flag (optional)
+            register();                        // do the register (again!?!)
+            Reply rep = doRequest(req, true);    // try it again
+            logmsg("....................Yep, that worked!");
+            return reply ;
+        }
+
+        /*
+         *  These errors could be conceivably caused naturally
+         *  by upper layers as caller failures beyond the control
+         *  of this package.  Or something.
+         */
+        else if (
+                     (err == Errors.ERROR_NOT_PERMITTED)           ||
+                     false
+                )
+            throw new SOXRequestException(err, txt);
+
+        /*
+         *  Anything else should never happen,
+         *  if it does, fix it or move it above.
+         */
+        else
+            throw new SOXSubAccountException(err, "Deposit Failure: " + txt);
+
+    }
+
+    /**
+     *  Sort out exceptions.
+     *  There's a problem here.  No positive ack is delivered for
+     *  failure, so can't tell whether it is positive failure or
+     *  lost/failures.
+     */
+    protected Reply doRequest(Request req)
+        throws SOXSubAccountException, SOXLaterException
+    {
+        byte[] replyBuf = requestExtra((Request)req);
+
+        Reply reply;
+        try {
+            reply = req.reconstructReply(replyBuf);
+        } catch (SOXPacketException ex) {
+            // ex.printStackTrace(System.err);
+            throw new SOXSubAccountException("SOXPacketException: " + ex);
+        }
+        return reply ;
+    }
+
+
+
+////// Persistant Data //////////////////////////////////
+
+    /* this should probably not be here, but in the persistant wallet */
+
+    /**
      * Call this to freeze the subaccount.
      */
     public void freeze()
@@ -394,6 +525,7 @@
         if (!NetWatcher.netAvailability())
             throw new SOXLaterException(SOXLaterException.LATER_NET, "No Net");
     }
+
 
 
 ////// Encode & Decode //////////////////////////////////



1.20      +46 -56    java/webfunds/sox/ValueAccount.java

Index: ValueAccount.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/ValueAccount.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ValueAccount.java	2000/09/24 23:32:04	1.19
+++ ValueAccount.java	2000/10/14 01:50:45	1.20
@@ -1,5 +1,5 @@
 /*
- * $Id: ValueAccount.java,v 1.19 2000/09/24 23:32:04 iang Exp $
+ * $Id: ValueAccount.java,v 1.20 2000/10/14 01:50:45 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -57,8 +57,7 @@
                  SOXArgsException, SOXDepositException
     {
         checkFrozen("deposit");
-        if (!NetWatcher.netAvailability())
-            throw new SOXLaterException(SOXLaterException.LATER_NET, "No Net");
+        checkNet();
         if (isNew())          // also see retry code in doDepositGetMail()
             register();
 
@@ -80,19 +79,31 @@
                                                 payment, did, desc.getBytes());
 
         //
+        //  Odd.  many ways to sign:
+        //
+        //        Account signs data,         clean, simple
+        //        Account signs request,      key is kept clean
+        //        this code signs request,    messy, duplicated, but knows
+        //        Request signs itself.       object, but key is distributed
+        //
+        //  Which is best?
+        //
         //  Sign the deposit request.  This is possibly
         //  uneccesary, depends on what the Issuer decides.
         //
-        byte[] reqData = req.encode();
-        byte[] sig;
-        try {
-            sig = acct.sign(reqData);
-        } catch (java.security.KeyException kex) {
-            kex.printStackTrace(System.err);
-            throw new SOXKeyException("KeyException: " + kex);
-        }
+        acct.sign(req);   // this method requires Acct to know about requests
+
+//        byte[] reqData = req.encode();
+//        byte[] sig;
+//        try {
+//            sig = acct.sign(reqData);
+//        } catch (java.security.KeyException kex) {
+//            kex.printStackTrace(System.err);
+//            throw new SOXKeyException("KeyException: " + kex);
+//        }
+//
+//        req.setSignature(sig);
 
-        req.setSignature(sig);
         return doDepositGetMail(req, false, myDID);
     }
 
@@ -192,6 +203,8 @@
 
 ////// Cancel ////////////////////////////////// 
 
+    public static final byte[] CANCEL_DESC = "Cancel".getBytes();
+
     /**
      *  Cancel a payment that had a particular id.
      *
@@ -202,41 +215,17 @@
           throws SOXSubAccountException, SOXLaterException, SOXKeyException,
                  SOXDepositException
     {
-//        return cancel(paymentid, new AccountId());
-
         //
         // build a payment with that Id.
         //
         Payment p = new Payment(paymentid,
                                 acct.getId(), new AccountId(),
                                 itemId, 0,
-                                "Cancel".getBytes(), false,
+                                CANCEL_DESC, false,
                                 0, System.currentTimeMillis() + 24*60*60*1000);
         return cancelFromPayment(p);
     }
 
-//////////////////
-//  What on earth were these for?  It seems inconceivable that
-//  the caller cares about the details of a cancel???
-//
-//    public MailItem[] cancel(String paymentid, AccountId target)
-//          throws SOXSubAccountException, SOXLaterException, SOXKeyException,
-//                 SOXPacketException, SOXDepositException
-//    {
-//        return cancel(paymentid, target,
-//                      0, System.currentTimeMillis() + 24*60*60*1000);
-//    }
-//
-//    public MailItem[] cancel(String paymentid, AccountId target,
-//                          long validFrom, long validTill)
-//          throws SOXSubAccountException, SOXLaterException, SOXKeyException,
-//                 SOXPacketException, SOXDepositException
-//    {
-//        return cancel(new Payment(paymentid, acct.getId(), target, itemId, 0,
-//                                  "".getBytes(), false, validFrom, validTill));
-//    }
-
-
     /**
      * Cancel this payment.  The full details are contained within.
      * Creates a deposit with an empty payment with the same pid.
@@ -249,13 +238,12 @@
                SOXDepositException
     {
         checkFrozen("cancel");
-        if (!NetWatcher.netAvailability())
-            throw new SOXLaterException(SOXLaterException.LATER_NET, "No Net");
+        checkNet();
 
         String cancelId = payment.getId();
         Payment cancel = new Payment(cancelId, acct.getId(),
                                             payment.getTarget(), itemId,
-                                            0, "Cancel".getBytes(),
+                                            0, CANCEL_DESC,
                                             false, payment.getValidFrom(),
                                             payment.getValidTill());
         return cancelFromPayment(cancel);
@@ -273,8 +261,7 @@
                SOXDepositException
     {
         checkFrozen("cancel");
-        if (!NetWatcher.netAvailability())
-            throw new SOXLaterException(SOXLaterException.LATER_NET, "No Net");
+        checkNet();
 
         byte[] paymentData = cancel.encode();
 
@@ -298,21 +285,24 @@
         DepositRequest req = new DepositRequest(num, acct.getId(),
                                                 cancel, did, desc);
 
-        //
-        //  Sign the deposit request.  This is possibly
-        //  uneccesary, depends on what the Issuer decides.
-        //
-        byte[] reqData = req.encode();
-        byte[] sig2;
-        try {
-            sig2 = acct.sign(reqData);
-            //sig2 = Crypto.sign(acct.getKey(), req.encode());
-        } catch (KeyException kex) {
-            kex.printStackTrace(System.err);
-            throw new SOXKeyException("KeyException: " + kex);
-        }
+        acct.sign(req);
+
+//        //
+//        //  Sign the deposit request.  This is possibly
+//        //  uneccesary, depends on what the Issuer decides.
+//        //
+//        byte[] reqData = req.encode();
+//        byte[] sig2;
+//        try {
+//            sig2 = acct.sign(reqData);
+//            //sig2 = Crypto.sign(acct.getKey(), req.encode());
+//        } catch (KeyException kex) {
+//            kex.printStackTrace(System.err);
+//            throw new SOXKeyException("KeyException: " + kex);
+//        }
+//
+//        req.setSignature(sig2);
 
-        req.setSignature(sig2);
         return doCancelGetMail(req);
     }