Skip to content

Commit

Permalink
add Messages to SDK Api
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Nov 21, 2024
1 parent 17dd080 commit 0c838b3
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 13 deletions.
38 changes: 37 additions & 1 deletion packages/js-sdk/src/Dm3Sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('Dm3Sdk', () => {
});
});

describe('messages', () => {
describe('Messages', () => {
it('can send a message', async () => {
const mockTldResolver = {
resolveTLDtoAlias: async () =>
Expand Down Expand Up @@ -235,6 +235,42 @@ describe('Dm3Sdk', () => {
expect(c?.messages.list().length).toBe(0);
await c?.messages.addMessage('bob.eth', msg1);

expect(c?.messages.list().length).toBe(1);
expect(c?.messages.list()[0].envelop.message.message).toBe('Hi');
});
it('can send a message', async () => {
const mockTldResolver = {
resolveTLDtoAlias: async () =>
`${normalizeEnsName(bob.address)}.addr.test`,
resolveAliasToTLD: async () => 'bob.eth',
} as unknown as ITLDResolver;

const mockConfig: Dm3SdkConfig = {
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
storageApi: {
addConversation: async () => {},
addMessage: async () => {},
} as unknown as StorageAPI,
nonce: '1',
defaultDeliveryService: 'test.io',
addressEnsSubdomain: '.addr.test',
userEnsSubdomain: '.user.test',
resolverBackendUrl: 'resolver.io',
backendUrl: 'http://localhost:4060',
_tld: mockTldResolver,
};

const dm3 = await new Dm3Sdk(mockConfig).login({
profileKeys: alice.profileKeys,
profile: alice.signedUserProfile,
accountAddress: alice.address,
});

const c = await dm3.conversations.addConversation('bob.eth');
expect(c?.messages.list().length).toBe(0);

await c?.messages.sendMessage('Hi');

expect(c?.messages.list().length).toBe(1);
expect(c?.messages.list()[0].envelop.message.message).toBe('Hi');
});
Expand Down
1 change: 1 addition & 0 deletions packages/js-sdk/src/Dm3Sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class Dm3Sdk {
tld,
this.mainnetProvider,
account,
profileKeys,
this.addressEnsSubdomain,
);

Expand Down
34 changes: 29 additions & 5 deletions packages/js-sdk/src/conversation/Conversations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable max-len */
import { Account, normalizeEnsName } from '@dm3-org/dm3-lib-profile';
import {
Account,
normalizeEnsName,
ProfileKeys,
} from '@dm3-org/dm3-lib-profile';
import {
Conversation as ConversationDto,
StorageAPI,
Expand All @@ -16,21 +20,23 @@ export class Conversations {
private readonly tld: ITLDResolver;
private readonly addressEnsSubdomain: string;
private readonly account: Account;

private readonly profileKeys: ProfileKeys;
public list: Conversation[];

constructor(
storageApi: StorageAPI,
tld: ITLDResolver,
mainnetProvider: ethers.providers.JsonRpcProvider,
account: Account,
profileKeys: ProfileKeys,
addressEnsSubdomain: string,
) {
this.storageApi = storageApi;
this.tld = tld;
this.account = account;
this.provider = mainnetProvider;
this.addressEnsSubdomain = addressEnsSubdomain;
this.profileKeys = profileKeys;
this.list = [];
}

Expand Down Expand Up @@ -89,7 +95,13 @@ export class Conversations {

const newConversation: Conversation = {
//TODO change that once Message class has been implemented
messages: new Messages(this.storageApi, this),
messages: new Messages(
this.storageApi,
this,
this.account,
this.profileKeys,
newContact,
),
contact: newContact,
};
//Set the new contact to the list
Expand All @@ -103,7 +115,13 @@ export class Conversations {
);

const hydratedConversation: Conversation = {
messages: new Messages(this.storageApi, this),
messages: new Messages(
this.storageApi,
this,
this.account,
this.profileKeys,
hydratedContact,
),
contact: hydratedContact,
};
//find existing contact and replace it with the hydrated one
Expand Down Expand Up @@ -132,7 +150,13 @@ export class Conversations {
this.addressEnsSubdomain,
);
const hydratedConversation: Conversation = {
messages: new Messages(this.storageApi, this),
messages: new Messages(
this.storageApi,
this,
this.account,
this.profileKeys,
hydratedContact,
),
contact: hydratedContact,
};
this.list.push(hydratedConversation);
Expand Down
46 changes: 39 additions & 7 deletions packages/js-sdk/src/message/Messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encryptAsymmetric } from '@dm3-org/dm3-lib-crypto';
import { encryptAsymmetric, sign } from '@dm3-org/dm3-lib-crypto';
import {
buildEnvelop,
EncryptionEnvelop,
Expand All @@ -22,21 +22,53 @@ import { MessageModel, MessageSource } from './types';
export class Messages {
private readonly storageApi: StorageAPI;
private readonly conversations: Conversations;
private readonly account: Account;
private readonly profileKeys: ProfileKeys;

private readonly _messages: MessageModel[];

constructor(storageApi: StorageAPI, conversations: Conversations) {
private readonly senderAccount: Account;
private readonly senderProfileKeys: ProfileKeys;
private readonly receiver: Contact;

constructor(
storageApi: StorageAPI,
conversations: Conversations,
senderAccount: Account,
senderProfileKeys: ProfileKeys,
receiver: Contact,
) {
this.storageApi = storageApi;
this.conversations = conversations;

this.senderAccount = senderAccount;
this.senderProfileKeys = senderProfileKeys;
this.receiver = receiver;
this._messages = [];
}

public list() {
return renderMessage(this._messages);
}
public async sendMessage(msg: string) {
const messageWithoutSig: Omit<Message, 'signature'> = {
message: msg,
attachments: [],
metadata: {
referenceMessageHash: undefined,
type: 'NEW',
to: this.receiver.account.ensName,
from: this.senderAccount.ensName,
timestamp: new Date().getTime(),
},
};

const message: Message = {
...messageWithoutSig,
signature: await sign(
this.senderProfileKeys.signingKeyPair.privateKey,
stringify(messageWithoutSig),
),
};
return await this.addMessage(this.receiver.account.ensName, message);
}

public async addMessage(
_contactName: string,
Expand Down Expand Up @@ -112,7 +144,7 @@ export class Messages {
(publicKey: string, msg: string) =>
encryptAsymmetric(publicKey, msg),
{
from: this.account!,
from: this.senderAccount!,
to: {
...recipient!.account,
//Cover edge case of lukso names. TODO discuss with the team and decide how to dela with non ENS names
Expand All @@ -121,7 +153,7 @@ export class Messages {
: recipient.name,
},
deliverServiceProfile,
keys: this.profileKeys!,
keys: this.senderProfileKeys,
},
);
},
Expand Down

0 comments on commit 0c838b3

Please sign in to comment.