Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding vc create #148

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,418 changes: 7,977 additions & 441 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 @@ -2,9 +2,9 @@
"workspaces": [
"packages/common",
"packages/crypto",
"packages/credentials",
"packages/web5-agent",
"packages/dids",
"packages/credentials",
"packages/web5-user-agent",
"packages/web5-proxy-agent",
"packages/web5"
Expand Down
1 change: 1 addition & 0 deletions packages/credentials/.c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
"exclude": [
"__tests__/src/main.js",
"__tests__/src/types.js",
"__tests__/types/**"
],
"reporter": [
Expand Down
5 changes: 4 additions & 1 deletion packages/credentials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@
"engines": {
"node": ">=18.0.0"
},
"dependencies": {},
"dependencies": {
"@sphereon/pex": "^2.1.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/chai": "4.3.0",
"@types/eslint": "8.37.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/credentials/src/main.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './types.js';
export * from './types.js';
export * from './utils.js';
63 changes: 14 additions & 49 deletions packages/credentials/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import { ICredential, IIssuer, ICredentialStatus, ICredentialSubject, ICredentialSchemaType} from '@sphereon/ssi-types';

/**
* @see {@link https://www.w3.org/TR/vc-data-model/#credentials | VC data model}
*/
export type VerifiableCredential = {
/** see {@link https://www.w3.org/TR/vc-data-model/#contexts | Contexts} */
'@context': ['https://www.w3.org/2018/credentials/v1', ...string[]];
credentialSubject: CredentialSubject | CredentialSubject[];
/** @see {@link https://www.w3.org/TR/vc-data-model/#identifiers | Identifiers} */
id?: string;
/** @see {@link IssuerType} */
issuer: Issuer;
/** @see {@link https://www.w3.org/TR/vc-data-model/#types | Types} */
type?: string[] | string;
/** @see {@link https://www.w3.org/TR/vc-data-model/#issuance-date | Issuance Date} */
issuanceDate: string;
/** @see {@link https://www.w3.org/TR/vc-data-model/#expiration | Expiration} */
expirationDate?: string;
/** @see {@link CredentialStatusReference} */
credentialStatus?: CredentialStatusReference;
[key: string]: any;
}
export type { ICredential as VerifiableCredential };


/**
* @see {@link https://www.w3.org/TR/vc-data-model/#data-schemas | Data schemas}
*/
export type { ICredentialSchemaType as CredentialSchemaType };

/**
* The issuer of a {@link VerifiableCredential}.
Expand All @@ -30,23 +20,18 @@ export type VerifiableCredential = {
*
* See {@link https://www.w3.org/TR/vc-data-model/#issuer | Issuer data model}
*/
export type Issuer = {
/** @see {@link https://www.w3.org/TR/vc-data-model/#identifiers | Identifiers} */
id: string;
[key: string]: any;
} | string
export type {IIssuer as Issuer};


/**
* The value of the `credentialSubject` property is defined as a set of objects that contain one or more properties that
* are each related to a subject of the verifiable credential. Each object MAY contain an id.
*
* See {@link https://www.w3.org/TR/vc-data-model/#credential-subject | Credential Subject}
*/
export type CredentialSubject = {
/** @see {@link https://www.w3.org/TR/vc-data-model/#identifiers | Identifiers} */
id?: string;
[key: string]: any;
}

export type {ICredentialSubject as CredentialSubject};


/**
* Used for the discovery of information about the current status of a verifiable credential, such as whether it is
Expand All @@ -56,24 +41,4 @@ export type CredentialSubject = {
*
* See {@link https://www.w3.org/TR/vc-data-model/#status | Credential Status}
*/
export type CredentialStatusReference = {
/** @see {@link https://www.w3.org/TR/vc-data-model/#identifiers | Identifiers} */
id: string;
/** expresses the credential status type (also referred to as the credential status method).
* It is expected that the value will provide enough information to determine the current status
* of the credential and that machine readable information needs to be retrievable from the URI.
* For example, the object could contain a link to an external document noting whether or not the credential
* is suspended or revoked.
*/
type: string;
[key: string]: any;
}

/**
* proof property of a {@link VerifiableCredential}
*/
export type ProofType = {
/** @see {@link https://www.w3.org/TR/vc-data-model/#types | Types} */
type?: string;
[key: string]: any;
}
export type {ICredentialStatus as CredentialStatus};
21 changes: 21 additions & 0 deletions packages/credentials/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function getCurrentXmlSchema112Timestamp() : string {
// Omit the milliseconds part from toISOString() output
return new Date().toISOString().replace(/\.\d+Z$/, 'Z');
}

export function getFutureXmlSchema112Timestamp(secondsInFuture: number): string {
const futureDate = new Date(Date.now() + secondsInFuture * 1000);
return futureDate.toISOString().replace(/\.\d+Z$/, 'Z');
}

export function isValidXmlSchema112Timestamp(timestamp: string): boolean {
// Format: yyyy-MM-ddTHH:mm:ssZ
const regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
if (!regex.test(timestamp)) {
return false;
}

const date = new Date(timestamp);

return !isNaN(date.getTime());
}
7 changes: 0 additions & 7 deletions packages/credentials/tests/example.spec.ts

This file was deleted.

67 changes: 67 additions & 0 deletions packages/credentials/tests/types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect } from 'chai';
import {getCurrentXmlSchema112Timestamp, getFutureXmlSchema112Timestamp} from '../src/utils.js';
import {VerifiableCredential, CredentialSubject, Issuer, CredentialStatus } from '../src/types.js';

describe('credential types', () => {
it('creates a vc', () => {
const credentialSubject: CredentialSubject = {
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
};

const issuer: Issuer = {
id : 'did:example:123456',
otherProp : 'value',
};

const credentialStatus: CredentialStatus = {
id : 'http://example.com/status/123',
type : 'CredentialStatusType1',
};

// Create the credential
const credential: VerifiableCredential = {
'@context' : ['https://www.w3.org/2018/credentials/v1'],
type : ['VerifiableCredential'],
issuer : issuer,
issuanceDate : getCurrentXmlSchema112Timestamp(),
credentialSubject : credentialSubject,
credentialStatus : credentialStatus,
credentialSchema : {
id : 'http://example.com/schema/123',
type : 'SchemaType1',
},
id : 'http://example.edu/credentials/3732',
expirationDate : getFutureXmlSchema112Timestamp(60 * 60 * 24 * 365), // expires in 1 year
name : 'Credential Name',
description : 'Credential Description',
};

expect(credential).to.have.property('@context');
expect(credential).to.have.property('type');
expect(credential).to.have.property('issuer');
expect(credential).to.have.property('issuanceDate');
expect(credential).to.have.property('credentialSubject');
expect(credential).to.have.property('credentialStatus');
expect(credential).to.have.property('credentialSchema');
expect(credential).to.have.property('id');
expect(credential).to.have.property('expirationDate');
expect(credential).to.have.property('name');
expect(credential).to.have.property('description');
});

it('creates a minimum viable vc', () => {
const credential: VerifiableCredential = {
'@context' : ['https://www.w3.org/2018/credentials/v1'],
type : ['VerifiableCredential'],
issuer : { id: 'did:example:123456' },
issuanceDate : getCurrentXmlSchema112Timestamp(),
credentialSubject : { id: 'did:example:ebfeb1f712ebc6f1c276e12ec21' },
};

expect(credential).to.have.property('@context');
expect(credential).to.have.property('type');
expect(credential).to.have.property('issuer');
expect(credential).to.have.property('issuanceDate');
expect(credential).to.have.property('credentialSubject');
});
});
47 changes: 47 additions & 0 deletions packages/credentials/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect } from 'chai';
import {getCurrentXmlSchema112Timestamp, getFutureXmlSchema112Timestamp, isValidXmlSchema112Timestamp} from '../src/utils.js';

describe('credentials utils', () => {

describe('getCurrentXmlSchema112Timestamp', () => {
it('gets correct time', () => {
const timestamp = getCurrentXmlSchema112Timestamp();
expect(timestamp).to.not.be.undefined;
expect(timestamp).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/);

});
});
describe('getFutureXmlSchema112Timestamp', () => {
it('gets correct time', () => {
const timestamp = getFutureXmlSchema112Timestamp(123);
expect(timestamp).to.not.be.undefined;
expect(timestamp).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/);
});
});

describe('validateXmlSchema112Timestamp', () => {
it('validates correctly formatted timestamps', () => {
const timestamp = '2023-07-31T12:34:56Z';
const result = isValidXmlSchema112Timestamp(timestamp);
expect(result).to.be.true;
});

it('rejects incorrectly formatted timestamps', () => {
const badTimestamp = '2023-07-31T12:34:56.789Z'; // includes milliseconds
const result = isValidXmlSchema112Timestamp(badTimestamp);
expect(result).to.be.false;
});

it('rejects non-timestamps', () => {
const notATimestamp = 'This is definitely not a timestamp';
const result = isValidXmlSchema112Timestamp(notATimestamp);
expect(result).to.be.false;
});

it('rejects empty string', () => {
const emptyString = '';
const result = isValidXmlSchema112Timestamp(emptyString);
expect(result).to.be.false;
});
});
});
6 changes: 3 additions & 3 deletions packages/web5-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"node": ">=18.0.0"
},
"dependencies": {
"readable-stream": "4.4.0",
"@tbd54566975/dwn-sdk-js": "0.0.37"
"@tbd54566975/dwn-sdk-js": "0.0.37",
"readable-stream": "4.4.0"
},
"devDependencies": {
"@playwright/test": "1.34.3",
Expand Down Expand Up @@ -114,4 +114,4 @@
"source-map-loader": "4.0.1",
"typescript": "5.0.4"
}
}
}
27 changes: 27 additions & 0 deletions packages/web5-agent/src/web5-agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Readable } from 'readable-stream';
import type { VerifiableCredential } from '@tbd54566975/credentials';

