Initial commit
This commit is contained in:
5
src/common.py
Normal file
5
src/common.py
Normal file
@@ -0,0 +1,5 @@
|
||||
def strip_hex_prefix(hx):
|
||||
if hx[:2] == '0x':
|
||||
return hx[2:]
|
||||
return hx
|
||||
|
||||
1
src/signer/__init__.py
Normal file
1
src/signer/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from signer.defaultsigner import ReferenceSigner, Signer
|
||||
39
src/signer/defaultsigner.py
Normal file
39
src/signer/defaultsigner.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import logging
|
||||
import sha3
|
||||
|
||||
from eth_keys import KeyAPI
|
||||
from eth_keys.backends import NativeECCBackend
|
||||
|
||||
keys = KeyAPI(NativeECCBackend)
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Signer:
|
||||
|
||||
def __init__(self, keyGetter):
|
||||
self.keyGetter = keyGetter
|
||||
|
||||
def signTransaction(self, tx):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class ReferenceSigner(Signer):
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
42
src/transaction.py
Normal file
42
src/transaction.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import logging
|
||||
import binascii
|
||||
|
||||
from rlp import encode as rlp_encode
|
||||
|
||||
from common import strip_hex_prefix
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
class Transaction:
|
||||
|
||||
def __init__(self, tx, nonce, chainId=1):
|
||||
|
||||
to = binascii.unhexlify(strip_hex_prefix(tx['to']))
|
||||
data = binascii.unhexlify(strip_hex_prefix(tx['data']))
|
||||
|
||||
self.nonce = nonce
|
||||
self.gas_price = int(tx['gasPrice'])
|
||||
self.start_gas = int(tx['gas'])
|
||||
self.to = to
|
||||
self.value = int(tx['value'])
|
||||
self.data = data
|
||||
self.v = chainId
|
||||
self.r = 0
|
||||
self.s = 0
|
||||
self.sender = tx['from']
|
||||
|
||||
|
||||
def serialize(self):
|
||||
b = self.nonce.to_bytes(8, byteorder='little')
|
||||
s = [
|
||||
self.nonce,
|
||||
self.gas_price,
|
||||
self.start_gas,
|
||||
self.to,
|
||||
self.value,
|
||||
self.data,
|
||||
self.v,
|
||||
self.r,
|
||||
self.s,
|
||||
]
|
||||
return rlp_encode(s)
|
||||
Reference in New Issue
Block a user