From 160a6c8f3ba267c94f4f75de1e33fe544f15769c Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 22 Dec 2015 17:13:11 +0100 Subject: Collapse --- account.go | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'account.go') diff --git a/account.go b/account.go index e0d24b0..68dae50 100644 --- a/account.go +++ b/account.go @@ -2,6 +2,7 @@ package acme import ( "crypto/rsa" + "errors" "io" "net/mail" "strings" @@ -20,52 +21,46 @@ type Account struct { nonce chan string } -func newMail(email string) (Mail, error) { - m, err := mail.ParseAddress(email) - if err != nil { - return "", err - } - mm := Mail(m.Address) - return mm, nil -} - -func newPhone(phone string) (Phone, error) { - p := strings.TrimSpace(phone) - return Phone(p), nil -} - // NewAccount ... func NewAccount(key *rsa.PrivateKey) (*Account, error) { return &Account{PrivKey: key}, nil } -func (a *Account) AddMail(mail string) error { - if m, _ := newMail(mail); m != "" { - a.Contact = append(a.Contact, m) +func (a *Account) AddMail(email string) error { + m, err := mail.ParseAddress(email) + if err != nil { + return err } + a.Contact = append(a.Contact, Mail(m.Address)) return nil } func (a *Account) AddPhone(phone string) error { - if ph, _ := newPhone(phone); ph != "" { - a.Contact = append(a.Contact, ph) + if ph := strings.TrimSpace(phone); ph != "" { + a.Contact = append(a.Contact, Phone(ph)) } return nil } // Signer describes a signing interface type Signer interface { - Sign([]byte, jose.NonceSource) (io.Reader, error) + Init(jose.NonceSource) error + Sign([]byte) (io.Reader, error) +} + +func (a *Account) Init(n jose.NonceSource) error { + var err error + a.signer, err = jose.NewSigner(jose.RS256, a.PrivKey) + if err != nil { + return err + } + a.signer.SetNonceSource(n) + return nil } -func (a *Account) Sign(msg []byte, n jose.NonceSource) (io.Reader, error) { +func (a *Account) Sign(msg []byte) (io.Reader, error) { if a.signer == nil { - var err error - a.signer, err = jose.NewSigner(jose.RS256, a.PrivKey) - if err != nil { - return nil, err - } - a.signer.SetNonceSource(n) + return nil, errors.New("init first") } obj, err := a.signer.Sign(msg) if err != nil { -- cgit v1.2.3