key.Id +fmt fixes
authornsz <nsz@port70.net>
Tue, 13 Dec 2011 20:01:20 +0000 (21:01 +0100)
committernsz <nsz@port70.net>
Tue, 13 Dec 2011 20:01:20 +0000 (21:01 +0100)
pkg/Makefile
pkg/document/document.go
pkg/key/key.go
pkg/key/key_test.go
pkg/server/server.go
pkg/store/store.go

index 4917a98..896c219 100644 (file)
@@ -11,12 +11,16 @@ DIRS=\
        ../cmd/epoint-client\
        ../cmd/epoint-server\
 
+fmt.dirs: $(addsuffix .fmt, $(DIRS))
 clean.dirs: $(addsuffix .clean, $(DIRS))
 install.dirs: $(addsuffix .install, $(DIRS))
 nuke.dirs: $(addsuffix .nuke, $(DIRS))
 test.dirs: $(addsuffix .test, $(DIRS))
 testshort.dirs: $(addsuffix .testshort, $(DIRS))
 
+%.fmt:
+       gofmt -w $*/*.go
+
 %.clean:
        +$(MAKE) -C $* clean
 
@@ -31,6 +35,8 @@ testshort.dirs: $(addsuffix .testshort, $(DIRS))
        +@echo test $*
        +@$(MAKE) -C $* test.clean >$*/test.out 2>&1 || (echo TEST FAIL $*; cat $*/test.out; exit 1)
 
+fmt: fmt.dirs
+
 clean: clean.dirs
 
 install: install.dirs
