-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathbase.py
166 lines (122 loc) · 5.13 KB
/
base.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
import logging
from .masterpassword import MasterPassword
from .interfaces import KeyInterface, ConfigInterface, EncryptedKeyInterface
from .ram import InRamStore
from .sqlite import SQLiteStore
from .exceptions import KeyAlreadyInStoreException
log = logging.getLogger(__name__)
# Configuration
class InRamConfigurationStore(InRamStore, ConfigInterface):
"""A simple example that stores configuration in RAM.
Internally, this works by simply inheriting
:class:`graphenestorage.ram.InRamStore`. The interface is defined in
:class:`graphenestorage.interfaces.ConfigInterface`.
"""
pass
class SqliteConfigurationStore(SQLiteStore, ConfigInterface):
"""This is the configuration storage that stores key/value
pairs in the `config` table of the SQLite3 database.
Internally, this works by simply inheriting
:class:`graphenestorage.sqlite.SQLiteStore`. The interface is defined
in :class:`graphenestorage.interfaces.ConfigInterface`.
"""
#: The table name for the configuration
__tablename__ = "config"
#: The name of the 'key' column
__key__ = "key"
#: The name of the 'value' column
__value__ = "value"
# Keys
class InRamPlainKeyStore(InRamStore, KeyInterface):
"""A simple in-RAM Store that stores keys unencrypted in RAM
Internally, this works by simply inheriting
:class:`graphenestorage.ram.InRamStore`. The interface is defined in
:class:`graphenestorage.interfaces.KeyInterface`.
"""
def getPublicKeys(self):
return [k for k, v in self.items()]
def getPrivateKeyForPublicKey(self, pub):
return self.get(str(pub), None)
def add(self, wif, pub):
if str(pub) in self:
raise KeyAlreadyInStoreException
self[str(pub)] = str(wif)
def delete(self, pub):
InRamStore.delete(self, str(pub))
class SqlitePlainKeyStore(SQLiteStore, KeyInterface):
"""This is the key storage that stores the public key and the
**unencrypted** private key in the `keys` table in the SQLite3
database.
Internally, this works by simply inheriting
:class:`graphenestorage.ram.InRamStore`. The interface is defined in
:class:`graphenestorage.interfaces.KeyInterface`.
"""
#: The table name for the configuration
__tablename__ = "keys"
#: The name of the 'key' column
__key__ = "pub"
#: The name of the 'value' column
__value__ = "wif"
def getPublicKeys(self):
return [k for k, v in self.items()]
def getPrivateKeyForPublicKey(self, pub):
return self[pub]
def add(self, wif, pub):
if str(pub) in self:
raise KeyAlreadyInStoreException
self[str(pub)] = str(wif)
def delete(self, pub):
SQLiteStore.delete(self, str(pub))
def is_encrypted(self):
"""Returns False, as we are not encrypted here"""
return False
class KeyEncryption(MasterPassword, EncryptedKeyInterface):
"""This is an interface class that provides the methods required for
EncryptedKeyInterface and links them to the MasterPassword-provided
functionatlity, accordingly.
"""
def __init__(self, *args, **kwargs):
EncryptedKeyInterface.__init__(self, *args, **kwargs)
MasterPassword.__init__(self, *args, **kwargs)
# Interface to deal with encrypted keys
def getPublicKeys(self):
return [k for k, v in self.items()]
def getPrivateKeyForPublicKey(self, pub):
wif = self.get(str(pub), None)
if wif:
return self.decrypt(wif) # From Masterpassword
def add(self, wif, pub):
if str(pub) in self:
raise KeyAlreadyInStoreException
self[str(pub)] = self.encrypt(str(wif)) # From Masterpassword
def is_encrypted(self):
return True
class InRamEncryptedKeyStore(InRamStore, KeyEncryption):
"""An in-RAM Store that stores keys **encrypted** in RAM.
Internally, this works by simply inheriting
:class:`graphenestorage.ram.InRamStore`. The interface is defined in
:class:`graphenestorage.interfaces.KeyInterface`.
.. note:: This module also inherits
:class:`graphenestorage.masterpassword.MasterPassword` which offers
additional methods and deals with encrypting the keys.
"""
def __init__(self, *args, **kwargs):
InRamStore.__init__(self, *args, **kwargs)
KeyEncryption.__init__(self, *args, **kwargs)
class SqliteEncryptedKeyStore(SQLiteStore, KeyEncryption):
"""This is the key storage that stores the public key and the
**encrypted** private key in the `keys` table in the SQLite3 database.
Internally, this works by simply inheriting
:class:`graphenestorage.ram.InRamStore`. The interface is defined in
:class:`graphenestorage.interfaces.KeyInterface`.
.. note:: This module also inherits
:class:`graphenestorage.masterpassword.MasterPassword` which offers
additional methods and deals with encrypting the keys.
"""
__tablename__ = "keys"
__key__ = "pub"
__value__ = "wif"
def __init__(self, *args, **kwargs):
SQLiteStore.__init__(self, *args, **kwargs)
KeyEncryption.__init__(self, *args, **kwargs)