-
Notifications
You must be signed in to change notification settings - Fork 940
/
keystore.go
61 lines (46 loc) · 1.35 KB
/
keystore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package keystore
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/multiformats/go-base32"
)
type (
// KeyName represents private key name.
KeyName string
// PrivKey represents private key with arbitrary body.
PrivKey struct {
Body []byte `json:"body"`
// TODO(@Wondertan): At later point, it might make sense to have a Type.
}
)
// Keystore is meant to manage private keys.
type Keystore interface {
// Put stores given PrivKey.
Put(KeyName, PrivKey) error
// Get reads PrivKey using given KeyName.
Get(KeyName) (PrivKey, error)
// Delete erases PrivKey using given KeyName.
Delete(name KeyName) error
// List lists all stored key names.
List() ([]KeyName, error)
// Path reports the path of the Keystore.
Path() string
// Keyring returns the keyring corresponding to the node's
// keystore.
Keyring() keyring.Keyring
}
// KeyNameFromBase32 decodes KeyName from Base32 format.
func KeyNameFromBase32(bs string) (KeyName, error) {
name, err := base32.RawStdEncoding.DecodeString(bs)
if err != nil {
return "", fmt.Errorf("keystore: can't convert base32 string to key name: %w", err)
}
return KeyName(name), nil
}
// Base32 formats KeyName to Base32 format.
func (kn KeyName) Base32() string {
return base32.RawStdEncoding.EncodeToString([]byte(kn))
}
func (kn KeyName) String() string {
return string(kn)
}