allow server signature
[epoint] / document / document.go
index e2a0859..0fd13ce 100644 (file)
@@ -31,6 +31,7 @@ package document
 // TODO: trailing space handling in ParseFields
 // TODO: fields of notice (last notice, serial, failure notice,..)
 // TODO: limits and cert type specific input validation
+// TODO: fix Cert mess
 
 import (
        "bytes"
@@ -139,6 +140,27 @@ type Notice struct {
        References   []string // may be empty (startup notice)
 }
 
+type Cert struct {
+       IsDebit          bool
+       Holder           string
+       Serial           int64
+       Balance          int64
+       Denomination     string
+       Issuer           string
+       Date             int64
+       Difference       int64
+       Draft            string
+       Beneficiary      *string // only in debit cert
+       Drawer           *string // only in credit cert
+       DebitCert        *string // only in credit cert
+       AuthorizedBy     string
+       Notes            *string // optional
+       LastDebitSerial  int64   // 0 if none
+       LastCreditSerial int64   // 0 if none
+       LastCert         *string // nil if serial == 1
+       References       []string
+}
+
 type DebitCert struct {
        Holder           string
        Serial           int64
@@ -153,7 +175,7 @@ type DebitCert struct {
        Notes            *string // optional
        LastDebitSerial  int64   // 0 if none
        LastCreditSerial int64   // 0 if none
-       LastCert         *string // ? if serial == 1
+       LastCert         *string // nil if serial == 1
        References       []string
 }
 
@@ -187,10 +209,57 @@ type BounceCert struct {
        References   []string
 }
 
-// sha1 sum of the (cleaned) document as uppercase hex string
-func Id(d []byte) string {
+func ToCert(v interface{}) (cert *Cert, err error) {
+       cert = new(Cert)
+       switch x := v.(type) {
+       case *DebitCert:
+               cert.IsDebit = true
+               cert.Beneficiary = &x.Beneficiary
+
+               cert.Holder = x.Holder
+               cert.Serial = x.Serial
+               cert.Balance = x.Balance
+               cert.Denomination = x.Denomination
+               cert.Issuer = x.Issuer
+               cert.Date = x.Date
+               cert.Difference = x.Difference
+               cert.Draft = x.Draft
+               cert.AuthorizedBy = x.AuthorizedBy
+               cert.Notes = x.Notes
+               cert.LastDebitSerial = x.LastDebitSerial
+               cert.LastCreditSerial = x.LastCreditSerial
+               cert.LastCert = x.LastCert
+               cert.References = x.References
+
+       case *CreditCert:
+               cert.IsDebit = false
+               cert.Drawer = &x.Drawer
+               cert.DebitCert = &x.DebitCert
+
+               cert.Holder = x.Holder
+               cert.Serial = x.Serial
+               cert.Balance = x.Balance
+               cert.Denomination = x.Denomination
+               cert.Issuer = x.Issuer
+               cert.Date = x.Date
+               cert.Difference = x.Difference
+               cert.Draft = x.Draft
+               cert.AuthorizedBy = x.AuthorizedBy
+               cert.Notes = x.Notes
+               cert.LastDebitSerial = x.LastDebitSerial
+               cert.LastCreditSerial = x.LastCreditSerial
+               cert.LastCert = x.LastCert
+               cert.References = x.References
+       default:
+               err = fmt.Errorf("ToCert: only debit or credit document can be converted to cert")
+       }
+       return
+}
+
+// sha1 sum of the (cleaned) document body as uppercase hex string
+func Id(c *Signed) string {
        h := sha1.New()
-       h.Write(d)
+       h.Write(c.Body)
        return fmt.Sprintf("%040X", h.Sum())
 }
 
@@ -209,7 +278,7 @@ func Parse(s []byte) (iv interface{}, c *Signed, err error) {
 }
 
 // format and sign an epoint document
-func Format(iv interface{}, key *openpgp.Entity) (s []byte, err error) {
+func Format(iv interface{}, key *openpgp.Entity) (s []byte, c *Signed, err error) {
        doc, err := FormatStruct(iv)
        if err != nil {
                return
@@ -218,31 +287,21 @@ func Format(iv interface{}, key *openpgp.Entity) (s []byte, err error) {
        if err != nil {
                return
        }
-       c, err := Sign(body, key)
+       c, err = Sign(body, key)
        if err != nil {
                return
        }
-       return FormatSigned(c)
+       s, err = FormatSigned(c)
+       return
 }
 
 // verify an epoint document, return the cleaned version as well
-func Verify(c *Signed, key openpgp.KeyRing) (cleaned []byte, err error) {
-       err = CleanSigned(c)
-       if err != nil {
-               return
-       }
-       err = VerifyCleaned(c, key)
-       if err != nil {
-               return
-       }
-       return FormatSigned(c)
-}
-
-// verify signature of body with given key
-func VerifyCleaned(c *Signed, key openpgp.KeyRing) (err error) {
+func Verify(c *Signed, key openpgp.KeyRing) (err error) {
        msg := bytes.NewBuffer(c.Body)
        sig := bytes.NewBuffer(c.Signature)
-       _, err = openpgp.CheckArmoredDetachedSignature(key, msg, sig)
+// TODO: verify signature
+       _,_ = msg,sig
+//     _, err = openpgp.CheckArmoredDetachedSignature(key, msg, sig)
        return
 }