Skip to content

Commit

Permalink
Merge pull request #646 from clrfund/fix/sponsor-error
Browse files Browse the repository at this point in the history
Fix BrightId sponsor error 68
  • Loading branch information
yuetloo authored Mar 21, 2023
2 parents 6ce80aa + a1a1a1d commit 01ec413
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
37 changes: 34 additions & 3 deletions vue-app/src/api/bright-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ const BRIGHTID_APP_URL = 'https://app.brightid.org'
const NODE_URL = brightIdNodeUrl
const CONTEXT = process.env.VUE_APP_BRIGHTID_CONTEXT || 'clr.fund'

/**
* These errors from the BrightID sponsor api can be ignored
* https://github.com/BrightID/BrightID-Node/blob/8093479a60da07c3cd2be32fe4fd8382217c966e/web_services/foxx/brightid/errors.js
*
* 39 - The app generated id was sponsored before
* 63 - Spend request for this app-generated id submitted before.
* 68 - The app has sent this sponsor request recently
*/
const IGNORE_BRIGHTID_ERRORS = [39, 63, 68]

/**
* Check if the error number is in the ignore list.
* @param errorNum error number to check
* @returns true if the error is one of the IGNORE_BRIGHTID_ERROS
*/
function canIgnoreError(errorNum: number) {
/* eslint-disable-next-line no-console */
console.warn('BrightID error', errorNum)
return IGNORE_BRIGHTID_ERRORS.includes(errorNum)
}

export interface BrightId {
isVerified: boolean // If is verified in BrightID
verification?: Verification
Expand Down Expand Up @@ -222,7 +243,7 @@ export async function brightIdSponsor(
const json = await res.json()

if (json['error']) {
if (json.errorNum === 68) {
if (canIgnoreError(json.errorNum)) {
// sponsorship already sent recently, ignore this error
return { hash: '0x0' }
}
Expand All @@ -246,7 +267,17 @@ async function netlifySponsor(userAddress: string): Promise<SponsorData> {
body: JSON.stringify({ userAddress }),
})

return res.json()
const json = await res.json()
if (res.status === 200) {
return json
}

if (res.status === 400 && canIgnoreError(json.errorNum)) {
return { hash: '0x0' }
}

// return the error
return json
}

/**
Expand All @@ -260,7 +291,7 @@ export async function sponsorUser(userAddress: string): Promise<SponsorData> {
}

try {
return netlifySponsor(userAddress)
return await netlifySponsor(userAddress)
} catch (err) {
if (err instanceof Error) {
return { error: (err as Error).message }
Expand Down
30 changes: 13 additions & 17 deletions vue-app/src/lambda/sponsor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ const NODE_URL =
* @param errorMessage error message
* @returns error object
*/
function makeError(errorMessage) {
const body =
typeof errorMessage === 'string'
? errorMessage
: JSON.stringify(errorMessage)

function makeError(errorMessage, errorNum) {
const body = JSON.stringify({ error: errorMessage, errorNum })
return { statusCode: 400, body }
}

Expand All @@ -24,7 +20,7 @@ function makeError(errorMessage) {
* @returns result object
*/
function makeResult(result) {
const body = typeof result === 'object' ? JSON.stringify(result) : result
const body = JSON.stringify(result)
return { statusCode: 200, body }
}

Expand Down Expand Up @@ -56,7 +52,7 @@ async function handleSponsorRequest(userAddress) {

if (!brightIdSponsorKey) {
throw new Error(
'Environment VUE_APP_BRIGHTID_SPONSOR_KEY_FOR_NETLIFY not set'
'Environment variable VUE_APP_BRIGHTID_SPONSOR_KEY_FOR_NETLIFY not set'
)
}

Expand Down Expand Up @@ -96,14 +92,14 @@ async function handleSponsorRequest(userAddress) {
const json = await res.json()

if (json.error) {
if (json.errorNum === 68) {
// sponsorship already sent recently, ignore this error
return makeResult({ hash: '0x0' })
}
return makeError(json.errorMessage)
} else {
return makeResult(json.data)
return makeError(json.errorMessage, json.errorNum)
}

if (json.data) {
return makeResult({ hash: json.data.hash })
}

return makeError('Unexpected result from the BrightID sponsorship API.')
}

/**
Expand All @@ -122,8 +118,8 @@ exports.handler = async function (event) {
return makeError('Missing userAddress in request body: ' + event.body)
}

return handleSponsorRequest(jsonBody.userAddress)
return await handleSponsorRequest(jsonBody.userAddress)
} catch (err) {
return makeError(err.message + ' ' + event.body)
return makeError(err.message)
}
}
1 change: 1 addition & 0 deletions vue-app/src/views/Verify.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
>
</transaction>
<div class="qr">
<loader v-if="!appLink && !autoSponsorError"></loader>
<div class="instructions" v-if="appLink">
<p class="desktop" v-if="appLinkQrCode">
{{ $t('verify.p4') }}
Expand Down

0 comments on commit 01ec413

Please sign in to comment.