"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _ = require(".");

var _fromPath = _interopRequireDefault(require("@polkadot/util-crypto/key/fromPath"));

var _key = require("@polkadot/util-crypto/key");

// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('naclOpen', () => {
  it('opens a sealed message by the sender', () => {
    const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
    const sender = (0, _.naclKeypairFromString)('sender');
    const receiver = (0, _.naclKeypairFromString)('receiver');
    const senderBox = (0, _.naclBoxKeypairFromSecret)(sender.secretKey);
    const receiverBox = (0, _.naclBoxKeypairFromSecret)(receiver.secretKey);
    const {
      sealed,
      nonce
    } = (0, _.naclSeal)(message, senderBox.secretKey, receiverBox.publicKey);
    expect((0, _.naclOpen)(sealed, nonce, senderBox.publicKey, receiverBox.secretKey)).toEqual(message);
  });
  it('polkadot does double ratchet', () => {
    const sender = (0, _.naclKeypairFromString)('sender');
    const receiver = (0, _.naclKeypairFromString)('receiver'); // Make id key for identifying each other

    const senderIdKey = (0, _fromPath.default)(sender, (0, _key.keyExtractPath)('//1//1//1//1').path, 'ed25519');
    const receiverIdKey = (0, _fromPath.default)(receiver, (0, _key.keyExtractPath)('//2//2//2//2').path, 'ed25519'); // Receiver sends encrypting public key to receive message to decrypt with his private key

    const receiverIdBoxKey = (0, _.naclBoxKeypairFromSecret)(receiverIdKey.secretKey);
    console.log("Receiver sends receiver's public key to sender ".concat(receiverIdBoxKey.publicKey)); // Sender encrypts message to send with the public key the receiver sent and send it to receiver

    const senderIdBoxKey = (0, _.naclBoxKeypairFromSecret)(senderIdKey.secretKey);
    const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
    const {
      sealed,
      nonce
    } = (0, _.naclSeal)(message, senderIdBoxKey.secretKey, receiverIdBoxKey.publicKey);
    console.log("Sender sends encrypted message to receiver ".concat(sealed, ", ").concat(nonce)); // Receiver opens encrypted message from the sender

    let opened = (0, _.naclOpen)(sealed, nonce, senderIdBoxKey.publicKey, receiverIdBoxKey.secretKey);
    console.log("The sealed message is ".concat(opened)); // Oh no the Id key is hacked, derive Id key again from another index

    const senderIdKey2 = (0, _fromPath.default)(sender, (0, _key.keyExtractPath)('//1//1//1//2').path, 'ed25519');
    const receiverIdKey2 = (0, _fromPath.default)(receiver, (0, _key.keyExtractPath)('//2//2//2//3').path, 'ed25519'); // Receiver sends encrypting public key to receive message to decrypt with his private key

    const receiverIdBoxKey2 = (0, _.naclBoxKeypairFromSecret)(receiverIdKey2.secretKey);
    console.log("Receiver sends receiver's public key to sender ".concat(receiverIdBoxKey2.publicKey)); // Sender encrypts message to send with the public key the receiver sent and send it to receiver

    const senderIdBoxKey2 = (0, _.naclBoxKeypairFromSecret)(senderIdKey2.secretKey);
    const message2 = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
    let ctx = (0, _.naclSeal)(message2, senderIdBoxKey2.secretKey, receiverIdBoxKey2.publicKey);
    const sealed2 = ctx.sealed;
    const nonce2 = ctx.nonce;
    console.log("Sender sends encrypted message to receiver ".concat(sealed, ", ").concat(nonce)); // Receiver opens encrypted message from the sender

    opened = (0, _.naclOpen)(sealed2, nonce2, senderIdBoxKey2.publicKey, receiverIdBoxKey2.secretKey);
    console.log("The sealed message is ".concat(opened)); // This time derive key for each message to send and receive
    // Receiver sends encrypting public key to receive message to decrypt with his private key

    const receiverIdBoxKey2One = (0, _.naclBoxKeypairFromSecret)(receiverIdBoxKey2.secretKey);
    console.log("Receiver sends receiver's public key to sender ".concat(receiverIdBoxKey2One.publicKey)); // Sender encrypts message to send with the public key the receiver sent and send it to receiver

    const senderIdBoxKey2One = (0, _.naclBoxKeypairFromSecret)(senderIdBoxKey2.secretKey);
    const message3 = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
    ctx = (0, _.naclSeal)(message3, senderIdBoxKey2One.secretKey, receiverIdBoxKey2One.publicKey);
    const sealed3 = ctx.sealed;
    const nonce3 = ctx.nonce;
    console.log("Sender sends encrypted message to receiver ".concat(sealed3, ", ").concat(nonce3)); // Receiver opens encrypted message from the sender

    opened = (0, _.naclOpen)(sealed2, nonce2, senderIdBoxKey2One.publicKey, receiverIdBoxKey2One.secretKey);
    console.log("The sealed message is ".concat(opened));
  });
  it('returns null on invalid', () => {
    expect((0, _.naclOpen)(new Uint8Array(), new Uint8Array(24), new Uint8Array(32), new Uint8Array(32))).toEqual(null);
  });
});