Implement personal_signTransaction in socket server

This commit is contained in:
nolash 2020-08-05 22:00:23 +02:00
parent acaff79ad2
commit 7c3208de4c
Signed by: lash
GPG Key ID: 93EC1C676274C889
4 changed files with 31 additions and 7 deletions

View File

@ -8,19 +8,21 @@ from jsonrpc.exceptions import *
from signer import ReferenceSigner from signer import ReferenceSigner
from keystore import ReferenceDatabase from keystore import ReferenceDatabase
from transaction import Transaction
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
db = None db = None
signer = None signer = None
chainId = 8995
def personal_new_account(p): def personal_new_account(p):
if p.__class__.__name__ != 'list': if p.__class__.__name__ != 'list':
e = JSONRPCInvalidParams() e = JSONRPCInvalidParams()
e.data = 'parameter must be list containing one string' e.data = 'parameter must be list containing one string'
raise ValueError(e ) raise ValueError(e)
if len(p) != 1: if len(p) != 1:
e = JSONRPCInvalidParams() e = JSONRPCInvalidParams()
e.data = 'parameter must be list containing one string' e.data = 'parameter must be list containing one string'
@ -32,11 +34,22 @@ def personal_new_account(p):
r = db.new(p[0]) r = db.new(p[0])
return [r] return r
def personal_sign_transaction(p):
t = Transaction(p[0], 0, 8995)
z = signer.signTransaction(t, p[1])
raw_signed_tx = t.rlp_serialize()
return {
'raw': '0x' + raw_signed_tx.hex(),
'tx': t.serialize(),
}
methods = { methods = {
'personal_newAccount': personal_new_account, 'personal_newAccount': personal_new_account,
'personal_signTransaction': personal_sign_transaction,
} }

View File

@ -28,7 +28,7 @@ class ReferenceSigner(Signer):
def signTransaction(self, tx, password=None): def signTransaction(self, tx, password=None):
s = tx.serialize() s = tx.rlp_serialize()
h = sha3.keccak_256() h = sha3.keccak_256()
h.update(s) h.update(s)
g = h.digest() g = h.digest()

View File

@ -23,10 +23,10 @@ class Transaction:
self.v = chainId self.v = chainId
self.r = 0 self.r = 0
self.s = 0 self.s = 0
self.sender = tx['from'] self.sender = strip_hex_prefix(tx['from'])
def serialize(self): def rlp_serialize(self):
b = self.nonce.to_bytes(8, byteorder='little') b = self.nonce.to_bytes(8, byteorder='little')
s = [ s = [
self.nonce, self.nonce,
@ -40,3 +40,16 @@ class Transaction:
self.s, self.s,
] ]
return rlp_encode(s) return rlp_encode(s)
def serialize(self):
return {
'nonce': '0x' + hex(self.nonce),
'gasPrice': '0x' + hex(self.gas_price),
'gas': '0x' + hex(self.start_gas),
'to': '0x' + self.to.hex(),
'value': '0x' + hex(self.value),
'data': '0x' + self.data.hex(),
'v': '0x' + hex(self.v),
'r': '0x' + self.r.hex(),
's': '0x' + self.s.hex(),
}

View File

@ -56,8 +56,6 @@ class TestSign(unittest.TestCase):
t = Transaction(tx, 461, 8995) t = Transaction(tx, 461, 8995)
s = ReferenceSigner(self.getPk) s = ReferenceSigner(self.getPk)
z = s.signTransaction(t) z = s.signTransaction(t)
logg.debug('{}'.format(z.to_bytes()))
logg.debug('{}'.format(t.serialize().hex()))
if __name__ == '__main__': if __name__ == '__main__':