aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/key_manager.go
blob: cc2b9ff90dba0e50659dc9cf3a3968bc432e3c2a (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package crypto

import (
    "fmt"
    "sync"

    "github.com/ethereum/go-ethereum/ethutil"
)

type KeyManager struct {
    keyRing  *KeyRing
    session  string
    keyStore KeyStore            // interface
    keyRings map[string]*KeyRing // cache
    keyPair  *KeyPair
}

func NewDBKeyManager(db ethutil.Database) *KeyManager {
    return &KeyManager{keyStore: &DBKeyStore{db: db}, keyRings: make(map[string]*KeyRing)}
}

func NewFileKeyManager(basedir string) *KeyManager {
    return &KeyManager{keyStore: &FileKeyStore{basedir: basedir}, keyRings: make(map[string]*KeyRing)}
}

func (k *KeyManager) KeyPair() *KeyPair {
    return k.keyPair
}

func (k *KeyManager) KeyRing() *KeyPair {
    return k.keyPair
}

func (k *KeyManager) PrivateKey() []byte {
    return k.keyPair.PrivateKey
}

func (k *KeyManager) PublicKey() []byte {
    return k.keyPair.PublicKey
}

func (k *KeyManager) Address() []byte {
    return k.keyPair.Address()
}

func (k *KeyManager) save(session string, keyRing *KeyRing) error {
    err := k.keyStore.Save(session, keyRing)
    if err != nil {
        return err
    }
    k.keyRings[session] = keyRing
    return nil
}

func (k *KeyManager) load(session string) (*KeyRing, error) {
    keyRing, found := k.keyRings[session]
    if !found {
        var err error
        keyRing, err = k.keyStore.Load(session)
        if err != nil {
            return nil, err
        }
    }
    return keyRing, nil
}

func cursorError(cursor int, len int) error {
    return fmt.Errorf("cursor %d out of range (0..%d)", cursor, len)
}

func (k *KeyManager) reset(session string, cursor int, keyRing *KeyRing) error {
    if cursor >= keyRing.Len() {
        return cursorError(cursor, keyRing.Len())
    }
    lock := &sync.Mutex{}
    lock.Lock()
    defer lock.Unlock()
    err := k.save(session, keyRing)
    if err != nil {
        return err
    }
    k.session = session
    k.keyRing = keyRing
    k.keyPair = keyRing.GetKeyPair(cursor)
    return nil
}

func (k *KeyManager) SetCursor(cursor int) error {
    if cursor >= k.keyRing.Len() {
        return cursorError(cursor, k.keyRing.Len())
    }
    k.keyPair = k.keyRing.GetKeyPair(cursor)
    return nil
}

func (k *KeyManager) Init(session string, cursor int, force bool) error {
    var keyRing *KeyRing
    if !force {
        var err error
        keyRing, err = k.load(session)
        if err != nil {
            return err
        }
    }
    if keyRing == nil {
        keyRing = NewGeneratedKeyRing(1)
    }
    return k.reset(session, cursor, keyRing)
}

func (k *KeyManager) InitFromSecretsFile(session string, cursor int, secretsfile string) error {
    keyRing, err := NewKeyRingFromFile(secretsfile)
    if err != nil {
        return err
    }
    return k.reset(session, cursor, keyRing)
}

func (k *KeyManager) InitFromString(session string, cursor int, secrets string) error {
    keyRing, err := NewKeyRingFromString(secrets)
    if err != nil {
        return err
    }
    return k.reset(session, cursor, keyRing)
}

func (k *KeyManager) Export(dir string) error {
    fileKeyStore := FileKeyStore{dir}
    return fileKeyStore.Save(k.session, k.keyRing)
}