Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
feat: Implement Messaging and improve test coverage to decent levels.
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo authored Oct 1, 2023
1 parent d493479 commit 38acb81
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 49 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
],
"coverageThreshold": {
"global": {
"branches": 36,
"functions": 61,
"lines": 58,
"statements": 59
"branches": 58,
"functions": 71,
"lines": 74,
"statements": 71
}
},
"coverageDirectory": "coverage"
Expand Down
170 changes: 128 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@input-output-hk/atala-prism-wallet-sdk";
import { getRxStorageDexie } from "rxdb/plugins/storage-dexie";
import { wrappedKeyEncryptionCryptoJsStorage } from "rxdb/plugins/encryption-crypto-js";
import { RxCollection, RxDatabase, RxDatabaseCreator, RxDocument, RxJsonSchema, createRxDatabase } from "rxdb";
import { MangoQuerySelector, RxCollection, RxDatabase, RxDatabaseCreator, RxDocument, RxJsonSchema, createRxDatabase } from "rxdb";
import { RxError } from "rxdb/dist/lib/rx-error";
import { addRxPlugin } from "rxdb";
import { RxDBMigrationPlugin } from "rxdb/plugins/migration";
Expand Down Expand Up @@ -93,19 +93,28 @@ export class Database implements Domain.Pluto {
}

async getMessage(id: string): Promise<Domain.Message | null> {
return this.db.messages.findOne().where({ id: id }).exec();
const message = await this.db.messages.findOne().where({ id: id }).exec()
if (message) {
return this.getDomainMessage(message)
}
return null;
}

async storeMessage(message: Domain.Message): Promise<void> {
await this.db.messages.insert(message);
await this.db.messages.insert({
...message,
to: message.to?.toString(),
from: message.from?.toString()
});
}

async storeMessages(messages: Domain.Message[]): Promise<void> {
await Promise.all(messages.map((message) => this.storeMessage(message)));
}

async getAllMessages(): Promise<Domain.Message[]> {
return this.db.messages.find().exec();
const messages = await this.db.messages.find().exec()
return messages.map((message) => this.getDomainMessage(message))
}

