Skip to content

Commit 5e9a641

Browse files
authored
feat(core): update agent label and imageUrl plus per connection label and imageUrl (#516)
* feat(core): support for updating agent label * feat(core): added support for custom label to connection invitation and request * feat(core): implemented custom image url Signed-off-by: janrtvld <jan@animo.id>
1 parent a4d6b6b commit 5e9a641

6 files changed

Lines changed: 98 additions & 9 deletions

File tree

packages/core/src/agent/Agent.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,8 @@ export class Agent {
215215
public get injectionContainer() {
216216
return this.container
217217
}
218+
219+
public get config() {
220+
return this.agentConfig
221+
}
218222
}

packages/core/src/agent/AgentConfig.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DidCommMimeType } from '../types'
1515

1616
export class AgentConfig {
1717
private initConfig: InitConfig
18+
public label: string
1819
public logger: Logger
1920
public readonly agentDependencies: AgentDependencies
2021
public readonly fileSystem: FileSystem
@@ -24,6 +25,7 @@ export class AgentConfig {
2425

2526
public constructor(initConfig: InitConfig, agentDependencies: AgentDependencies) {
2627
this.initConfig = initConfig
28+
this.label = initConfig.label
2729
this.logger = initConfig.logger ?? new ConsoleLogger(LogLevel.off)
2830
this.agentDependencies = agentDependencies
2931
this.fileSystem = new agentDependencies.FileSystem()
@@ -38,10 +40,6 @@ export class AgentConfig {
3840
}
3941
}
4042

41-
public get label() {
42-
return this.initConfig.label
43-
}
44-
4543
public get publicDidSeed() {
4644
return this.initConfig.publicDidSeed
4745
}

packages/core/src/agent/__tests__/Agent.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ describe('Agent', () => {
9494
})
9595
})
9696

