Skip to content

Commit

Permalink
feat!: Make pluto encrypted package storage agnostic.
Browse files Browse the repository at this point in the history
BREAKING: In order to create a database you need to now pass an object with the same properties, rather than 2 parameters, now you have an object which also accepts a storage resolver so you can integrate any compatible storage.
Signed-off-by: Francisco Javier Ribó Labrador <elribonazo@gmail.com>
  • Loading branch information
elribonazo committed Dec 3, 2023
1 parent 8f9d6b7 commit 23a4f09
Show file tree
Hide file tree
Showing 6 changed files with 614 additions and 560 deletions.
14 changes: 11 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
"request": "launch",
"name": "TEST",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": ["--colors", "--workerThreads", "--maxWorkers", "1"],
"args": [
"--colors",
"--workerThreads",
"--maxWorkers",
"1"
],
"skipFiles": [
"${workspaceRoot}/../../node_modules/**/*",
"<node_internals>/**/*"
]
],
"env": {
"NODE_ENV": "debug"
}
}
]
}
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
"main": "./build/index.js",
"types": "./build/index.d.ts",
"type": "module",
"exports": {
"./indexdb": "./build/IndexDB.js",
"./inmemory": "./build/InMemory.js"
},
"scripts": {
"clean": "rm -rf build",
"build": "npx rollup -c rollup/rollup.mjs",
"prepublishOnly": "npm run build",
"test": "jest tests/*.test.ts",
"test": "NODE_ENV=debug jest tests/*.test.ts",
"coverage": "npm run test -- --coverage",
"docs": "npx typedoc --options typedoc.json --hideGenerator"
},
Expand Down Expand Up @@ -148,4 +152,4 @@
"rxdb": "^14.17.0",
"uuid": "^9.0.1"
}
}
}
5 changes: 4 additions & 1 deletion rollup/rollup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ function CreateConfig(buildPath, plugins = [], extraInputs = []) {
};
}

export default CreateConfig(undefined, [nodePolyfills()]);
export default CreateConfig(undefined, [nodePolyfills()], [
"src/storage/IndexDB.ts"

]);
39 changes: 22 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Domain } from "@atala/prism-wallet-sdk";
import { getRxStorageDexie } from "rxdb/plugins/storage-dexie";
import { wrappedKeyEncryptionCryptoJsStorage } from "rxdb/plugins/encryption-crypto-js";
import {
MangoQuerySelector,
RxCollection,
RxDatabase,
RxDatabaseCreator,
RxDumpDatabase,
RxStorage,
createRxDatabase,
} from "rxdb";
import { RxError } from "rxdb";
Expand Down Expand Up @@ -84,23 +83,29 @@ export class Database implements Domain.Pluto {
return this._db;
}

constructor(private dbOptions: RxDatabaseCreator) {}
constructor(private dbOptions: RxDatabaseCreator) { }

async backup() {
return this.db.exportJSON();
}

static async createEncrypted(
name: string,
encryptionKey: Uint8Array,
importData?: RxDumpDatabase<PlutoCollections>
options: {
name: string,
encryptionKey: Uint8Array,
importData?: RxDumpDatabase<PlutoCollections>,
storageResolver: () => Promise<RxStorage<any, any>>
}
) {
const { name, storageResolver, encryptionKey, importData } = options;
if (!storageResolver) {
throw new Error("Please provide a valid storage resolver fn");
}
const storage = await storageResolver()
const database = new Database({
ignoreDuplicate: true,
name: name,
storage: wrappedKeyEncryptionCryptoJsStorage({
storage: getRxStorageDexie(),
}),
storage: storage,
password: Buffer.from(encryptionKey).toString("hex"),
});

Expand Down Expand Up @@ -332,10 +337,10 @@ export class Database implements Domain.Pluto {

return didPair
? new DIDPair(
DID.fromString(didPair.hostDID),
DID.fromString(didPair.receiverDID),
didPair.name
)
DID.fromString(didPair.hostDID),
DID.fromString(didPair.receiverDID),
didPair.name
)
: null;
}

Expand All @@ -354,10 +359,10 @@ export class Database implements Domain.Pluto {

return didPair
? new DIDPair(
DID.fromString(didPair.hostDID),
DID.fromString(didPair.receiverDID),
didPair.name
)
DID.fromString(didPair.hostDID),
DID.fromString(didPair.receiverDID),
didPair.name
)
: null;
}

Expand Down
9 changes: 9 additions & 0 deletions src/storage/IndexDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RxStorage } from "rxdb";
import { getRxStorageDexie } from "rxdb/plugins/storage-dexie";
import { wrappedKeyEncryptionCryptoJsStorage } from "rxdb/plugins/encryption-crypto-js";

const storage: RxStorage<any, any> = wrappedKeyEncryptionCryptoJsStorage({
storage: getRxStorageDexie(),
})

export default storage;
Loading

0 comments on commit 23a4f09

Please sign in to comment.