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

Add trustping protocol #17

Merged
merged 4 commits into from
Jan 29, 2020
Merged
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
11 changes: 11 additions & 0 deletions src/lib/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ConnectionService } from '../protocols/connections/ConnectionService';
import { MessageType as ConnectionsMessageType } from '../protocols/connections/messages';
import { MessageType as BasicMessageMessageType } from '../protocols/basicmessage/messages';
import { MessageType as RoutingMessageType } from '../protocols/routing/messages';
import { MessageType as TrustPingMessageType } from '../protocols/trustping/messages';
import { ProviderRoutingService } from '../protocols/routing/ProviderRoutingService';
import { BasicMessageService } from '../protocols/basicmessage/BasicMessageService';
import { ConsumerRoutingService } from '../protocols/routing/ConsumerRoutingService';
Expand All @@ -24,6 +25,9 @@ import { BasicMessageHandler } from '../handlers/BasicMessageHandler';
import { RouteUpdateHandler } from '../handlers/RouteUpdateHandler';
import { ForwardHandler } from '../handlers/ForwardHandler';
import { Handler } from '../handlers/Handler';
import { TrustPingService } from '../protocols/trustping/TrustPingService';
import { TrustPingMessageHandler } from '../handlers/TrustPingMessageHandler';
import { TrustPingResponseMessageHandler } from '../handlers/TrustPingResponseMessageHandler';

export class Agent {
inboundTransporter: InboundTransporter;
Expand All @@ -33,6 +37,7 @@ export class Agent {
basicMessageService: BasicMessageService;
providerRoutingService: ProviderRoutingService;
consumerRoutingService: ConsumerRoutingService;
trustPingService: TrustPingService;
handlers: { [key: string]: Handler } = {};

constructor(config: InitConfig, inboundTransporter: InboundTransporter, outboundTransporter: OutboundTransporter) {
Expand All @@ -53,6 +58,7 @@ export class Agent {
this.basicMessageService = new BasicMessageService();
this.providerRoutingService = new ProviderRoutingService();
this.consumerRoutingService = new ConsumerRoutingService(this.context);
this.trustPingService = new TrustPingService();

this.registerHandlers();

Expand Down Expand Up @@ -156,6 +162,11 @@ export class Agent {
this.providerRoutingService
),
[RoutingMessageType.ForwardMessage]: new ForwardHandler(this.providerRoutingService),
[TrustPingMessageType.TrustPingMessage]: new TrustPingMessageHandler(
this.trustPingService,
this.connectionService
),
[TrustPingMessageType.TrustPingResponseMessage]: new TrustPingResponseMessageHandler(this.trustPingService),
};

this.handlers = handlers;
Expand Down
24 changes: 24 additions & 0 deletions src/lib/handlers/TrustPingMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Handler } from './Handler';
import { InboundMessage } from '../types';
import { TrustPingService } from '../protocols/trustping/TrustPingService';
import { ConnectionService } from '../protocols/connections/ConnectionService';
import { MessageType } from '../protocols/trustping/messages';

export class TrustPingMessageHandler implements Handler {
trustPingService: TrustPingService;
connectionService: ConnectionService;

constructor(trustPingService: TrustPingService, connectionService: ConnectionService) {
this.trustPingService = trustPingService;
this.connectionService = connectionService;
}

async handle(inboundMessage: InboundMessage) {
const { recipient_verkey } = inboundMessage;
const connection = this.connectionService.findByVerkey(recipient_verkey);
if (!connection) {
throw new Error(`Connection for receipient_verkey ${recipient_verkey} not found`);
}
return this.trustPingService.processPing(inboundMessage, connection);
}
}
15 changes: 15 additions & 0 deletions src/lib/handlers/TrustPingResponseMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Handler } from './Handler';
import { InboundMessage } from '../types';
import { TrustPingService } from '../protocols/trustping/TrustPingService';

export class TrustPingResponseMessageHandler implements Handler {
trustPingService: TrustPingService;

constructor(trustPingService: TrustPingService) {
this.trustPingService = trustPingService;
}

async handle(inboundMessage: InboundMessage) {
return this.trustPingService.processPingResponse(inboundMessage);
}
}
3 changes: 2 additions & 1 deletion src/lib/protocols/connections/ConnectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Context } from '../../agent/Context';
import { createOutboundMessage } from '../helpers';
import { Connection } from './domain/Connection';
import { ConnectionState } from './domain/ConnectionState';
import { createTrustPingMessage } from '../trustping/messages';

class ConnectionService {
context: Context;
Expand Down Expand Up @@ -82,7 +83,7 @@ class ConnectionService {

validateSenderKey(connection, sender_verkey);

const response = createAckMessage(message['@id']);
const response = createTrustPingMessage();
connection.updateState(ConnectionState.COMPLETE);
return createOutboundMessage(connection, response);
}
Expand Down
22 changes: 22 additions & 0 deletions src/lib/protocols/trustping/TrustPingService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { InboundMessage } from '../../types';
import { createOutboundMessage } from '../helpers';
import { createTrustPingResponseMessage, MessageType } from './messages';
import { Connection } from '../..';
import { ConnectionState } from '../connections/domain/ConnectionState';

export class TrustPingService {
processPing(inboundMessage: InboundMessage, connection: Connection) {
if (connection.getState() != ConnectionState.COMPLETE) {
connection.updateState(ConnectionState.COMPLETE);
}
if (inboundMessage.message['response_requested']) {
const reply = createTrustPingResponseMessage(inboundMessage.message['@id']);
return createOutboundMessage(connection, reply);
}
return null;
}

processPingResponse(inboundMessage: InboundMessage) {
return null;
}
}
26 changes: 26 additions & 0 deletions src/lib/protocols/trustping/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import uuid from 'uuid/v4';

export enum MessageType {
TrustPingMessage = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/trust_ping/1.0/ping',
TrustPingResponseMessage = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/trust_ping/1.0/ping_response',
}

export function createTrustPingMessage(response_requested: boolean = true, comment: string = '') {
return {
'@id': uuid(),
'@type': MessageType.TrustPingMessage,
...(comment && { comment }),
response_requested,
};
}

export function createTrustPingResponseMessage(thid: string, comment: string = '') {
return {
'@id': uuid(),
'@type': MessageType.TrustPingResponseMessage,
'~thread': {
thid,
},
...(comment && { comment }),
};
}