forked from Dstack-TEE/dstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathethereum.py
More file actions
66 lines (53 loc) · 2.37 KB
/
Copy pathethereum.py
File metadata and controls
66 lines (53 loc) · 2.37 KB
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
# SPDX-FileCopyrightText: © 2025 Phala Network <dstack@phala.network>
#
# SPDX-License-Identifier: Apache-2.0
"""Ethereum helpers for deriving accounts from dstack keys.
Use with ``dstack_sdk.DstackClient`` responses to create ``eth_account``
objects for signing and transacting.
"""
import hashlib
import warnings
from eth_account import Account
from eth_account.signers.local import LocalAccount
from .dstack_client import GetKeyResponse
from .dstack_client import GetTlsKeyResponse
def to_account(get_key_response: GetKeyResponse | GetTlsKeyResponse) -> LocalAccount:
"""Create an Ethereum account from DstackClient key response.
DEPRECATED: Use to_account_secure instead. This method has security concerns.
Current implementation uses raw key material without proper hashing.
Args:
get_key_response: Response from get_key() or get_tls_key()
Returns:
Account: Ethereum account object
"""
if isinstance(get_key_response, GetTlsKeyResponse):
warnings.warn(
"to_account: Please don't use getTlsKey method to get key, use getKey instead.",
DeprecationWarning,
stacklevel=2,
)
key_bytes = get_key_response.as_uint8array(32)
return Account.from_key(key_bytes) # type: ignore[no-any-return]
else: # GetKeyResponse
return Account.from_key(get_key_response.decode_key()) # type: ignore[no-any-return]
def to_account_secure(
get_key_response: GetKeyResponse | GetTlsKeyResponse,
) -> LocalAccount:
"""Create an Ethereum account using SHA256 of full key material for security."""
if isinstance(get_key_response, GetTlsKeyResponse):
warnings.warn(
"to_account_secure: Please don't use getTlsKey method to get key, use getKey instead.",
DeprecationWarning,
stacklevel=2,
)
try:
# Hash the complete key material with SHA256
key_bytes = get_key_response.as_uint8array()
hashed_key = hashlib.sha256(key_bytes).digest()
return Account.from_key(hashed_key) # type: ignore[no-any-return]
except Exception as e:
raise RuntimeError(
"to_account_secure: missing SHA256 support, please upgrade your system"
) from e
else: # GetKeyResponse
return Account.from_key(get_key_response.decode_key()) # type: ignore[no-any-return]