Skip to content

Commit

Permalink
feat: consent proof updates
Browse files Browse the repository at this point in the history
Updated consent proof handling to be on the updateV2Conversations method instead
  • Loading branch information
Alex Risch authored and Alex Risch committed Apr 30, 2024
1 parent acf204b commit 854779e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/conversations/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
message,
content as proto,
type invitation,
type keystore,
type messageApi,
} from '@xmtp/proto'
import type { ConsentProofPayload } from '@xmtp/proto/ts/dist/types/message_contents/invitation.pb'
import { getAddress } from 'viem'
import type { OnConnectionLostCallback } from '@/ApiClient'
import type {
Expand Down Expand Up @@ -89,7 +89,7 @@ export interface Conversation<ContentTypes = any> {
/**
* Proof of consent for the conversation, used when a user has pre-consented to a conversation
*/
consentProof?: ConsentProofPayload
consentProof?: invitation.ConsentProofPayload

/**
* Retrieve messages in this conversation. Default to returning all messages.
Expand Down Expand Up @@ -508,15 +508,15 @@ export class ConversationV2<ContentTypes>
peerAddress: string
createdAt: Date
context?: InvitationContext
consentProof?: ConsentProofPayload
consentProof?: invitation.ConsentProofPayload

constructor(
client: Client<ContentTypes>,
topic: string,
peerAddress: string,
createdAt: Date,
context: InvitationContext | undefined,
consentProof: ConsentProofPayload | undefined
consentProof: invitation.ConsentProofPayload | undefined
) {
this.topic = topic
this.createdAt = createdAt
Expand Down Expand Up @@ -550,7 +550,7 @@ export class ConversationV2<ContentTypes>
return this.client.contacts.consentState(this.peerAddress)
}

get consentProofPayload(): ConsentProofPayload | undefined {
get consentProofPayload(): invitation.ConsentProofPayload | undefined {
return this.consentProof
}

Expand Down
22 changes: 12 additions & 10 deletions src/conversations/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,25 @@ export default class Conversations<ContentTypes = any> {
startTime,
direction: SortDirection.SORT_DIRECTION_ASCENDING,
})

return this.decodeInvites(envelopes)
const newConversations = await this.decodeInvites(envelopes)
newConversations.forEach((convo) => {
if (convo.consentProofPayload) {
this.handleConsentProof(convo.consentProofPayload, convo.peerAddress)
}
})
return newConversations
}

private async validateConsentSignature(
signature: `0x${string}`,
timestamp: number,
timestampMs: number,
peerAddress: string
): Promise<boolean> {
const signatureData = splitSignature(signature)
const message = WalletSigner.consentProofRequestText(peerAddress, timestamp)
const message = WalletSigner.consentProofRequestText(
peerAddress,
timestampMs
)
const digest = hexToBytes(hashMessage(message))
// Recover public key
const publicKey = ecdsaSignerKey(digest, signatureData)
Expand Down Expand Up @@ -209,12 +217,6 @@ export default class Conversations<ContentTypes = any> {
const out: ConversationV2<ContentTypes>[] = []
for (const response of responses) {
try {
if (response.result?.conversation?.consentProofPayload) {
this.handleConsentProof(
response.result.conversation.consentProofPayload,
response.result.conversation.peerAddress
)
}
out.push(this.saveInviteResponseToConversation(response))
} catch (e) {
console.warn('Error saving invite response to conversation: ', e)
Expand Down

0 comments on commit 854779e

Please sign in to comment.