github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/signing/sawtooth_signing/__init__.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  from sawtooth_signing.core import NoSuchAlgorithmError
    16  from sawtooth_signing.core import ParseError
    17  from sawtooth_signing.core import SigningError
    18  
    19  from sawtooth_signing.secp256k1 import Secp256k1Context
    20  
    21  
    22  class Signer:
    23      """A convenient wrapper of Context and PrivateKey
    24      """
    25  
    26      def __init__(self, context, private_key):
    27          """
    28          """
    29          self._context = context
    30          self._private_key = private_key
    31          self._public_key = None
    32  
    33      def sign(self, message):
    34          """Signs the given message
    35  
    36          Args:
    37              message (bytes): the message bytes
    38  
    39          Returns:
    40              The signature in a hex-encoded string
    41  
    42          Raises:
    43              SigningError: if any error occurs during the signing process
    44          """
    45          return self._context.sign(message, self._private_key)
    46  
    47      def get_public_key(self):
    48          """Return the public key for this Signer instance.
    49          """
    50          # Lazy-eval the public key
    51          if self._public_key is None:
    52              self._public_key = self._context.get_public_key(self._private_key)
    53          return self._public_key
    54  
    55  
    56  class CryptoFactory:
    57      """Factory for generating Signers.
    58      """
    59  
    60      def __init__(self, context):
    61          self._context = context
    62  
    63      @property
    64      def context(self):
    65          """Return the context that backs this factory instance
    66          """
    67          return self._context
    68  
    69      def new_signer(self, private_key):
    70          """Create a new signer for the given private key.
    71  
    72          Args:
    73              private_key (:obj:`PrivateKey`): a private key
    74  
    75          Returns:
    76              (:obj:`Signer`): a signer instance
    77          """
    78          return Signer(self._context, private_key)
    79  
    80  
    81  def create_context(algorithm_name):
    82      """Returns an algorithm instance by name.
    83  
    84      Args:
    85          algorithm_name (str): the algorithm name
    86  
    87      Returns:
    88          (:obj:`Context`): a context instance for the given algorithm
    89  
    90      Raises:
    91          NoSuchAlgorithmError if the algorithm is unknown
    92      """
    93      if algorithm_name == 'secp256k1':
    94          return Secp256k1Context()
    95  
    96      raise NoSuchAlgorithmError("no such algorithm: {}".format(algorithm_name))