"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const libp2p_crypto_1 = require("libp2p-crypto");
const threads_core_1 = require("@textile/threads-core");
const coding_1 = require("./coding");
const event_1 = require("./event");
const record_1 = require("./record");
const readKey = libp2p_crypto_1.randomBytes(32);
const replicatorKey = libp2p_crypto_1.randomBytes(32);
const raw = { txt: 'hello world' };
describe('Encoding...', () => {
    describe('Event...', () => {
        it('should encode and encrypt log events', () => __awaiter(void 0, void 0, void 0, function* () {
            const key = libp2p_crypto_1.randomBytes(32);
            const body = threads_core_1.Block.encoder(raw, coding_1.defaultOptions.codec);
            const obj = yield event_1.createEvent(body, readKey, key);
            chai_1.expect(obj).to.have.haveOwnProperty('value');
            chai_1.expect(obj).to.have.haveOwnProperty('body');
            chai_1.expect(obj).to.have.haveOwnProperty('header');
            const decodedBody = coding_1.decodeBlock(obj.body, key);
            chai_1.expect(decodedBody.decodeUnsafe()).to.deep.equal(raw);
            const decodedHeader = coding_1.decodeBlock(obj.header, readKey);
            const header = decodedHeader.decodeUnsafe();
            chai_1.expect(header).to.haveOwnProperty('key');
            chai_1.expect(header.key).to.deep.equal(key);
        }));
    });
    describe('Record...', () => {
        it('should encode and encrypt a log record', () => __awaiter(void 0, void 0, void 0, function* () {
            const privKey = yield libp2p_crypto_1.keys.generateKeyPair('Ed25519', 256);
            const body = threads_core_1.Block.encoder(raw, coding_1.defaultOptions.codec);
            const event = yield event_1.createEvent(body, readKey);
            // We just use the public key from the private key here for testing
            const pubKey = privKey.public;
            const { value } = yield record_1.createRecord(event, { privKey, servKey: replicatorKey, pubKey });
            const decoded = coding_1.decodeBlock(value, replicatorKey).decode();
            chai_1.expect(decoded.prev).to.be.undefined;
            chai_1.expect(decoded).to.haveOwnProperty('block');
            chai_1.expect(decoded).to.haveOwnProperty('sig');
        }));
    });
});
//# sourceMappingURL=index.spec.js.map