Skip to content

Commit

Permalink
Merge pull request #473 from xmtp/rygine/fix-address-comparisons
Browse files Browse the repository at this point in the history
fix: use lowercase to compare addresses
  • Loading branch information
rygine authored Oct 26, 2023
2 parents 645490b + 4acf501 commit d9bdfc6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
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

0 comments on commit d9bdfc6

Please sign in to comment.