Check warning on line 2 in packages/web5-agent/src/web5-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-agent/src/web5-agent.ts#L2

Added line #L2 was not covered by tests

import {
EventsGetMessage,
Expand All @@ -11,9 +12,12 @@
ProtocolsConfigureMessage,
} from '@tbd54566975/dwn-sdk-js';


Check warning on line 15 in packages/web5-agent/src/web5-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-agent/src/web5-agent.ts#L15

Added line #L15 was not covered by tests
export interface Web5Agent {
processDwnRequest(request: ProcessDwnRequest): Promise<DwnResponse>
sendDwnRequest(request: SendDwnRequest): Promise<DwnResponse>;
processVcRequest(request: ProcessVcRequest): Promise<VcResponse>;
sendVcRequest(request: SendVcRequest): Promise<VcResponse>;

Check warning on line 20 in packages/web5-agent/src/web5-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-agent/src/web5-agent.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
}

export type DwnMessages = {
Expand Down Expand Up @@ -54,6 +58,29 @@
reply: UnionMessageReply;
};

/**
* TODO: add JSDoc
*/
export type ProcessVcRequest = {
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
author: string;
target: string;
vc: VerifiableCredential;
kid?: string
};

export type SendVcRequest = {
author: string;
target: string;
vc: VerifiableCredential;
messageCid?: string;
};

