def test_sign_via_private_key(self): signer = SignWithKey(GANACHE_PRIVATE_KEY) action_signer = SignApiKeyAction(signer, NETWORK_ID_MAINNET) signature = action_signer.sign(GANACHE_ADDRESS, **PARAMS) assert action_signer.verify( signature, GANACHE_ADDRESS, **PARAMS, ) assert signature == EXPECTED_SIGNATURE
def __init__( self, host, api_timeout=3000, # TODO: Actually use this. api_private_key=None, stark_private_key=None, eth_private_key=None, web3=None, web3_account=None, web3_provider=None, ): # Remove trailing '/' if present, from host. if host.endswith('/'): host = host[:-1] self.host = host self.api_timeout = api_timeout self.api_private_key = api_private_key self.stark_private_key = stark_private_key self.web3 = None self.eth_signer = None self.default_address = None if web3 is not None or web3_provider is not None: self.web3 = web3 or Web3(web3_provider) self.eth_signer = SignWithWeb3(self.web3) self.default_address = self.web3.eth.defaultAccount or None if eth_private_key is not None or web3_account is not None: # May override web3 or web3_provider configuration. key = eth_private_key or web3_account.key self.eth_signer = SignWithKey(key) self.default_address = self.eth_signer.address # Initialize the public module. Other modules are initialized on # demand, if the necessary configuration options were provided. self._public = Public(host) self._private = None self._api_keys = None self._eth = None self._onboarding = None # Derive the public keys. if stark_private_key is not None: self.stark_public_key = private_key_to_public_hex( stark_private_key, ) else: self.stark_public_key = None if api_private_key is not None: self.api_public_key = private_key_to_public_hex(api_private_key, ) else: self.api_public_key = None
def test_sign_via_account(self): web3 = Web3(None) web3_account = web3.eth.account.create() signer = SignWithKey(web3_account.key) action_signer = SignApiKeyAction(signer, NETWORK_ID_MAINNET) signature = action_signer.sign(signer.address, **PARAMS) assert action_signer.verify( signature, signer.address, **PARAMS, )
def test_sign_via_private_key_no_expiration(self): signer = SignWithKey(MOCK_KEY) signature = sign_off_chain_action( signer, signer.address, MOCK_ACTION, ) assert off_chain_action_signature_is_valid( signature, signer.address, MOCK_ACTION, )
def test_sign_via_private_key(self): signer = SignWithKey(GANACHE_PRIVATE_KEY) action_signer = SignOnboardingAction(signer, NETWORK_ID_MAINNET) signature = action_signer.sign( GANACHE_ADDRESS, action=OFF_CHAIN_ONBOARDING_ACTION, ) assert action_signer.verify( signature, GANACHE_ADDRESS, action=OFF_CHAIN_ONBOARDING_ACTION, ) assert signature == EXPECTED_SIGNATURE
def test_sign_via_account_no_expiration(self): web3 = Web3(None) web3_account = web3.eth.account.create() signer = SignWithKey(web3_account.key) signer_address = web3_account.address signature = sign_off_chain_action( signer, signer_address, MOCK_ACTION, ) assert off_chain_action_signature_is_valid( signature, signer_address, MOCK_ACTION, )
def test_sign_via_account(self): web3 = Web3(None) web3_account = web3.eth.account.create() signer = SignWithKey(web3_account.key) action_signer = SignOnboardingAction(signer, NETWORK_ID_MAINNET) signature = action_signer.sign( signer.address, action=OFF_CHAIN_ONBOARDING_ACTION, ) assert action_signer.verify( signature, signer.address, action=OFF_CHAIN_ONBOARDING_ACTION, )
def __init__( self, host, api_timeout=3000, # TODO: Actually use this. default_ethereum_address=None, eth_private_key=None, eth_send_options=None, network_id=None, stark_private_key=None, stark_public_key=None, stark_public_key_y_coordinate=None, web3=None, web3_account=None, web3_provider=None, api_key_credentials=None, crypto_c_exports_path=None, ): # Remove trailing '/' if present, from host. if host.endswith('/'): host = host[:-1] self.host = host self.api_timeout = api_timeout self.eth_send_options = eth_send_options or {} self.stark_private_key = stark_private_key self.api_key_credentials = api_key_credentials self.stark_public_key_y_coordinate = stark_public_key_y_coordinate self.web3 = None self.eth_signer = None self.default_address = None self.network_id = None if crypto_c_exports_path is not None: get_cpp_lib(crypto_c_exports_path) if web3 is not None or web3_provider is not None: if isinstance(web3_provider, str): web3_provider = Web3.HTTPProvider(web3_provider) self.web3 = web3 or Web3(web3_provider) self.eth_signer = SignWithWeb3(self.web3) self.default_address = self.web3.eth.defaultAccount or None self.network_id = self.web3.net.version if eth_private_key is not None or web3_account is not None: # May override web3 or web3_provider configuration. key = eth_private_key or web3_account.key self.eth_signer = SignWithKey(key) self.default_address = self.eth_signer.address self.default_address = default_ethereum_address or self.default_address self.network_id = int(network_id or self.network_id or NETWORK_ID_MAINNET) # Initialize the public module. Other modules are initialized on # demand, if the necessary configuration options were provided. self._public = Public(host) self._private = None self._api_keys = None self._eth = None self._onboarding = None # Derive the public keys. if stark_private_key is not None: self.stark_public_key, self.stark_public_key_y_coordinate = ( private_key_to_public_key_pair_hex(stark_private_key)) if (stark_public_key is not None and stark_public_key != self.stark_public_key): raise ValueError('STARK public/private key mismatch') if (stark_public_key_y_coordinate is not None and stark_public_key_y_coordinate != self.stark_public_key_y_coordinate): raise ValueError('STARK public/private key mismatch (y)') else: self.stark_public_key = stark_public_key self.stark_public_key_y_coordinate = stark_public_key_y_coordinate # Generate default API key credentials if needed and possible. if (self.eth_signer and self.default_address and not self.api_key_credentials): # This may involve a web3 call, so recover on failure. try: self.api_key_credentials = ( self.onboarding.recover_default_api_key_credentials( ethereum_address=self.default_address, )) except Exception as e: print( 'Warning: Failed to derive default API key credentials:', e, )