Test implementing mocked HW wallet via Web3 signer; Cleanup.

pull/1664/head
Kieran Prasch 2020-03-06 14:25:36 -08:00
parent 9c1aec73af
commit 4e17bbe70b
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
3 changed files with 23 additions and 21 deletions

View File

@ -36,7 +36,6 @@ from constant_sorrow.constants import (
READ_ONLY_INTERFACE
)
from eth_tester import EthereumTester
from eth_tester.exceptions import TransactionFailed
from eth_utils import to_checksum_address
from twisted.logger import Logger
from web3 import Web3, WebsocketProvider, HTTPProvider, IPCProvider, middleware
@ -191,18 +190,10 @@ class BlockchainInterface:
self._provider = provider
self._provider_process = provider_process
self.w3 = NO_BLOCKCHAIN_CONNECTION
self.client = NO_BLOCKCHAIN_CONNECTION # type: Web3Client
self.client = NO_BLOCKCHAIN_CONNECTION # type: Web3Client
self.transacting_power = READ_ONLY_INTERFACE
self.is_light = light
try:
gas_strategy = self.GAS_STRATEGIES[gas_strategy]
except KeyError:
if gas_strategy and not callable(gas_strategy):
raise ValueError(f"{gas_strategy} must be callable to be a valid gas strategy.")
else:
gas_strategy = self.GAS_STRATEGIES[self.DEFAULT_GAS_STRATEGY]
self.gas_strategy = gas_strategy
self.gas_strategy = self.get_gas_strategy(gas_strategy)
def __repr__(self):
r = '{name}({uri})'.format(name=self.__class__.__name__, uri=self.provider_uri)
@ -227,6 +218,17 @@ class BlockchainInterface:
return False
return self.client.is_connected
@classmethod
def get_gas_strategy(cls, gas_strategy: Union[str, Callable]) -> Callable:
try:
gas_strategy = cls.GAS_STRATEGIES[gas_strategy]
except KeyError:
if gas_strategy and not callable(gas_strategy):
raise ValueError(f"{gas_strategy} must be callable to be a valid gas strategy.")
else:
gas_strategy = cls.GAS_STRATEGIES[cls.DEFAULT_GAS_STRATEGY]
return gas_strategy
def attach_middleware(self):
# For use with Proof-Of-Authority test-blockchains
@ -452,7 +454,7 @@ class BlockchainInterface:
cost_wei = price * unsigned_transaction['gas']
cost = Web3.fromWei(cost_wei, 'gwei')
if self.transacting_power.device:
if self.transacting_power.is_device:
emitter.message(f'Confirm transaction {transaction_name} on hardware wallet... ({cost} gwei @ {price})', color='yellow')
signed_raw_transaction = self.transacting_power.sign_transaction(unsigned_transaction)

View File

@ -20,15 +20,15 @@ import inspect
from typing import List, Tuple, Optional
from hexbytes import HexBytes
from nucypher.keystore import keypairs
from nucypher.keystore.keypairs import SigningKeypair, DecryptingKeypair
from umbral import pre
from umbral.keys import UmbralPublicKey, UmbralPrivateKey, UmbralKeyingMaterial
from nucypher.blockchain.eth.decorators import validate_checksum_address
from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory, BlockchainInterface
from nucypher.blockchain.eth.signers import Signer
from nucypher.blockchain.eth.signers import Web3Signer
from nucypher.blockchain.eth.signers import Signer, Web3Signer
from nucypher.datastore import keypairs
from nucypher.datastore.keypairs import SigningKeypair, DecryptingKeypair
class PowerUpError(TypeError):
@ -201,11 +201,11 @@ class TransactingPower(CryptoPowerUp):
raise self.AccountLocked("Failed to unlock account {}".format(self.__account))
return self._signer.sign_message(account=self.__account, message=message)
def sign_transaction(self, transaction: dict) -> HexBytes:
def sign_transaction(self, transaction_dict: dict) -> HexBytes:
"""Signs the transaction with the private key of the TransactingPower."""
if not self.is_unlocked:
raise self.AccountLocked("Failed to unlock account {}".format(self.__account))
return self._signer.sign_transaction(transaction_dict=transaction)
return self._signer.sign_transaction(transaction_dict=transaction_dict)
class KeyPairBasedPower(CryptoPowerUp):

View File

@ -72,12 +72,12 @@ def test_transacting_power_sign_transaction(testerchain):
# Test a signature without unlocking the account
with pytest.raises(power.AccountLocked):
power.sign_transaction(transaction=transaction_dict)
power.sign_transaction(transaction_dict=transaction_dict)
# Sign
power.activate()
assert power.is_unlocked is True
signed_transaction = power.sign_transaction(transaction=transaction_dict)
signed_transaction = power.sign_transaction(transaction_dict=transaction_dict)
# Demonstrate that the transaction is valid RLP encoded.
from eth_account._utils.transactions import Transaction
@ -89,12 +89,12 @@ def test_transacting_power_sign_transaction(testerchain):
del transaction_dict['gas']
del transaction_dict['nonce']
with pytest.raises(TypeError):
power.sign_transaction(transaction=transaction_dict)
power.sign_transaction(transaction_dict=transaction_dict)
# Try signing with a re-locked account.
power.lock_account()
with pytest.raises(power.AccountLocked):
power.sign_transaction(transaction=transaction_dict)
power.sign_transaction(transaction_dict=transaction_dict)
power.unlock_account(password=INSECURE_DEVELOPMENT_PASSWORD)
assert power.is_unlocked is True