export type VcResponse = {
vcJwt?: string;
message?: unknown;
messageCid?: string;
reply: UnionMessageReply;
};

Check warning on line 83 in packages/web5-agent/src/web5-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-agent/src/web5-agent.ts#L61-L83

Added lines #L61 - L83 were not covered by tests

/**
* TODO: add JSDoc
Expand Down
22 changes: 19 additions & 3 deletions packages/web5-proxy-agent/src/web5-proxy-agent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import type { DwnRequest, DwnResponse, Web5Agent } from '@tbd54566975/web5-agent';
import {
Web5Agent,
SendVcRequest,
ProcessVcRequest,
ProcessDwnRequest,
VcResponse,
SendDwnRequest,
DwnResponse,
} from '@tbd54566975/web5-agent';

Check warning on line 9 in packages/web5-proxy-agent/src/web5-proxy-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-proxy-agent/src/web5-proxy-agent.ts#L2-L9

Added lines #L2 - L9 were not covered by tests

// TODO: concretely define json-rpc types specific to each Web5Agent interface method
// TODO: write http transport. that transport will be shareable with client for dwn-server
// TODO: write ws transport. that transport will be shareable with client for dwn-server
// TODO: figure out where to put DidConnect.
export class Web5ProxyAgent implements Web5Agent {
processDwnRequest(_request: DwnRequest): Promise<DwnResponse> {
processDwnRequest(_request: ProcessDwnRequest): Promise<DwnResponse> {

Check warning on line 16 in packages/web5-proxy-agent/src/web5-proxy-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-proxy-agent/src/web5-proxy-agent.ts#L16

Added line #L16 was not covered by tests
throw new Error('Method not implemented.');
}

sendDwnRequest(_request: DwnRequest): Promise<any> {
sendDwnRequest(_request: SendDwnRequest): Promise<DwnResponse> {
throw new Error('Method not implemented.');
}

processVcRequest(_request: ProcessVcRequest): Promise<VcResponse> {
throw new Error('Method not implemented.');
}

sendVcRequest(_request: SendVcRequest): Promise<VcResponse> {

Check warning on line 28 in packages/web5-proxy-agent/src/web5-proxy-agent.ts

View check run for this annotation

Codecov / codecov/patch

packages/web5-proxy-agent/src/web5-proxy-agent.ts#L20-L28

Added lines #L20 - L28 were not covered by tests
throw new Error('Method not implemented.');
}
}
Loading