From 7c3208de4c800def824d17391f72735180ab8353 Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 5 Aug 2020 22:00:23 +0200 Subject: [PATCH] Implement personal_signTransaction in socket server --- scripts/server.py | 17 +++++++++++++++-- src/signer/defaultsigner.py | 2 +- src/transaction.py | 17 +++++++++++++++-- test/sign.py | 2 -- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/scripts/server.py b/scripts/server.py index b079912..57df180 100644 --- a/scripts/server.py +++ b/scripts/server.py @@ -8,19 +8,21 @@ from jsonrpc.exceptions import * from signer import ReferenceSigner from keystore import ReferenceDatabase +from transaction import Transaction logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() db = None signer = None +chainId = 8995 def personal_new_account(p): if p.__class__.__name__ != 'list': e = JSONRPCInvalidParams() e.data = 'parameter must be list containing one string' - raise ValueError(e ) + raise ValueError(e) if len(p) != 1: e = JSONRPCInvalidParams() e.data = 'parameter must be list containing one string' @@ -32,11 +34,22 @@ def personal_new_account(p): 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 = { 'personal_newAccount': personal_new_account, + 'personal_signTransaction': personal_sign_transaction, } diff --git a/src/signer/defaultsigner.py b/src/signer/defaultsigner.py index 1039cf8..757cfb4 100644 --- a/src/signer/defaultsigner.py +++ b/src/signer/defaultsigner.py @@ -28,7 +28,7 @@ class ReferenceSigner(Signer): def signTransaction(self, tx, password=None): - s = tx.serialize() + s = tx.rlp_serialize() h = sha3.keccak_256() h.update(s) g = h.digest() diff --git a/src/transaction.py b/src/transaction.py index 3de3e51..22c99df 100644 --- a/src/transaction.py +++ b/src/transaction.py @@ -23,10 +23,10 @@ class Transaction: self.v = chainId self.r = 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') s = [ self.nonce, @@ -40,3 +40,16 @@ class Transaction: self.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(), + } diff --git a/test/sign.py b/test/sign.py index 814dcf4..1da4e8c 100644 --- a/test/sign.py +++ b/test/sign.py @@ -56,8 +56,6 @@ class TestSign(unittest.TestCase): t = Transaction(tx, 461, 8995) s = ReferenceSigner(self.getPk) z = s.signTransaction(t) - logg.debug('{}'.format(z.to_bytes())) - logg.debug('{}'.format(t.serialize().hex())) if __name__ == '__main__':