message.py (1313B)
1 # external imports 2 import sha3 3 import logging 4 5 # local imports 6 from .encoding import to_checksum_address 7 8 logg = logging.getLogger(__name__) 9 10 def to_digest(data): 11 h = sha3.keccak_256() 12 h.update(data) 13 z = h.digest() 14 return z 15 16 # ERC191 - version 0x00 17 def to_validator_message(data, validator, digest=False): 18 a = to_checksum_address(validator) 19 v = bytes.fromhex(a) 20 r = b'\x19\x00' + v + data 21 logg.debug('raw message data: ' + r.hex()) 22 if digest: 23 r = to_digest(r) 24 logg.debug('sign validator message digest: {}'.format(r.hex())) 25 return r 26 27 28 # ERC191/ERC712 - version 0x01 29 def to_typed_message(data, domain, digest=False): 30 assert len(data) == 32 31 assert len(domain) == 32 32 r = b'\x19\x01' + domain + data 33 logg.debug('raw message data: ' + r.hex()) 34 if digest: 35 r = to_digest(r) 36 logg.debug('sign typed message digest: {}'.format(r.hex())) 37 return r 38 39 40 # ERC191 - version 0x45 41 def to_personal_message(data, digest=False): 42 ethereumed_message_header = b'\x19\x45' + 'thereum Signed Message:\n{}'.format(len(data)).encode('utf-8') 43 r = ethereumed_message_header + data 44 logg.debug('raw message data: ' + r.hex()) 45 if digest: 46 r = to_digest(r) 47 logg.debug('sign personal message digest: {}'.format(r.hex())) 48 return r