index 22065fc..eecd68a 100644 (file)
@@ -160,13 +160,13 @@ type Cert struct {
 
 type DebitCert struct {
        Cert
-       Beneficiary      string
+       Beneficiary string
 }
 
 type CreditCert struct {
        Cert
-       Drawer           string
-       DebitCert        string
+       Drawer    string
+       DebitCert string
 }
 
 type BounceCert struct {
@@ -180,6 +180,7 @@ type BounceCert struct {
        References   []string
 }
 
+// Common cert part of a debit or credit cert
 func ToCert(v interface{}) (cert *Cert, err error) {
        cert = new(Cert)
        switch x := v.(type) {
@@ -209,7 +210,7 @@ func Id(c *Signed) string {
        return fmt.Sprintf("%040X", h.Sum())
 }
 
-// parse an epoint document without checking the signature and format details
+// Parse an epoint document without checking the signature and format details
 func Parse(s []byte) (iv interface{}, c *Signed, err error) {
        c, err = ParseSigned(s)
        if err != nil {
@@ -223,7 +224,7 @@ func Parse(s []byte) (iv interface{}, c *Signed, err error) {
        return
 }
 
-// format and sign an epoint document
+// Format and sign an epoint document
 func Format(iv interface{}, key *openpgp.Entity) (s []byte, c *Signed, err error) {
        doc, err := FormatStruct(iv)
        if err != nil {
@@ -241,7 +242,7 @@ func Format(iv interface{}, key *openpgp.Entity) (s []byte, c *Signed, err error
        return
 }
 
-// verify an epoint document, return the cleaned version as well
+// Verify an epoint document, return the cleaned version as well
 func Verify(c *Signed, key openpgp.KeyRing) (err error) {
        msg := bytes.NewBuffer(c.Body)
        sig := bytes.NewBuffer(c.Signature)
@@ -251,7 +252,7 @@ func Verify(c *Signed, key openpgp.KeyRing) (err error) {
        return
 }
 
-// sign body with given secret key
+// Sign body with given secret key
 func Sign(body []byte, key *openpgp.Entity) (c *Signed, err error) {
        c = new(Signed)
        c.Hash = "SHA256"
@@ -494,6 +495,7 @@ func parseStruct(v reflect.Value, fields map[string]string, seen map[string]bool
        return
 }
 
+// ParseStruct parses an epoint document and returns a struct representation
 func ParseStruct(doc *Document) (iv interface{}, err error) {
        switch doc.Type {
        case "Draft":
@@ -580,7 +582,7 @@ func formatStruct(v reflect.Value, doc *Document) (err error) {
        return
 }
 
-// turn a struct into a document
+// FormatStruct turns a struct into a document
 func FormatStruct(iv interface{}) (doc *Document, err error) {
        v := reflect.ValueOf(iv)
        if v.Kind() != reflect.Ptr || v.IsNil() || v.Elem().Kind() != reflect.Struct {
@@ -593,6 +595,7 @@ func FormatStruct(iv interface{}) (doc *Document, err error) {
        return
 }
 
+// ParseFields parses a key value sequence into a fields map
 func ParseFields(s []byte) (fields map[string]string, rest []byte, err error) {
        rest = s
        fields = make(map[string]string)
index f5f9143..29e92bd 100644 (file)
@@ -109,6 +109,11 @@ func Holder(r []byte, issuer, denomination string) (e *openpgp.Entity, err error
        return New(DsaKey(r), 0, "Holder of "+issuer, denomination, "")
 }
 
+// Key id (fingerprint)
+func Id(e *openpgp.Entity) string {
+       return fmt.Sprintf("%X", e.PrimaryKey.Fingerprint)
+}
+
 // Check the issuer and denomination associated with the given pgp key
 func Check(e *openpgp.Entity) (isIssuer bool, issuer, denomination string, err error) {
        // allow multiple identities, use the first one that looks like an epoint uid
@@ -116,7 +121,7 @@ func Check(e *openpgp.Entity) (isIssuer bool, issuer, denomination string, err e
                denomination = id.UserId.Comment
                if id.UserId.Name == "Issuer" {
                        isIssuer = true
-                       issuer = fmt.Sprintf("%X", e.PrimaryKey.Fingerprint)
+                       issuer = Id(e)
                        return
                }
                const prefix = "Holder of "
index 1e594fe..0226d12 100644 (file)
@@ -37,6 +37,19 @@ func TestKey(t *testing.T) {
        }
 }
 
+func TestId(t *testing.T) {
+       idwant := "E51F405B809FA2DEA760603F9D33F730611CBCD9"
+       key, err := Issuer([]byte("rand"), "")
+       if err != nil {
+               t.Errorf("Issuer failed: %s", err)
+               return
+       }
+       id := Id(key)
+       if id != idwant {
+               t.Errorf("Id failed: expected %s, got %s", idwant, id)
+       }
+}
+
 func TestIssuerHolder(t *testing.T) {
        denomination := "1/100 EUR"
        priv, err := Issuer([]byte("issuer-rand"), denomination)
index 71bd3fc..614c256 100644 (file)
@@ -24,7 +24,7 @@ func StoreSk(sk *openpgp.Entity) (err error) {
        if err != nil {
                return
        }
-       return db.Set("key", fmt.Sprintf("%X", sk.PrimaryKey.Fingerprint), b.Bytes())
+       return db.Set("key", key.Id(sk), b.Bytes())
 }
 
 func GetKeys(fpr string) (es openpgp.EntityList, err error) {
@@ -75,7 +75,7 @@ func AddKeys(d []byte) (err error) {
                if err != nil {
                        return
                }
-               fpr := fmt.Sprintf("%X", e.PrimaryKey.Fingerprint)
+               fpr := key.Id(e)
                err = db.Set("key", fpr, b.Bytes())
                if err != nil {
                        return
@@ -259,7 +259,7 @@ func NewDebitCert(draftid string, draft *document.Draft) (*document.DebitCert, e
                }
                cert.LastDebitSerial = oldcert.LastDebitSerial
                cert.LastCreditSerial = oldcert.LastCreditSerial
-               if _,ok := iv.(*document.DebitCert); ok {
+               if _, ok := iv.(*document.DebitCert); ok {
                        cert.LastDebitSerial = oldcert.Serial
                } else {
                        cert.LastCreditSerial = oldcert.Serial
@@ -315,7 +315,7 @@ func NewCreditCert(draftid string, draft *document.Draft, dcertid string, dcert
                }
                cert.LastDebitSerial = oldcert.LastDebitSerial
                cert.LastCreditSerial = oldcert.LastCreditSerial
-               if _,ok := iv.(*document.DebitCert); ok {
+               if _, ok := iv.(*document.DebitCert); ok {
                        cert.LastDebitSerial = oldcert.Serial
                } else {
                        cert.LastCreditSerial = oldcert.Serial
index 2a6d2f8..a27935c 100644 (file)
@@ -42,7 +42,7 @@ func (c *Conn) Get(name, k string) (v []byte, err error) {
        v, err = ioutil.ReadFile(filepath.Join(c.path, name, k))
        if err != nil {
                if p, ok := err.(*os.PathError); ok && p.Err == os.ENOENT {
-                       err = NotFoundError{name+"/"+k}
+                       err = NotFoundError{name + "/" + k}
                }
        }
        return