github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/javascript/signing/core.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ------------------------------------------------------------------------------
    16   */
    17  
    18  'use strict'
    19  
    20  /**
    21   * Thrown when trying to create a context for an algorithm which does not exist.
    22   */
    23  class NoSuchAlgorithmError extends Error {
    24    /**
    25     * Constructs a new NoSuchAlgorithmError
    26     *
    27     * @param {string} [message] - an optional message, defaults to the empty
    28     * string
    29     */
    30    constructor (message = '') {
    31      super(message)
    32      this.name = this.constructor.name
    33    }
    34  }
    35  
    36  /**
    37   * Thrown when an error occurs during the signing process.
    38   */
    39  class SigningError extends Error {
    40    /**
    41     * Constructs a new SigningError
    42     *
    43     * @param {string} [message] - an optional message, defaults to the empty
    44     * string
    45     */
    46    constructor (message = '') {
    47      super(message)
    48      this.name = this.constructor.name
    49    }
    50  }
    51  
    52  /**
    53   * Thrown when an error occurs during deserialization of a Private or Public
    54   * key from various formats.
    55   */
    56  class ParseError extends Error {
    57    /**
    58     * Constructs a new ParseError
    59     *
    60     * @param {string} [message] - an optional message, defaults to the empty
    61     * string
    62     */
    63    constructor (message = '') {
    64      super(message)
    65      this.name = this.constructor.name
    66    }
    67  }
    68  
    69  /**
    70   * A private key instance.
    71   *
    72   * The underlying content is dependent on implementation.
    73   */
    74  class PrivateKey {
    75    constructor () {
    76      if (this.constructor === PrivateKey) {
    77        throw new TypeError('Cannot construct abstract class')
    78      }
    79    }
    80  
    81    /**
    82     * Returns the algorithm name used for this private key.
    83     */
    84    getAlgorithmName () {
    85      throw new TypeError('Abstract method not implemented')
    86    }
    87  
    88    /**
    89     * Return the private key encoded as a hex string
    90     */
    91    asHex () {
    92      return this.asBytes().toString('hex')
    93    }
    94  
    95    /**
    96     * Returns the private key bytes in a Buffer.
    97     */
    98    asBytes () {
    99      throw new TypeError('Abstract method not implemented')
   100    }
   101  }
   102  
   103  /**
   104   * A public key instance.
   105   *
   106   * The underlying content is dependent on implementation.
   107   */
   108  class PublicKey {
   109    constructor () {
   110      if (this.constructor === PublicKey) {
   111        throw new TypeError('Cannot construct abstract class')
   112      }
   113    }
   114  
   115    /**
   116     * Returns the algorithm name used for this public key.
   117     */
   118    getAlgorithmName () {
   119      throw new TypeError('Abstract method not implemented')
   120    }
   121  
   122    /**
   123     * Return the public key encoded as a hex string
   124     */
   125    asHex () {
   126      return this.asBytes().toString('hex')
   127    }
   128  
   129    /**
   130     * Returns the public key bytes in a Buffer.
   131     */
   132    asBytes () {
   133      throw new TypeError('Abstract method not implemented')
   134    }
   135  }
   136  
   137  /**
   138   * A context for a cryptographic signing algorithm.
   139   */
   140  class Context {
   141    constructor () {
   142      if (this.constructor === Context) {
   143        throw new TypeError('Cannot construct abstract class')
   144      }
   145    }
   146  
   147    /**
   148     * Returns the algorithm name used for this context.
   149     */
   150    getAlgorithmName () {
   151      throw new TypeError('Abstract method not implemented')
   152    }
   153  
   154    /**
   155     * Sign a message.
   156     *
   157     * Given a private key for this algorithm, sign the given message bytes
   158     * and return a hex-encoded string of the resulting signature.
   159     *
   160     * @param {Buffer} message - the message bytes
   161     * @param {PrivateKey} privateKey - the private key
   162     *
   163     * @returns {string} - The signature in a hex-encoded string
   164     *
   165     * @throws {SigningError} - if any error occurs during the signing process
   166     */
   167    sign (message, privateKey) {
   168      throw new TypeError('Abstract method not implemented')
   169    }
   170  
   171    /**
   172     * Verifies that a signature of a message was produced with the associated
   173     * public key.
   174     *
   175     * @param {string} signature - the hex-encoded signature
   176     * @param {Buffer} message - the message bytes
   177     * @param {PublicKey} publicKey - the public key to use for verification
   178     *
   179     * @returns {boolean} - true if the public key is associated with the
   180     * signature for that method, false otherwise
   181     */
   182    verify (signature, message, publicKey) {
   183      throw new TypeError('Abstract method not implemented')
   184    }
   185  
   186    /**
   187     * Produce a public key for the given private key.
   188     *
   189     * @param {PrivateKey} privateKey - a private key
   190     *
   191     * @return {PublicKey} - the public key for the given private key
   192     */
   193    getPublicKey (privateKey) {
   194      throw new TypeError('Abstract method not implemented')
   195    }
   196  
   197    /**
   198     * Generate a new random private key, based on the underlying algorithm.
   199     *
   200     * @return {PrivateKey} - a private key instance
   201     */
   202    newRandomPrivateKey () {
   203      throw new TypeError('Abstract method not implemented')
   204    }
   205  }
   206  
   207  module.exports = {
   208    NoSuchAlgorithmError,
   209    SigningError,
   210    ParseError,
   211    PublicKey,
   212    PrivateKey,
   213    Context
   214  }