Skip to content

Commit

Permalink
normalize ensName when adding conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Sep 20, 2024
1 parent efe7592 commit cfa2b7e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../utils/enum-type-utils';
import { closeLoader, startLoader } from '../Loader/Loader';
import './AddConversation.css';
import { normalizeEnsName } from '@dm3-org/dm3-lib-profile';

// class for input field
export const INPUT_FIELD_CLASS =
Expand Down Expand Up @@ -56,7 +57,8 @@ export default function AddConversation() {
return;
}

const newContact = await addConversation(tldName);
const normalizedEnsName = normalizeEnsName(tldName);
const newContact = await addConversation(normalizedEnsName);

const addConversationData = {
active: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DM3Configuration } from '../../interfaces/config';
import { ContactPreview, getEmptyContact } from '../../interfaces/utils';
import { useMainnetProvider } from '../mainnetprovider/useMainnetProvider';
import { hydrateContract } from './hydrateContact';
import { ethers } from 'ethers';

const DEFAULT_CONVERSATION_PAGE_SIZE = 10;

Expand Down Expand Up @@ -224,13 +225,21 @@ export const useConversation = (config: DM3Configuration) => {
const contactTldName = normalizeEnsName(_ensName);
//rrsolves the TLD name. That name becomes the identifier for the conversation
const aliasName = await resolveTLDtoAlias(contactTldName);

const defaultProfileLocation = ethers.utils.isAddress(contactTldName)
? normalizeEnsName(
contactTldName + dm3Configuration.addressEnsSubdomain,
)
: contactTldName;

const newConversation: Conversation = {
contactEnsName: aliasName,
contactProfileLocation: [contactTldName], //(ID)
contactProfileLocation: [defaultProfileLocation], //(ID)
isHidden: false,
previewMessage: undefined,
updatedAt: new Date().getTime(),
};

//Adds the conversation to the conversation state
const conversationPreview = _addConversation(newConversation);
//Add the contact to the storage in the background
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Envelop,
MessageState,
} from '@dm3-org/dm3-lib-messaging';
import { ProfileKeys } from '@dm3-org/dm3-lib-profile';
import { normalizeEnsName, ProfileKeys } from '@dm3-org/dm3-lib-profile';
import { ContactPreview } from '../../../interfaces/utils';
import { StoreMessageAsync } from '../../storage/useStorage';
import { ReceiptDispatcher } from '../receipt/ReceiptDispatcher';
Expand Down Expand Up @@ -40,7 +40,7 @@ export const handleMessagesFromWebSocket = async (

//we wait for the contact to be added to resolve TLD to alias
const contactPreview = await addConversation(
decryptedEnvelop.message.metadata.from,
normalizeEnsName(decryptedEnvelop.message.metadata.from),
);

// Resolve TLD to alias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export class EthAddressResolver implements ITLDResolver {
return ensName.endsWith(this.addrEnsSubdomain);
}
//The alias format is used to display in the UI
//e.g. 0x1234.user.dm3.eth -> 0x1234
//Since that is the lowest tier there is no alias for such an tldName left.
//e.g. 0x1234.user.dm3.eth -> 0x1234.user.dm3.eth
async resolveAliasToTLD(ensName: string): Promise<string> {
return ensName.split('.')[0];
//return ensName.split('.')[0];
return ensName;
}
//The alias format is used to store the contact in the DB
//e.g. 0x1234 -> 0x1234.user.dm3.eth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { OptimismNames } from './nameService/OptimismNames';
import { DM3Configuration } from '../../interfaces/config';
import { ITLDResolver } from './nameService/ITLDResolver';
import { ForeignName } from './nameService/ForeignName';
import { normalizeEnsName } from '@dm3-org/dm3-lib-profile';

const SUPPORTED_NAMESERVICES = (
provider: ethers.providers.JsonRpcProvider,
Expand Down Expand Up @@ -63,9 +64,11 @@ export const useTopLevelAlias = () => {
foreignTldName,
)
) {
const tldName = await nameservice.resolveAliasToTLD(
ensName,
foreignTldName,
const tldName = normalizeEnsName(
await nameservice.resolveAliasToTLD(
ensName,
foreignTldName,
),
);
setAliasTldCache((prev) => ({ ...prev, [ensName]: tldName }));
return tldName;
Expand All @@ -83,7 +86,9 @@ export const useTopLevelAlias = () => {
dm3Configuration,
)) {
if (await nameservice.isResolverForTldName(ensName)) {
const aliasName = await nameservice.resolveTLDtoAlias(ensName);
const aliasName = normalizeEnsName(
await nameservice.resolveTLDtoAlias(ensName),
);
setTldAliasCache((prev) => ({ ...prev, [ensName]: aliasName }));
return aliasName;
}
Expand Down

0 comments on commit cfa2b7e

Please sign in to comment.