-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.py
172 lines (148 loc) · 5.21 KB
/
common.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
167
168
169
170
171
172
import logging
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Dict, Optional
from coincurve.keys import PrivateKey
from typing_extensions import deprecated
from aleph.sdk.conf import settings
from aleph.sdk.utils import enum_as_str
logger = logging.getLogger(__name__)
def get_verification_buffer(message: Dict) -> bytes:
"""
Returns the verification buffer that aleph.im nodes use to verify the signature of a message.
Note:
The verification buffer is a string of the following format:
b"{chain}\\n{sender}\\n{type}\\n{item_hash}"
Args:
message: Message to get the verification buffer for
Returns:
bytes: Verification buffer
"""
# Convert Enum values to strings
return "\n".join(
(
enum_as_str(message["chain"]) or "",
message["sender"],
enum_as_str(message["type"]) or "",
message["item_hash"],
)
).encode()
def get_public_key(private_key):
privkey = PrivateKey(private_key)
return privkey.public_key.format()
class BaseAccount(ABC):
CHAIN: str
CURVE: str
private_key: bytes
def _setup_sender(self, message: Dict) -> Dict:
"""
Set the sender of the message as the account's public key.
If a sender is already specified, check that it matches the account's public key.
Args:
message: Message to add the sender to
Returns:
Dict: Message with the sender set
"""
if not message.get("sender"):
message["sender"] = self.get_address()
return message
elif message["sender"] == self.get_address():
return message
else:
raise ValueError("Message sender does not match the account's public key.")
async def sign_message(self, message: Dict) -> Dict:
"""
Returns a signed message from an aleph.im message.
Args:
message: Message to sign
Returns:
Dict: Signed message
"""
message = self._setup_sender(message)
signature = await self.sign_raw(get_verification_buffer(message))
message["signature"] = "0x" + signature.hex()
return message
@abstractmethod
async def sign_raw(self, buffer: bytes) -> bytes:
"""
Returns a signed message from a raw buffer.
Args:
buffer: Buffer to sign
Returns:
bytes: Signature in preferred format
"""
raise NotImplementedError
@abstractmethod
def get_address(self) -> str:
"""
Returns the account's displayed address.
"""
raise NotImplementedError
@abstractmethod
def get_public_key(self) -> str:
"""
Returns the account's public key.
"""
raise NotImplementedError
@deprecated("This method will be moved to its own module `aleph.sdk.encryption`")
async def encrypt(self, content: bytes) -> bytes:
"""
Encrypts a message using the account's public key.
Args:
content: Content bytes to encrypt
Returns:
bytes: Encrypted content as bytes
"""
try:
from ecies import encrypt
except ImportError:
raise ImportError(
"Install `eciespy` or `aleph-sdk-python[encryption]` to use this method"
)
if self.CURVE == "secp256k1":
value: bytes = encrypt(self.get_public_key(), content)
return value
else:
raise NotImplementedError
@deprecated("This method will be moved to its own module `aleph.sdk.encryption`")
async def decrypt(self, content: bytes) -> bytes:
"""
Decrypts a message using the account's private key.
Args:
content: Content bytes to decrypt
Returns:
bytes: Decrypted content as bytes
"""
try:
from ecies import decrypt
except ImportError:
raise ImportError(
"Install `eciespy` or `aleph-sdk-python[encryption]` to use this method"
)
if self.CURVE == "secp256k1":
value: bytes = decrypt(self.private_key, content)
return value
else:
raise NotImplementedError
# Start of the ugly stuff
def generate_key() -> bytes:
privkey = PrivateKey()
return privkey.secret
def get_fallback_private_key(path: Optional[Path] = None) -> bytes:
path = path or settings.PRIVATE_KEY_FILE
private_key: bytes
if path.exists() and path.stat().st_size > 0:
private_key = path.read_bytes()
else:
private_key = generate_key()
path.parent.mkdir(exist_ok=True, parents=True)
path.write_bytes(private_key)
default_key_path = path.parent / "default.key"
# If the symlink exists but does not point to a file, delete it.
if default_key_path.is_symlink() and not default_key_path.resolve().exists():
default_key_path.unlink()
logger.warning("The symlink to the private key is broken")
# Create a symlink to use this key by default
if not default_key_path.exists():
default_key_path.symlink_to(path)
return private_key