diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index a11161dec..b917c69b7 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -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 @@ -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' } } @@ -246,7 +267,17 @@ async function netlifySponsor(userAddress: string): Promise { 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 } /** @@ -260,7 +291,7 @@ export async function sponsorUser(userAddress: string): Promise { } try { - return netlifySponsor(userAddress) + return await netlifySponsor(userAddress) } catch (err) { if (err instanceof Error) { return { error: (err as Error).message } diff --git a/vue-app/src/lambda/sponsor.js b/vue-app/src/lambda/sponsor.js index 4da4e4469..2d5e4739b 100644 --- a/vue-app/src/lambda/sponsor.js +++ b/vue-app/src/lambda/sponsor.js @@ -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 } } @@ -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 } } @@ -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' ) } @@ -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.') } /** @@ -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) } } diff --git a/vue-app/src/views/Verify.vue b/vue-app/src/views/Verify.vue index 1fa5478a5..6167d7ae9 100644 --- a/vue-app/src/views/Verify.vue +++ b/vue-app/src/views/Verify.vue @@ -129,6 +129,7 @@ >
+

{{ $t('verify.p4') }}