2020-08-04 23:41:31 +02:00
|
|
|
import logging
|
|
|
|
import sha3
|
|
|
|
|
|
|
|
from eth_keys import KeyAPI
|
|
|
|
from eth_keys.backends import NativeECCBackend
|
|
|
|
|
|
|
|
keys = KeyAPI(NativeECCBackend)
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Signer:
|
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2020-08-04 23:41:31 +02:00
|
|
|
def __init__(self, keyGetter):
|
|
|
|
self.keyGetter = keyGetter
|
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2020-08-04 23:41:31 +02:00
|
|
|
def signTransaction(self, tx):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2020-08-04 23:41:31 +02:00
|
|
|
class ReferenceSigner(Signer):
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
|
2020-08-04 23:41:31 +02:00
|
|
|
def __init__(self, keyGetter):
|
|
|
|
super(ReferenceSigner, self).__init__(keyGetter)
|
|
|
|
|
|
|
|
|
|
|
|
def signTransaction(self, tx):
|
|
|
|
s = tx.serialize()
|
|
|
|
h = sha3.keccak_256()
|
|
|
|
h.update(s)
|
|
|
|
g = h.digest()
|
|
|
|
k = keys.PrivateKey(self.keyGetter(tx.sender))
|
|
|
|
z = keys.ecdsa_sign(message_hash=g, private_key=k)
|
|
|
|
tx.v = (tx.v * 2) + 35 + z[64]
|
|
|
|
tx.r = z[:32]
|
|
|
|
tx.s = z[32:64]
|
|
|
|
return z
|