Skip to content

Commit

Permalink
Merge pull request #449 from xmtp/nmolnar/topic-non-determinism
Browse files Browse the repository at this point in the history
Topic non-determinism fix
  • Loading branch information
nplasterer authored Aug 31, 2023
2 parents 0bcb160 + d0863cf commit ee62135
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/keystore/InMemoryKeystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,16 @@ export default class InMemoryKeystore implements Keystore {
}
const created = nsToDate(req.createdNs)
const recipient = toSignedPublicKeyBundle(req.recipient)
const myAddress = await this.getAccountAddress()
const theirAddress = await recipient.walletSignatureAddress()

const secret = await this.v2Keys.sharedSecret(
recipient,
this.v2Keys.getCurrentPreKey().publicKey,
false
myAddress < theirAddress
)

const sortedAddresses = [
this.accountAddress,
await recipient.walletSignatureAddress(),
].sort()
const sortedAddresses = [myAddress, theirAddress].sort()

const msgString =
(req.context?.conversationId || '') + sortedAddresses.join()
Expand Down
54 changes: 51 additions & 3 deletions test/keystore/InMemoryKeystore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,15 @@ describe('InMemoryKeystore', () => {
)
).v1!
)
bobKeystore = await InMemoryKeystore.create(bobKeys)

expect(await aliceKeystore.getAccountAddress()).toEqual(
'0xF56d1F3b1290204441Cb3843C2Cac1C2f5AEd690'
) // alice
expect(bobKeys.getPublicKeyBundle().walletSignatureAddress()).toEqual(
'0x3De402A325323Bb97f00cE3ad5bFAc96A11F9A34'
) // bob
const response = await aliceKeystore.createInvite({
const aliceInvite = await aliceKeystore.createInvite({
recipient: SignedPublicKeyBundle.fromLegacyBundle(
bobKeys.getPublicKeyBundle()
),
Expand All @@ -570,7 +572,21 @@ describe('InMemoryKeystore', () => {
metadata: {},
},
})
expect(response.conversation!.topic).toEqual(
expect(aliceInvite.conversation!.topic).toEqual(
'/xmtp/0/m-4b52be1e8567d72d0bc407debe2d3c7fca2ae93a47e58c3f9b5c5068aff80ec5/proto'
)

const bobInvite = await bobKeystore.createInvite({
recipient: SignedPublicKeyBundle.fromLegacyBundle(
aliceKeys.getPublicKeyBundle()
),
createdNs: dateToNs(new Date()),
context: {
conversationId: 'test',
metadata: {},
},
})
expect(bobInvite.conversation!.topic).toEqual(
'/xmtp/0/m-4b52be1e8567d72d0bc407debe2d3c7fca2ae93a47e58c3f9b5c5068aff80ec5/proto'
)
})
Expand Down Expand Up @@ -616,7 +632,39 @@ describe('InMemoryKeystore', () => {
).toHaveLength(25)
})

it('works with persistence', async () => {})
it('creates deterministic topics bidirectionally', async () => {
const aliceInvite = await aliceKeystore.createInvite({
recipient: SignedPublicKeyBundle.fromLegacyBundle(
bobKeys.getPublicKeyBundle()
),
createdNs: dateToNs(new Date()),
context: undefined,
})
const bobInvite = await bobKeystore.createInvite({
recipient: SignedPublicKeyBundle.fromLegacyBundle(
aliceKeys.getPublicKeyBundle()
),
createdNs: dateToNs(new Date()),
context: undefined,
})
expect(
await aliceKeys.sharedSecret(
bobKeys.getPublicKeyBundle(),
aliceKeys.getCurrentPreKey().publicKey,
false
)
).toEqual(
await bobKeys.sharedSecret(
aliceKeys.getPublicKeyBundle(),
bobKeys.getCurrentPreKey().publicKey,
true
)
)

expect(aliceInvite.conversation!.topic).toEqual(
bobInvite.conversation!.topic
)
})
})

describe('createAuthToken', () => {
Expand Down

0 comments on commit ee62135

Please sign in to comment.