diff --git a/package-lock.json b/package-lock.json index aa790ecc..e19b564c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23084,7 +23084,7 @@ "dependencies": { "@atala/prism-wallet-sdk": "^4.0.2", "@pluto-encrypted/encryption": "1.11.0", - "@pluto-encrypted/schemas": "^1.0.0", + "@pluto-encrypted/schemas": "^1.3.3", "@pluto-encrypted/shared": "1.11.3", "rxdb": "^14.17.0", "rxjs": "7.8.1", @@ -23095,7 +23095,7 @@ "@pluto-encrypted/indexdb": "1.12.2", "@pluto-encrypted/inmemory": "1.12.2", "@pluto-encrypted/leveldb": "1.12.2", - "@pluto-encrypted/schemas": "^1.0.0", + "@pluto-encrypted/schemas": "^1.3.3", "@pluto-encrypted/shared": "1.11.3", "@vitest/coverage-v8": "^1.0.4", "level": "^6.0.1" @@ -23238,7 +23238,7 @@ }, "packages/schemas": { "name": "@pluto-encrypted/schemas", - "version": "1.3.2", + "version": "1.3.3", "license": "Apache-2.0", "dependencies": { "@atala/prism-wallet-sdk": "^4.0.2", diff --git a/packages/database/package.json b/packages/database/package.json index f774d3b5..9a151f07 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -46,7 +46,7 @@ "@pluto-encrypted/inmemory": "1.12.2", "@pluto-encrypted/leveldb": "1.12.2", "@pluto-encrypted/shared": "1.11.3", - "@pluto-encrypted/schemas": "^1.0.0", + "@pluto-encrypted/schemas": "^1.3.3", "@vitest/coverage-v8": "^1.0.4", "level": "^6.0.1" }, @@ -54,7 +54,7 @@ "@atala/prism-wallet-sdk": "^4.0.2", "@pluto-encrypted/encryption": "1.11.0", "@pluto-encrypted/shared": "1.11.3", - "@pluto-encrypted/schemas": "^1.0.0", + "@pluto-encrypted/schemas": "^1.3.3", "rxdb": "^14.17.0", "rxjs": "7.8.1", "uuid": "^9.0.1" @@ -63,4 +63,4 @@ "build/*" ], "gitHead": "6aa4ce8bbb9938fc4c4e790efffa5b902631362c" -} +} \ No newline at end of file diff --git a/packages/database/tests/pluto.test.ts b/packages/database/tests/pluto.test.ts index cf33f6db..1a2c4e52 100644 --- a/packages/database/tests/pluto.test.ts +++ b/packages/database/tests/pluto.test.ts @@ -4,7 +4,7 @@ import { describe, it, beforeEach, afterEach } from 'vitest'; import fs from 'fs'; import path from 'path'; import { randomUUID } from "crypto"; -import SDK from "@atala/prism-wallet-sdk"; +import SDK, { JWTCredential } from "@atala/prism-wallet-sdk"; import * as sinon from "sinon"; import { RxCollection, RxStorage } from "rxdb"; import InMemory from "../../inmemory/src"; @@ -215,15 +215,17 @@ describe("Pluto encrypted testing with different storages", () => { "did:prism:733e594871d7700d35e6116011a08fc11e88ff9d366d8b5571ffc1aa18d249ea:Ct8BCtwBEnQKH2F1dGhlbnRpY2F0aW9uYXV0aGVudGljYXRpb25LZXkQBEJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpxJkCg9tYXN0ZXJtYXN0ZXJLZXkQAUJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpw2" ); const privateKey = Fixtures.secp256K1.privateKey; + expect(await db.getPrismLastKeyPathIndex()).toBe(0); + await db.storePrismDID(did, 0, privateKey, null); expect((await db.getAllPrismDIDs()).length).toBe(1); expect(await db.getDIDInfoByDID(did)).not.toBe(null); - expect(await db.getPrismDIDKeyPathIndex(did)).toBe(0); - expect(await db.getPrismLastKeyPathIndex()).toBe(0); - await db.storePrismDID(did2, 1, privateKey, null); - expect(await db.getPrismDIDKeyPathIndex(did2)).toBe(1); + expect(await db.getPrismDIDKeyPathIndex(did)).toBe(1); expect(await db.getPrismLastKeyPathIndex()).toBe(1); - expect(await db.getPrismDIDKeyPathIndex(did3)).toBe(null); + await db.storePrismDID(did2, 1, privateKey, null); + expect(await db.getPrismDIDKeyPathIndex(did2)).toBe(2); + expect(await db.getPrismLastKeyPathIndex()).toBe(2); + expect(await db.getPrismDIDKeyPathIndex(did3)).toBe(0); }); it(storageName + "Should throw an exception if a wrong key object from Database is loaded", async ({ expect }) => { @@ -665,6 +667,93 @@ describe("Pluto encrypted testing with different storages", () => { expect(results.length).toBe(1); }); + it(storageName + "Should store and fetch a JWT Credential and then generate a proof", async ({ expect }) => { + class PresentationRequest { + /** + * @constructor + * @param type - CredentialType the json is related to + * @param json - the raw value + */ + constructor(type: SDK.Domain.CredentialType.AnonCreds, json: SDK.Domain.Anoncreds.PresentationRequest); + constructor(type: SDK.Domain.CredentialType.JWT, json: any); + constructor( + private readonly type: SDK.Domain.CredentialType, + private readonly json: T + ) { } + + /** + * Type guard that the instance is for the given CredentialType + * + * @param type + * @returns {boolean} + */ + isType(type: SDK.Domain.CredentialType.AnonCreds): this is PresentationRequest; + isType(type: SDK.Domain.CredentialType.JWT): this is PresentationRequest; + isType(target: SDK.Domain.CredentialType) { + return this.type === target; + } + + /** + * Get the raw PresentationRequest JSON + * + * @returns JSON + */ + toJSON(): T { + return this.json; + } + } + + expect((await db.getAllCredentials()).length).toBe(0); + const jwtPayload = Fixtures.createJWTPayload( + "jwtid", + "proof", + SDK.Domain.CredentialType.JWT + ); + const encoded = encodeJWTCredential(jwtPayload); + + const parsedCredential = await pollux.parseCredential(Buffer.from(encoded), { + type: SDK.Domain.CredentialType.JWT, + }); + await db.storeCredential(parsedCredential); + + + const credentials = await db.getAllCredentials() + expect(credentials.length).toBe(1); + + const credential = credentials[0]; + const apollo = new Apollo(); + + const pr = new PresentationRequest(SDK.Domain.CredentialType.JWT, { + "options": { + "challenge": "11c91493-01b3-4c4d-ac36-b336bab5bddf", + "domain": "http://localhost:8000/prism-agent" + }, + "presentation_definition": { + "format": null, + "id": "b2a49475-f8ba-4952-a719-a28e909858fa", + "input_descriptors": [], + "name": null, + "purpose": null + } + }); + const key = apollo.createPrivateKey({ + type: SDK.Domain.KeyTypes.EC, + curve: SDK.Domain.Curve.SECP256K1, + seed: Buffer.from(apollo.createRandomSeed().seed.value).toString('hex') + }); + const did = SDK.Domain.DID.fromString("did:prism:333333"); + const result = await pollux.createPresentationProof(pr as any, credential as any, { + did, + privateKey: key + }); + + expect(result).not.to.be.null; + }) + + + + + it(storageName + "Should store and fetch a Anoncreds Credential", async ({ expect }) => { expect((await db.getAllCredentials()).length).toBe(0); const payload = Fixtures.createAnonCredsPayload(); diff --git a/packages/schemas/src/schemas/did/static.ts b/packages/schemas/src/schemas/did/static.ts index 5474ab90..b316cc07 100644 --- a/packages/schemas/src/schemas/did/static.ts +++ b/packages/schemas/src/schemas/did/static.ts @@ -111,9 +111,9 @@ export const DIDStaticMethods: DIDStaticMethodTypes = { async getPrismDIDKeyPathIndex(did: SDK.Domain.DID): Promise { const [key] = await this.getDIDPrivateKeysByDID!(did) if (!key) { - return null + return 0 } - return key.index++ || 0 + return key.index + 1 }, async getPrismLastKeyPathIndex(): Promise { @@ -121,7 +121,8 @@ export const DIDStaticMethods: DIDStaticMethodTypes = { if (!results || results.length === 0) { return 0 } - return Math.max(...results.map((result) => result.keyPathIndex)) + const maxim = Math.max(...results.map((result) => result.keyPathIndex)) + return maxim + 1 }, async getAllPeerDIDs(): Promise { const peerDIDs: SDK.PeerDID[] = []