97+
describe('Change label', () => {
98+
let agent: Agent
99+
100+
it('should return new label after setter is called', async () => {
101+
expect.assertions(2)
102+
const newLabel = 'Agent: Agent Class Test 2'
103+
104+
agent = new Agent(config, dependencies)
105+
expect(agent.config.label).toBe(config.label)
106+
107+
agent.config.label = newLabel
108+
expect(agent.config.label).toBe(newLabel)
109+
})
110+
})
111+
97112
describe('Dependency Injection', () => {
98113
it('should be able to resolve registered instances', () => {
99114
const agent = new Agent(config, dependencies)

packages/core/src/modules/connections/ConnectionsModule.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export class ConnectionsModule {
4848
alias?: string
4949
mediatorId?: string
5050
multiUseInvitation?: boolean
51+
myLabel?: string
52+
myImageUrl?: string
5153
}): Promise<{
5254
invitation: ConnectionInvitationMessage
5355
connectionRecord: ConnectionRecord
@@ -60,6 +62,8 @@ export class ConnectionsModule {
6062
alias: config?.alias,
6163
routing: myRouting,
6264
multiUseInvitation: config?.multiUseInvitation,
65+
myLabel: config?.myLabel,
66+
myImageUrl: config?.myImageUrl,
6367
})
6468

6569
return { connectionRecord, invitation }

packages/core/src/modules/connections/__tests__/ConnectionService.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,43 @@ describe('ConnectionService', () => {
151151
// Defaults to false
152152
expect(multiUseUndefined.multiUseInvitation).toBe(false)
153153
})
154+
155+
it('returns a connection record with the custom label from the config', async () => {
156+
expect.assertions(1)
157+
158+
const { message: invitation } = await connectionService.createInvitation({
159+
routing: myRouting,
160+
myLabel: 'custom-label',
161+
})
162+
163+
expect(invitation).toEqual(
164+
expect.objectContaining({
165+
label: 'custom-label',
166+
recipientKeys: [expect.any(String)],
167+
routingKeys: [],
168+
serviceEndpoint: config.endpoints[0],
169+
})
170+
)
171+
})
172+
173+
it('returns a connection record with the custom image url from the config', async () => {
174+
expect.assertions(1)
175+
176+
const { message: invitation } = await connectionService.createInvitation({
177+
routing: myRouting,
178+
myImageUrl: 'custom-image-url',
179+
})
180+
181+
expect(invitation).toEqual(
182+
expect.objectContaining({
183+
label: config.label,
184+
imageUrl: 'custom-image-url',
185+
recipientKeys: [expect.any(String)],
186+
routingKeys: [],
187+
serviceEndpoint: config.endpoints[0],
188+
})
189+
)
190+
})
154191
})
155192

156193
describe('processInvitation', () => {
@@ -248,6 +285,28 @@ describe('ConnectionService', () => {
248285
expect(message.imageUrl).toBe(connectionImageUrl)
249286
})
250287

288+
it('returns a connection request message containing a custom label', async () => {
289+
expect.assertions(1)
290+
291+
const connection = getMockConnection()
292+
mockFunction(connectionRepository.getById).mockReturnValue(Promise.resolve(connection))
293+
294+
const { message } = await connectionService.createRequest('test', { myLabel: 'custom-label' })
295+
296+
expect(message.label).toBe('custom-label')
297+
})
298+
299+
it('returns a connection request message containing a custom image url', async () => {
300+
expect.assertions(1)
301+
302+
const connection = getMockConnection()
303+
mockFunction(connectionRepository.getById).mockReturnValue(Promise.resolve(connection))
304+
305+
const { message } = await connectionService.createRequest('test', { myImageUrl: 'custom-image-url' })
306+
307+
expect(message.imageUrl).toBe('custom-image-url')
308+
})
309+
251310
it(`throws an error when connection role is ${ConnectionRole.Inviter} and not ${ConnectionRole.Invitee}`, async () => {
252311
expect.assertions(1)
253312

packages/core/src/modules/connections/services/ConnectionService.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export class ConnectionService {
6969
autoAcceptConnection?: boolean
7070
alias?: string
7171
multiUseInvitation?: boolean
72+
myLabel?: string
73+
myImageUrl?: string
7274
}): Promise<ConnectionProtocolMsgReturnType<ConnectionInvitationMessage>> {
7375
// TODO: public did
7476
const connectionRecord = await this.createConnection({
@@ -83,11 +85,11 @@ export class ConnectionService {
8385
const { didDoc } = connectionRecord
8486
const [service] = didDoc.didCommServices
8587
const invitation = new ConnectionInvitationMessage({
86-
label: this.config.label,
88+
label: config?.myLabel ?? this.config.label,
8789
recipientKeys: service.recipientKeys,
8890
serviceEndpoint: service.serviceEndpoint,
8991
routingKeys: service.routingKeys,
90-
imageUrl: this.config.connectionImageUrl,
92+
imageUrl: config?.myImageUrl ?? this.config.connectionImageUrl,
9193
})
9294

9395
connectionRecord.invitation = invitation
@@ -152,19 +154,26 @@ export class ConnectionService {
152154
* Create a connection request message for the connection with the specified connection id.
153155
*
154156
* @param connectionId the id of the connection for which to create a connection request
157+
* @param config config for creation of connection request
155158
* @returns outbound message containing connection request
156159
*/
157-
public async createRequest(connectionId: string): Promise<ConnectionProtocolMsgReturnType<ConnectionRequestMessage>> {
160+
public async createRequest(
161+
connectionId: string,
162+
config?: {
163+
myLabel?: string
164+
myImageUrl?: string
165+
}
166+
): Promise<ConnectionProtocolMsgReturnType<ConnectionRequestMessage>> {
158167
const connectionRecord = await this.connectionRepository.getById(connectionId)
159168

160169
connectionRecord.assertState(ConnectionState.Invited)
161170
connectionRecord.assertRole(ConnectionRole.Invitee)
162171

163172
const connectionRequest = new ConnectionRequestMessage({
164-
label: this.config.label,
173+
label: config?.myLabel ?? this.config.label,
165174
did: connectionRecord.did,
166175
didDoc: connectionRecord.didDoc,
167-
imageUrl: this.config.connectionImageUrl,
176+
imageUrl: config?.myImageUrl ?? this.config.connectionImageUrl,
168177
})
169178

170179
await this.updateState(connectionRecord, ConnectionState.Requested)

0 commit comments

Comments
 (0)