|
|
|
@ -72,6 +72,8 @@ module.exports = {
|
|
|
|
|
removeContactSignedPreKeyByIdentityKey,
|
|
|
|
|
removeAllContactSignedPreKeys,
|
|
|
|
|
|
|
|
|
|
createOrUpdatePairingAuthorisation,
|
|
|
|
|
|
|
|
|
|
createOrUpdateItem,
|
|
|
|
|
getItemById,
|
|
|
|
|
getAllItems,
|
|
|
|
@ -770,6 +772,91 @@ async function updateSchema(instance) {
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
await runSchemaUpdate(schemaVersion, instance);
|
|
|
|
|
}
|
|
|
|
|
await updateLokiSchema(instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const LOKI_SCHEMA_VERSIONS = [updateToLokiSchemaVersion2];
|
|
|
|
|
|
|
|
|
|
async function updateToLokiSchemaVersion2(currentVersion, instance) {
|
|
|
|
|
if (currentVersion >= 2) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log('updateToLokiSchemaVersion2: starting...');
|
|
|
|
|
await instance.run('BEGIN TRANSACTION;');
|
|
|
|
|
|
|
|
|
|
await instance.run(
|
|
|
|
|
`CREATE TABLE pairingAuthorisations(
|
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
issuerPubKey VARCHAR(255),
|
|
|
|
|
secondaryDevicePubKey VARCHAR(255),
|
|
|
|
|
signature VARCHAR(255),
|
|
|
|
|
json TEXT
|
|
|
|
|
);`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await instance.run(
|
|
|
|
|
`INSERT INTO loki_schema (
|
|
|
|
|
version
|
|
|
|
|
) values (
|
|
|
|
|
2
|
|
|
|
|
);`
|
|
|
|
|
);
|
|
|
|
|
await instance.run('COMMIT TRANSACTION;');
|
|
|
|
|
console.log('updateToLokiSchemaVersion2: success!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function updateLokiSchema(instance) {
|
|
|
|
|
const result = await instance.get(
|
|
|
|
|
"SELECT name FROM sqlite_master WHERE type = 'table' AND name='loki_schema'"
|
|
|
|
|
);
|
|
|
|
|
if (!result) {
|
|
|
|
|
await createLokiSchemaTable(instance);
|
|
|
|
|
}
|
|
|
|
|
const lokiSchemaVersion = await getLokiSchemaVersion(instance);
|
|
|
|
|
console.log(
|
|
|
|
|
'updateLokiSchema:',
|
|
|
|
|
`Current loki schema version: ${lokiSchemaVersion};`,
|
|
|
|
|
`Most recent schema version: ${LOKI_SCHEMA_VERSIONS.length};`
|
|
|
|
|
);
|
|
|
|
|
for (
|
|
|
|
|
let index = 0, max = LOKI_SCHEMA_VERSIONS.length;
|
|
|
|
|
index < max;
|
|
|
|
|
index += 1
|
|
|
|
|
) {
|
|
|
|
|
const runSchemaUpdate = LOKI_SCHEMA_VERSIONS[index];
|
|
|
|
|
|
|
|
|
|
// Yes, we really want to do this asynchronously, in order
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
await runSchemaUpdate(lokiSchemaVersion, instance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getLokiSchemaVersion(instance) {
|
|
|
|
|
const result = await instance.get(
|
|
|
|
|
'SELECT version FROM loki_schema WHERE version = (SELECT MAX(version) FROM loki_schema);'
|
|
|
|
|
);
|
|
|
|
|
if (!result.version) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return result.version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createLokiSchemaTable(instance) {
|
|
|
|
|
await instance.run('BEGIN TRANSACTION;');
|
|
|
|
|
await instance.run(
|
|
|
|
|
`CREATE TABLE loki_schema(
|
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
version INTEGER
|
|
|
|
|
);`
|
|
|
|
|
);
|
|
|
|
|
await instance.run(
|
|
|
|
|
`INSERT INTO loki_schema (
|
|
|
|
|
version
|
|
|
|
|
) values (
|
|
|
|
|
0
|
|
|
|
|
);`
|
|
|
|
|
);
|
|
|
|
|
await instance.run('COMMIT TRANSACTION;');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let db;
|
|
|
|
@ -1135,6 +1222,57 @@ async function removeAllSignedPreKeys() {
|
|
|
|
|
return removeAllFromTable(SIGNED_PRE_KEYS_TABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PAIRING_AUTHORISATIONS_TABLE = 'pairingAuthorisations';
|
|
|
|
|
async function getPairingAuthorisation(issuerPubKey, secondaryDevicePubKey) {
|
|
|
|
|
const row = await db.get(
|
|
|
|
|
`SELECT * FROM ${PAIRING_AUTHORISATIONS_TABLE} WHERE
|
|
|
|
|
issuerPubKey = $issuerPubKey AND secondaryDevicePubKey = $secondaryDevicePubKey
|
|
|
|
|
LIMIT 1;`,
|
|
|
|
|
{
|
|
|
|
|
$issuerPubKey: issuerPubKey,
|
|
|
|
|
$secondaryDevicePubKey: secondaryDevicePubKey,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return jsonToObject(row.json);
|
|
|
|
|
}
|
|
|
|
|
async function createOrUpdatePairingAuthorisation(data) {
|
|
|
|
|
const { issuerPubKey, secondaryDevicePubKey, signature } = data;
|
|
|
|
|
|
|
|
|
|
const existing = await getPairingAuthorisation(
|
|
|
|
|
issuerPubKey,
|
|
|
|
|
secondaryDevicePubKey
|
|
|
|
|
);
|
|
|
|
|
// prevent adding duplicate entries
|
|
|
|
|
if (existing) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.run(
|
|
|
|
|
`INSERT INTO ${PAIRING_AUTHORISATIONS_TABLE} (
|
|
|
|
|
issuerPubKey,
|
|
|
|
|
secondaryDevicePubKey,
|
|
|
|
|
signature,
|
|
|
|
|
json
|
|
|
|
|
) values (
|
|
|
|
|
$issuerPubKey,
|
|
|
|
|
$secondaryDevicePubKey,
|
|
|
|
|
$signature,
|
|
|
|
|
$json
|
|
|
|
|
)`,
|
|
|
|
|
{
|
|
|
|
|
$issuerPubKey: issuerPubKey,
|
|
|
|
|
$secondaryDevicePubKey: secondaryDevicePubKey,
|
|
|
|
|
$signature: signature,
|
|
|
|
|
$json: objectToJSON(data),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ITEMS_TABLE = 'items';
|
|
|
|
|
async function createOrUpdateItem(data) {
|
|
|
|
|
return createOrUpdate(ITEMS_TABLE, data);
|
|
|
|
|