Browse Source

Updated Manager interface to return seed on create, fix server tests

pull/1782/head
Ethan Frey 7 years ago
parent
commit
15609e1219
5 changed files with 33 additions and 14 deletions
  1. +9
    -3
      keys/cryptostore/holder.go
  2. +3
    -2
      keys/server/keys.go
  3. +10
    -8
      keys/server/keys_test.go
  4. +7
    -0
      keys/server/types/keys.go
  5. +4
    -1
      keys/transactions.go

+ 9
- 3
keys/cryptostore/holder.go View File

@ -32,14 +32,20 @@ func (s Manager) assertKeyManager() keys.Manager {
// //
// algo must be a supported go-crypto algorithm: // algo must be a supported go-crypto algorithm:
// //
func (s Manager) Create(name, passphrase, algo string) (keys.Info, error) {
func (s Manager) Create(name, passphrase, algo string) (keys.Info, string, error) {
gen, err := getGenerator(algo) gen, err := getGenerator(algo)
if err != nil { if err != nil {
return keys.Info{}, err
return keys.Info{}, "", err
} }
key := gen.Generate() key := gen.Generate()
err = s.es.Put(name, passphrase, key) err = s.es.Put(name, passphrase, key)
return info(name, key), err
// TODO
return info(name, key), "", err
}
func (s Manager) Recover(name, passphrase, seedphrase string) (keys.Info, error) {
// TODO
return keys.Info{}, nil
} }
// List loads the keys from the storage and enforces alphabetical order // List loads the keys from the storage and enforces alphabetical order


+ 3
- 2
keys/server/keys.go View File

@ -31,13 +31,14 @@ func (k Keys) GenerateKey(w http.ResponseWriter, r *http.Request) {
return return
} }
key, err := k.manager.Create(req.Name, req.Passphrase, req.Algo)
key, seed, err := k.manager.Create(req.Name, req.Passphrase, req.Algo)
if err != nil { if err != nil {
writeError(w, err) writeError(w, err)
return return
} }
writeSuccess(w, &key)
res := types.CreateKeyResponse{key, seed}
writeSuccess(w, &res)
} }
func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) { func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) {


+ 10
- 8
keys/server/keys_test.go View File

@ -40,13 +40,15 @@ func TestKeyServer(t *testing.T) {
key, code, err := createKey(r, n1, p1, algo) key, code, err := createKey(r, n1, p1, algo)
require.Nil(err, "%+v", err) require.Nil(err, "%+v", err)
require.Equal(http.StatusOK, code) require.Equal(http.StatusOK, code)
require.Equal(key.Name, n1)
require.Equal(n1, key.Key.Name)
require.NotEmpty(n1, key.Seed)
// the other one works // the other one works
key2, code, err := createKey(r, n2, p2, algo) key2, code, err := createKey(r, n2, p2, algo)
require.Nil(err, "%+v", err) require.Nil(err, "%+v", err)
require.Equal(http.StatusOK, code) require.Equal(http.StatusOK, code)
require.Equal(key2.Name, n2)
require.Equal(key2.Key.Name, n2)
require.NotEmpty(n2, key.Seed)
// let's abstract this out a bit.... // let's abstract this out a bit....
keys, code, err = listKeys(r) keys, code, err = listKeys(r)
@ -62,9 +64,9 @@ func TestKeyServer(t *testing.T) {
k, code, err := getKey(r, n1) k, code, err := getKey(r, n1)
require.Nil(err, "%+v", err) require.Nil(err, "%+v", err)
require.Equal(http.StatusOK, code) require.Equal(http.StatusOK, code)
assert.Equal(k.Name, n1)
assert.Equal(n1, k.Name)
assert.NotNil(k.Address) assert.NotNil(k.Address)
assert.Equal(k.Address, key.Address)
assert.Equal(key.Key.Address, k.Address)
// delete with proper key // delete with proper key
_, code, err = deleteKey(r, n1, p1) _, code, err = deleteKey(r, n1, p1)
@ -134,7 +136,7 @@ func getKey(h http.Handler, name string) (*keys.Info, int, error) {
return &data, rr.Code, err return &data, rr.Code, err
} }
func createKey(h http.Handler, name, passphrase, algo string) (*keys.Info, int, error) {
func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyResponse, int, error) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
post := types.CreateKeyRequest{ post := types.CreateKeyRequest{
Name: name, Name: name,
@ -157,9 +159,9 @@ func createKey(h http.Handler, name, passphrase, algo string) (*keys.Info, int,
return nil, rr.Code, nil return nil, rr.Code, nil
} }
data := keys.Info{}
err = json.Unmarshal(rr.Body.Bytes(), &data)
return &data, rr.Code, err
data := new(types.CreateKeyResponse)
err = json.Unmarshal(rr.Body.Bytes(), data)
return data, rr.Code, err
} }
func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) { func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) {


+ 7
- 0
keys/server/types/keys.go View File

@ -1,5 +1,7 @@
package types package types
import "github.com/tendermint/go-crypto/keys"
// CreateKeyRequest is sent to create a new key // CreateKeyRequest is sent to create a new key
type CreateKeyRequest struct { type CreateKeyRequest struct {
Name string `json:"name" validate:"required,min=4,printascii"` Name string `json:"name" validate:"required,min=4,printascii"`
@ -26,3 +28,8 @@ type ErrorResponse struct {
Error string `json:"error"` // error message if Success is false Error string `json:"error"` // error message if Success is false
Code int `json:"code"` // error code if Success is false Code int `json:"code"` // error code if Success is false
} }
type CreateKeyResponse struct {
Key keys.Info `json:"key"`
Seed string `json:"seed_phrase"`
}

+ 4
- 1
keys/transactions.go View File

@ -63,7 +63,10 @@ type Signer interface {
// Manager allows simple CRUD on a keystore, as an aid to signing // Manager allows simple CRUD on a keystore, as an aid to signing
type Manager interface { type Manager interface {
Signer Signer
Create(name, passphrase, algo string) (Info, error)
// Create also returns a seed phrase for cold-storage
Create(name, passphrase, algo string) (Info, string, error)
// Recover takes a seedphrase and loads in the private key
Recover(name, passphrase, seedphrase string) (Info, error)
List() (Infos, error) List() (Infos, error)
Get(name string) (Info, error) Get(name string) (Info, error)
Update(name, oldpass, newpass string) error Update(name, oldpass, newpass string) error


Loading…
Cancel
Save