const assert = require('assert');
const electron = require('electron');
const security = require('../pairing-security');

electron.app.whenReady().then(() => {
  const alice = security.createIdentity();
  const bob = security.createIdentity();
  const aliceEphemeral = security.createEphemeralKeyPair();
  const bobEphemeral = security.createEphemeralKeyPair();
  const transcript = 'electron-secure-channel-smoke';
  const aliceKey = security.deriveSessionKey(
    aliceEphemeral.privateKeyPem,
    bobEphemeral.publicKeyPem,
    transcript
  );
  const bobKey = security.deriveSessionKey(
    bobEphemeral.privateKeyPem,
    aliceEphemeral.publicKeyPem,
    transcript
  );
  assert.deepStrictEqual(aliceKey, bobKey);
  const signature = security.signTranscript(alice.privateKeyPem, transcript);
  assert.strictEqual(
    security.verifyTranscript(alice.publicKeyPem, transcript, signature),
    true
  );
  assert.strictEqual(
    security.verifyTranscript(bob.publicKeyPem, transcript, signature),
    false
  );
  console.log(`electron crypto smoke passed (Node ${process.versions.node})`);
  electron.app.quit();
}).catch(error => {
  console.error(error.stack || error.message);
  electron.app.exit(1);
});
