From 48e3361e2c7018acaafd6b90b85d042f8edc620b Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 10 Dec 2015 17:54:35 +0100 Subject: Play with interfaces --- account.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'account.go') diff --git a/account.go b/account.go index 85092de..86f44cc 100644 --- a/account.go +++ b/account.go @@ -3,7 +3,9 @@ package acme import ( "crypto/rand" "crypto/rsa" + "encoding/json" "fmt" + "io/ioutil" "net/mail" "github.com/square/go-jose" @@ -14,7 +16,7 @@ const KeySize = 2048 // Account ... type Account struct { - Contact []Contact `json:"contact"` + Contact Contacts `json:"contact"` PrivKey *rsa.PrivateKey `json:"key"` signer jose.Signer nonce chan string @@ -25,7 +27,8 @@ func newMail(email string) (Mail, error) { if err != nil { return "", err } - return Mail(m.Address), nil + mm := Mail(m.Address) + return mm, nil } // NewAccount ... @@ -39,13 +42,27 @@ func NewAccount(email string, bits int) (*Account, error) { return nil, err } return &Account{ - Contact: []Contact{m}, + Contact: Contacts{m}, PrivKey: key, }, nil } -func LoadAccount(email string) (*Account, error) { - return nil, nil +func LoadAccount(fname string) (*Account, error) { + body, err := ioutil.ReadFile(fname) + if err != nil { + return nil, err + } + a := new(Account) + err = json.Unmarshal(body, a) + return a, err +} + +func (a Account) SaveAccount(fname string) error { + body, err := json.MarshalIndent(a, "", "\t") + if err != nil { + return err + } + return ioutil.WriteFile(fname, body, 0600) } func (a *Account) Sign(msg []byte, n jose.NonceSource) ([]byte, error) { -- cgit v1.2.3