funga-eth

Ethereum implementation of the funga keystore and signer
Info | Log | Files | Refs | README | LICENSE

msg.py (2333B)


      1 # standard imports
      2 import os
      3 import logging
      4 import sys
      5 import json
      6 import argparse
      7 import getpass
      8 
      9 # external impors
     10 import coincurve
     11 from hexathon import strip_0x
     12 
     13 # local imports
     14 from funga.error import DecryptError
     15 from funga.eth.keystore.dict import DictKeystore
     16 from funga.eth.signer import EIP155Signer
     17 
     18 
     19 logging.basicConfig(level=logging.WARNING)
     20 logg = logging.getLogger()
     21 
     22 argparser = argparse.ArgumentParser()
     23 argparser.add_argument('-f', type=str, help='Keyfile to use for signing')
     24 argparser.add_argument('-z', action='store_true', help='zero-length password')
     25 argparser.add_argument('-v', action='store_true', help='be verbose')
     26 argparser.add_argument('-0', dest='nonl', action='store_true', help='no newline at end of output')
     27 argparser.add_argument('-b', '--binary', dest='binary', action='store_true', help='parse input as binary hex')
     28 argparser.add_argument('--validator', type=str, help='if set, will sign an ERC191 version 0 message')
     29 argparser.add_argument('--pure', action='store_true', help='Omit EIP191 transformation')
     30 argparser.add_argument('msg', type=str, help='Message to sign')
     31 args = argparser.parse_args()
     32 
     33 if args.v:
     34     logg.setLevel(logging.DEBUG)
     35 
     36 
     37 def main():
     38     passphrase = os.environ.get('WALLET_PASSPHRASE', os.environ.get('PASSPHRASE'))
     39     if args.z:
     40         passphrase = ''
     41     if passphrase == None:
     42         passphrase = getpass.getpass('decryption phrase: ')
     43    
     44     keystore = DictKeystore()
     45     address = keystore.import_keystore_file(args.f, password=passphrase)
     46 
     47     signer = EIP155Signer(keystore)
     48 
     49     msg = None
     50     if args.binary:
     51         hx = strip_0x(args.msg, pad=True)
     52         msg = bytes.fromhex(hx)
     53     else:
     54         msg = args.msg.encode('utf-8').hex()
     55 
     56     sig = None
     57     if args.pure:
     58         logg.info('signing pure message (no ERC191)')
     59         sig = signer.sign_pure(address, msg, password=passphrase)
     60     elif args.validator:
     61         logg.info('signing validator message (ERC191 version 0)')
     62         sig = signer.sign_validator_message(address, args.validator, msg, password=passphrase)
     63     else:
     64         logg.info('signing personal message (ERC191 version 0x45)')
     65         sig = signer.sign_ethereum_message(address, msg, password=passphrase)
     66 
     67     r = sig.hex()
     68     if not args.nonl:
     69         r += "\n"
     70     sys.stdout.write(r)
     71 
     72 
     73 if __name__ == '__main__':
     74     main()