Skip to content

Commit

Permalink
fix: Export AgentMessage (DidCommV1Message) to keep backward compatib…
Browse files Browse the repository at this point in the history
…ility

Signed-off-by: Artemkaaas <artem.ivanov@dsr-corporation.com>
  • Loading branch information
Artemkaaas committed Aug 4, 2023
1 parent a70f587 commit 3b2e494
Show file tree
Hide file tree
Showing 24 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ServiceDecorator } from '../decorators/service/ServiceDecorator'
import type { ReturnRouteTypes } from '../decorators/transport/TransportDecorator'
import type { DidCommMessageVersion } from '../didcomm/types'

export interface AgentMessage {
export interface AgentBaseMessage {
readonly type: string

get didCommVersion(): DidCommMessageVersion
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/Dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentBaseMessage } from './AgentBaseMessage'
import type { AgentMessageProcessedEvent } from './Events'
import type { InboundMessageContext } from './models/InboundMessageContext'

Expand Down Expand Up @@ -42,7 +42,7 @@ class Dispatcher {
throw new AriesFrameworkError(`No handler for message type "${message.type}" found`)
}

let outboundMessage: OutboundMessageContext<AgentMessage> | void
let outboundMessage: OutboundMessageContext<AgentBaseMessage> | void

try {
outboundMessage = await messageHandler.handle(messageContext)
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/agent/EnvelopeService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentBaseMessage } from './AgentBaseMessage'
import type { AgentContext } from './context'
import type { Key } from '../crypto'
import type {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class EnvelopeService {

public async packMessage(
agentContext: AgentContext,
message: AgentMessage,
message: AgentBaseMessage,
params: PackMessageParams
): Promise<EncryptedMessage> {
if (message.didCommVersion === DidCommMessageVersion.V1) {
Expand Down Expand Up @@ -110,7 +110,7 @@ export class EnvelopeService {

private async packDIDCommV1Message(
agentContext: AgentContext,
message: AgentMessage,
message: AgentBaseMessage,
params: DidCommV1PackMessageParams
): Promise<EncryptedMessage> {
const { recipientKeys, senderKey } = params
Expand Down Expand Up @@ -170,7 +170,7 @@ export class EnvelopeService {

private async packDIDCommV2Message(
agentContext: AgentContext,
message: AgentMessage,
message: AgentBaseMessage,
params: DidCommV2PackMessageParams
): Promise<EncryptedMessage> {
const unboundMessage = message.toJSON()
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/Events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentBaseMessage } from './AgentBaseMessage'
import type { OutboundMessageContext, OutboundMessageSendStatus } from './models'
import type { ConnectionRecord } from '../modules/connections'
import type { Observable } from 'rxjs'
Expand Down Expand Up @@ -39,7 +39,7 @@ export interface AgentMessageReceivedEvent extends BaseEvent {
export interface AgentMessageProcessedEvent extends BaseEvent {
type: typeof AgentEventTypes.AgentMessageProcessed
payload: {
message: AgentMessage
message: AgentBaseMessage
connection?: ConnectionRecord
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/agent/MessageReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentBaseMessage } from './AgentBaseMessage'
import type { TransportSession } from './TransportService'
import type { AgentContext } from './context'
import type { EncryptedMessage, PlaintextMessage } from '../didcomm'
Expand Down Expand Up @@ -169,7 +169,7 @@ export class MessageReceiver {

// We want to save a session if there is a chance of returning outbound message via inbound transport.
// That can happen when inbound message has `return_route` set to `all` or `thread`.
// If `return_route` defines just `thread`, we decide later whether to use session according to outbound message `threadId`.
// If `return_route` defines just `thread`, we decide later whether to use session according to outbound message `thid`.
if (senderKey && recipientKey && message.hasAnyReturnRoute() && session) {
this.logger.debug(`Storing session for inbound message '${message.id}'`)
const keys = {
Expand Down Expand Up @@ -197,8 +197,8 @@ export class MessageReceiver {
agentContext: AgentContext,
plaintextMessage: PlaintextMessage,
connection?: ConnectionRecord | null
): Promise<AgentMessage> {
let message: AgentMessage
): Promise<AgentBaseMessage> {
let message: AgentBaseMessage
try {
message = await this.transformMessage(plaintextMessage)
} catch (error) {
Expand All @@ -207,7 +207,7 @@ export class MessageReceiver {
throw new AriesFrameworkError(`Not sending problem report in response to problem report: ${error.message}`)
}

const problemReportMessage: AgentMessage | undefined = isPlaintextMessageV1(plaintextMessage)
const problemReportMessage: AgentBaseMessage | undefined = isPlaintextMessageV1(plaintextMessage)
? buildProblemReportV1Message(plaintextMessage, error.message)
: buildProblemReportV2Message(plaintextMessage, error.message)

Expand Down Expand Up @@ -279,7 +279,7 @@ export class MessageReceiver {
*
* @param message the plaintext message for which to transform the message in to a class instance
*/
private async transformMessage(message: PlaintextMessage): Promise<AgentMessage> {
private async transformMessage(message: PlaintextMessage): Promise<AgentBaseMessage> {
// replace did:sov:BzCbsNYhMrjHiqZDTUASHg;spec prefix for message type with https://didcomm.org
if (message['@type']) {
// replace did:sov:BzCbsNYhMrjHiqZDTUASHg;spec prefix for record type with https://didcomm.org
Expand All @@ -300,7 +300,7 @@ export class MessageReceiver {
}

// Cast the plain JSON object to specific instance of Message extended from AgentMessage
let messageTransformed: AgentMessage
let messageTransformed: AgentBaseMessage
try {
messageTransformed = JsonTransformer.fromJSON(message, MessageClass)
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/agent/MessageSender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentBaseMessage } from './AgentBaseMessage'
import type { PackMessageParams } from './EnvelopeService'
import type { AgentMessageSentEvent } from './Events'
import type { TransportSession } from './TransportService'
Expand Down Expand Up @@ -86,7 +86,7 @@ export class MessageSender {
endpoint,
}: {
params: PackMessageParams
message: AgentMessage
message: AgentBaseMessage
endpoint: string
}
): Promise<OutboundPackage> {
Expand All @@ -99,7 +99,7 @@ export class MessageSender {
}
}

private async sendMessageToSession(agentContext: AgentContext, session: TransportSession, message: AgentMessage) {
private async sendMessageToSession(agentContext: AgentContext, session: TransportSession, message: AgentBaseMessage) {
this.logger.debug(`Existing ${session.type} transport session has been found.`)
if (!session.keys) {
throw new AriesFrameworkError(`There are no keys for the given ${session.type} transport session.`)
Expand Down Expand Up @@ -517,7 +517,7 @@ export class MessageSender {
} else if (outOfBand) {
this.logger.debug(`Resolving services from out-of-band record ${outOfBand.id}.`)
if (connection.isRequester) {
const services = outOfBand.outOfBandInvitation?.getServices() || []
const services = outOfBand.getOutOfBandInvitation().getServices() || []
for (const service of services) {
// Resolve dids to DIDDocs to retrieve services
if (typeof service === 'string') {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/TransportService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from './AgentMessage'
import type { AgentBaseMessage } from './AgentBaseMessage'
import type { Key } from '../crypto'
import type { EncryptedMessage } from '../didcomm'
import type { DidDocument } from '../modules/dids'
Expand Down Expand Up @@ -71,7 +71,7 @@ export interface TransportSession {
keys?: SessionKeys

// A received message that will be used to check whether it has any return routing.
inboundMessage?: AgentMessage
inboundMessage?: AgentBaseMessage

// A stored connection id used to find this session via the `TransportService` for a specific connection
connectionId?: string
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/__tests__/stubs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AgentMessage } from '../AgentMessage'
import type { AgentBaseMessage } from '../AgentBaseMessage'
import type { TransportSession, SessionKeys } from '../TransportService'

export class DummyTransportSession implements TransportSession {
public id: string
public readonly type = 'http'
public keys?: SessionKeys
public inboundMessage?: AgentMessage
public inboundMessage?: AgentBaseMessage
public connectionId?: string

public constructor(id: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/models/InboundMessageContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Key } from '../../crypto'
import type { ConnectionRecord } from '../../modules/connections'
import type { AgentMessage } from '../AgentMessage'
import type { AgentBaseMessage } from '../AgentBaseMessage'
import type { AgentContext } from '../context'

import { AriesFrameworkError } from '../../error'
Expand All @@ -13,7 +13,7 @@ export interface MessageContextParams {
agentContext: AgentContext
}

export class InboundMessageContext<T extends AgentMessage = AgentMessage> {
export class InboundMessageContext<T extends AgentBaseMessage = AgentBaseMessage> {
public message: T
public connection?: ConnectionRecord
public sessionId?: string
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/models/OutboundMessageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ResolvedDidCommService } from '../../modules/didcomm'
import type { DidDocument } from '../../modules/dids/domain/DidDocument'
import type { OutOfBandRecord } from '../../modules/oob'
import type { BaseRecord } from '../../storage/BaseRecord'
import type { AgentMessage } from '../AgentMessage'
import type { AgentBaseMessage } from '../AgentBaseMessage'
import type { AgentContext } from '../context'

import { AriesFrameworkError } from '../../error'
Expand All @@ -29,7 +29,7 @@ export interface OutboundMessageContextParams {
senderDidDocument?: DidDocument
}

export class OutboundMessageContext<T extends AgentMessage = AgentMessage> {
export class OutboundMessageContext<T extends AgentBaseMessage = AgentBaseMessage> {
public message: T
public connection?: ConnectionRecord
public serviceParams?: ServiceMessageParams
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/decorators/attachment/v1/Attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface AttachmentOptions {
mimeType?: string
lastmodTime?: Date
byteCount?: number
data: AttachmentData
data: AttachmentDataOptions
}

export interface AttachmentDataOptions {
Expand Down Expand Up @@ -97,7 +97,7 @@ export class Attachment {
this.mimeType = options.mimeType
this.lastmodTime = options.lastmodTime
this.byteCount = options.byteCount
this.data = options.data
this.data = new AttachmentData(options.data)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/decorators/thread/ThreadDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ThreadDecorator {
public threadId?: string

/**
* An optional parent `threadId`. Used when branching or nesting a new interaction off of an existing one.
* An optional parent `thid`. Used when branching or nesting a new interaction off of an existing one.
*/
@Expose({ name: 'pthid' })
@Matches(MessageIdRegExp)
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/didcomm/versions/v1/DidCommV1Message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from '../../../agent/AgentMessage'
import type { AgentBaseMessage } from '../../../agent/AgentBaseMessage'
import type { ServiceDecorator } from '../../../decorators/service/ServiceDecorator'

import { Exclude } from 'class-transformer'
Expand All @@ -22,7 +22,7 @@ const Decorated = ThreadDecorated(
)
)

export class DidCommV1Message extends Decorated implements AgentMessage {
export class DidCommV1Message extends Decorated implements AgentBaseMessage {
/**
* Whether the protocol RFC was initially written using the legacy did:prefix instead of the
* new https://didcomm.org message type prefix.
Expand Down Expand Up @@ -60,3 +60,6 @@ export class DidCommV1Message extends Decorated implements AgentMessage {
return this.type === Class.type.messageTypeUri
}
}

// type alias to keep backward compatibility
export class AgentMessage extends DidCommV1Message {}
4 changes: 2 additions & 2 deletions packages/core/src/didcomm/versions/v1/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidCommV1Message } from './DidCommV1Message'
import type { PlaintextDidCommV1Message } from './types'
import type { AgentMessage } from '../../../agent/AgentMessage'
import type { AgentBaseMessage } from '../../../agent/AgentBaseMessage'
import type { EncryptedMessage, ProtectedMessage } from '../../types'

import { AriesFrameworkError } from '../../../error'
Expand All @@ -9,7 +9,7 @@ import { DidCommMessageVersion } from '../../types'

import { DidCommV1Algorithms, DidCommV1Types } from './types'

export function isDidCommV1Message(message: AgentMessage): message is DidCommV1Message {
export function isDidCommV1Message(message: AgentBaseMessage): message is DidCommV1Message {
return message.didCommVersion == DidCommMessageVersion.V1
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/didcomm/versions/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Key } from '../../../crypto'

export { DidCommV1Message } from './DidCommV1Message'
export { DidCommV1Message, AgentMessage } from './DidCommV1Message'
export { DidCommV1BaseMessage, DidComV1BaseMessageConstructor } from './DidCommV1BaseMessage'

export interface DidCommV1PackMessageParams {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/didcomm/versions/v2/DidCommV2Message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentMessage } from '../../../agent/AgentMessage'
import type { AgentBaseMessage } from '../../../agent/AgentBaseMessage'
import type { ServiceDecorator } from '../../../decorators/service/ServiceDecorator'

import { AriesFrameworkError } from '../../../error'
Expand All @@ -7,7 +7,7 @@ import { DidCommMessageVersion } from '../../types'

import { DidCommV2BaseMessage } from './DidCommV2BaseMessage'

export class DidCommV2Message extends DidCommV2BaseMessage implements AgentMessage {
export class DidCommV2Message extends DidCommV2BaseMessage implements AgentBaseMessage {
public get didCommVersion(): DidCommMessageVersion {
return DidCommMessageVersion.V2
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/didcomm/versions/v2/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { DidCommV2Message } from './DidCommV2Message'
import type { PlaintextDidCommV2Message } from './types'
import type { AgentMessage } from '../../../agent/AgentMessage'
import type { AgentBaseMessage } from '../../../agent/AgentBaseMessage'

import { DidCommMessageVersion } from '../../types'

export function isDidCommV2Message(message: AgentMessage): message is DidCommV2Message {
export function isDidCommV2Message(message: AgentBaseMessage): message is DidCommV2Message {
return message.didCommVersion == DidCommMessageVersion.V2
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { MessageHandler, MessageHandlerInboundMessage } from './agent/MessageHan
export { MessageHandlerRegistry } from './agent/MessageHandlerRegistry'
export * from './agent/models'
export { AgentConfig } from './agent/AgentConfig'
export { AgentMessage } from './agent/AgentMessage'
export { AgentBaseMessage } from './agent/AgentBaseMessage'
export { Dispatcher } from './agent/Dispatcher'
export { MessageSender } from './agent/MessageSender'
export type { AgentDependencies } from './agent/AgentDependencies'
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/modules/oob/OutOfBandApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export class OutOfBandApi {
public async receiveInvitation(
invitation: OutOfBandInvitation | ConnectionInvitationMessage | V2OutOfBandInvitation,
config: ReceiveOutOfBandInvitationConfig = {}
): Promise<{ outOfBandRecord?: OutOfBandRecord; connectionRecord?: ConnectionRecord }> {
): Promise<{ outOfBandRecord: OutOfBandRecord; connectionRecord?: ConnectionRecord }> {
return this._receiveInvitation(invitation, config)
}

Expand Down Expand Up @@ -414,7 +414,7 @@ export class OutOfBandApi {
private async _receiveInvitation(
invitation: OutOfBandInvitation | ConnectionInvitationMessage | V2OutOfBandInvitation,
config: BaseReceiveOutOfBandInvitationConfig = {}
): Promise<{ outOfBandRecord?: OutOfBandRecord; connectionRecord?: ConnectionRecord }> {
): Promise<{ outOfBandRecord: OutOfBandRecord; connectionRecord?: ConnectionRecord }> {
const { routing } = config

const autoAcceptInvitation = config.autoAcceptInvitation ?? true
Expand Down Expand Up @@ -546,7 +546,11 @@ export class OutOfBandApi {
const outOfBandRecord = await this.outOfBandService.getById(this.agentContext, outOfBandId)

if (outOfBandRecord.v2OutOfBandInvitation) {
return this.v2OutOfBandService.acceptInvitation(this.agentContext, outOfBandRecord.v2OutOfBandInvitation)
const { connectionRecord } = await this.v2OutOfBandService.acceptInvitation(
this.agentContext,
outOfBandRecord.v2OutOfBandInvitation
)
return { outOfBandRecord, connectionRecord }
}
const timeoutMs = config.timeoutMs ?? 20000

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/storage/didcomm/DidCommMessageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidCommMessageRole } from './DidCommMessageRole'
import type { AgentContext } from '../../agent'
import type { AgentMessage } from '../../agent/AgentMessage'
import type { AgentBaseMessage } from '../../agent/AgentBaseMessage'
import type { ConstructableDidCommMessage } from '../../didcomm'
import type { JsonObject } from '../../types'

Expand Down Expand Up @@ -85,7 +85,7 @@ export class DidCommMessageRepository extends Repository<DidCommMessageRecord> {

export interface SaveAgentMessageOptions {
role: DidCommMessageRole
agentMessage: AgentMessage
agentMessage: AgentBaseMessage
associatedRecordId: string
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/connections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('connections', () => {
multiUseInvitation: true,
})

const invitation = faberOutOfBandRecord.outOfBandInvitation!
const invitation = faberOutOfBandRecord.getOutOfBandInvitation()
const invitationUrl = invitation.toUrl({ domain: 'https://example.com' })

// Receive invitation first time with alice agent
Expand Down
2 changes: 1 addition & 1 deletion samples/extension-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ agent.events.on(DummyEventTypes.StateChanged, async (event: DummyStateChangedEve
}
})

const { outOfBandRecord } = await agent.modules.dummy.request(connection)
const record = await agent.modules.dummy.request(connection)
```

## Run demo
Expand Down
Loading

0 comments on commit 3b2e494

Please sign in to comment.