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

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Tue, 20 Feb 2001 17:58:28 -0400 (AST)


gelderen    01/02/20 17:58:28

  Modified:    webfunds/client/sox SOXWallet.java
  Log:
  - Add createAccount function that
    - does not use any GUI features.
    - has a somewhat sane signature.
  - Redefine existing addAccount function in terms of createAccount +
    Addressbook munging.
  - Fix spelling error in comment.
  
  I can created and spend between accounts created with this new version.

Revision  Changes    Path
1.141     +58 -50    java/webfunds/client/sox/SOXWallet.java

Index: SOXWallet.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/sox/SOXWallet.java,v
retrieving revision 1.140
retrieving revision 1.141
diff -u -r1.140 -r1.141
--- SOXWallet.java	2000/12/18 04:21:18	1.140
+++ SOXWallet.java	2001/02/20 21:58:27	1.141
@@ -1,4 +1,4 @@
-/* $Id: SOXWallet.java,v 1.140 2000/12/18 04:21:18 iang Exp $
+/* $Id: SOXWallet.java,v 1.141 2001/02/20 21:58:27 gelderen Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All Rights Reserved.
@@ -15,7 +15,7 @@
 import java.awt.event.ActionEvent;
 import java.beans.*;
 
-// SOX tool kit
+// SOX toolkit
 import webfunds.ricardian.Contract;
 import webfunds.ricardian.ContractStore;
 import webfunds.ricardian.SOXServerException;
@@ -76,6 +76,8 @@
  *
  *  See quiteCancel for example of low call.  It should not use AccountInfo
  *  (artifact of Receipt Store).
+ *
+ * @version $Revision: 1.141 $
  */
 public class SOXWallet
     extends Debug
@@ -2123,71 +2125,77 @@
 
 ///////////  Account / Contract manipulations ////////////////////////
 
+
+    /**
+     * Create a new account in the Wallet and return it's AccountId.
+     *
+     * This method either returns a valid AccountId or throws an exception,
+     * it never returns null. Argument 'name' may be null.
+     */
+    public AccountId createAccount(String name) throws WalletException {
+
+        if( this.isClosed() )
+            throw new WalletException(WalletException.CLOSED);
+
+        try {
+            Account acc = Account.getInstance();
+            acc.setName(name);
+
+            logmsg("createAccount: Created new account");
+
+            AccountId accId = acc.getId();
+            this.accountStore.addAccount(acc);
+            this.getAccount(accId);
+
+            logmsg("createAccount: Added account to Store");
+
+            logmsg("createAccount -> " + accId);
+            return accId;
+
+        } catch(SOXAccountException e) {
+            throw new WalletException(WalletException.INTERNAL, e.toString());
+        } catch(StoreException e) {
+            throw new WalletException(WalletException.INTERNAL, e.toString());
+        }
+    }
+
+
     /**
      *  Client Wallet Interface
      */
     public AccountInfo addAccount()
     {
-        if (isClosed())
-        {
+        if (isClosed()) {
             error(closeErrorReason);
             return null ;
         }
 
-        Account newacc = null;
-        try
-        {
-            newacc = Account.getInstance();
-        } catch (SOXAccountException e) {
-            e.printStackTrace();
-            error("failed to create account (2) : " + e.toString());
-            return null ;
-        }
+        try {
+            String q = "Please specify a name for this account: ";
+            String newAccName = context.getUInterface(this).getInput(q);
 
-        String q = "Please specify a name for this account: ";
-        String name = context.getUInterface(this).getInput(q);
-        newacc.setName(name);
+            AccountId newAccId = this.createAccount(newAccName);
+            AccountInfo newAccInfo = 
+                new AccountInfo(newAccId, null, this);
 
-        //
-        // Shove it into the store for persistance.
-        //
-        AccountId acct = newacc.getId();
-        logmsg("Created new account");
-        try
-        {
-            accountStore.addAccount(newacc);
-
             //
-            //  Test it by getting it out again.
+            // Enter this acc's name into the address book
             //
-            // AccountInfo info = new AccountInfo();
-            // info.setId(id.getId());
-            getAccount(acct);
+            Addressbook book = this.context.getAddressbook(this);
+            book.addAccountInfo(newAccInfo, newAccName);
 
-        } catch (StoreException ex) {
-            ex.printStackTrace();
-            error("store cannot add account: " + ex);
-            return null ;
-        }
-
-        logmsg("Added account to Store");
-        AccountInfo info;
-        info = new AccountInfo(acct.getId(), null, this);
+            logmsg("addAccount -> " + newAccInfo);
+            return newAccInfo;
 
-        //
-        // Tell the address book the name.
-        //
-        try
-        {
-            context.getAddressbook(this).addAccountInfo(info, name);
-            //context.getAddressbook(this).addAccountInfo(info);
-        } catch (IOException e) {
+        } catch(WalletException e) {
+            e.printStackTrace();
+            error("addAccount: error is " + e.toString());
+            return null;
+        } catch(IOException e) {
             e.printStackTrace();
-            error("addressbook failed: " + e.toString());
+            error("addAccount: addressbook failed, error is " + e.toString());
+            return null;
         }
-logmsg("Succesfully added account: " + info);
-        return info;
-
     }