# All files / src/utils privKey.js

100% Statements 22/22
100% Branches 12/12
100% Functions 3/3
100% Lines 22/22

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46

    1x
    1x
    1x

    1x
    6x

    6x
    2x

    4x

    1x
    3x
    3x
    3x

    3x
    2x

    1x

    1x
    3x
    3x
    3x

    3x
    3x
    1x

     /**
     * Copyright (c) 2018-present, Leap DAO (leapdao.org)
     *
     * This source code is licensed under the Mozilla Public License Version 2.0
     * found in the LICENSE file in the root directory of this source tree.
     */

    /* eslint-disable no-console */

    const fs = require('fs');
    const path = require('path');
    const { promisify } = require('util');

    const privKeyPath = async (app, cliArgs) => {
      const exists = promisify(fs.exists);

      if (cliArgs.privateKey && (await exists(cliArgs.privateKey))) {
        return cliArgs.privateKey;
      }

      return path.join(app.lotionPath(), '.priv');
    };

    exports.readPrivKey = async (app, cliArgs) => {
      const exists = promisify(fs.exists);
      const readFile = promisify(fs.readFile);
      const filePath = await privKeyPath(app, cliArgs);

      if (filePath && (await exists(filePath))) {
        return readFile(filePath, 'utf-8');
      }

      return undefined;
    };

    exports.writePrivKey = async (app, cliArgs, privateKey) => {
      const exists = promisify(fs.exists);
      const writeFile = promisify(fs.writeFile);
      const filePath = await privKeyPath(app, cliArgs);

      const privFilename = path.join(app.lotionPath(), '.priv');
      if (!filePath || !(await exists(filePath))) {
        await writeFile(privFilename, privateKey);
      }
    };