[Webfunds-commits] java/webfunds/ricardian Contract.java

Ian Grigg iang@cypherpunks.ai
Sun, 27 Aug 2000 17:52:55 -0400 (AST)


iang        00/08/27 17:52:54

  Modified:    webfunds/ricardian Contract.java
  Log:
  added getBase that slices out the base contract from a signed or
  unsigned string contract.  Base is the content after BEGIN SIGN,
  up to but not including [keys] section.
  Used by SCW.  Seems to work but only limited testing so far.

Revision  Changes    Path
1.38      +91 -0     java/webfunds/ricardian/Contract.java

Index: Contract.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Contract.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- Contract.java	2000/08/27 17:42:52	1.37
+++ Contract.java	2000/08/27 21:52:54	1.38
@@ -1,4 +1,4 @@
-/* $Id: Contract.java,v 1.37 2000/08/27 17:42:52 iang Exp $
+/* $Id: Contract.java,v 1.38 2000/08/27 21:52:54 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -1522,6 +1522,97 @@
     {
         return getName();
     }
+
+    /**  
+     *  Take a string, interpret it is a Ricardian Contract,
+     *  and extract out the base - the part from the BEGIN SIG
+     *  to the keys.
+     */
+    public static String getBase(String contract)
+        throws ContractException
+    {
+        if (contract == null)
+            throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                        "null");
+        if (contract.length() == 0)
+            throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                        "empty");
+
+        byte[] buf = contract.getBytes();
+        String[] lines = IniFileReader.toLines(buf);
+        int numLines = lines.length;
+System.err.println("lines " + numLines);
+     
+        String s;
+        int start = 0;
+        while(start < numLines)          // skip empty
+        {
+            s = lines[start++];
+            if (s != null && s.length() > 0)
+            {
+                start--;
+                break;
+            }
+System.err.println("skipping line " + start);
+        }
+
+        if (start >= numLines)
+            throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                       "all lines are empty");
+
+        if (lines[start].equals("-----BEGIN PGP SIGNED MESSAGE-----"))
+        {
+System.err.println("signed! at line " + start);
+            PGPArmoury armoury;
+            try {
+                armoury = new PGPArmoury(contract);
+            } catch (IllegalArgumentException ex) {
+                throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                            "unrecognised format: " + ex);
+            }
+            String payload = new String(armoury.getPayload());
+            if (payload == null)
+                 throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                       "null unsigned part");
+            if (payload.length() < 10)
+                 throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                       "base part empty or too small");
+            if (payload.equals(contract))   // traps rescursion?
+                 throw new ContractException(ContractException.NOT_A_CONTRACT,
+                                       "Armoury failed to unsign!");
+
+            return getBase(payload); //  RECURSIVE !?! 
+        }
+
+        // ok, should have some content lines, unsigned (unarmoured), by now
+        int end = start;
+System.err.println("starting search at line " + start);
+        while (end < numLines)
+        {
+            s = lines[end++];
+            if (s == null || (s.length() == 0))
+                continue;
+
+            if (s.equals("[keys]"))
+            {
+System.err.println("found at line " + end);
+                end -= 2;
+                break;
+            }
+        }
+
+        String eoln = KeyUtil.getPlatformEndOfLine();
+
+System.err.println("concatenating strings 0 .. " + end);
+        StringBuffer sb = new StringBuffer(contract.length());
+        for (int i = 0; i < end; i++)
+        {
+            sb.append(lines[i]);
+            sb.append(eoln);
+        }
+        return sb.toString();
+    }
+
 
     public static void main(String[] arg)
         throws ContractException, IOException