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

Feat/generic messages #399

Merged
merged 8 commits into from
May 28, 2020
41 changes: 22 additions & 19 deletions js/identityWallet/identityWallet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { Credential } from '../credentials/credential/credential';
import { SignedCredential } from '../credentials/signedCredential/signedCredential';
import { ExclusivePartial, IIdentityWalletCreateArgs } from './types';
import { Identity } from '../identity/identity';
import { JSONWebToken, JWTEncodable } from '../interactionTokens/JSONWebToken';
import { JSONWebToken } from '../interactionTokens/JSONWebToken';
import { PaymentRequest } from '../interactionTokens/paymentRequest';
import { Authentication } from '../interactionTokens/authentication';
import { CredentialRequest } from '../interactionTokens/credentialRequest';
import { KeyTypes } from '../vaultedKeyProvider/types';
import { IKeyMetadata, ISignedCredCreationArgs } from '../credentials/signedCredential/types';
import { ITransactionEncodable } from '../contracts/types';
import { IRegistry } from '../registries/types';
import { CredentialOfferRequest } from '../interactionTokens/credentialOfferRequest';
import { CredentialOfferResponse } from '../interactionTokens/credentialOfferResponse';
import { CredentialOfferRequestAttrs, CredentialOfferResponseAttrs, IAuthenticationAttrs, ICredentialRequestAttrs, ICredentialResponseAttrs, ICredentialsReceiveAttrs, IPaymentRequestAttrs, IPaymentResponseAttrs } from '../interactionTokens/interactionTokens.types';
interface PaymentRequestCreationArgs {
callbackURL: string;
Expand All @@ -36,37 +38,38 @@ export declare class IdentityWallet {
private vaultedKeyProvider;
constructor({ identity, publicKeyMetadata, vaultedKeyProvider, contractsGateway, contractsAdapter, }: IIdentityWalletCreateArgs);
private createSignedCred;
private createAuth;
private createCredOfferRequest;
private createCredentialOfferResponse;
private createCredReq;
private createCredResp;
private createCredReceive;
private createMessage;
private makeReq;
private makeRes;
getPublicKeys: (encryptionPass: string) => PublicKeyMap;
private createPaymentReq;
private createPaymentResp;
private initializeAndSign;
validateJWT<T extends JWTEncodable, A extends JWTEncodable>(receivedJWT: JSONWebToken<T>, sendJWT?: JSONWebToken<A>, customRegistry?: IRegistry): Promise<void>;
validateJWT<T, R>(receivedJWT: JSONWebToken<T>, sendJWT?: JSONWebToken<R>, customRegistry?: IRegistry): Promise<void>;
private sendTransaction;
transactions: {
sendTransaction: (request: ITransactionEncodable, pass: string) => Promise<string>;
};
create: {
credential: typeof Credential.create;
signedCredential: <T extends BaseMetadata>({ expires, ...credentialParams }: WithExtraOptions<ISignedCredCreationArgs<T>>, pass: string) => Promise<SignedCredential>;
message: <T_1, R>(args: {
message: T_1;
typ: string;
expires?: Date;
target?: string;
}, pass: string, recieved?: JSONWebToken<R>) => Promise<JSONWebToken<T_1>>;
interactionTokens: {
request: {
auth: (authArgs: WithExtraOptions<ExclusivePartial<IAuthenticationAttrs, "callbackURL">>, pass: string, receivedJWT?: JSONWebToken<JWTEncodable>) => Promise<JSONWebToken<JWTEncodable>>;
offer: (credOffer: WithExtraOptions<CredentialOfferRequestAttrs>, pass: string) => Promise<JSONWebToken<CredentialOfferRequest>>;
share: (credReq: WithExtraOptions<ICredentialRequestAttrs>, pass: string) => Promise<JSONWebToken<CredentialRequest>>;
payment: (paymentReq: WithExtraOptions<PaymentRequestCreationArgs>, pass: string) => Promise<JSONWebToken<PaymentRequest>>;
auth: ({ expires, ...args }: WithExtraOptions<ExclusivePartial<IAuthenticationAttrs, "callbackURL">>, pass: string) => Promise<JSONWebToken<Pick<WithExtraOptions<ExclusivePartial<IAuthenticationAttrs, "callbackURL">>, "description" | "callbackURL">>>;
offer: ({ expires, ...args }: WithExtraOptions<CredentialOfferRequestAttrs>, pass: string) => Promise<JSONWebToken<Pick<WithExtraOptions<CredentialOfferRequestAttrs>, "callbackURL" | "offeredCredentials">>>;
share: ({ expires, ...args }: WithExtraOptions<ICredentialRequestAttrs>, pass: string) => Promise<JSONWebToken<Pick<WithExtraOptions<ICredentialRequestAttrs>, "callbackURL" | "credentialRequirements">>>;
payment: ({ expires, ...args }: WithExtraOptions<PaymentRequestCreationArgs>, pass: string) => Promise<JSONWebToken<Pick<WithExtraOptions<PaymentRequestCreationArgs>, "description" | "transactionOptions" | "callbackURL">>>;
};
response: {
auth: (authArgs: WithExtraOptions<ExclusivePartial<IAuthenticationAttrs, "callbackURL">>, pass: string, receivedJWT?: JSONWebToken<JWTEncodable>) => Promise<JSONWebToken<JWTEncodable>>;
offer: (credentialOfferResponse: WithExtraOptions<CredentialOfferResponseAttrs>, pass: string, receivedJWT?: JSONWebToken<JWTEncodable>) => Promise<JSONWebToken<JWTEncodable>>;
share: (credResp: WithExtraOptions<ICredentialResponseAttrs>, pass: string, receivedJWT: JSONWebToken<JWTEncodable>) => Promise<JSONWebToken<JWTEncodable>>;
issue: (credReceive: WithExtraOptions<ICredentialsReceiveAttrs>, pass: string, receivedJWT: JSONWebToken<JWTEncodable>) => Promise<JSONWebToken<JWTEncodable>>;
payment: (paymentResp: WithExtraOptions<IPaymentResponseAttrs>, pass: string, receivedJWT: JSONWebToken<JWTEncodable>) => Promise<JSONWebToken<JWTEncodable>>;
auth: ({ expires, ...args }: WithExtraOptions<ExclusivePartial<IAuthenticationAttrs, "callbackURL">>, pass: string, recieved?: JSONWebToken<Authentication>) => Promise<JSONWebToken<Pick<WithExtraOptions<ExclusivePartial<IAuthenticationAttrs, "callbackURL">>, "description" | "callbackURL">>>;
offer: ({ expires, ...args }: WithExtraOptions<CredentialOfferResponseAttrs>, pass: string, recieved?: JSONWebToken<CredentialOfferRequest>) => Promise<JSONWebToken<Pick<WithExtraOptions<CredentialOfferResponseAttrs>, "callbackURL" | "selectedCredentials">>>;
share: ({ expires, ...args }: WithExtraOptions<ICredentialResponseAttrs>, pass: string, recieved?: JSONWebToken<CredentialRequest>) => Promise<JSONWebToken<Pick<WithExtraOptions<ICredentialResponseAttrs>, "callbackURL" | "suppliedCredentials">>>;
issue: ({ expires, ...args }: WithExtraOptions<ICredentialsReceiveAttrs>, pass: string, recieved?: JSONWebToken<CredentialOfferResponse>) => Promise<JSONWebToken<Pick<WithExtraOptions<ICredentialsReceiveAttrs>, "signedCredentials">>>;
payment: ({ expires, ...args }: WithExtraOptions<IPaymentResponseAttrs>, pass: string, recieved?: JSONWebToken<PaymentRequest>) => Promise<JSONWebToken<Pick<WithExtraOptions<IPaymentResponseAttrs>, "txHash">>>;
};
};
};
Expand Down
14 changes: 7 additions & 7 deletions js/interactionTokens/JSONWebToken.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="node" />
import { IJWTHeader } from './types';
import { IJSONWebTokenAttrs, InteractionType } from './types';
import { IJSONWebTokenAttrs } from './types';
import { IDigestable } from '../linkedDataSignature/types';
import { CredentialResponse } from './credentialResponse';
import { CredentialRequest } from './credentialRequest';
Expand All @@ -17,10 +17,10 @@ interface IPayloadSection<T> {
jti?: string;
iss?: string;
aud?: string;
typ?: InteractionType;
typ?: string;
interactionToken?: T;
}
export declare class JSONWebToken<T extends JWTEncodable> implements IDigestable {
export declare class JSONWebToken<T> implements IDigestable {
private _header;
private _signature;
private _payload;
Expand All @@ -32,19 +32,19 @@ export declare class JSONWebToken<T extends JWTEncodable> implements IDigestable
readonly expires: number;
nonce: string;
interactionToken: T;
interactionType: InteractionType;
interactionType: string;
header: IJWTHeader;
readonly signer: {
did: string;
keyId: string;
};
static fromJWTEncodable<T extends JWTEncodable>(toEncode: T): JSONWebToken<T>;
static fromJWTEncodable<T>(toEncode: T): JSONWebToken<T>;
timestampAndSetExpiry(expiry?: Date): void;
setIssueAndExpiryTime: (expiry?: Date) => void;
static decode<T extends JWTEncodable>(jwt: string): JSONWebToken<T>;
static decode<T>(jwt: string): JSONWebToken<T>;
encode(): string;
digest(): Promise<Buffer>;
toJSON(): IJSONWebTokenAttrs;
static fromJSON<T extends JWTEncodable>(json: IJSONWebTokenAttrs): JSONWebToken<T>;
static fromJSON<T>(json: IJSONWebTokenAttrs): JSONWebToken<T>;
}
export {};
6 changes: 3 additions & 3 deletions js/parse/parse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Credential } from '../credentials/credential/credential';
import { SignedCredential } from '../credentials/signedCredential/signedCredential';
import { ICredentialAttrs } from '../credentials/credential/types';
import { ISignedCredentialAttrs } from '../credentials/signedCredential/types';
import { JSONWebToken, JWTEncodable } from '../interactionTokens/JSONWebToken';
import { JSONWebToken } from '../interactionTokens/JSONWebToken';
import { IJSONWebTokenAttrs } from '../interactionTokens/types';
export interface ParseMethods {
interactionToken: {
fromJWT: <T extends JWTEncodable>(jwt: string) => JSONWebToken<T>;
fromJSON: <T extends JWTEncodable>(json: IJSONWebTokenAttrs) => JSONWebToken<T>;
fromJWT: <T>(jwt: string) => JSONWebToken<T>;
fromJSON: <T>(json: IJSONWebTokenAttrs) => JSONWebToken<T>;
};
credential: (json: ICredentialAttrs) => Credential;
signedCredential: (json: ISignedCredentialAttrs) => SignedCredential;
Expand Down
21 changes: 11 additions & 10 deletions tests/identityWallet/identityWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ describe('IdentityWallet', () => {
})

it('Should expose aggregated creation methods', () => {
const categories = ['credential', 'signedCredential', 'interactionTokens']
const categories = [
'credential',
'signedCredential',
'message',
'interactionTokens',
]
const flowTypes = ['request', 'response']
const tokenTypesRequest = ['auth', 'offer', 'share', 'payment']
const tokenTypesResponse = ['auth', 'offer', 'share', 'issue', 'payment']
Expand Down Expand Up @@ -126,10 +131,7 @@ describe('IdentityWallet', () => {
)

sandbox.assert.calledOnce(spyFromJWTEncodable)
sandbox.assert.calledWith(
spyFromJWTEncodable,
CredentialRequest.fromJSON(simpleCredRequestJSON),
)
sandbox.assert.calledWith(spyFromJWTEncodable, simpleCredRequestJSON)

const expectedExpiry = 60 * 60 * 1000
expect(interactionToken.expires - interactionToken.issued).to.eq(
Expand All @@ -148,10 +150,7 @@ describe('IdentityWallet', () => {
)

sandbox.assert.calledOnce(spyFromJWTEncodable)
sandbox.assert.calledWith(
spyFromJWTEncodable,
CredentialRequest.fromJSON(simpleCredRequestJSON),
)
sandbox.assert.calledWith(spyFromJWTEncodable, simpleCredRequestJSON)
expect(interactionTokenCustomExpiry.expires).to.eq(customExpiry.getTime())
})

Expand All @@ -174,7 +173,9 @@ describe('IdentityWallet', () => {
})

it('Should create an interaction token as a response', async () => {
const decodedToken = JSONWebToken.decode(interactionToken.encode())
const decodedToken = JSONWebToken.decode<CredentialRequest>(
interactionToken.encode(),
)
const interactionResponeToken = await iw.create.interactionTokens.response.share(
credentialResponseJSON,
encryptionPass,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/credentialOffer.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Integration Test - Token interaction flow Credential Offer', () => {

it('Should correctly create a credential receive token by service', async () => {
const decodedCredOfferResponse = JSONWebToken.decode<
CredentialOfferRequest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the previous / original test broken? The line didn't look right.

CredentialOfferResponse
>(credOfferResponseEncoded)
const signedCredForUser = await serviceIdentityWallet.create.signedCredential(
{
Expand Down
Loading