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 3 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 changes: 8 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,8 @@ import { BasicMessageHandler } from '../handlers/BasicMessageHandler';
import { RouteUpdateHandler } from '../handlers/RouteUpdateHandler';
import { ForwardHandler } from '../handlers/ForwardHandler';
import { Handler } from '../handlers/Handler';
import { TrustPingHandler } from '../handlers/TrustPingHandler';
import { TrustPingService } from '../protocols/trustping/TrustPingService';

export class Agent {
inboundTransporter: InboundTransporter;
Expand All @@ -33,6 +36,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 +57,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 @@ -142,6 +147,7 @@ export class Agent {
}

private registerHandlers() {
const trustPingHandler = new TrustPingHandler(this.trustPingService, this.connectionService);
const handlers = {
[ConnectionsMessageType.ConnectionInvitation]: new InvitationHandler(
this.connectionService,
Expand All @@ -156,6 +162,8 @@ export class Agent {
this.providerRoutingService
),
[RoutingMessageType.ForwardMessage]: new ForwardHandler(this.providerRoutingService),
[TrustPingMessageType.TrustPingMessage]: trustPingHandler,
[TrustPingMessageType.TrustPingResponseMessage]: trustPingHandler,
};

this.handlers = handlers;
Expand Down
31 changes: 31 additions & 0 deletions src/lib/handlers/TrustPingHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 TrustPingHandler implements Handler {
trustPingService: TrustPingService;
connectionService: ConnectionService;

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

async handle(inboundMessage: InboundMessage) {
switch (inboundMessage.message['@type']) {
case MessageType.TrustPingMessage:
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.process_ping(inboundMessage, connection);
case MessageType.TrustPingResponseMessage:
return this.trustPingService.process_ping_response(inboundMessage);
default:
throw new Error('Invalid message type for handler');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather create a handler for each type as it is with other handlers. We can group handlers by a protocol in handlers folder. Maybe it's not the best design, but let's keep it consistent for now. Or is there any problem with that I don't see? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

uhm no problem as such but I figured it's easy to see all handlers for a particular protocol at one place rather than switching b/w files.

You're right that this breaks consistency with what we have at the moment. I'll split this into two different handlers. Maybe we can have another issue that refactors the handlers and groups them by protocol type? If so, could you create one please?

}
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 {
process_ping(inboundMessage: InboundMessage, connection: Connection) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is perhaps no rule about camelCase method/function naming, my fault. Could you please rename it? And if you want, ESLint rules update would be appreciated :)

I understand there will be exceptions, mainly in relation to message attributes defined by spec, but I would recommend to keep it camelCase whenever possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aye aye!

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;
}

process_ping_response(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 }),
};
}