github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/javascript/signing/index.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 const { NoSuchAlgorithmError, SigningError, ParseError } = require('./core') 21 const secp256k1 = require('./secp256k1') 22 23 /** 24 * A convenient wrapper of Context and PrivateKey 25 */ 26 class Signer { 27 /** 28 * Constructs a new Signer 29 * 30 * @param {Context} context - a cryptographic context 31 * @param {PrivateKey} privateKey - private key 32 */ 33 constructor (context, privateKey) { 34 this._context = context 35 this._privateKey = privateKey 36 this._publicKey = null 37 } 38 39 /** 40 * Signs the given message. 41 * 42 * @param {Buffer} message - the message bytes 43 * @return {string} - the signature in a hex-encoded string 44 * @throws {SigningError} - if any error occurs during the signing process 45 */ 46 sign (message) { 47 return this._context.sign(message, this._privateKey) 48 } 49 50 /** 51 * Return the public key for this Signer instance. 52 * 53 * @return {PublicKey} the public key instance 54 */ 55 getPublicKey () { 56 if (this._publicKey === null) { 57 this._publicKey = this._context.getPublicKey(this._privateKey) 58 } 59 60 return this._publicKey 61 } 62 } 63 64 /** 65 * Factory for generating signers. 66 */ 67 class CryptoFactory { 68 /** 69 * Constructs a CryptoFactory. 70 * 71 * @param {Context} context - a cryptographic context 72 */ 73 constructor (context) { 74 this._context = context 75 } 76 77 /** 78 * Returns the context associated with this factory 79 * 80 * @return {Context} 81 */ 82 getContext () { 83 return this._context 84 } 85 86 /** 87 * Create a new signer for the given private key. 88 * 89 * @param {PrivateKey} privateKey - a private key 90 * @return {Signer} - a signer instance 91 */ 92 newSigner (privateKey) { 93 return new Signer(this._context, privateKey) 94 } 95 } 96 97 /** 98 * Returns an Context instance by algorithm name. 99 * 100 * @param {string} algorithmName - the algorithm name 101 * @return {Context} a context instance for the given algorithm 102 * @throws {NoSuchAlgorithmError} if the algorithm is unknown 103 */ 104 const createContext = algorithmName => { 105 if (algorithmName === 'secp256k1') { 106 return new secp256k1.Secp256k1Context() 107 } else { 108 throw new NoSuchAlgorithmError(`No such algorithm: ${algorithmName}`) 109 } 110 } 111 112 module.exports = { 113 // Re-export the errors 114 NoSuchAlgorithmError, 115 SigningError, 116 ParseError, 117 118 Signer, 119 CryptoFactory, 120 createContext 121 }