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

feat: check callback error signature #35

Merged
merged 1 commit into from
May 27, 2024
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
11 changes: 10 additions & 1 deletion src/farcaster-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface ICallbackDataCheck extends IAddressesInfo {
* Auth service proof signature
*/
proof: string
/**
* Error message in case of failure
*/
errorMessage?: string
}

export class FarcasterClient {
Expand Down Expand Up @@ -207,7 +211,12 @@ export class FarcasterClient {

if (
(await extractSignerAddress(
DelegatedFs.getDelegatedText(data.userMainAddress, data.userDelegatedAddress, expectedApplicationAddress),
DelegatedFs.getDelegatedText(
data.userMainAddress,
data.userDelegatedAddress,
expectedApplicationAddress,
data.errorMessage,
),
data.proof,
)) !== expectedAuthServiceAddress
) {
Expand Down
22 changes: 19 additions & 3 deletions src/service/delegated-fs/delegated-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ export class DelegatedFs {
* @param userAddress User address
* @param userDelegatedAddress User delegated address
* @param applicationAddress Application address
* @param errorText Error text
*/
static getDelegatedText(userAddress: string, userDelegatedAddress: string, applicationAddress: string): string {
static getDelegatedText(
userAddress: string,
userDelegatedAddress: string,
applicationAddress: string,
errorText?: string,
): string {
userAddress = prepareEthAddress(userAddress)
userDelegatedAddress = prepareEthAddress(userDelegatedAddress)
applicationAddress = prepareEthAddress(applicationAddress)
Expand All @@ -54,7 +60,13 @@ export class DelegatedFs {
throw new Error('Delegated text addresses must be unique')
}

return `${userAddress}${userDelegatedAddress}${applicationAddress}`
let text = `${userAddress}${userDelegatedAddress}${applicationAddress}`

if (errorText) {
text = `ERROR${text}${errorText}`
}

return text
}

/**
Expand All @@ -63,15 +75,19 @@ export class DelegatedFs {
* @param userDelegatedAddress User delegated address
* @param applicationAddress Application address
* @param signer Signer
* @param errorText Error text
*/
static async createDelegateSignature(
userAddress: string,
userDelegatedAddress: string,
applicationAddress: string,
signer: ISigner,
errorText?: string,
): Promise<string> {
return prepareEthSignature(
await signer.signMessage(DelegatedFs.getDelegatedText(userAddress, userDelegatedAddress, applicationAddress)),
await signer.signMessage(
DelegatedFs.getDelegatedText(userAddress, userDelegatedAddress, applicationAddress, errorText),
),
)
}
}
123 changes: 123 additions & 0 deletions test/farcaster-client/check-callback-data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { createSdk } from '../utils/sdk'
import { optimismMainnetConfig } from '../../src/network-config'
import { Wallet } from 'ethers'
import { ICallbackDataCheck } from '../../src/farcaster-client'
import { DelegatedFs } from '../../src/service/delegated-fs/delegated-fs'

describe('Farcaster Client', () => {
it('should check correct callback data', async () => {
const sdk = await createSdk(optimismMainnetConfig, Wallet.createRandom())

const userMainWallet = Wallet.createRandom()
const userDelegatedWallet = Wallet.createRandom()
const applicationWallet = Wallet.createRandom()
const authServiceWallet = Wallet.createRandom()

const proof = await DelegatedFs.createDelegateSignature(
userMainWallet.address,
userDelegatedWallet.address,
applicationWallet.address,
authServiceWallet,
)

const dataToCheck: ICallbackDataCheck = {
userMainAddress: userMainWallet.address,
userDelegatedAddress: userDelegatedWallet.address,
applicationAddress: applicationWallet.address,
proof,
}

await expect(
sdk.farcasterClient.checkCallbackData(dataToCheck, applicationWallet.address, authServiceWallet.address),
).resolves.toBeUndefined()
})

it('should throw on incorrect callback data', async () => {
const sdk = await createSdk(optimismMainnetConfig, Wallet.createRandom())

const userMainWallet = Wallet.createRandom()
const userDelegatedWallet = Wallet.createRandom()
const applicationWallet = Wallet.createRandom()
const authServiceWallet = Wallet.createRandom()

const proof = await DelegatedFs.createDelegateSignature(
userMainWallet.address,
userDelegatedWallet.address,
applicationWallet.address,
// incorrect signer
applicationWallet,
)

const dataToCheck: ICallbackDataCheck = {
userMainAddress: userMainWallet.address,
userDelegatedAddress: userDelegatedWallet.address,
applicationAddress: applicationWallet.address,
proof,
}

await expect(
sdk.farcasterClient.checkCallbackData(dataToCheck, applicationWallet.address, authServiceWallet.address),
).rejects.toThrow('Invalid proof')
})

it('should check correct callback data with error', async () => {
const sdk = await createSdk(optimismMainnetConfig, Wallet.createRandom())

const userMainWallet = Wallet.createRandom()
const userDelegatedWallet = Wallet.createRandom()
const applicationWallet = Wallet.createRandom()
const authServiceWallet = Wallet.createRandom()

const errorMessage = 'My Custom Error'

const proofError = await DelegatedFs.createDelegateSignature(
userMainWallet.address,
userDelegatedWallet.address,
applicationWallet.address,
authServiceWallet,
errorMessage,
)

const dataToCheckError: ICallbackDataCheck = {
userMainAddress: userMainWallet.address,
userDelegatedAddress: userDelegatedWallet.address,
applicationAddress: applicationWallet.address,
proof: proofError,
errorMessage,
}

await expect(
sdk.farcasterClient.checkCallbackData(dataToCheckError, applicationWallet.address, authServiceWallet.address),
).resolves.toBeUndefined()
})

it('should throw with incorrect error', async () => {
const sdk = await createSdk(optimismMainnetConfig, Wallet.createRandom())

const userMainWallet = Wallet.createRandom()
const userDelegatedWallet = Wallet.createRandom()
const applicationWallet = Wallet.createRandom()
const authServiceWallet = Wallet.createRandom()

const errorMessage = 'My Custom Error'
const proofError = await DelegatedFs.createDelegateSignature(
userMainWallet.address,
userDelegatedWallet.address,
applicationWallet.address,
authServiceWallet,
errorMessage,
)

const dataToCheckError: ICallbackDataCheck = {
userMainAddress: userMainWallet.address,
userDelegatedAddress: userDelegatedWallet.address,
applicationAddress: applicationWallet.address,
proof: proofError,
errorMessage,
}

await expect(
sdk.farcasterClient.checkCallbackData(dataToCheckError, applicationWallet.address, authServiceWallet.address),
).resolves.toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion test/manual/delegatedFs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function getAuthProofByAddress() {

const proofData = await sdkMainnet.farcasterClient.getAuthProofByAddress(getUserAddress(), getApplicationAddress())
try {
sdkMainnet.farcasterClient.checkCallbackData(
await sdkMainnet.farcasterClient.checkCallbackData(
{
userMainAddress: proofData.userMainAddress,
userDelegatedAddress: proofData.userDelegatedAddress,
Expand Down
Loading