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

fix: use lowercase to compare addresses #473

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ async function getUserContactFromNetwork(
address = undefined
}

if (address === peerAddress) {
if (address?.toLowerCase() === peerAddress.toLowerCase()) {
return keyBundle
}
}
Expand Down Expand Up @@ -815,7 +815,7 @@ async function getUserContactsFromNetwork(
try {
const keyBundle = decodeContactBundle(env.message)
const signingAddress = await keyBundle?.walletSignatureAddress()
if (address === signingAddress) {
if (address.toLowerCase() === signingAddress.toLowerCase()) {
return keyBundle
} else {
console.info('Received contact bundle with incorrect address')
Expand Down
12 changes: 7 additions & 5 deletions src/conversations/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ export default class Conversations<ContentTypes = any> {
if (msg instanceof DecodedMessage && msg.contentTopic === introTopic) {
const convo = new ConversationV1(
this.client,
msg.recipientAddress === this.client.address
msg.recipientAddress?.toLowerCase() ===
this.client.address.toLowerCase()
? (msg.senderAddress as string)
: (msg.recipientAddress as string),
msg.sent
Expand Down Expand Up @@ -474,7 +475,7 @@ export default class Conversations<ContentTypes = any> {
throw new Error(`Recipient ${peerAddress} is not on the XMTP network`)
}

if (peerAddress === this.client.address) {
if (peerAddress.toLowerCase() === this.client.address.toLowerCase()) {
throw new Error('self messaging not supported')
}

Expand All @@ -487,7 +488,7 @@ export default class Conversations<ContentTypes = any> {
if (!context?.conversationId) {
const v1Convos = await this.listV1Conversations()
const matchingConvo = v1Convos.find(
(convo) => convo.peerAddress === peerAddress
(convo) => convo.peerAddress.toLowerCase() === peerAddress.toLowerCase()
)
// If intro already exists, return V1 conversation
// if both peers have V1 compatible key bundles
Expand Down Expand Up @@ -516,7 +517,7 @@ export default class Conversations<ContentTypes = any> {

// Define a function for matching V2 conversations
const matcherFn = (convo: Conversation<ContentTypes>) =>
convo.peerAddress === peerAddress &&
convo.peerAddress.toLowerCase() === peerAddress.toLowerCase() &&
isMatchingContext(context, convo.context ?? undefined)

const existing = await this.getV2ConversationsFromKeystore()
Expand Down Expand Up @@ -571,7 +572,8 @@ export default class Conversations<ContentTypes = any> {

private getPeerAddress(message: MessageV1): string {
const peerAddress =
message.recipientAddress === this.client.address
message.recipientAddress?.toLowerCase() ===
this.client.address.toLowerCase()
? message.senderAddress
: message.recipientAddress

Expand Down
3 changes: 3 additions & 0 deletions test/conversations/Conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ describe('conversation', () => {
expect(
alice.conversations.newConversation(alice.address)
).rejects.toThrow('self messaging not supported')
expect(
alice.conversations.newConversation(alice.address.toLowerCase())
).rejects.toThrow('self messaging not supported')
})

it('can send a prepared message v1', async () => {
Expand Down
13 changes: 13 additions & 0 deletions test/conversations/Conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,19 @@ describe('conversations', () => {
expect(bobConvo instanceof ConversationV1).toBeTruthy()
})

it('does not create a duplicate conversation with an address case mismatch', async () => {
const convo1 = await alice.conversations.newConversation(bob.address)
await convo1.send('gm')
const convos = await alice.conversations.list()
expect(convos).toHaveLength(1)
const convo2 = await alice.conversations.newConversation(
bob.address.toLowerCase()
)
await convo2.send('gm')
const convos2 = await alice.conversations.list()
expect(convos2).toHaveLength(1)
})

it('continues to use v1 conversation even after upgrading bundle', async () => {
const aliceConvo = await alice.conversations.newConversation(bob.address)
await aliceConvo.send('gm')
Expand Down
Loading