2020-10-28 17:35:56 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrKeyInfoNotFound = fmt.Errorf("key info not found")
|
|
|
|
ErrKeyExists = fmt.Errorf("key already exists")
|
|
|
|
)
|
|
|
|
|
|
|
|
// KeyType defines a type of a key
|
|
|
|
type KeyType string
|
|
|
|
|
|
|
|
const (
|
2020-11-12 14:18:30 +00:00
|
|
|
KTEd25519 KeyType = "ed25519"
|
2020-10-28 17:35:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (kt *KeyType) UnmarshalJSON(bb []byte) error {
|
|
|
|
{
|
|
|
|
// first option, try unmarshaling as string
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(bb, &s)
|
|
|
|
if err == nil {
|
|
|
|
*kt = KeyType(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
var b byte
|
|
|
|
err := json.Unmarshal(bb, &b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal KeyType either as string nor integer: %w", err)
|
|
|
|
}
|
2020-11-12 14:18:30 +00:00
|
|
|
bst := SigType(b)
|
2020-10-28 17:35:56 +00:00
|
|
|
|
|
|
|
switch bst {
|
2020-11-12 14:18:30 +00:00
|
|
|
case SigTypeEd25519:
|
|
|
|
*kt = KTEd25519
|
2020-10-28 17:35:56 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown sigtype: %d", bst)
|
|
|
|
}
|
|
|
|
logrus.Warn("deprecation: integer style 'KeyType' is deprecated, switch to string style")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyInfo is used for storing keys in KeyStore
|
|
|
|
type KeyInfo struct {
|
|
|
|
Type KeyType
|
|
|
|
PrivateKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyStore is used for storing secret keys
|
|
|
|
type KeyStore interface {
|
|
|
|
// List lists all the keys stored in the KeyStore
|
|
|
|
List() ([]string, error)
|
|
|
|
// Get gets a key out of keystore and returns KeyInfo corresponding to named key
|
|
|
|
Get(string) (KeyInfo, error)
|
|
|
|
// Put saves a key info under given name
|
|
|
|
Put(string, KeyInfo) error
|
|
|
|
// Delete removes a key from keystore
|
|
|
|
Delete(string) error
|
|
|
|
}
|