@sebastianp265/safe-server-side-storage-client
Version:
Library for Confidential Server-Side Message Storage Using the Labyrinth Protocol
53 lines (52 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThisDevice = void 0;
const DeviceKeyBundle_1 = require("./key-bundles/DeviceKeyBundle");
const join_epoch_1 = require("../phases/join-epoch");
const authenticate_device_to_epoch_1 = require("../phases/authenticate-device-to-epoch");
const open_first_epoch_1 = require("../phases/open-first-epoch");
const BytesSerializer_1 = require("../BytesSerializer");
class ThisDevice {
id;
keyBundle;
constructor(id, keyBundle) {
this.id = id;
this.keyBundle = keyBundle;
}
static async deserialize(thisDeviceSerialized, epochStorage, labyrinthServerClient) {
const thisDevice = new ThisDevice(thisDeviceSerialized.id, DeviceKeyBundle_1.DeviceKeyBundle.deserialize(thisDeviceSerialized.keyBundle));
const newestEpochSequenceIdBefore = epochStorage.getNewestEpoch().sequenceId;
await (0, join_epoch_1.joinAllEpochs)(thisDevice, epochStorage, labyrinthServerClient);
const newestEpochAfter = epochStorage.getNewestEpoch();
if (newestEpochSequenceIdBefore != newestEpochAfter.sequenceId) {
await labyrinthServerClient.authenticateDeviceToEpoch(newestEpochAfter.id, thisDevice.id, {
epochDeviceMac: BytesSerializer_1.bytesSerializerProvider.bytesSerializer.serialize((0, authenticate_device_to_epoch_1.generateEpochDeviceMac)(newestEpochAfter, thisDevice.keyBundle.pub.deviceKeyPub)),
});
}
return new ThisDevice(thisDeviceSerialized.id, DeviceKeyBundle_1.DeviceKeyBundle.deserialize(thisDeviceSerialized.keyBundle));
}
serialize() {
return {
id: this.id,
keyBundle: this.keyBundle.serialize(),
};
}
static async initialize(virtualDevice, virtualDeviceDecryptionKey, labyrinthWebClient) {
const deviceKeyBundle = DeviceKeyBundle_1.DeviceKeyBundle.generate();
const { deviceId, firstEpoch } = await (0, open_first_epoch_1.openFirstEpoch)(deviceKeyBundle.pub, virtualDeviceDecryptionKey, virtualDevice, labyrinthWebClient);
const thisDevice = new ThisDevice(deviceId, deviceKeyBundle);
return {
thisDevice,
firstEpoch,
};
}
static async fromRecoveryCode(newestRecoveredEpoch, webClient) {
const deviceKeyBundle = DeviceKeyBundle_1.DeviceKeyBundle.generate();
const { assignedDeviceId } = await webClient.authenticateDeviceToEpochAndRegisterDevice(newestRecoveredEpoch.id, {
devicePublicKeyBundle: deviceKeyBundle.pub.serialize(),
epochDeviceMac: BytesSerializer_1.bytesSerializerProvider.bytesSerializer.serialize((0, authenticate_device_to_epoch_1.generateEpochDeviceMac)(newestRecoveredEpoch, deviceKeyBundle.pub.deviceKeyPub)),
});
return new ThisDevice(assignedDeviceId, deviceKeyBundle);
}
}
exports.ThisDevice = ThisDevice;