Skip to content

Commit

Permalink
feat: update from feedback
Browse files Browse the repository at this point in the history
Updated to validate and approve as array
Added timestamp validation
  • Loading branch information
Alex Risch authored and Alex Risch committed May 1, 2024
1 parent 99b08ea commit 240faf2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
58 changes: 41 additions & 17 deletions src/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ export class Contacts {
timestampMs: number,
peerAddress: string
): Promise<boolean> {
if (!signature || !timestampMs) {
return false
}
if (timestampMs > Date.now()) {
return false
}
if (timestampMs < Date.now() - 1000 * 60 * 60 * 24 * 30) {
return false
}
const signatureData = splitSignature(signature)
const message = WalletSigner.consentProofRequestText(
peerAddress,
Expand All @@ -271,20 +280,28 @@ export class Contacts {
return publicKey?.getEthereumAddress() === this.client.address
}

private async handleConsentProof(
consentProof: invitation.ConsentProofPayload,
peerAddress: string
): Promise<void> {
const { signature, timestamp } = consentProof
const isValid = await this.validateConsentSignature(
signature as `0x${string}`,
Number(timestamp),
peerAddress
private async handleConsentProofs(
consentProofs: {
consentProof: invitation.ConsentProofPayload
peerAddress: string
}[]
) {
const validConsentProofAddresses: string[] = []
const validationResults = await Promise.allSettled(
consentProofs.map((proofItem) => {
return this.validateConsentSignature(
proofItem.consentProof.signature as `0x${string}`,
Number(proofItem.consentProof.timestamp),
proofItem.peerAddress
)
})
)
if (!isValid) {
return
}
await this.client.contacts.allow([peerAddress])
validationResults.forEach((result, index) => {
if (result.status === 'fulfilled' && result.value) {
validConsentProofAddresses.push(consentProofs[index].peerAddress)
}
})
this.client.contacts.allow(validConsentProofAddresses)
}

async loadConsentList(startTime?: Date) {
Expand All @@ -293,17 +310,24 @@ export class Contacts {
const entries = await this.consentList.load(startTime ?? lastRun)
try {
const conversations = await this.client.conversations.list()
const consentProofs: {
consentProof: invitation.ConsentProofPayload
peerAddress: string
}[] = []
conversations.forEach((conversation) => {
if (
conversation.consentProof &&
this.consentState(conversation.peerAddress) === 'unknown'
) {
this.handleConsentProof(
conversation.consentProof,
conversation.peerAddress
)
consentProofs.push({
consentProof: conversation.consentProof,
peerAddress: conversation.peerAddress,
})
}
})
if (consentProofs.length) {
this.handleConsentProofs(consentProofs)
}
} catch (err) {
console.log(err)
}
Expand Down
7 changes: 0 additions & 7 deletions test/Contacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ describe('Contacts', () => {
expect(convo).toBeTruthy()
const isApproved = await convo?.isAllowed
expect(isApproved).toBe(true)
await alix.close()
await bo.close()
})

it('consent proof yields to network consent', async () => {
Expand All @@ -237,7 +235,6 @@ describe('Contacts', () => {
env: 'local',
})
alix1.contacts.deny([bo.address])
await alix1.close()
const alix2 = await Client.create(wallet, {
env: 'local',
})
Expand All @@ -264,8 +261,6 @@ describe('Contacts', () => {
await alix2.contacts.refreshConsentList()
const isDenied = await alix2.contacts.isDenied(bo.address)
expect(isDenied).toBeTruthy()
await alix2.close()
await bo.close()
})

it('consent proof correctly validates', async () => {
Expand Down Expand Up @@ -299,8 +294,6 @@ describe('Contacts', () => {
await alix.contacts.refreshConsentList()
const isAllowed = await alix.contacts.isAllowed(bo.address)
expect(isAllowed).toBeFalsy()
await alix.close()
await bo.close()
})
})
})

0 comments on commit 240faf2

Please sign in to comment.