@wireapp/cryptobox
Version:
High-level API with persistent storage for Proteus.
138 lines (137 loc) • 6.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var ProteusKeys = require("@wireapp/proteus/dist/keys/root");
var ProteusSession = require("@wireapp/proteus/dist/session/root");
var root_1 = require("../store/root");
var store_engine_1 = require("@wireapp/store-engine");
var bazinga64_1 = require("bazinga64");
var CryptoboxCRUDStore = (function () {
function CryptoboxCRUDStore(engine) {
this.engine = engine;
}
Object.defineProperty(CryptoboxCRUDStore, "KEYS", {
get: function () {
return {
LOCAL_IDENTITY: 'local_identity',
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(CryptoboxCRUDStore, "STORES", {
get: function () {
return {
LOCAL_IDENTITY: 'keys',
PRE_KEYS: 'prekeys',
SESSIONS: 'sessions',
};
},
enumerable: true,
configurable: true
});
CryptoboxCRUDStore.prototype.from_store = function (record) {
return typeof record.serialised === 'string'
? bazinga64_1.Decoder.fromBase64(record.serialised).asBytes.buffer
: record.serialised;
};
CryptoboxCRUDStore.prototype.to_store = function (serialised) {
return bazinga64_1.Encoder.toBase64(serialised).asString;
};
CryptoboxCRUDStore.prototype.delete_all = function () {
var _this = this;
return Promise.resolve()
.then(function () { return _this.engine.deleteAll(CryptoboxCRUDStore.STORES.LOCAL_IDENTITY); })
.then(function () { return _this.engine.deleteAll(CryptoboxCRUDStore.STORES.PRE_KEYS); })
.then(function () { return _this.engine.deleteAll(CryptoboxCRUDStore.STORES.SESSIONS); })
.then(function () { return true; });
};
CryptoboxCRUDStore.prototype.delete_prekey = function (prekey_id) {
return this.engine.delete(CryptoboxCRUDStore.STORES.PRE_KEYS, prekey_id.toString()).then(function () { return prekey_id; });
};
CryptoboxCRUDStore.prototype.load_identity = function () {
var _this = this;
return this.engine
.read(CryptoboxCRUDStore.STORES.LOCAL_IDENTITY, CryptoboxCRUDStore.KEYS.LOCAL_IDENTITY)
.then(function (record) {
var payload = _this.from_store(record);
var identity = ProteusKeys.IdentityKeyPair.deserialise(payload);
return identity;
})
.catch(function (error) {
if (error instanceof store_engine_1.StoreEngine.error.RecordNotFoundError) {
return undefined;
}
throw error;
});
};
CryptoboxCRUDStore.prototype.load_prekey = function (prekey_id) {
var _this = this;
return this.engine
.read(CryptoboxCRUDStore.STORES.PRE_KEYS, prekey_id.toString())
.then(function (record) {
var payload = _this.from_store(record);
return ProteusKeys.PreKey.deserialise(payload);
})
.catch(function (error) {
if (error instanceof store_engine_1.StoreEngine.error.RecordNotFoundError) {
return undefined;
}
throw error;
});
};
CryptoboxCRUDStore.prototype.load_prekeys = function () {
var _this = this;
return this.engine.readAll(CryptoboxCRUDStore.STORES.PRE_KEYS).then(function (records) {
var preKeys = [];
records.forEach(function (record) {
var payload = _this.from_store(record);
var preKey = ProteusKeys.PreKey.deserialise(payload);
preKeys.push(preKey);
});
return preKeys;
});
};
CryptoboxCRUDStore.prototype.save_identity = function (identity) {
var serialised = this.to_store(identity.serialise());
var payload = new root_1.SerialisedRecord(serialised, CryptoboxCRUDStore.KEYS.LOCAL_IDENTITY);
return this.engine.create(CryptoboxCRUDStore.STORES.LOCAL_IDENTITY, payload.id, payload).then(function () { return identity; });
};
CryptoboxCRUDStore.prototype.save_prekey = function (pre_key) {
var serialised = this.to_store(pre_key.serialise());
var payload = new root_1.SerialisedRecord(serialised, pre_key.key_id.toString());
return this.engine.create(CryptoboxCRUDStore.STORES.PRE_KEYS, payload.id, payload).then(function () { return pre_key; });
};
CryptoboxCRUDStore.prototype.save_prekeys = function (pre_keys) {
var _this = this;
var promises = pre_keys.map(function (pre_key) { return _this.save_prekey(pre_key); });
return Promise.all(promises).then(function () { return pre_keys; });
};
CryptoboxCRUDStore.prototype.create_session = function (session_id, session) {
var serialised = this.to_store(session.serialise());
var payload = new root_1.SerialisedRecord(serialised, session_id);
return this.engine.create(CryptoboxCRUDStore.STORES.SESSIONS, payload.id, payload).then(function () { return session; });
};
CryptoboxCRUDStore.prototype.read_session = function (identity, session_id) {
var _this = this;
return this.engine
.read(CryptoboxCRUDStore.STORES.SESSIONS, session_id)
.then(function (record) {
var payload = _this.from_store(record);
return ProteusSession.Session.deserialise(identity, payload);
});
};
CryptoboxCRUDStore.prototype.update_session = function (session_id, session) {
var serialised = this.to_store(session.serialise());
var payload = new root_1.SerialisedRecord(serialised, session_id);
return this.engine
.update(CryptoboxCRUDStore.STORES.SESSIONS, payload.id, { serialised: payload.serialised })
.then(function () { return session; });
};
CryptoboxCRUDStore.prototype.delete_session = function (session_id) {
return this.engine
.delete(CryptoboxCRUDStore.STORES.SESSIONS, session_id)
.then(function (primary_key) { return primary_key; });
};
return CryptoboxCRUDStore;
}());
exports.default = CryptoboxCRUDStore;
;