Skip to content

Commit

Permalink
fix: database and schemas fix for last key index for prism did keys.
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribó Labrador <elribonazo@gmail.com>
  • Loading branch information
elribonazo committed Feb 10, 2024
1 parent 0882d43 commit b88fa6e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 15 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
"@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"
},
"dependencies": {
"@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"
Expand All @@ -63,4 +63,4 @@
"build/*"
],
"gitHead": "6aa4ce8bbb9938fc4c4e790efffa5b902631362c"
}
}
101 changes: 95 additions & 6 deletions packages/database/tests/pluto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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<T = unknown> {
/**
* @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<SDK.Domain.Anoncreds.PresentationRequest>;
isType(type: SDK.Domain.CredentialType.JWT): this is PresentationRequest<any>;
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();
Expand Down
7 changes: 4 additions & 3 deletions packages/schemas/src/schemas/did/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,18 @@ export const DIDStaticMethods: DIDStaticMethodTypes = {
async getPrismDIDKeyPathIndex(did: SDK.Domain.DID): Promise<number | null> {
const [key] = await this.getDIDPrivateKeysByDID!(did)
if (!key) {
return null
return 0
}
return key.index++ || 0
return key.index + 1
},

async getPrismLastKeyPathIndex(): Promise<number> {
const results = await this.getAllPrismDIDs!()
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<SDK.PeerDID[]> {
const peerDIDs: SDK.PeerDID[] = []
Expand Down

0 comments on commit b88fa6e

Please sign in to comment.