Created
          April 5, 2018 10:31 
        
      - 
      
- 
        Save nakov/b0dccf6cea1a5fe7ca52cf6f4f0c5eaa to your computer and use it in GitHub Desktop. 
    ECDSA with sec256k1 Example in Python - Generate Keys, Sign, Verify
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import eth_keys, eth_utils, binascii, os | |
| # privKey = eth_keys.keys.PrivateKey(os.urandom(32)) | |
| privKey = eth_keys.keys.PrivateKey(binascii.unhexlify( | |
| '97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a')) | |
| pubKey = privKey.public_key | |
| pubKeyCompressed = '0' + str(2 + int(pubKey) % 2) + str(pubKey)[2:66] | |
| address = pubKey.to_checksum_address() | |
| print('Private key (64 hex digits):', privKey) | |
| print('Public key (plain, 128 hex digits):', pubKey) | |
| print('Public key (compressed, 66 hex digits):', pubKeyCompressed) | |
| print('Signer address:', address) | |
| print() | |
| msg = b'Message for signing' | |
| signature = privKey.sign_msg(msg) | |
| print('Msg:', msg) | |
| print('Msg hash:', binascii.hexlify(eth_utils.keccak(msg))) | |
| print('Signature: [v = {0}, r = {1}, s = {2}]'.format( | |
| hex(signature.v), hex(signature.r), hex(signature.s))) | |
| print('Signature (130 hex digits):', signature) | |
| print() | |
| msg = b'Message for signing' | |
| msgSigner = '0xa44f70834a711F0DF388ab016465f2eEb255dEd0' | |
| signature = eth_keys.keys.Signature(binascii.unhexlify( | |
| '6f0156091cbe912f2d5d1215cc3cd81c0963c8839b93af60e0921b61a19c54300c71006dd93f3508c432daca21db0095f4b16542782b7986f48a5d0ae3c583d401')) | |
| signerRecoveredPubKey = signature.recover_public_key_from_msg(msg) | |
| signerRecoveredAddress = signerRecoveredPubKey.to_checksum_address() | |
| print('Signer public key (128 hex digits):', signerRecoveredPubKey) | |
| print('Signer address:', signerRecoveredAddress) | |
| print('Signature valid?:', signerRecoveredAddress == msgSigner) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
It should work like this: