const protocol = require('entropy-protocol')
const test = require('tape')
test('Convert Uint8Array to and from hex', function (t) {
t.plan(2)
const empty = new Uint8Array(32)
const emptyHex = protocol.toHex(empty)
t.equal(Buffer.from(empty).toString('hex'), emptyHex)
const emptyArray = protocol.fromHex(emptyHex)
t.true(protocol.constantTimeEq(empty, emptyArray))
})
test('Encrypt, decrypt with HPKE', function (t) {
t.plan(2)
const { generateSigningKey, encryptAndSign, decryptAndVerify } = protocol.Hpke
const aliceSk = generateSigningKey()
const bobX25519Keypair = protocol.X25519Keypair.generate()
const plaintext = new Uint8Array(32)
const encryptedAndSignedMessage = encryptAndSign(aliceSk, plaintext, bobX25519Keypair.publicKey())
const decryptedPlaintext = decryptAndVerify(bobX25519Keypair.secretKey(), encryptedAndSignedMessage)
t.true(protocol.constantTimeEq(decryptedPlaintext, plaintext))
const malloryX25519Keypair = protocol.X25519Keypair.generate()
let error
try {
decryptAndVerify(malloryX25519Keypair.secretKey(), encryptedAndSignedMessage)
} catch (e) {
error = e.toString()
}
t.equals(error, 'Error: Hpke: HPKE Error: OpenError')
})