Create an Ethereum account using SHA256 of full key material for security.
(
get_key_response: GetKeyResponse | GetTlsKeyResponse,
)
| 44 | |
| 45 | |
| 46 | def to_account_secure( |
| 47 | get_key_response: GetKeyResponse | GetTlsKeyResponse, |
| 48 | ) -> LocalAccount: |
| 49 | """Create an Ethereum account using SHA256 of full key material for security.""" |
| 50 | if isinstance(get_key_response, GetTlsKeyResponse): |
| 51 | warnings.warn( |
| 52 | "to_account_secure: Please don't use getTlsKey method to get key, use getKey instead.", |
| 53 | DeprecationWarning, |
| 54 | stacklevel=2, |
| 55 | ) |
| 56 | try: |
| 57 | # Hash the complete key material with SHA256 |
| 58 | key_bytes = get_key_response.as_uint8array() |
| 59 | hashed_key = hashlib.sha256(key_bytes).digest() |
| 60 | return Account.from_key(hashed_key) # type: ignore[no-any-return] |
| 61 | except Exception as e: |
| 62 | raise RuntimeError( |
| 63 | "to_account_secure: missing SHA256 support, please upgrade your system" |
| 64 | ) from e |
| 65 | else: # GetKeyResponse |
| 66 | return Account.from_key(get_key_response.decode_key()) # type: ignore[no-any-return] |