Updated Manager interface to return seed on create, fix server tests
This commit is contained in:
parent
56200e167a
commit
15609e1219
|
@ -32,14 +32,20 @@ func (s Manager) assertKeyManager() keys.Manager {
|
|||
//
|
||||
// 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)
|
||||
if err != nil {
|
||||
return keys.Info{}, err
|
||||
return keys.Info{}, "", err
|
||||
}
|
||||
key := gen.Generate()
|
||||
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
|
||||
|
|
|
@ -31,13 +31,14 @@ func (k Keys) GenerateKey(w http.ResponseWriter, r *http.Request) {
|
|||
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 {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeSuccess(w, &key)
|
||||
res := types.CreateKeyResponse{key, seed}
|
||||
writeSuccess(w, &res)
|
||||
}
|
||||
|
||||
func (k Keys) GetKey(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -40,13 +40,15 @@ func TestKeyServer(t *testing.T) {
|
|||
key, code, err := createKey(r, n1, p1, algo)
|
||||
require.Nil(err, "%+v", err)
|
||||
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
|
||||
key2, code, err := createKey(r, n2, p2, algo)
|
||||
require.Nil(err, "%+v", err)
|
||||
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....
|
||||
keys, code, err = listKeys(r)
|
||||
|
@ -62,9 +64,9 @@ func TestKeyServer(t *testing.T) {
|
|||
k, code, err := getKey(r, n1)
|
||||
require.Nil(err, "%+v", err)
|
||||
require.Equal(http.StatusOK, code)
|
||||
assert.Equal(k.Name, n1)
|
||||
assert.Equal(n1, k.Name)
|
||||
assert.NotNil(k.Address)
|
||||
assert.Equal(k.Address, key.Address)
|
||||
assert.Equal(key.Key.Address, k.Address)
|
||||
|
||||
// delete with proper key
|
||||
_, 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
|
||||
}
|
||||
|
||||
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()
|
||||
post := types.CreateKeyRequest{
|
||||
Name: name,
|
||||
|
@ -157,9 +159,9 @@ func createKey(h http.Handler, name, passphrase, algo string) (*keys.Info, int,
|
|||
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) {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package types
|
||||
|
||||
import "github.com/tendermint/go-crypto/keys"
|
||||
|
||||
// CreateKeyRequest is sent to create a new key
|
||||
type CreateKeyRequest struct {
|
||||
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
|
||||
Code int `json:"code"` // error code if Success is false
|
||||
}
|
||||
|
||||
type CreateKeyResponse struct {
|
||||
Key keys.Info `json:"key"`
|
||||
Seed string `json:"seed_phrase"`
|
||||
}
|
||||
|
|
|
@ -63,7 +63,10 @@ type Signer interface {
|
|||
// Manager allows simple CRUD on a keystore, as an aid to signing
|
||||
type Manager interface {
|
||||
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)
|
||||
Get(name string) (Info, error)
|
||||
Update(name, oldpass, newpass string) error
|
||||
|
|
Loading…
Reference in New Issue