github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/signing/sawtooth_signing/secp256k1.py (about) 1 # Copyright 2016, 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import binascii 17 import warnings 18 19 import secp256k1 20 21 from sawtooth_signing.core import SigningError 22 from sawtooth_signing.core import ParseError 23 24 from sawtooth_signing.core import PrivateKey 25 from sawtooth_signing.core import PublicKey 26 from sawtooth_signing.core import Context 27 28 __CONTEXTBASE__ = secp256k1.Base(ctx=None, flags=secp256k1.ALL_FLAGS) 29 __CTX__ = __CONTEXTBASE__.ctx 30 __PK__ = secp256k1.PublicKey(ctx=__CTX__) # Cache object to use as factory 31 32 33 class Secp256k1PrivateKey(PrivateKey): 34 def __init__(self, secp256k1_private_key): 35 self._private_key = secp256k1_private_key 36 37 def get_algorithm_name(self): 38 return "secp256k1" 39 40 def as_hex(self): 41 return binascii.hexlify(self.as_bytes()).decode() 42 43 def as_bytes(self): 44 return bytes(self._private_key.private_key) 45 46 @property 47 def secp256k1_private_key(self): 48 return self._private_key 49 50 @staticmethod 51 def from_hex(hex_str): 52 try: 53 priv = binascii.unhexlify(hex_str) 54 return Secp256k1PrivateKey(secp256k1.PrivateKey(priv, ctx=__CTX__)) 55 except Exception as e: 56 raise ParseError('Unable to parse hex private key: {}'.format(e)) 57 58 @staticmethod 59 def new_random(): 60 return Secp256k1PrivateKey(secp256k1.PrivateKey(ctx=__CTX__)) 61 62 63 class Secp256k1PublicKey(PublicKey): 64 def __init__(self, secp256k1_public_key): 65 self._public_key = secp256k1_public_key 66 67 @property 68 def secp256k1_public_key(self): 69 return self._public_key 70 71 def get_algorithm_name(self): 72 return "secp256k1" 73 74 def as_hex(self): 75 return binascii.hexlify(self.as_bytes()).decode() 76 77 def as_bytes(self): 78 with warnings.catch_warnings(): # squelch secp256k1 warning 79 warnings.simplefilter('ignore') 80 return self._public_key.serialize() 81 82 @staticmethod 83 def from_hex(hex_str): 84 try: 85 public_key = __PK__.deserialize(binascii.unhexlify(hex_str)) 86 87 return Secp256k1PublicKey( 88 secp256k1.PublicKey(public_key, ctx=__CTX__)) 89 except Exception as e: 90 raise ParseError('Unable to parse public key: {}'.format(e)) 91 92 93 class Secp256k1Context(Context): 94 def __init__(self): 95 self._ctx = __CTX__ 96 97 def get_algorithm_name(self): 98 return "secp256k1" 99 100 def sign(self, message, private_key): 101 try: 102 signature = private_key.secp256k1_private_key.ecdsa_sign(message) 103 signature = private_key.secp256k1_private_key \ 104 .ecdsa_serialize_compact(signature) 105 106 return signature.hex() 107 except Exception as e: 108 raise SigningError('Unable to sign message: {}'.format(str(e))) 109 110 def verify(self, signature, message, public_key): 111 try: 112 sig_bytes = bytes.fromhex(signature) 113 114 sig = public_key.secp256k1_public_key.ecdsa_deserialize_compact( 115 sig_bytes) 116 return public_key.secp256k1_public_key.ecdsa_verify(message, sig) 117 # pylint: disable=broad-except 118 except Exception: 119 return False 120 121 def new_random_private_key(self): 122 return Secp256k1PrivateKey.new_random() 123 124 def get_public_key(self, private_key): 125 return Secp256k1PublicKey(private_key.secp256k1_private_key.pubkey)