async start(): Promise<void> {
Expand Down Expand Up @@ -198,21 +207,21 @@ export class Database implements Domain.Pluto {
privateKey: Domain.PrivateKey,
did: Domain.DID,
keyPathIndex: number,
metaId: string | null
metaId?: string | null
): Promise<void> {
await this.db.privateKeys.insert({
id: uuidv4(),
did: did.toString(),
type: privateKey.type,
keySpecification: Array.from(privateKey.keySpecification).reduce(
(all, [key, value]) => {
all.push({
(all, [key, value]) => [
...all,
{
type: "string",
name: key,
value: `${value}`,
});
return all;
},
},
],
[
{
type: "string",
Expand Down Expand Up @@ -447,15 +456,18 @@ export class Database implements Domain.Pluto {
Domain.DID.fromString(didDB.did)
);

if (privateKey) {
const indexProp = privateKey.getProperty(Domain.KeyProperties.index);
const index = indexProp ? parseInt(indexProp) : undefined;
return new Domain.PrismDIDInfo(
Domain.DID.fromString(didDB.did),
index,
didDB.alias
);
if (!privateKey) {
throw new Error("Imposible to recover PrismDIDInfo without its privateKey data.")
}

const indexProp = privateKey.getProperty(Domain.KeyProperties.index);
const index = indexProp ? parseInt(indexProp) : undefined;
return new Domain.PrismDIDInfo(
Domain.DID.fromString(didDB.did),
index,
didDB.alias
);

}

return null;
Expand Down Expand Up @@ -483,53 +495,127 @@ export class Database implements Domain.Pluto {
return prismDIDInfo;
}

getPrismDIDKeyPathIndex(did: Domain.DID): Promise<number | null> {
throw new Error("Method not implemented.");

private getDomainMessage(message: RxDocument<MessageSchemaType, {}>) {
return Domain.Message.fromJson(JSON.stringify(message.toJSON()))
}

getPrismLastKeyPathIndex(): Promise<number> {
throw new Error("Method not implemented.");
async getAllMessagesByDID(did: Domain.DID): Promise<Domain.Message[]> {
const messages = await this.db.messages.find().where({
$or: [
{
to: did.toString()
},
{
from: did.toString()
}
]
}).exec()
return messages.map(message => this.getDomainMessage(message))
}

storeCredential(credential: Domain.VerifiableCredential): Promise<void> {
throw new Error("Method not implemented.");
async getAllMessagesSent(): Promise<Domain.Message[]> {
const messages = await this.db.messages.find().where({
$or: [
{
direction: Domain.MessageDirection.SENT
}
]
}).exec()
return messages.map(message => this.getDomainMessage(message))
}

getAllPeerDIDs(): Promise<Domain.PeerDID[]> {
throw new Error("Method not implemented.");
async getAllMessagesReceived(): Promise<Domain.Message[]> {
const messages = await this.db.messages.find().where({
$or: [
{
direction: Domain.MessageDirection.SENT
}
]
}).exec()
return messages.map(message => this.getDomainMessage(message))
}

getAllMessagesByDID(did: Domain.DID): Promise<Domain.Message[]> {
throw new Error("Method not implemented.");
async getAllMessagesSentTo(did: Domain.DID): Promise<Domain.Message[]> {
const messages = await this.db.messages.find().where({
$or: [
{
to: did.toString()
}
]
}).exec()
return messages.map(message => this.getDomainMessage(message))
}

getAllMessagesSent(): Promise<Domain.Message[]> {
throw new Error("Method not implemented.");
async getAllMessagesReceivedFrom(did: Domain.DID): Promise<Domain.Message[]> {
const messages = await this.db.messages.find().where({
$or: [
{
from: did.toString()
}
]
}).exec()
return messages.map(message => this.getDomainMessage(message))
}

getAllMessagesReceived(): Promise<Domain.Message[]> {
throw new Error("Method not implemented.");
async getAllMessagesOfType(
type: string,
relatedWithDID?: Domain.DID | undefined
): Promise<Domain.Message[]> {
const query: MangoQuerySelector<MessageSchemaType>[] = [
{
piuri: type
},
];
if (relatedWithDID) {
query.push({
$or: [
{
from: relatedWithDID.toString()
},
{
to: relatedWithDID.toString()
}
]
})
}
const messages = await this.db.messages.find().where({
$and: query
}).exec()
return messages.map(message => this.getDomainMessage(message))
}

getAllMessagesSentTo(did: Domain.DID): Promise<Domain.Message[]> {
async getAllMessagesByFromToDID(
from: Domain.DID,
to: Domain.DID
): Promise<Domain.Message[]> {
const messages = await this.db.messages.find().where({
$or: [
{
from: from.toString()
},
{
to: to.toString()
}
]
}).exec()
return messages.map(message => this.getDomainMessage(message))
}


getPrismDIDKeyPathIndex(did: Domain.DID): Promise<number | null> {
throw new Error("Method not implemented.");
}

getAllMessagesReceivedFrom(did: Domain.DID): Promise<Domain.Message[]> {
getPrismLastKeyPathIndex(): Promise<number> {
throw new Error("Method not implemented.");
}

getAllMessagesOfType(
type: string,
relatedWithDID?: Domain.DID | undefined
): Promise<Domain.Message[]> {
storeCredential(credential: Domain.VerifiableCredential): Promise<void> {
throw new Error("Method not implemented.");
}

getAllMessagesByFromToDID(
from: Domain.DID,
to: Domain.DID
): Promise<Domain.Message[]> {
getAllPeerDIDs(): Promise<Domain.PeerDID[]> {
throw new Error("Method not implemented.");
}

Expand Down
52 changes: 51 additions & 1 deletion src/schemas/Message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import type { Domain } from "@input-output-hk/atala-prism-wallet-sdk";
import type { Schema } from "../types";

export type MessageSchemaType = Domain.Message;


export type MessageSchemaType = {
readonly body: string;
readonly id: string;
readonly piuri: string;
readonly from?: string | undefined;
readonly to?: string | undefined;
readonly attachments: Domain.AttachmentDescriptor[];
readonly thid?: string;
readonly extraHeaders: string[];
readonly createdTime: string;
readonly expiresTimePlus: string;
readonly ack: string[];
readonly direction: Domain.MessageDirection;
readonly fromPrior?: string | undefined;
readonly pthid?: string | undefined;
};

/**
* MessageSchema
Expand All @@ -23,6 +40,39 @@ const MessageSchema: Schema<MessageSchemaType> = {
},
attachments: {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "id",
maxLength: 60,
},
description: {
type: "string",
},
byteCount: {
type: "number",
},
lastModTime: {
type: "string",
},
format: {
type: 'string'
},
filename: {
type: "array",
items: {
type: "string"
}
},
mediaType: {
type: 'string'
},
data: {
type: 'object'
}
},
},
},
extraHeaders: {
type: "array",
Expand Down
Loading

0 comments on commit 38acb81

Please sign in to comment.