-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from typing import List, Optional, Set, Tuple | ||
|
||
from eth_typing import ChecksumAddress | ||
from ledgereth.accounts import get_account_by_path | ||
from ledgereth.comms import init_dongle | ||
from ledgereth.exceptions import LedgerLocked, LedgerNotFound | ||
|
||
from gnosis.eth.eip712 import eip712_encode | ||
from gnosis.safe import SafeTx | ||
from gnosis.safe.signatures import signature_to_bytes | ||
|
||
from safe_cli.operators.hw_accounts.ledger_account import LedgerAccount | ||
|
||
|
||
class LedgerManager: | ||
|
||
LEDGER_SEARCH_DEEP = 10 | ||
|
||
def __init__(self): | ||
self.accounts: Set[LedgerAccount] = set() | ||
try: | ||
self.dongle = init_dongle() | ||
except LedgerNotFound: | ||
|
||
raise IOError | ||
|
||
def get_accounts( | ||
self, legacy_account: Optional[bool] = False | ||
) -> List[Tuple[ChecksumAddress, str]]: | ||
""" | ||
:param legacy_account: | ||
:return: a list of tuples with address and derivation path | ||
""" | ||
accounts = [] | ||
for i in range(self.LEDGER_SEARCH_DEEP): | ||
if legacy_account: | ||
path_string = f"44'/60'/0'/{i}" | ||
else: | ||
path_string = f"44'/60'/{i}'/0/0" | ||
try: | ||
account = get_account_by_path(path_string, self.dongle) | ||
except LedgerLocked as ledger_error: | ||
print(f"Ledger exception: {ledger_error}") | ||
accounts.append((account.address, account.path)) | ||
return accounts | ||
|
||
def add_account(self, derivation_path: str): | ||
try: | ||
account = get_account_by_path(derivation_path, self.dongle) | ||
except LedgerLocked as ledger_error: | ||
print(f"Ledger exception: {ledger_error}") | ||
self.accounts.add(LedgerAccount(account.path, account.address, self.dongle)) | ||
|
||
@staticmethod | ||
def safe_tx_hash_sign(safe_tx: SafeTx, account: LedgerAccount) -> bytes: | ||
""" | ||
{bytes32 r}{bytes32 s}{uint8 v} | ||
:param private_key: | ||
:return: Signature | ||
""" | ||
encode_hash = eip712_encode(safe_tx.eip712_structured_data) | ||
v, r, s = account.signHash(encode_hash[1], encode_hash[2]) | ||
signature = signature_to_bytes(v, r, s) | ||
# Insert signature sorted | ||
if account.address not in safe_tx.signers: | ||
new_owners = safe_tx.signers + [account.address] | ||
new_owner_pos = sorted(new_owners, key=lambda x: int(x, 16)).index( | ||
account.address | ||
) | ||
safe_tx.signatures = ( | ||
safe_tx.signatures[: 65 * new_owner_pos] | ||
+ signature | ||
+ safe_tx.signatures[65 * new_owner_pos :] | ||
) | ||
return safe_tx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters