daf-data-store
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -1,2 +0,2 @@ | ||
import { Types } from 'daf-core'; | ||
import { Message } from 'daf-core'; | ||
import { DbDriver } from './types'; | ||
@@ -11,3 +11,3 @@ export declare class DataStore { | ||
}): Promise<any>; | ||
credentialsForMessageHash(hash: string): Promise<any>; | ||
credentialsForMessageId(id: string): Promise<any>; | ||
credentialsFieldsForClaimHash(hash: string): Promise<any>; | ||
@@ -19,9 +19,9 @@ findCredentialsByFields({ iss, sub, claim_type, }: { | ||
}): Promise<any>; | ||
findMessages({ iss, sub, tag, limit }: { | ||
iss?: string; | ||
sub?: string; | ||
tag?: string; | ||
findMessages({ sender, receiver, threadId, limit, }: { | ||
sender?: string; | ||
receiver?: string; | ||
threadId?: string; | ||
limit?: number; | ||
}): Promise<any>; | ||
findMessage(hash: string): Promise<any>; | ||
findMessage(id: string): Promise<any>; | ||
allIdentities(): Promise<{ | ||
@@ -37,10 +37,15 @@ did: any; | ||
shortId(did: string): Promise<any>; | ||
saveMessage(message: Types.ValidatedMessage): Promise<{ | ||
hash: string; | ||
saveMessage(message: Message): Promise<{ | ||
hash: any; | ||
iss: { | ||
did: string; | ||
did: string | null; | ||
}; | ||
}>; | ||
saveVerifiableCredential(vc: any, messageHash: string): Promise<any>; | ||
private updateMetaData; | ||
private saveMetaData; | ||
saveVerifiableCredentials(message: Message): Promise<void>; | ||
saveVerifiableCredential(vc: any, messageId: string): Promise<any>; | ||
findMessagesByVC(hash: string): Promise<any>; | ||
messageMetaData(id: string): Promise<any>; | ||
deleteMessage(hash: string): Promise<any>; | ||
} |
@@ -112,3 +112,2 @@ "use strict"; | ||
hash: row.hash, | ||
parentHash: row.parent_hash, | ||
iss: { did: row.iss }, | ||
@@ -124,3 +123,3 @@ sub: { did: row.sub }, | ||
}; | ||
DataStore.prototype.credentialsForMessageHash = function (hash) { | ||
DataStore.prototype.credentialsForMessageId = function (id) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -132,5 +131,6 @@ var query, rows; | ||
query = sql_bricks_sqlite_1.default | ||
.select('rowid', '*') | ||
.from('verifiable_credentials') | ||
.where({ parent_hash: hash }) | ||
.select('md.rowid', 'vc.*') | ||
.from('verifiable_credentials_meta_data as md') | ||
.leftJoin('verifiable_credentials as vc', { 'md.hash': 'vc.hash' }) | ||
.where({ 'md.message_id': id }) | ||
.toParams(); | ||
@@ -143,3 +143,2 @@ return [4 /*yield*/, this.db.rows(query.text, query.values)]; | ||
hash: row.hash, | ||
parentHash: row.parent_hash, | ||
iss: { did: row.iss }, | ||
@@ -218,3 +217,2 @@ sub: { did: row.sub }, | ||
hash: row.hash, | ||
parentHash: row.parent_hash, | ||
iss: { did: row.iss }, | ||
@@ -232,3 +230,3 @@ sub: { did: row.sub }, | ||
DataStore.prototype.findMessages = function (_a) { | ||
var iss = _a.iss, sub = _a.sub, tag = _a.tag, limit = _a.limit; | ||
var sender = _a.sender, receiver = _a.receiver, threadId = _a.threadId, limit = _a.limit; | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -240,14 +238,16 @@ var where, query, rows; | ||
where = {}; | ||
if (iss && sub) { | ||
where = sql_bricks_sqlite_1.default.or(where, { iss: iss, sub: sub }); | ||
if (sender && receiver) { | ||
where = sql_bricks_sqlite_1.default.or(where, { sender: sender, receiver: receiver }); | ||
} | ||
else { | ||
if (iss) | ||
where = sql_bricks_sqlite_1.default.and(where, { iss: iss }); | ||
if (sub) | ||
where = sql_bricks_sqlite_1.default.and(where, { sub: sub }); | ||
if (sender) | ||
where = sql_bricks_sqlite_1.default.and(where, { sender: sender }); | ||
if (receiver) | ||
where = sql_bricks_sqlite_1.default.and(where, { receiver: receiver }); | ||
} | ||
if (tag) | ||
where = sql_bricks_sqlite_1.default.and(where, { tag: tag }); | ||
where = sql_bricks_sqlite_1.default.or(where, { sub: null }); | ||
if (sender || receiver) { | ||
where = sql_bricks_sqlite_1.default.or(where, { receiver: null }); | ||
} | ||
if (threadId) | ||
where = sql_bricks_sqlite_1.default.and(where, { thread_id: threadId }); | ||
query = sql_bricks_sqlite_1.default | ||
@@ -257,3 +257,3 @@ .select('rowid', '*') | ||
.where(where) | ||
.orderBy('nbf desc'); | ||
.orderBy('timestamp desc'); | ||
if (limit) { | ||
@@ -268,11 +268,10 @@ query = query.limit(limit); | ||
rowId: "" + row.rowid, | ||
hash: row.hash, | ||
iss: { did: row.iss }, | ||
sub: row.sub ? { did: row.sub } : null, | ||
id: row.id, | ||
sender: row.sender ? { did: row.sender } : null, | ||
receiver: row.receiver ? { did: row.receiver } : null, | ||
type: row.type, | ||
tag: row.tag, | ||
threadId: row.thread_id, | ||
data: row.data, | ||
jwt: row.jwt, | ||
nbf: row.nbf, | ||
iat: row.iat, | ||
raw: row.raw, | ||
timestamp: row.timestamp, | ||
}); })]; | ||
@@ -283,3 +282,3 @@ } | ||
}; | ||
DataStore.prototype.findMessage = function (hash) { | ||
DataStore.prototype.findMessage = function (id) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -293,3 +292,3 @@ var query, rows, mapped; | ||
.from('messages') | ||
.where({ hash: hash }) | ||
.where({ id: id }) | ||
.toParams(); | ||
@@ -301,10 +300,10 @@ return [4 /*yield*/, this.db.rows(query.text, query.values)]; | ||
rowId: "" + row.rowid, | ||
hash: row.hash, | ||
iss: { did: row.iss }, | ||
sub: row.sub ? { did: row.sub } : null, | ||
id: row.id, | ||
sender: row.sender ? { did: row.sender } : null, | ||
receiver: row.receiver ? { did: row.receiver } : null, | ||
type: row.type, | ||
tag: row.tag, | ||
jwt: row.jwt, | ||
threadId: row.thread_id, | ||
data: row.data, | ||
nbf: row.nbf, | ||
raw: row.raw, | ||
timestamp: row.timestamp, | ||
}); }); | ||
@@ -318,3 +317,3 @@ return [2 /*return*/, mapped[0]]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var vcSubjects, vcIssuers, messageSubjects, messageIssuers, uniqueDids; | ||
var vcSubjects, vcIssuers, messageReceivers, messageSenders, uniqueDids; | ||
return __generator(this, function (_a) { | ||
@@ -328,9 +327,9 @@ switch (_a.label) { | ||
vcIssuers = _a.sent(); | ||
return [4 /*yield*/, this.db.rows('select distinct sub as did from messages where sub is not null', null)]; | ||
return [4 /*yield*/, this.db.rows('select distinct receiver as did from messages where receiver is not null', null)]; | ||
case 3: | ||
messageSubjects = _a.sent(); | ||
return [4 /*yield*/, this.db.rows('select distinct iss as did from messages', null)]; | ||
messageReceivers = _a.sent(); | ||
return [4 /*yield*/, this.db.rows('select distinct sender as did from messages where sender is not null', null)]; | ||
case 4: | ||
messageIssuers = _a.sent(); | ||
uniqueDids = __spread(new Set(__spread(messageSubjects.map(function (item) { return item.did; }), messageIssuers.map(function (item) { return item.did; }), vcIssuers.map(function (item) { return item.did; }), vcSubjects.map(function (item) { return item.did; })))).map(function (did) { return ({ did: did }); }); | ||
messageSenders = _a.sent(); | ||
uniqueDids = __spread(new Set(__spread(messageReceivers.map(function (item) { return item.did; }), messageSenders.map(function (item) { return item.did; }), vcIssuers.map(function (item) { return item.did; }), vcSubjects.map(function (item) { return item.did; })))).map(function (did) { return ({ did: did }); }); | ||
return [2 /*return*/, uniqueDids]; | ||
@@ -370,3 +369,3 @@ } | ||
.from('messages') | ||
.where(sql_bricks_sqlite_1.default.or(sql_bricks_sqlite_1.default.and({ iss: did1 }, { sub: did2 }), sql_bricks_sqlite_1.default.and({ iss: did2 }, { sub: did1 }))) | ||
.where(sql_bricks_sqlite_1.default.or(sql_bricks_sqlite_1.default.and({ sender: did1 }, { receiver: did2 }), sql_bricks_sqlite_1.default.and({ sender: did2 }, { receiver: did1 }))) | ||
.toParams(); | ||
@@ -387,3 +386,3 @@ return [4 /*yield*/, this.db.rows(query.text, query.values)]; | ||
case 0: | ||
query = 'SELECT * from (select sub as did, nbf as timestamp, source_type as sourceType FROM messages ORDER BY nbf desc) GROUP BY did, sourceType'; | ||
query = "SELECT * FROM ( SELECT m.id, m.\"timestamp\", m.sender AS did, md. \"type\" AS sourceType, md.id AS sourceId FROM messages AS m\n LEFT JOIN messages_meta_data AS md ON m.id = md.message_id) GROUP BY did, sourceType"; | ||
return [4 /*yield*/, this.db.rows(query, [])]; | ||
@@ -422,45 +421,93 @@ case 1: return [2 /*return*/, _a.sent()]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var source_type, source_id, query, _a, _b, vc, e_1_1; | ||
var e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var messageId, searchQuery, searchResult, query; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
source_type = message.meta && message.meta[0].sourceType; | ||
source_id = message.meta && message.meta[0].sourceId; | ||
messageId = message.id; | ||
searchQuery = sql_bricks_sqlite_1.default | ||
.select('id') | ||
.from('messages') | ||
.where({ id: messageId }) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.rows(searchQuery.text, searchQuery.values)]; | ||
case 1: | ||
searchResult = _a.sent(); | ||
if (!(searchResult.length > 0)) return [3 /*break*/, 2]; | ||
this.updateMetaData(message); | ||
return [3 /*break*/, 6]; | ||
case 2: | ||
query = sql_bricks_sqlite_1.default | ||
.insert('messages', { | ||
hash: message.hash, | ||
iss: message.issuer, | ||
sub: message.subject, | ||
nbf: message.time, | ||
id: messageId, | ||
sender: message.sender, | ||
receiver: message.receiver, | ||
timestamp: message.timestamp, | ||
type: message.type, | ||
tag: message.tag, | ||
jwt: message.raw, | ||
meta: message.meta && JSON.stringify(message.meta), | ||
source_type: source_type, | ||
source_id: source_id, | ||
thread_id: message.threadId, | ||
raw: message.raw, | ||
data: message.data && JSON.stringify(message.data), | ||
}) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.run(query.text, query.values)]; | ||
case 3: | ||
_a.sent(); | ||
return [4 /*yield*/, this.saveMetaData(message)]; | ||
case 4: | ||
_a.sent(); | ||
return [4 /*yield*/, this.saveVerifiableCredentials(message)]; | ||
case 5: | ||
_a.sent(); | ||
_a.label = 6; | ||
case 6: return [2 /*return*/, { hash: message.id, iss: { did: message.sender } }]; | ||
} | ||
}); | ||
}); | ||
}; | ||
DataStore.prototype.updateMetaData = function (message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var id, allMeta, allMeta_1, allMeta_1_1, metaData, query, rows, insertQuery, e_1_1; | ||
var e_1, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
id = message.id, allMeta = message.allMeta; | ||
_b.label = 1; | ||
case 1: | ||
_d.sent(); | ||
if (!(message.type == 'w3c.vp' || message.type == 'w3c.vc')) return [3 /*break*/, 9]; | ||
_d.label = 2; | ||
_b.trys.push([1, 7, 8, 9]); | ||
allMeta_1 = __values(allMeta), allMeta_1_1 = allMeta_1.next(); | ||
_b.label = 2; | ||
case 2: | ||
_d.trys.push([2, 7, 8, 9]); | ||
_a = __values(message.custom.vc), _b = _a.next(); | ||
_d.label = 3; | ||
if (!!allMeta_1_1.done) return [3 /*break*/, 6]; | ||
metaData = allMeta_1_1.value; | ||
query = sql_bricks_sqlite_1.default | ||
.select('type, id, data') | ||
.from('messages_meta_data') | ||
.where({ | ||
message_id: id, | ||
type: metaData.type, | ||
id: metaData.id, | ||
}) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.rows(query.text, query.values)]; | ||
case 3: | ||
if (!!_b.done) return [3 /*break*/, 6]; | ||
vc = _b.value; | ||
return [4 /*yield*/, this.saveVerifiableCredential(vc, message.hash)]; | ||
rows = _b.sent(); | ||
if (!(rows.length === 0)) return [3 /*break*/, 5]; | ||
insertQuery = sql_bricks_sqlite_1.default | ||
.insert('messages_meta_data', { | ||
message_id: id, | ||
type: metaData.type, | ||
id: metaData.id, | ||
data: metaData.data && JSON.stringify(metaData.data), | ||
}) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.run(insertQuery.text, insertQuery.values)]; | ||
case 4: | ||
_d.sent(); | ||
_d.label = 5; | ||
_b.sent(); | ||
_b.label = 5; | ||
case 5: | ||
_b = _a.next(); | ||
return [3 /*break*/, 3]; | ||
allMeta_1_1 = allMeta_1.next(); | ||
return [3 /*break*/, 2]; | ||
case 6: return [3 /*break*/, 9]; | ||
case 7: | ||
e_1_1 = _d.sent(); | ||
e_1_1 = _b.sent(); | ||
e_1 = { error: e_1_1 }; | ||
@@ -470,7 +517,7 @@ return [3 /*break*/, 9]; | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
if (allMeta_1_1 && !allMeta_1_1.done && (_a = allMeta_1.return)) _a.call(allMeta_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
return [7 /*endfinally*/]; | ||
case 9: return [2 /*return*/, { hash: message.hash, iss: { did: message.issuer } }]; | ||
case 9: return [2 /*return*/]; | ||
} | ||
@@ -480,5 +527,92 @@ }); | ||
}; | ||
DataStore.prototype.saveVerifiableCredential = function (vc, messageHash) { | ||
DataStore.prototype.saveMetaData = function (message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var verifiableCredential, vcHash, query, claim, _a, _b, _i, type, value, isObj, fieldsQuery; | ||
var messageId, _a, _b, metaData, query, e_2_1; | ||
var e_2, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
messageId = message.id; | ||
_d.label = 1; | ||
case 1: | ||
_d.trys.push([1, 6, 7, 8]); | ||
_a = __values(message.allMeta), _b = _a.next(); | ||
_d.label = 2; | ||
case 2: | ||
if (!!_b.done) return [3 /*break*/, 5]; | ||
metaData = _b.value; | ||
query = sql_bricks_sqlite_1.default | ||
.insert('messages_meta_data', { | ||
message_id: messageId, | ||
type: metaData.type, | ||
id: metaData.id, | ||
data: metaData.data && JSON.stringify(metaData.data), | ||
}) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.run(query.text, query.values)]; | ||
case 3: | ||
_d.sent(); | ||
_d.label = 4; | ||
case 4: | ||
_b = _a.next(); | ||
return [3 /*break*/, 2]; | ||
case 5: return [3 /*break*/, 8]; | ||
case 6: | ||
e_2_1 = _d.sent(); | ||
e_2 = { error: e_2_1 }; | ||
return [3 /*break*/, 8]; | ||
case 7: | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
return [7 /*endfinally*/]; | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
DataStore.prototype.saveVerifiableCredentials = function (message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var messageId, _a, _b, vc, e_3_1; | ||
var e_3, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
messageId = message.id; | ||
if (!(message.type == 'w3c.vp' || message.type == 'w3c.vc')) return [3 /*break*/, 8]; | ||
_d.label = 1; | ||
case 1: | ||
_d.trys.push([1, 6, 7, 8]); | ||
_a = __values(message.vc), _b = _a.next(); | ||
_d.label = 2; | ||
case 2: | ||
if (!!_b.done) return [3 /*break*/, 5]; | ||
vc = _b.value; | ||
return [4 /*yield*/, this.saveVerifiableCredential(vc, messageId)]; | ||
case 3: | ||
_d.sent(); | ||
_d.label = 4; | ||
case 4: | ||
_b = _a.next(); | ||
return [3 /*break*/, 2]; | ||
case 5: return [3 /*break*/, 8]; | ||
case 6: | ||
e_3_1 = _d.sent(); | ||
e_3 = { error: e_3_1 }; | ||
return [3 /*break*/, 8]; | ||
case 7: | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
return [7 /*endfinally*/]; | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
DataStore.prototype.saveVerifiableCredential = function (vc, messageId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var verifiableCredential, vcHash, metaData, searchQuery, searchResult, query, claim, _a, _b, _i, type, value, isObj, fieldsQuery; | ||
return __generator(this, function (_c) { | ||
@@ -489,6 +623,27 @@ switch (_c.label) { | ||
vcHash = blakejs_1.default.blake2bHex(vc.jwt); | ||
metaData = sql_bricks_sqlite_1.default | ||
.insert('verifiable_credentials_meta_data', { | ||
message_id: messageId, | ||
hash: vcHash, | ||
}) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.run(metaData.text, metaData.values) | ||
// Check if | ||
]; | ||
case 1: | ||
_c.sent(); | ||
searchQuery = sql_bricks_sqlite_1.default | ||
.select('hash') | ||
.from('verifiable_credentials') | ||
.where({ hash: vcHash }) | ||
.toParams(); | ||
return [4 /*yield*/, this.db.rows(searchQuery.text, searchQuery.values)]; | ||
case 2: | ||
searchResult = _c.sent(); | ||
if (searchResult.length > 0) { | ||
return [2 /*return*/, vcHash]; | ||
} | ||
query = sql_bricks_sqlite_1.default | ||
.insert('verifiable_credentials', { | ||
hash: vcHash, | ||
parent_hash: messageHash, | ||
iss: verifiableCredential.iss, | ||
@@ -502,3 +657,3 @@ sub: verifiableCredential.sub, | ||
return [4 /*yield*/, this.db.run(query.text, query.values)]; | ||
case 1: | ||
case 3: | ||
_c.sent(); | ||
@@ -510,7 +665,7 @@ claim = verifiableCredential.vc.credentialSubject; | ||
_i = 0; | ||
_c.label = 2; | ||
case 2: | ||
if (!(_i < _a.length)) return [3 /*break*/, 5]; | ||
_c.label = 4; | ||
case 4: | ||
if (!(_i < _a.length)) return [3 /*break*/, 7]; | ||
type = _a[_i]; | ||
if (!claim.hasOwnProperty(type)) return [3 /*break*/, 4]; | ||
if (!claim.hasOwnProperty(type)) return [3 /*break*/, 6]; | ||
value = claim[type]; | ||
@@ -531,9 +686,9 @@ isObj = typeof value === 'function' || (typeof value === 'object' && !!value); | ||
return [4 /*yield*/, this.db.run(fieldsQuery.text, fieldsQuery.values)]; | ||
case 3: | ||
case 5: | ||
_c.sent(); | ||
_c.label = 4; | ||
case 4: | ||
_c.label = 6; | ||
case 6: | ||
_i++; | ||
return [3 /*break*/, 2]; | ||
case 5: return [2 /*return*/, vcHash]; | ||
return [3 /*break*/, 4]; | ||
case 7: return [2 /*return*/, vcHash]; | ||
} | ||
@@ -543,2 +698,56 @@ }); | ||
}; | ||
DataStore.prototype.findMessagesByVC = function (hash) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var query, rows; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
query = sql_bricks_sqlite_1.default | ||
.select('md.rowid', 'm.*') | ||
.from('verifiable_credentials_meta_data as md') | ||
.leftJoin('messages as m', { 'md.message_id': 'm.id' }) | ||
.where({ 'md.hash': hash }); | ||
query = query.toParams(); | ||
return [4 /*yield*/, this.db.rows(query.text, query.values)]; | ||
case 1: | ||
rows = _a.sent(); | ||
return [2 /*return*/, rows.map(function (row) { return ({ | ||
rowId: "" + row.rowid, | ||
id: row.id, | ||
sender: row.sender ? { did: row.sender } : null, | ||
receiver: row.receiver ? { did: row.receiver } : null, | ||
type: row.type, | ||
threadId: row.thread_id, | ||
data: row.data, | ||
raw: row.raw, | ||
timestamp: row.timestamp, | ||
}); })]; | ||
} | ||
}); | ||
}); | ||
}; | ||
DataStore.prototype.messageMetaData = function (id) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var query, rows; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
query = sql_bricks_sqlite_1.default | ||
.select('rowid', '*') | ||
.from('messages_meta_data') | ||
.where({ message_id: id }); | ||
query = query.toParams(); | ||
return [4 /*yield*/, this.db.rows(query.text, query.values)]; | ||
case 1: | ||
rows = _a.sent(); | ||
return [2 /*return*/, rows.map(function (row) { return ({ | ||
rowId: "" + row.rowid, | ||
type: row.type, | ||
id: row.id, | ||
data: row.data, | ||
}); })]; | ||
} | ||
}); | ||
}); | ||
}; | ||
DataStore.prototype.deleteMessage = function (hash) { | ||
@@ -545,0 +754,0 @@ return this.db.run('DELETE FROM messages where hash=?', [hash]); |
@@ -8,5 +8,8 @@ import { DataStore } from './data-store'; | ||
vc: (message: any, {}: {}, { dataStore }: Context) => Promise<any>; | ||
metaData: (message: any, {}: {}, { dataStore }: Context) => Promise<any>; | ||
thread: (message: any, {}: {}, { dataStore }: Context) => Promise<any>; | ||
}; | ||
VerifiableClaim: { | ||
fields: (vc: any, {}: {}, { dataStore }: Context) => Promise<any>; | ||
inMessages: (vc: any, {}: {}, { dataStore }: Context) => Promise<any>; | ||
}; | ||
@@ -43,10 +46,10 @@ Identity: { | ||
}[]>; | ||
messages: (_: any, { iss, sub, tag, limit }: { | ||
iss: string; | ||
sub: string; | ||
tag: string; | ||
messages: (_: any, { sender, receiver, threadId, limit, }: { | ||
sender: string; | ||
receiver: string; | ||
threadId: string; | ||
limit: number; | ||
}, { dataStore }: Context) => Promise<any>; | ||
message: (_: any, { hash }: { | ||
hash: string; | ||
message: (_: any, { id }: { | ||
id: string; | ||
}, { dataStore }: Context) => Promise<any>; | ||
@@ -59,8 +62,8 @@ credentials: (_: any, { iss, sub }: { | ||
Mutation: { | ||
deleteMessage: (_: any, { hash }: { | ||
hash: string; | ||
deleteMessage: (_: any, { id }: { | ||
id: string; | ||
}, { dataStore }: Context) => Promise<any>; | ||
}; | ||
}; | ||
export declare const typeDefs = "\n extend type Query {\n identity(did: ID!): Identity\n identities(dids: [ID!]): [Identity]\n messages(iss: ID, sub: ID, tag: String, limit: Int): [Message]\n message(hash: ID!): Message!\n credentials(iss: ID, sub: ID): [VerifiableClaim]\n }\n\n extend type Mutation {\n deleteMessage(hash: ID!): Boolean\n }\n\n extend type Identity {\n shortId: String\n firstName: String\n lastName: String\n profileImage: String\n url: String\n description: String\n interactionCount: Int!\n messagesSent: [Message]\n messagesReceived: [Message]\n messagesAll: [Message]\n credentialsIssued: [VerifiableClaim]\n credentialsReceived: [VerifiableClaim]\n credentialsAll: [VerifiableClaim]\n }\n \n extend type Message {\n iss: Identity!\n sub: Identity\n jwt: String!\n data: String!\n iat: Int\n nbf: Int\n vis: String\n tag: String\n vc: [VerifiableClaim]\n }\n\n type VerifiableClaim {\n hash: ID!\n parentHash: ID!\n rowId: String!\n iss: Identity!\n sub: Identity!\n json: String!\n jwt: String!\n nbf: Int\n iat: Int\n exp: Int\n fields: [VerifiableClaimField]\n }\n\n type VerifiableClaimField {\n rowId: String!\n hash: ID!\n parentHash: ID!\n iss: Identity!\n sub: Identity!\n type: String!\n value: String!\n isObj: Boolean!\n }\n\n"; | ||
export declare const typeDefs = "\n extend type Query {\n identity(did: ID!): Identity\n identities(dids: [ID!]): [Identity]\n messages(sender: ID, reveiver: ID, threadId: String, limit: Int): [Message]\n message(id: ID!): Message!\n credentials(iss: ID, sub: ID): [VerifiableClaim]\n }\n\n extend type Mutation {\n deleteMessage(hash: ID!): Boolean\n }\n\n extend type Identity {\n shortId: String\n firstName: String\n lastName: String\n profileImage: String\n url: String\n description: String\n interactionCount: Int!\n messagesSent: [Message]\n messagesReceived: [Message]\n messagesAll: [Message]\n credentialsIssued: [VerifiableClaim]\n credentialsReceived: [VerifiableClaim]\n credentialsAll: [VerifiableClaim]\n }\n \n extend type Message {\n vc: [VerifiableClaim]\n }\n\n type VerifiableClaim {\n hash: ID!\n rowId: String!\n iss: Identity!\n sub: Identity!\n json: String!\n jwt: String!\n nbf: Int\n iat: Int\n exp: Int\n fields: [VerifiableClaimField]\n inMessages: [Message]\n }\n\n type VerifiableClaimField {\n rowId: String!\n hash: ID!\n parentHash: ID!\n iss: Identity!\n sub: Identity!\n type: String!\n value: String!\n isObj: Boolean!\n }\n\n"; | ||
export {}; |
@@ -44,5 +44,26 @@ "use strict"; | ||
return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_c) { | ||
return [2 /*return*/, dataStore.credentialsForMessageHash(message.hash)]; | ||
return [2 /*return*/, dataStore.credentialsForMessageId(message.id)]; | ||
}); }); | ||
}, | ||
metaData: function (message, _a, _b) { | ||
var dataStore = _b.dataStore; | ||
return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_c) { | ||
return [2 /*return*/, dataStore.messageMetaData(message.id)]; | ||
}); }); | ||
}, | ||
thread: function (message, _a, _b) { | ||
var dataStore = _b.dataStore; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var messages; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, dataStore.findMessages({ threadId: message.threadId })]; | ||
case 1: | ||
messages = _c.sent(); | ||
console.log('AAAA', messages); | ||
return [2 /*return*/, messages.filter(function (item) { return item.id !== message.id; })]; | ||
} | ||
}); | ||
}); | ||
}, | ||
}, | ||
@@ -56,2 +77,8 @@ VerifiableClaim: { | ||
}, | ||
inMessages: function (vc, _a, _b) { | ||
var dataStore = _b.dataStore; | ||
return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_c) { | ||
return [2 /*return*/, dataStore.findMessagesByVC(vc.hash)]; | ||
}); }); | ||
}, | ||
}, | ||
@@ -149,3 +176,3 @@ Identity: { | ||
return __generator(this, function (_b) { | ||
return [2 /*return*/, dataStore.findMessages({ iss: identity.did })]; | ||
return [2 /*return*/, dataStore.findMessages({ sender: identity.did })]; | ||
}); | ||
@@ -158,3 +185,3 @@ }); | ||
return __generator(this, function (_b) { | ||
return [2 /*return*/, dataStore.findMessages({ sub: identity.did })]; | ||
return [2 /*return*/, dataStore.findMessages({ receiver: identity.did })]; | ||
}); | ||
@@ -167,3 +194,3 @@ }); | ||
return __generator(this, function (_b) { | ||
return [2 /*return*/, dataStore.findMessages({ iss: identity.did, sub: identity.did })]; | ||
return [2 /*return*/, dataStore.findMessages({ sender: identity.did, receiver: identity.did })]; | ||
}); | ||
@@ -191,7 +218,7 @@ }); | ||
messages: function (_, _a, _b) { | ||
var iss = _a.iss, sub = _a.sub, tag = _a.tag, limit = _a.limit; | ||
var sender = _a.sender, receiver = _a.receiver, threadId = _a.threadId, limit = _a.limit; | ||
var dataStore = _b.dataStore; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_c) { | ||
return [2 /*return*/, dataStore.findMessages({ iss: iss, sub: sub, tag: tag, limit: limit })]; | ||
return [2 /*return*/, dataStore.findMessages({ sender: sender, receiver: receiver, threadId: threadId, limit: limit })]; | ||
}); | ||
@@ -201,6 +228,6 @@ }); | ||
message: function (_, _a, _b) { | ||
var hash = _a.hash; | ||
var id = _a.id; | ||
var dataStore = _b.dataStore; | ||
return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_c) { | ||
return [2 /*return*/, dataStore.findMessage(hash)]; | ||
return [2 /*return*/, dataStore.findMessage(id)]; | ||
}); }); | ||
@@ -226,6 +253,6 @@ }, | ||
deleteMessage: function (_, _a, _b) { | ||
var hash = _a.hash; | ||
var id = _a.id; | ||
var dataStore = _b.dataStore; | ||
return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_c) { | ||
return [2 /*return*/, dataStore.deleteMessage(hash)]; | ||
return [2 /*return*/, dataStore.deleteMessage(id)]; | ||
}); }); | ||
@@ -235,3 +262,3 @@ }, | ||
}; | ||
exports.typeDefs = "\n extend type Query {\n identity(did: ID!): Identity\n identities(dids: [ID!]): [Identity]\n messages(iss: ID, sub: ID, tag: String, limit: Int): [Message]\n message(hash: ID!): Message!\n credentials(iss: ID, sub: ID): [VerifiableClaim]\n }\n\n extend type Mutation {\n deleteMessage(hash: ID!): Boolean\n }\n\n extend type Identity {\n shortId: String\n firstName: String\n lastName: String\n profileImage: String\n url: String\n description: String\n interactionCount: Int!\n messagesSent: [Message]\n messagesReceived: [Message]\n messagesAll: [Message]\n credentialsIssued: [VerifiableClaim]\n credentialsReceived: [VerifiableClaim]\n credentialsAll: [VerifiableClaim]\n }\n \n extend type Message {\n iss: Identity!\n sub: Identity\n jwt: String!\n data: String!\n iat: Int\n nbf: Int\n vis: String\n tag: String\n vc: [VerifiableClaim]\n }\n\n type VerifiableClaim {\n hash: ID!\n parentHash: ID!\n rowId: String!\n iss: Identity!\n sub: Identity!\n json: String!\n jwt: String!\n nbf: Int\n iat: Int\n exp: Int\n fields: [VerifiableClaimField]\n }\n\n type VerifiableClaimField {\n rowId: String!\n hash: ID!\n parentHash: ID!\n iss: Identity!\n sub: Identity!\n type: String!\n value: String!\n isObj: Boolean!\n }\n\n"; | ||
exports.typeDefs = "\n extend type Query {\n identity(did: ID!): Identity\n identities(dids: [ID!]): [Identity]\n messages(sender: ID, reveiver: ID, threadId: String, limit: Int): [Message]\n message(id: ID!): Message!\n credentials(iss: ID, sub: ID): [VerifiableClaim]\n }\n\n extend type Mutation {\n deleteMessage(hash: ID!): Boolean\n }\n\n extend type Identity {\n shortId: String\n firstName: String\n lastName: String\n profileImage: String\n url: String\n description: String\n interactionCount: Int!\n messagesSent: [Message]\n messagesReceived: [Message]\n messagesAll: [Message]\n credentialsIssued: [VerifiableClaim]\n credentialsReceived: [VerifiableClaim]\n credentialsAll: [VerifiableClaim]\n }\n \n extend type Message {\n vc: [VerifiableClaim]\n }\n\n type VerifiableClaim {\n hash: ID!\n rowId: String!\n iss: Identity!\n sub: Identity!\n json: String!\n jwt: String!\n nbf: Int\n iat: Int\n exp: Int\n fields: [VerifiableClaimField]\n inMessages: [Message]\n }\n\n type VerifiableClaimField {\n rowId: String!\n hash: ID!\n parentHash: ID!\n iss: Identity!\n sub: Identity!\n type: String!\n value: String!\n isObj: Boolean!\n }\n\n"; | ||
//# sourceMappingURL=graphql.js.map |
@@ -43,15 +43,15 @@ "use strict"; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS messages (\n hash TEXT,\n parent_hash TEXT,\n iss TEXT,\n sub TEXT,\n type TEXT,\n tag TEXT,\n data TEXT,\n iat NUMERIC,\n nbf NUMERIC,\n jwt TEXT,\n meta TEXT,\n source_type TEXT,\n source_id TEXT,\n internal NUMERIC NOT NULL default 1\n );", [])]; | ||
case 0: return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS messages (\n id TEXT PRIMARY KEY,\n thread_id TEXT,\n sender TEXT,\n receiver TEXT,\n type TEXT,\n data TEXT,\n raw TEXT,\n timestamp NUMERIC\n );", [])]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS verifiable_credentials (\n hash TEXT,\n parent_hash TEXT,\n iss TEXT,\n aud TEXT,\n sub TEXT,\n nbf NUMERIC,\n iat NUMERIC,\n jwt TEXT,\n internal NUMERIC NOT NULL default 1\n );", [])]; | ||
return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS messages_meta_data (\n message_id TEXT,\n data TEXT,\n type TEXT,\n id TEXT\n );", [])]; | ||
case 2: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS verifiable_credentials_fields (\n parent_hash INTEGER,\n iss TEXT, sub TEXT,\n nbf NUMERIC,\n iat NUMERIC,\n claim_type TEXT,\n claim_value TEXT,\n is_obj NUMERIC NOT NULL default 0\n );", [])]; | ||
return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS verifiable_credentials (\n hash TEXT,\n iss TEXT,\n aud TEXT,\n sub TEXT,\n nbf NUMERIC,\n iat NUMERIC,\n jwt TEXT,\n internal NUMERIC NOT NULL default 1\n );", [])]; | ||
case 3: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE TRIGGER IF NOT EXISTS delete_messages BEFORE DELETE ON \"messages\" BEGIN\n DELETE FROM verifiable_credentials where parent_hash = old.hash;\n END;", [])]; | ||
return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS verifiable_credentials_meta_data (\n message_id TEXT,\n hash TEXT\n );", [])]; | ||
case 4: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE TRIGGER IF NOT EXISTS delete_verifiable_credentials BEFORE DELETE ON \"verifiable_credentials\" BEGIN\n DELETE FROM verifiable_credentials_fields where parent_hash = old.hash;\n END;", [])]; | ||
return [4 /*yield*/, db.run("CREATE TABLE IF NOT EXISTS verifiable_credentials_fields (\n parent_hash INTEGER,\n iss TEXT, sub TEXT,\n nbf NUMERIC,\n iat NUMERIC,\n claim_type TEXT,\n claim_value TEXT,\n is_obj NUMERIC NOT NULL default 0\n );", [])]; | ||
case 5: | ||
@@ -58,0 +58,0 @@ _a.sent(); |
@@ -43,41 +43,47 @@ "use strict"; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_hash\" ON \"messages\" (\"hash\");", [])]; | ||
case 0: return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_id\" ON \"messages\" (\"id\");", [])]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_parent_hash\" ON \"messages\" (\"parent_hash\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_thread_id\" ON \"messages\" (\"thread_id\");", [])]; | ||
case 2: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_sub\" ON \"messages\" (\"sub\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_receiver\" ON \"messages\" (\"receiver\");", [])]; | ||
case 3: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_iss\" ON \"messages\" (\"iss\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_sender\" ON \"messages\" (\"sender\");", [])]; | ||
case 4: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_source_type\" ON \"messages\" (\"source_type\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_meta_data_message_id\" ON \"messages_meta_data\" (\"message_id\");", [])]; | ||
case 5: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_source_id\" ON \"messages\" (\"source_id\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_meta_data_type\" ON \"messages_meta_data\" (\"type\");", [])]; | ||
case 6: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_parent_hash\" ON \"verifiable_credentials\" (\"parent_hash\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"messages_meta_data_id\" ON \"messages_meta_data\" (\"id\");", [])]; | ||
case 7: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_iss\" ON \"verifiable_credentials\" (\"iss\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_meta_data_message_id\" ON \"verifiable_credentials_meta_data\" (\"message_id\");", [])]; | ||
case 8: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_sub\" ON \"verifiable_credentials\" (\"sub\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_meta_data_hash\" ON \"verifiable_credentials_meta_data\" (\"hash\");", [])]; | ||
case 9: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_parent_hash\" ON \"verifiable_credentials_fields\" (\"parent_hash\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_iss\" ON \"verifiable_credentials\" (\"iss\");", [])]; | ||
case 10: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_sub\" ON \"verifiable_credentials_fields\" (\"sub\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_sub\" ON \"verifiable_credentials\" (\"sub\");", [])]; | ||
case 11: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_iss\" ON \"verifiable_credentials_fields\" (\"iss\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_parent_hash\" ON \"verifiable_credentials_fields\" (\"parent_hash\");", [])]; | ||
case 12: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_claim_type\" ON \"verifiable_credentials_fields\" (\"claim_type\");", [])]; | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_sub\" ON \"verifiable_credentials_fields\" (\"sub\");", [])]; | ||
case 13: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_iss\" ON \"verifiable_credentials_fields\" (\"iss\");", [])]; | ||
case 14: | ||
_a.sent(); | ||
return [4 /*yield*/, db.run("CREATE INDEX IF NOT EXISTS \"verifiable_credentials_fields_claim_type\" ON \"verifiable_credentials_fields\" (\"claim_type\");", [])]; | ||
case 15: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
@@ -84,0 +90,0 @@ } |
@@ -6,2 +6,20 @@ # Change Log | ||
# [0.10.0](https://github.com/uport-project/daf/compare/v0.9.0...v0.10.0) (2019-12-10) | ||
### Bug Fixes | ||
* Renaming to sender and receiver ([bf33a2d](https://github.com/uport-project/daf/commit/bf33a2de268cf07b06faa04283ec066573c37ffc)) | ||
### Features | ||
* Data deduplication ([c5c10b1](https://github.com/uport-project/daf/commit/c5c10b17eebd1d6f82a43f0d5cc46da9b9270c3e)) | ||
* Message object with validation ([8bf6a9d](https://github.com/uport-project/daf/commit/8bf6a9d47e73d6e2be9003854718b67f59c636dd)) | ||
* Saving message and VC meta data ([1928125](https://github.com/uport-project/daf/commit/1928125e3c3ff17e86e838a9c84ddfadb2631a48)) | ||
# [0.9.0](https://github.com/uport-project/daf/compare/v0.8.0...v0.9.0) (2019-12-05) | ||
@@ -8,0 +26,0 @@ |
{ | ||
"name": "daf-data-store", | ||
"description": "DID Agent Framework Data Store", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"main": "build/index.js", | ||
@@ -12,3 +12,3 @@ "types": "build/index.d.ts", | ||
"blakejs": "^1.1.0", | ||
"daf-core": "^0.9.0", | ||
"daf-core": "^0.10.0", | ||
"debug": "^4.1.1", | ||
@@ -31,3 +31,3 @@ "sql-bricks-sqlite": "^0.1.0" | ||
"keywords": [], | ||
"gitHead": "d185517608d45eaff66690b6be2128ded6946b1c" | ||
"gitHead": "a194a5cf6c9bbe98b033f3add2044175dcc68391" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Types } from 'daf-core' | ||
import { Message } from 'daf-core' | ||
import { DbDriver } from './types' | ||
@@ -40,3 +40,2 @@ import { runMigrations } from './migrations' | ||
hash: row.hash, | ||
parentHash: row.parent_hash, | ||
iss: { did: row.iss }, | ||
@@ -50,7 +49,8 @@ sub: { did: row.sub }, | ||
async credentialsForMessageHash(hash: string) { | ||
async credentialsForMessageId(id: string) { | ||
const query = sql | ||
.select('rowid', '*') | ||
.from('verifiable_credentials') | ||
.where({ parent_hash: hash }) | ||
.select('md.rowid', 'vc.*') | ||
.from('verifiable_credentials_meta_data as md') | ||
.leftJoin('verifiable_credentials as vc', { 'md.hash': 'vc.hash' }) | ||
.where({ 'md.message_id': id }) | ||
.toParams() | ||
@@ -63,3 +63,2 @@ | ||
hash: row.hash, | ||
parentHash: row.parent_hash, | ||
iss: { did: row.iss }, | ||
@@ -131,3 +130,2 @@ sub: { did: row.sub }, | ||
hash: row.hash, | ||
parentHash: row.parent_hash, | ||
iss: { did: row.iss }, | ||
@@ -142,13 +140,25 @@ sub: { did: row.sub }, | ||
async findMessages({ iss, sub, tag, limit }: { iss?: string; sub?: string; tag?: string; limit?: number }) { | ||
async findMessages({ | ||
sender, | ||
receiver, | ||
threadId, | ||
limit, | ||
}: { | ||
sender?: string | ||
receiver?: string | ||
threadId?: string | ||
limit?: number | ||
}) { | ||
let where = {} | ||
if (iss && sub) { | ||
where = sql.or(where, { iss, sub }) | ||
if (sender && receiver) { | ||
where = sql.or(where, { sender, receiver }) | ||
} else { | ||
if (iss) where = sql.and(where, { iss }) | ||
if (sub) where = sql.and(where, { sub }) | ||
if (sender) where = sql.and(where, { sender }) | ||
if (receiver) where = sql.and(where, { receiver }) | ||
} | ||
if (tag) where = sql.and(where, { tag }) | ||
where = sql.or(where, { sub: null }) | ||
if (sender || receiver) { | ||
where = sql.or(where, { receiver: null }) | ||
} | ||
if (threadId) where = sql.and(where, { thread_id: threadId }) | ||
@@ -159,3 +169,3 @@ let query = sql | ||
.where(where) | ||
.orderBy('nbf desc') | ||
.orderBy('timestamp desc') | ||
@@ -171,19 +181,18 @@ if (limit) { | ||
rowId: `${row.rowid}`, | ||
hash: row.hash, | ||
iss: { did: row.iss }, | ||
sub: row.sub ? { did: row.sub } : null, | ||
id: row.id, | ||
sender: row.sender ? { did: row.sender } : null, | ||
receiver: row.receiver ? { did: row.receiver } : null, | ||
type: row.type, | ||
tag: row.tag, | ||
threadId: row.thread_id, | ||
data: row.data, | ||
jwt: row.jwt, | ||
nbf: row.nbf, | ||
iat: row.iat, | ||
raw: row.raw, | ||
timestamp: row.timestamp, | ||
})) | ||
} | ||
async findMessage(hash: string) { | ||
async findMessage(id: string) { | ||
const query = sql | ||
.select('rowid', '*') | ||
.from('messages') | ||
.where({ hash }) | ||
.where({ id }) | ||
.toParams() | ||
@@ -195,10 +204,10 @@ | ||
rowId: `${row.rowid}`, | ||
hash: row.hash, | ||
iss: { did: row.iss }, | ||
sub: row.sub ? { did: row.sub } : null, | ||
id: row.id, | ||
sender: row.sender ? { did: row.sender } : null, | ||
receiver: row.receiver ? { did: row.receiver } : null, | ||
type: row.type, | ||
tag: row.tag, | ||
jwt: row.jwt, | ||
threadId: row.thread_id, | ||
data: row.data, | ||
nbf: row.nbf, | ||
raw: row.raw, | ||
timestamp: row.timestamp, | ||
})) | ||
@@ -212,11 +221,14 @@ | ||
const vcIssuers = await this.db.rows('select distinct iss as did from verifiable_credentials', null) | ||
const messageSubjects = await this.db.rows( | ||
'select distinct sub as did from messages where sub is not null', | ||
const messageReceivers = await this.db.rows( | ||
'select distinct receiver as did from messages where receiver is not null', | ||
null, | ||
) | ||
const messageIssuers = await this.db.rows('select distinct iss as did from messages', null) | ||
const messageSenders = await this.db.rows( | ||
'select distinct sender as did from messages where sender is not null', | ||
null, | ||
) | ||
const uniqueDids = [ | ||
...new Set([ | ||
...messageSubjects.map((item: any) => item.did), | ||
...messageIssuers.map((item: any) => item.did), | ||
...messageReceivers.map((item: any) => item.did), | ||
...messageSenders.map((item: any) => item.did), | ||
...vcIssuers.map((item: any) => item.did), | ||
@@ -248,3 +260,5 @@ ...vcSubjects.map((item: any) => item.did), | ||
.from('messages') | ||
.where(sql.or(sql.and({ iss: did1 }, { sub: did2 }), sql.and({ iss: did2 }, { sub: did1 }))) | ||
.where( | ||
sql.or(sql.and({ sender: did1 }, { receiver: did2 }), sql.and({ sender: did2 }, { receiver: did1 })), | ||
) | ||
.toParams() | ||
@@ -257,4 +271,4 @@ const rows = await this.db.rows(query.text, query.values) | ||
async latestMessageTimestamps() { | ||
let query = | ||
'SELECT * from (select sub as did, nbf as timestamp, source_type as sourceType FROM messages ORDER BY nbf desc) GROUP BY did, sourceType' | ||
let query = `SELECT * FROM ( SELECT m.id, m."timestamp", m.sender AS did, md. "type" AS sourceType, md.id AS sourceId FROM messages AS m | ||
LEFT JOIN messages_meta_data AS md ON m.id = md.message_id) GROUP BY did, sourceType` | ||
@@ -277,32 +291,91 @@ return await this.db.rows(query, []) | ||
async saveMessage(message: Types.ValidatedMessage) { | ||
const source_type = message.meta && message.meta[0].sourceType | ||
const source_id = message.meta && message.meta[0].sourceId | ||
const query = sql | ||
.insert('messages', { | ||
hash: message.hash, | ||
iss: message.issuer, | ||
sub: message.subject, | ||
nbf: message.time, | ||
type: message.type, | ||
tag: message.tag, | ||
jwt: message.raw, | ||
meta: message.meta && JSON.stringify(message.meta), | ||
source_type, | ||
source_id, | ||
}) | ||
async saveMessage(message: Message) { | ||
const messageId = message.id | ||
// Check if the message is already saved | ||
const searchQuery = sql | ||
.select('id') | ||
.from('messages') | ||
.where({ id: messageId }) | ||
.toParams() | ||
const searchResult = await this.db.rows(searchQuery.text, searchQuery.values) | ||
if (searchResult.length > 0) { | ||
this.updateMetaData(message) | ||
} else { | ||
const query = sql | ||
.insert('messages', { | ||
id: messageId, | ||
sender: message.sender, | ||
receiver: message.receiver, | ||
timestamp: message.timestamp, | ||
type: message.type, | ||
thread_id: message.threadId, | ||
raw: message.raw, | ||
data: message.data && JSON.stringify(message.data), | ||
}) | ||
.toParams() | ||
await this.db.run(query.text, query.values) | ||
await this.db.run(query.text, query.values) | ||
if (message.type == 'w3c.vp' || message.type == 'w3c.vc') { | ||
for (const vc of message.custom.vc) { | ||
await this.saveVerifiableCredential(vc, message.hash) | ||
await this.saveMetaData(message) | ||
await this.saveVerifiableCredentials(message) | ||
} | ||
return { hash: message.id, iss: { did: message.sender } } | ||
} | ||
private async updateMetaData(message: Message) { | ||
const { id, allMeta } = message | ||
for (const metaData of allMeta) { | ||
const query = sql | ||
.select('type, id, data') | ||
.from('messages_meta_data') | ||
.where({ | ||
message_id: id, | ||
type: metaData.type, | ||
id: metaData.id, | ||
}) | ||
.toParams() | ||
const rows = await this.db.rows(query.text, query.values) | ||
if (rows.length === 0) { | ||
const insertQuery = sql | ||
.insert('messages_meta_data', { | ||
message_id: id, | ||
type: metaData.type, | ||
id: metaData.id, | ||
data: metaData.data && JSON.stringify(metaData.data), | ||
}) | ||
.toParams() | ||
await this.db.run(insertQuery.text, insertQuery.values) | ||
} | ||
} | ||
} | ||
return { hash: message.hash, iss: { did: message.issuer } } | ||
private async saveMetaData(message: Message) { | ||
const messageId = message.id | ||
for (const metaData of message.allMeta) { | ||
const query = sql | ||
.insert('messages_meta_data', { | ||
message_id: messageId, | ||
type: metaData.type, | ||
id: metaData.id, | ||
data: metaData.data && JSON.stringify(metaData.data), | ||
}) | ||
.toParams() | ||
await this.db.run(query.text, query.values) | ||
} | ||
} | ||
async saveVerifiableCredential(vc: any, messageHash: string) { | ||
async saveVerifiableCredentials(message: Message) { | ||
const messageId = message.id | ||
if (message.type == 'w3c.vp' || message.type == 'w3c.vc') { | ||
for (const vc of message.vc) { | ||
await this.saveVerifiableCredential(vc, messageId) | ||
} | ||
} | ||
} | ||
async saveVerifiableCredential(vc: any, messageId: string) { | ||
const verifiableCredential = vc.payload as any | ||
@@ -312,6 +385,25 @@ | ||
const metaData = sql | ||
.insert('verifiable_credentials_meta_data', { | ||
message_id: messageId, | ||
hash: vcHash, | ||
}) | ||
.toParams() | ||
await this.db.run(metaData.text, metaData.values) | ||
// Check if | ||
const searchQuery = sql | ||
.select('hash') | ||
.from('verifiable_credentials') | ||
.where({ hash: vcHash }) | ||
.toParams() | ||
const searchResult = await this.db.rows(searchQuery.text, searchQuery.values) | ||
if (searchResult.length > 0) { | ||
return vcHash | ||
} | ||
const query = sql | ||
.insert('verifiable_credentials', { | ||
hash: vcHash, | ||
parent_hash: messageHash, | ||
iss: verifiableCredential.iss, | ||
@@ -354,2 +446,42 @@ sub: verifiableCredential.sub, | ||
async findMessagesByVC(hash: string) { | ||
let query = sql | ||
.select('md.rowid', 'm.*') | ||
.from('verifiable_credentials_meta_data as md') | ||
.leftJoin('messages as m', { 'md.message_id': 'm.id' }) | ||
.where({ 'md.hash': hash }) | ||
query = query.toParams() | ||
const rows = await this.db.rows(query.text, query.values) | ||
return rows.map((row: any) => ({ | ||
rowId: `${row.rowid}`, | ||
id: row.id, | ||
sender: row.sender ? { did: row.sender } : null, | ||
receiver: row.receiver ? { did: row.receiver } : null, | ||
type: row.type, | ||
threadId: row.thread_id, | ||
data: row.data, | ||
raw: row.raw, | ||
timestamp: row.timestamp, | ||
})) | ||
} | ||
async messageMetaData(id: string) { | ||
let query = sql | ||
.select('rowid', '*') | ||
.from('messages_meta_data') | ||
.where({ message_id: id }) | ||
query = query.toParams() | ||
const rows = await this.db.rows(query.text, query.values) | ||
return rows.map((row: any) => ({ | ||
rowId: `${row.rowid}`, | ||
type: row.type, | ||
id: row.id, | ||
data: row.data, | ||
})) | ||
} | ||
deleteMessage(hash: string) { | ||
@@ -356,0 +488,0 @@ return this.db.run('DELETE FROM messages where hash=?', [hash]) |
@@ -9,6 +9,13 @@ import { DataStore } from './data-store' | ||
Message: { | ||
vc: async (message: any, {}, { dataStore }: Context) => dataStore.credentialsForMessageHash(message.hash), | ||
vc: async (message: any, {}, { dataStore }: Context) => dataStore.credentialsForMessageId(message.id), | ||
metaData: async (message: any, {}, { dataStore }: Context) => dataStore.messageMetaData(message.id), | ||
thread: async (message: any, {}, { dataStore }: Context) => { | ||
const messages = await dataStore.findMessages({ threadId: message.threadId }) | ||
console.log('AAAA', messages) | ||
return messages.filter((item: any) => item.id !== message.id) | ||
}, | ||
}, | ||
VerifiableClaim: { | ||
fields: async (vc: any, {}, { dataStore }: Context) => dataStore.credentialsFieldsForClaimHash(vc.hash), | ||
inMessages: async (vc: any, {}, { dataStore }: Context) => dataStore.findMessagesByVC(vc.hash), | ||
}, | ||
@@ -51,9 +58,9 @@ Identity: { | ||
messagesSent: async (identity: any, args: any, { dataStore }: Context) => { | ||
return dataStore.findMessages({ iss: identity.did }) | ||
return dataStore.findMessages({ sender: identity.did }) | ||
}, | ||
messagesReceived: async (identity: any, args: any, { dataStore }: Context) => { | ||
return dataStore.findMessages({ sub: identity.did }) | ||
return dataStore.findMessages({ receiver: identity.did }) | ||
}, | ||
messagesAll: async (identity: any, args: any, { dataStore }: Context) => { | ||
return dataStore.findMessages({ iss: identity.did, sub: identity.did }) | ||
return dataStore.findMessages({ sender: identity.did, receiver: identity.did }) | ||
}, | ||
@@ -69,9 +76,13 @@ }, | ||
_: any, | ||
{ iss, sub, tag, limit }: { iss: string; sub: string; tag: string; limit: number }, | ||
{ | ||
sender, | ||
receiver, | ||
threadId, | ||
limit, | ||
}: { sender: string; receiver: string; threadId: string; limit: number }, | ||
{ dataStore }: Context, | ||
) => { | ||
return dataStore.findMessages({ iss, sub, tag, limit }) | ||
return dataStore.findMessages({ sender, receiver, threadId, limit }) | ||
}, | ||
message: async (_: any, { hash }: { hash: string }, { dataStore }: Context) => | ||
dataStore.findMessage(hash), | ||
message: async (_: any, { id }: { id: string }, { dataStore }: Context) => dataStore.findMessage(id), | ||
credentials: async (_: any, { iss, sub }: { iss: string; sub: string }, { dataStore }: Context) => { | ||
@@ -83,4 +94,4 @@ const res = await dataStore.findCredentials({ iss, sub }) | ||
Mutation: { | ||
deleteMessage: async (_: any, { hash }: { hash: string }, { dataStore }: Context) => | ||
dataStore.deleteMessage(hash), | ||
deleteMessage: async (_: any, { id }: { id: string }, { dataStore }: Context) => | ||
dataStore.deleteMessage(id), | ||
}, | ||
@@ -93,4 +104,4 @@ } | ||
identities(dids: [ID!]): [Identity] | ||
messages(iss: ID, sub: ID, tag: String, limit: Int): [Message] | ||
message(hash: ID!): Message! | ||
messages(sender: ID, reveiver: ID, threadId: String, limit: Int): [Message] | ||
message(id: ID!): Message! | ||
credentials(iss: ID, sub: ID): [VerifiableClaim] | ||
@@ -120,10 +131,2 @@ } | ||
extend type Message { | ||
iss: Identity! | ||
sub: Identity | ||
jwt: String! | ||
data: String! | ||
iat: Int | ||
nbf: Int | ||
vis: String | ||
tag: String | ||
vc: [VerifiableClaim] | ||
@@ -134,3 +137,2 @@ } | ||
hash: ID! | ||
parentHash: ID! | ||
rowId: String! | ||
@@ -145,2 +147,3 @@ iss: Identity! | ||
fields: [VerifiableClaimField] | ||
inMessages: [Message] | ||
} | ||
@@ -147,0 +150,0 @@ |
@@ -7,17 +7,11 @@ import { DbDriver, Migration } from '../types' | ||
`CREATE TABLE IF NOT EXISTS messages ( | ||
hash TEXT, | ||
parent_hash TEXT, | ||
iss TEXT, | ||
sub TEXT, | ||
id TEXT PRIMARY KEY, | ||
thread_id TEXT, | ||
sender TEXT, | ||
receiver TEXT, | ||
type TEXT, | ||
tag TEXT, | ||
data TEXT, | ||
iat NUMERIC, | ||
nbf NUMERIC, | ||
jwt TEXT, | ||
meta TEXT, | ||
source_type TEXT, | ||
source_id TEXT, | ||
internal NUMERIC NOT NULL default 1 | ||
);`, | ||
raw TEXT, | ||
timestamp NUMERIC | ||
);`, | ||
[], | ||
@@ -27,5 +21,14 @@ ) | ||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS messages_meta_data ( | ||
message_id TEXT, | ||
data TEXT, | ||
type TEXT, | ||
id TEXT | ||
);`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS verifiable_credentials ( | ||
hash TEXT, | ||
parent_hash TEXT, | ||
iss TEXT, | ||
@@ -43,2 +46,10 @@ aud TEXT, | ||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS verifiable_credentials_meta_data ( | ||
message_id TEXT, | ||
hash TEXT | ||
);`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS verifiable_credentials_fields ( | ||
@@ -55,17 +66,3 @@ parent_hash INTEGER, | ||
) | ||
await db.run( | ||
`CREATE TRIGGER IF NOT EXISTS delete_messages BEFORE DELETE ON "messages" BEGIN | ||
DELETE FROM verifiable_credentials where parent_hash = old.hash; | ||
END;`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE TRIGGER IF NOT EXISTS delete_verifiable_credentials BEFORE DELETE ON "verifiable_credentials" BEGIN | ||
DELETE FROM verifiable_credentials_fields where parent_hash = old.hash; | ||
END;`, | ||
[], | ||
) | ||
}, | ||
} |
@@ -5,21 +5,17 @@ import { DbDriver, Migration } from '../types' | ||
run: async (db: DbDriver) => { | ||
await db.run(`CREATE INDEX IF NOT EXISTS "messages_id" ON "messages" ("id");`, []) | ||
await db.run(`CREATE INDEX IF NOT EXISTS "messages_thread_id" ON "messages" ("thread_id");`, []) | ||
await db.run(`CREATE INDEX IF NOT EXISTS "messages_receiver" ON "messages" ("receiver");`, []) | ||
await db.run(`CREATE INDEX IF NOT EXISTS "messages_sender" ON "messages" ("sender");`, []) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "messages_hash" ON "messages" ("hash");`, | ||
`CREATE INDEX IF NOT EXISTS "messages_meta_data_message_id" ON "messages_meta_data" ("message_id");`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "messages_parent_hash" ON "messages" ("parent_hash");`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "messages_sub" ON "messages" ("sub");`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "messages_iss" ON "messages" ("iss");`, | ||
[], | ||
) | ||
await db.run(`CREATE INDEX IF NOT EXISTS "messages_meta_data_type" ON "messages_meta_data" ("type");`, []) | ||
await db.run(`CREATE INDEX IF NOT EXISTS "messages_meta_data_id" ON "messages_meta_data" ("id");`, []) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "messages_source_type" ON "messages" ("source_type");`, | ||
`CREATE INDEX IF NOT EXISTS "verifiable_credentials_meta_data_message_id" ON "verifiable_credentials_meta_data" ("message_id");`, | ||
[], | ||
@@ -29,3 +25,3 @@ ) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "messages_source_id" ON "messages" ("source_id");`, | ||
`CREATE INDEX IF NOT EXISTS "verifiable_credentials_meta_data_hash" ON "verifiable_credentials_meta_data" ("hash");`, | ||
[], | ||
@@ -35,6 +31,2 @@ ) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "verifiable_credentials_parent_hash" ON "verifiable_credentials" ("parent_hash");`, | ||
[], | ||
) | ||
await db.run( | ||
`CREATE INDEX IF NOT EXISTS "verifiable_credentials_iss" ON "verifiable_credentials" ("iss");`, | ||
@@ -41,0 +33,0 @@ [], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
137977
2201
+ Addeddaf-core@0.10.3(transitive)
- Removeddaf-core@0.9.0(transitive)
Updateddaf-core@^0.10.0