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

Commit

Permalink
fix: backup improvement. (#26)
Browse files Browse the repository at this point in the history
* feat: add pluto export and import database functionality

* fix: Improve database.
  • Loading branch information
elribonazo authored Oct 28, 2023
1 parent 8447f6f commit 205d93a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const db = await Database.createEncrypted(
password
);
const messages = await db.getAllMessages();

const backup = await db.backup();
const db = await Database.createEncrypted(
databaseName + "restored",
password,
backup
);


```

If the database is later initialised with the wrong password the "createEncrypted" async function will throw an exception and will not let you decrypt any encrypted content.
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,27 @@ export class Database implements Domain.Pluto {
return this.db.exportJSON();
}

async import(importData: RxDumpDatabase<PlutoCollections>) {
await this.db.importJSON(importData);
}

static async createEncrypted(name: string, encryptionKey: Uint8Array) {
return new Database({
static async createEncrypted(
name: string,
encryptionKey: Uint8Array,
importData?: RxDumpDatabase<PlutoCollections>
) {
const database = new Database({
ignoreDuplicate: true,
name: name,
storage: wrappedKeyEncryptionCryptoJsStorage({
storage: getRxStorageDexie(),
}),
password: Buffer.from(encryptionKey).toString("hex"),
});

await database.start();

if (importData) {
await database.db.importJSON(importData);
}

return database;
}

async getMessage(id: string): Promise<Domain.Message | null> {
Expand Down
14 changes: 2 additions & 12 deletions tests/pluto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,12 @@ describe("Pluto + Dexie encrypted integration for browsers", () => {
sandbox = sinon.createSandbox();
});

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);
}
Expand All @@ -84,7 +76,6 @@ describe("Pluto + Dexie encrypted integration for browsers", () => {
`${databaseName}${randomUUID()}`,
defaultPassword
);
await db.start();
});

it("Should store a new Prism DID and its privateKeys", async () => {
Expand Down Expand Up @@ -443,10 +434,9 @@ describe("Pluto + Dexie encrypted integration for browsers", () => {

const restored = await Database.createEncrypted(
`${databaseName}${randomUUID()}`,
defaultPassword
defaultPassword,
backup
);
await restored.start();
await restored.import(backup);

expect((await restored.getAllMediators()).length).toBe(1);
});
Expand Down

0 comments on commit 205d93a

Please sign in to comment.