github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/server/blockchain/batcher.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  'use strict'
    18  
    19  const secp256k1 = require('sawtooth-sdk/signing/secp256k1')
    20  const {
    21    Batch,
    22    BatchHeader,
    23    TransactionHeader,
    24    TransactionList
    25  } = require('sawtooth-sdk/protobuf')
    26  const { BadRequest } = require('../api/errors')
    27  const config = require('../system/config')
    28  
    29  const PRIVATE_KEY = config.PRIVATE_KEY
    30  
    31  // Initialize secp256k1 Context and PrivateKey wrappers
    32  const context = new secp256k1.Secp256k1Context()
    33  const privateKey = secp256k1.Secp256k1PrivateKey.fromHex(PRIVATE_KEY)
    34  const publicKeyHex = context.getPublicKey(privateKey).asHex()
    35  console.log(`Batch signer initialized with public key: ${publicKeyHex}`)
    36  
    37  // Decode transaction headers and throw errors if invalid
    38  const validateTxns = txns => {
    39    const headers = txns.map(txn => TransactionHeader.decode(txn.header))
    40  
    41    headers.forEach(header => {
    42      if (header.batcherPublicKey !== publicKeyHex) {
    43        throw new BadRequest(
    44          `Transactions must use batcherPublicKey: ${publicKeyHex}`)
    45      }
    46    })
    47  }
    48  
    49  // Wrap an array of transactions in an encoded BatchList
    50  const batchTxns = txns => {
    51    const header = BatchHeader.encode({
    52      signerPublicKey: publicKeyHex,
    53      transactionIds: txns.map(txn => txn.headerSignature)
    54    }).finish()
    55  
    56    return Batch.create({
    57      header,
    58      headerSignature: context.sign(header, privateKey),
    59      transactions: txns
    60    })
    61  }
    62  
    63  // Validate an encoded TransactionList, then wrap in an encoded BatchList
    64  const batch = txnList => {
    65    const txns = TransactionList.decode(txnList).transactions
    66    validateTxns(txns)
    67    return batchTxns(txns)
    68  }
    69  
    70  // Return the server's hex encoded public key
    71  const getPublicKey = () => publicKeyHex
    72  
    73  module.exports = {
    74    batch,
    75    getPublicKey
    76  }