# All files index.ts

95.83% Statements 23/24
40% Branches 2/5
33.33% Functions 2/6
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    891x

    1x

    1x

    1x
    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x
    1x

    1x
    1x

    1x

    1x

     import * as crypto from '@node-dlc/crypto';
    import { ILogger } from '@node-dlc/logger';
    import { Socket } from 'net';

    import { NoiseServer } from './noise-server';
    import { NoiseServerOptions } from './noise-server-options';
    import { NoiseSocket } from './noise-socket';
    import { NoiseState } from './noise-state';

    export { NoiseError } from './noise-error';
    export { NoiseServer } from './noise-server';
    export { NoiseServerListenOptions } from './noise-server-listen-options';
    export { NoiseServerOptions } from './noise-server-options';
    export { NoiseSocket } from './noise-socket';
    export { NoiseSocketOptions } from './noise-socket-options';
    export { NoiseState } from './noise-state';

    export type NoiseConnectOptions = {
      /**
       * Local secret as a 32-byte secp256k1 private key
       */
      ls: Buffer;

      /**
       * Optional ephemeral 32-byte secp256k1 private key. If not provided, one is generated.
       */
      es?: Buffer;

      /**
       * remote compressed public key, 33-bytes.
       */
      rpk: Buffer;

      /**
       * Optional host. Defaults to localhost.
       */
      host?: string;

      /**
       * Optional port. Defaults to 9735.
       */
      port?: number;

      /**
       * Optional logger
       */
      logger?: ILogger;
    };

    /**
     * Connect to a remote noise socket server.
     */
    export function connect({
      ls,
      es,
      rpk,
      host,
      port = 9735,
      logger,
    }: NoiseConnectOptions): NoiseSocket {
      Iif (!es) {
        es = crypto.createPrivateKey();
      }

      const noiseLogger = logger
        ? logger.sub('noise', rpk.slice(0, 8).toString('hex'))
        : undefined;
      const noiseState = new NoiseState({ ls, es, logger: noiseLogger });
      const socket = new Socket();
      const instance = new NoiseSocket({
        socket,
        noiseState,
        rpk,
        logger: noiseLogger,
      });
      socket.connect({ host, port });
      return instance;
    }

    /**
     * Factory function to create a new server
     */
    export function createServer(
      { ls, esFactory }: NoiseServerOptions,
      connListener: (socket: NoiseSocket) => void,
    ): NoiseServer {
      return new NoiseServer({ ls, esFactory }, connListener);
    }