77from crypto .identity .private_key import PrivateKey
88from crypto .identity .public_key import PublicKey
99
10+ MESSAGE_PREFIX = b"\x19 Ethereum Signed Message:\n "
11+
1012class Message (object ):
1113 public_key : bytes
1214 message : bytes
@@ -62,9 +64,9 @@ def sign(cls, message: Union[bytes, str], passphrase: Union[bytes, str]):
6264 private_key = PrivateKey .from_passphrase (passphrase )
6365 public_key = private_key .public_key
6466
65- transaction_signature = private_key .sign (message )
67+ transaction_signature = private_key .sign (MESSAGE_PREFIX + ( str ( len ( message ))). encode () + message )
6668
67- signature_v = bytes ([transaction_signature [0 ]]).hex ()
69+ signature_v = bytes ([int ( transaction_signature [0 ]) + 27 ]).hex ()
6870 signature_r = transaction_signature [1 :33 ].hex ()
6971 signature_s = transaction_signature [33 :].hex ()
7072
@@ -84,13 +86,15 @@ def verify(self):
8486 """
8587
8688 signature = unhexlify (self .signature )
87- message_hash = keccak .new (data = self .message , digest_bits = 256 ).digest ()
89+
90+ message = MESSAGE_PREFIX + (str (len (self .message ))).encode () + self .message
91+ message_hash = keccak .new (data = message , digest_bits = 256 ).digest ()
8892
8993 signature_r = signature [0 :32 ]
9094 signature_s = signature [32 :64 ]
9195 signature_v = signature [64 ]
9296
93- signature = signature_r + signature_s + bytes ([signature_v ])
97+ signature = signature_r + signature_s + bytes ([signature_v - 27 ])
9498
9599 public_key = PublicKey .recover (message_hash , signature )
96100
0 commit comments