Skip to content

Commit

Permalink
feat: Integrate Typescript SDK 2.5.0 (#27)
Browse files Browse the repository at this point in the history
* fix: Improve testing for Credentials.

* add support for cryptographic abstraction.

* split unit tests.

* Increase package version.
  • Loading branch information
elribonazo authored Nov 2, 2023
1 parent 9df98af commit fbe46b5
Show file tree
Hide file tree
Showing 8 changed files with 630 additions and 853 deletions.
634 changes: 74 additions & 560 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"author": "elribonazo@gmail.com",
"license": "Apache-2.0",
"peerDependencies": {
"@input-output-hk/atala-prism-wallet-sdk": "^2.4.1"
"@input-output-hk/atala-prism-wallet-sdk": "^2.5.0"
},
"dependencies": {
"dexie": "^3.2.4",
Expand Down
126 changes: 79 additions & 47 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import DIDSchema, { DIDSchemaType } from "./schemas/DID";
import CredentialSchema, {
CredentialCollection,
CredentialMethods,
CredentialSchemaType,
CredentialSubjectType,
} from "./schemas/Credential";
import DIDPairSchema, { DIDPairSchemaType } from "./schemas/DIDPair";
Expand All @@ -38,7 +37,14 @@ import PrivateKeySchema, {
PrivateKeyDocument,
PrivateKeyMethods,
} from "./schemas/PrivateKey";
import { RxDBDevModePlugin } from "rxdb/plugins/dev-mode";
import LinkSecretSchema, {
LinkSecretColletion,
LinkSecretMethods,
} from "./schemas/LinkSecret";
import CredentialRequestMetadataSchema, {
CredentialRequestMetadataCollection,
CredentialRequestMetadataMethods,
} from "./schemas/CredentialRequestMetadata";

addRxPlugin(RxDBMigrationPlugin);
addRxPlugin(RxDBQueryBuilderPlugin);
Expand All @@ -58,7 +64,9 @@ export type PlutoCollections = {
didpairs: RxCollection<DIDPairSchemaType>;
mediators: MediatorCollection;
privatekeys: PrivateKeyColletion;
verifiablecredentials: CredentialCollection;
credentials: CredentialCollection;
credentialrequestmetadatas: CredentialRequestMetadataCollection;
linksecrets: LinkSecretColletion;
};
export type PlutoDatabase = RxDatabase<PlutoCollections>;

Expand Down Expand Up @@ -167,10 +175,18 @@ export class Database implements Domain.Pluto {
schema: PrivateKeySchema,
methods: PrivateKeyMethods,
},
verifiablecredentials: {
credentials: {
schema: CredentialSchema,
methods: CredentialMethods,
},
credentialrequestmetadatas: {
schema: CredentialRequestMetadataSchema,
methods: CredentialRequestMetadataMethods,
},
linksecrets: {
schema: LinkSecretSchema,
methods: LinkSecretMethods,
},
});
this._db = database;
} catch (err) {
Expand Down Expand Up @@ -621,48 +637,14 @@ export class Database implements Domain.Pluto {
return peerDIDs;
}

private parseCredentialSubject(credentialSubject: {
[name: string]: any;
}): CredentialSubjectType[] {
return Object.keys(credentialSubject).reduce<CredentialSubjectType[]>(
(all, key) => {
const value = credentialSubject[key];
all.push({
type: "string",
value: value,
name: key,
});
return all;
},
[]
);
}
async storeCredential(credential: Domain.Credential): Promise<void> {
if (!credential.isStorable || !credential.isStorable()) {
throw new Error("Credential is not storable");
}
const storable = credential.toStorable();
if (!storable.id) storable.id = uuidv4();

async storeCredential(
credential: Domain.VerifiableCredential
): Promise<void> {
await this.db.verifiablecredentials.insert({
id: credential.id,
credentialType: credential.credentialType,
context: credential.context,
type: credential.type,
credentialSchema: credential.credentialSchema,
credentialSubject: this.parseCredentialSubject(
credential.credentialSubject
),
credentialStatus: credential.credentialStatus,
refreshService: credential.refreshService,
evidence: credential.evidence,
subject: credential.subject ? credential.subject.toString() : undefined,
termsOfUse: credential.termsOfUse,
issuer: credential.issuer.toString(),
issuanceDate: credential.issuanceDate,
expirationDate: credential.expirationDate,
validFrom: credential.validFrom,
validUntil: credential.validUntil,
proof: credential.proof,
aud: credential.aud,
});
await this.db.credentials.insert(storable);
}

async getAllMediators(): Promise<Domain.Mediator[]> {
Expand All @@ -671,9 +653,59 @@ export class Database implements Domain.Pluto {
);
}

async getAllCredentials(): Promise<Domain.VerifiableCredential[]> {
return (await this.db.verifiablecredentials.find().exec()).map(
async getAllCredentials(): Promise<Domain.Credential[]> {
return (await this.db.credentials.find().exec()).map(
(verifiableCredential) => verifiableCredential.toDomainCredential()
);
}

async getLinkSecret(
linkSecretName?: string | undefined
): Promise<string | null> {
const linkSecret = await this.db.linksecrets
.findOne()
.where({ name: linkSecretName })
.exec();

if (linkSecret) {
return linkSecret.toDomainLinkSecret();
}

return null;
}

async storeLinkSecret(
linkSecret: string,
linkSecretName: string
): Promise<void> {
await this.db.linksecrets.insert({
name: linkSecretName,
secret: linkSecret,
});
}

async storeCredentialMetadata(
metadata: Domain.Anoncreds.CredentialRequestMeta,
linkSecret: string
): Promise<void> {
await this.db.credentialrequestmetadatas.insert({
...metadata,
id: uuidv4(),
link_secret_name: linkSecret,
});
}

async fetchCredentialMetadata(
linkSecretName: string
): Promise<Domain.Anoncreds.CredentialRequestMeta | null> {
const credentialRequestMetadata = await this.db.credentialrequestmetadatas
.findOne()
.where({ link_secret_name: linkSecretName })
.exec();

if (credentialRequestMetadata) {
return credentialRequestMetadata.toDomainCredentialRequestMetadata();
}
return null;
}
}
Loading

0 comments on commit fbe46b5

Please sign in to comment.