Skip to content

Commit

Permalink
fix: Implementing getDIDInfoByAlias, getDIDInfoByDID, getAllPrismDID…
Browse files Browse the repository at this point in the history
…s, storeMediator, getDIDPrivateKeyByID, getDIDPrivateKeysByDID

* fix: Implement pluto start method and improve error handling.

* fix: Implementing getDIDInfoByAlias, getDIDInfoByDID, getAllPrismDIDs, storeMediator, getDIDPrivateKeyByID, getDIDPrivateKeysByDID

* Add tests.
  • Loading branch information
elribonazo authored Oct 1, 2023
1 parent 48ec5f7 commit 3e2e9e5
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 128 deletions.
398 changes: 280 additions & 118 deletions src/index.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/schemas/Credential.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CredentialSchemaType, Schema } from "../types";
import type { CredentialSchemaType, Schema } from "../types";

const CredentialSchema: Schema<CredentialSchemaType> = {
version: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/schemas/DID.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DIDSchemaType, Schema } from "../types";
import type { DIDSchemaType, Schema } from "../types";

const DIDSchema: Schema<DIDSchemaType> = {
version: 0,
Expand Down Expand Up @@ -26,7 +26,7 @@ const DIDSchema: Schema<DIDSchemaType> = {
maxLength: 60,
},
},
encrypted: ["method", "methodId", "schema"],
encrypted: [],
required: ["method", "methodId", "did", "schema"],
};

Expand Down
2 changes: 1 addition & 1 deletion src/schemas/DIDPair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DIDPairSchemaType, Schema } from "../types";
import type { DIDPairSchemaType, Schema } from "../types";

const DIDPairSchema: Schema<DIDPairSchemaType> = {
version: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/Mediator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, MediarorSchemaType } from "../types";
import type { Schema, MediarorSchemaType } from "../types";

const MediatorSchema: Schema<MediarorSchemaType> = {
version: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/Message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, MessageSchemaType } from "../types";
import type { Schema, MessageSchemaType } from "../types";

const MessageSchema: Schema<MessageSchemaType> = {
version: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, KeySchemaType } from "../types";
import type { Schema, KeySchemaType } from "../types";

const PrivateKeySchema: Schema<KeySchemaType> = {
version: 0,
Expand Down
115 changes: 112 additions & 3 deletions tests/pluto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@ import { randomUUID } from "crypto";
import { Apollo, Domain } from "@input-output-hk/atala-prism-wallet-sdk";

const databaseName = "prism-db";
const keyData = new Uint8Array(32);

const createMessage = () => new Domain.Message("{}", randomUUID(), "");
const defaultPassword = Buffer.from(keyData);
const apollo = new Apollo();

describe("Pluto + Dexie encrypted integration for browsers", () => {
it("Should require to start pluto database before using it", async () => {
const db = await Database.createEncrypted(databaseName, defaultPassword);
expect(db.getAllMessages()).rejects.toThrowError(
new Error("Start Pluto first.")
);
});

it("Should be able to instanciate an encrypted IndexDB Database and throw an error if started with wrong password", async () => {
async function createAndLoad(password: Uint8Array) {
const db = await Database.createEncrypted(
databaseName,
Buffer.from(password)
);
await db.start();
const messages = await db.getAllMessages();
expect(messages.length).toEqual(0);
}

const keyData = new Uint8Array(32);
await createAndLoad(keyData);

const keyData2 = keyData;
keyData2[0] = 1;
keyData2[1] = 2;
Expand All @@ -32,15 +45,111 @@ describe("Pluto + Dexie encrypted integration for browsers", () => {
it("Should store a new DID and its privateKeys", async () => {
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
Buffer.from(new Uint8Array(32))
defaultPassword
);
await db.start();
const did = Domain.DID.fromString(
"did:prism:733e594871d7700d35e6116011a08fc11e88ff9d366d8b5571ffc1aa18d249ea:Ct8BCtwBEnQKH2F1dGhlbnRpY2F0aW9uYXV0aGVudGljYXRpb25LZXkQBEJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpxJkCg9tYXN0ZXJtYXN0ZXJLZXkQAUJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpw"
);
const privateKey = new Apollo().createPrivateKey({
const privateKey = apollo.createPrivateKey({
type: Domain.KeyTypes.EC,
curve: Domain.Curve.ED25519,
});
await db.storePrismDID(did, 0, privateKey);
});

it("Should store a Message", async () => {
const message = createMessage();
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
await db.storeMessage(message);
const dbMesaage = await db.getMessage(message.id);
expect(dbMesaage).not.toBe(null);
expect(dbMesaage!.id).toBe(message.id);
});

it("Should return null if message is not found by id ", async () => {
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
const dbMesaage = await db.getMessage("notfound");
expect(dbMesaage).toBe(null);
});

it("Should store multiple messages", async () => {
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
await db.storeMessages([createMessage(), createMessage()]);
});

it("Should store a peerDID", async () => {
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
const did = new Domain.DID(
"did",
"peer",
"2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0"
);
await db.storePeerDID(did, [
apollo.createPrivateKey({
type: Domain.KeyTypes.EC,
curve: Domain.Curve.ED25519,
}),
apollo.createPrivateKey({
type: Domain.KeyTypes.Curve25519,
curve: Domain.Curve.X25519,
}),
]);
});

it("Should store a didPair", async () => {
const host = Domain.DID.fromString("did:prism:123456");
const receiver = Domain.DID.fromString("did:prism:654321");
const name = "example";
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
await db.storeDIDPair(host, receiver, name);
});

it("Should get all the didPairs", async () => {
const host = Domain.DID.fromString("did:prism:123456");
const receiver = Domain.DID.fromString("did:prism:654321");
const name = "example";
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();

expect((await db.getAllDidPairs()).length).toBe(0);
await db.storeDIDPair(host, receiver, name);
expect((await db.getAllDidPairs()).length).toBe(1);
});

it("Should get a did pair by its did", async () => {
const host = Domain.DID.fromString("did:prism:123456");
const receiver = Domain.DID.fromString("did:prism:654321");
const name = "example";
const db = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
await db.storeDIDPair(host, receiver, name);
expect(await db.getPairByDID(host)).not.toBe(null);
});
});

0 comments on commit 3e2e9e5

Please sign in to comment.