from dydx_constants import COLLATERAL_ASSET_ID_BY_NETWORK_ID
from starkex.helpers import deserialize_signature
from starkex.helpers import serialize_signature
from starkex.starkex_resources.proxy import sign
from starkex.starkex_resources.proxy import verify
class Signable(object):
def __init__(self, network_id, message):
self.network_id = network_id
self._message = message
self._hash = None
if not COLLATERAL_ASSET_ID_BY_NETWORK_ID[self.network_id]:
raise ValueError(
'Unknown network ID or unknown collateral asset for network: '
'{}'.format(network_id),
)
@property
def hash(self):
if self._hash is None:
self._hash = self._calculate_hash()
return self._hash
def sign(self, private_key_hex):
r, s = sign(self.hash, int(private_key_hex, 16))
return serialize_signature(r, s)
def verify_signature(self, signature_hex, public_key_hex):
r, s = deserialize_signature(signature_hex)
return verify(self.hash, r, s, int(public_key_hex, 16))
def _calculate_hash(self):
raise NotImplementedError