From 99db0421151e091477ac0d44618689b4ae38a2ca Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 15:16:48 -0400 Subject: [PATCH 01/14] wrap brightid error for netlify function --- vue-app/src/api/bright-id.ts | 6 ++++-- vue-app/src/lambda/sponsor.js | 16 ++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index a11161dec..121df466e 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -246,7 +246,9 @@ async function netlifySponsor(userAddress: string): Promise { body: JSON.stringify({ userAddress }), }) - return res.json() + const json = await res.json() + + return json.statusCode === 200 ? json.body : { error: json.body } } /** @@ -260,7 +262,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..90e1874ba 100644 --- a/vue-app/src/lambda/sponsor.js +++ b/vue-app/src/lambda/sponsor.js @@ -10,11 +10,7 @@ const NODE_URL = * @returns error object */ function makeError(errorMessage) { - const body = - typeof errorMessage === 'string' - ? errorMessage - : JSON.stringify(errorMessage) - + const body = JSON.stringify({ error: errorMessage }) 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 } } @@ -101,9 +97,13 @@ async function handleSponsorRequest(userAddress) { return makeResult({ hash: '0x0' }) } return makeError(json.errorMessage) - } else { - return makeResult(json.data) } + + if (json.data) { + return makeResult({ hash: json.data.hash }) + } + + return makeError('Unexpected result from the BrightID sponsorship API.') } /** From a2f4f998c7a99900df55d61c04adbb46b743cbef Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 16:38:51 -0400 Subject: [PATCH 02/14] debug sponsorship api result --- vue-app/src/api/bright-id.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index 121df466e..1035417a5 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -248,7 +248,12 @@ async function netlifySponsor(userAddress: string): Promise { const json = await res.json() - return json.statusCode === 200 ? json.body : { error: json.body } + /* eslint-disable-next-line no-console */ + console.log('netlify sponsor response debug', json) + + return json.statusCode === 200 + ? json.body + : { error: json.body || 'empty error' } } /** From d984519b89ab428b265f5321d2493687a0bdde36 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 16:48:21 -0400 Subject: [PATCH 03/14] print fetch result --- vue-app/src/api/bright-id.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index 1035417a5..ac2b7a08a 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -249,7 +249,7 @@ async function netlifySponsor(userAddress: string): Promise { const json = await res.json() /* eslint-disable-next-line no-console */ - console.log('netlify sponsor response debug', json) + console.log('netlify sponsor response debug', res, json) return json.statusCode === 200 ? json.body From e5779dacd5dc1aa93140c6726662781b5f902968 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 16:55:31 -0400 Subject: [PATCH 04/14] return the body from netlify sponsor call --- vue-app/src/api/bright-id.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index ac2b7a08a..756139909 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -246,14 +246,9 @@ async function netlifySponsor(userAddress: string): Promise { body: JSON.stringify({ userAddress }), }) - const json = await res.json() - /* eslint-disable-next-line no-console */ - console.log('netlify sponsor response debug', res, json) - - return json.statusCode === 200 - ? json.body - : { error: json.body || 'empty error' } + console.log('netlify sponsor response debug', res) + return res.json() } /** From 07c12172ba69a242a4e3287bccadb96ca9bb78db Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 17:28:20 -0400 Subject: [PATCH 05/14] ignore already sponsored error 69 from brightId --- vue-app/src/api/bright-id.ts | 26 ++++++++++++++++++++++++-- vue-app/src/lambda/sponsor.js | 10 +++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index 756139909..27bcf719e 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -10,6 +10,17 @@ 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 + * 68 - sponsorship already sent + * 69 - The app generated id was sponsored before + */ +const IGNORE_BRIGHTID_ERRORS = [68, 69] + +function canIgnoreError(errorNum: number) { + return IGNORE_BRIGHTID_ERRORS.includes(errorNum) +} + export interface BrightId { isVerified: boolean // If is verified in BrightID verification?: Verification @@ -222,7 +233,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' } } @@ -248,7 +259,18 @@ async function netlifySponsor(userAddress: string): Promise { /* eslint-disable-next-line no-console */ console.log('netlify sponsor response debug', res) - 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 in the json body + return json } /** diff --git a/vue-app/src/lambda/sponsor.js b/vue-app/src/lambda/sponsor.js index 90e1874ba..534590063 100644 --- a/vue-app/src/lambda/sponsor.js +++ b/vue-app/src/lambda/sponsor.js @@ -9,8 +9,8 @@ const NODE_URL = * @param errorMessage error message * @returns error object */ -function makeError(errorMessage) { - const body = JSON.stringify({ error: errorMessage }) +function makeError(errorMessage, errorNum) { + const body = JSON.stringify({ error: errorMessage, errorNum }) return { statusCode: 400, body } } @@ -92,11 +92,7 @@ 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) + return makeError(json.errorMessage, json.errorNum) } if (json.data) { From 2268a70590a08ce5d08a12971b359a3cd8a9e69d Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 17:33:35 -0400 Subject: [PATCH 06/14] debug print the error number --- vue-app/src/api/bright-id.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index 27bcf719e..dd2fdfcbb 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -258,8 +258,8 @@ async function netlifySponsor(userAddress: string): Promise { }) /* eslint-disable-next-line no-console */ - console.log('netlify sponsor response debug', res) const json = await res.json() + console.log('netlify sponsor response debug', res, json) if (res.status === 200) { return json From 7bad984019ff8784a6b33150f4f38830ce6e510b Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 17:38:05 -0400 Subject: [PATCH 07/14] disable eslint no console complaint --- vue-app/src/api/bright-id.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index dd2fdfcbb..bbb336c33 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -257,8 +257,8 @@ async function netlifySponsor(userAddress: string): Promise { body: JSON.stringify({ userAddress }), }) - /* eslint-disable-next-line no-console */ const json = await res.json() + /* eslint-disable-next-line no-console */ console.log('netlify sponsor response debug', res, json) if (res.status === 200) { From 57141c51c371c32be6a60409c377ec171e3ace1c Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 18:02:43 -0400 Subject: [PATCH 08/14] update error number for the already sponsored error --- vue-app/src/api/bright-id.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index bbb336c33..98362accf 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -12,10 +12,10 @@ const CONTEXT = process.env.VUE_APP_BRIGHTID_CONTEXT || 'clr.fund' /** * These errors from the BrightID sponsor api can be ignored + * 39 - The app generated id was sponsored before * 68 - sponsorship already sent - * 69 - The app generated id was sponsored before */ -const IGNORE_BRIGHTID_ERRORS = [68, 69] +const IGNORE_BRIGHTID_ERRORS = [39, 68] function canIgnoreError(errorNum: number) { return IGNORE_BRIGHTID_ERRORS.includes(errorNum) From 3a8aa258e9997f900a6ad8105c10ae9566b9f167 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 20 Mar 2023 18:16:10 -0400 Subject: [PATCH 09/14] clean up the debug logging --- vue-app/src/api/bright-id.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index 98362accf..d7e991654 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -258,9 +258,6 @@ async function netlifySponsor(userAddress: string): Promise { }) const json = await res.json() - /* eslint-disable-next-line no-console */ - console.log('netlify sponsor response debug', res, json) - if (res.status === 200) { return json } @@ -269,7 +266,7 @@ async function netlifySponsor(userAddress: string): Promise { return { hash: '0x0' } } - // return the error in the json body + // return the error return json } From cbdfd9b569a3303627b07704c0ae6bc809b2827c Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 21 Mar 2023 10:38:45 -0400 Subject: [PATCH 10/14] show loader if QR code is not available yet --- vue-app/src/views/Verify.vue | 1 + 1 file changed, 1 insertion(+) diff --git a/vue-app/src/views/Verify.vue b/vue-app/src/views/Verify.vue index 1fa5478a5..5d63af0ff 100644 --- a/vue-app/src/views/Verify.vue +++ b/vue-app/src/views/Verify.vue @@ -129,6 +129,7 @@ >
+

{{ $t('verify.p4') }} From 4dfe2211ae99613c15f8ffec27b59bd986397ee4 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 21 Mar 2023 10:52:20 -0400 Subject: [PATCH 11/14] hide loader on error --- vue-app/src/views/Verify.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue-app/src/views/Verify.vue b/vue-app/src/views/Verify.vue index 5d63af0ff..6167d7ae9 100644 --- a/vue-app/src/views/Verify.vue +++ b/vue-app/src/views/Verify.vue @@ -129,7 +129,7 @@ >

- +

{{ $t('verify.p4') }} From b0bc2fc02bd535801e74f0f292f299d958827654 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 21 Mar 2023 10:53:19 -0400 Subject: [PATCH 12/14] ignore brightid error 63 --- vue-app/src/api/bright-id.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/vue-app/src/api/bright-id.ts b/vue-app/src/api/bright-id.ts index d7e991654..b917c69b7 100644 --- a/vue-app/src/api/bright-id.ts +++ b/vue-app/src/api/bright-id.ts @@ -12,12 +12,22 @@ 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 - * 68 - sponsorship already sent + * 63 - Spend request for this app-generated id submitted before. + * 68 - The app has sent this sponsor request recently */ -const IGNORE_BRIGHTID_ERRORS = [39, 68] +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) } From fd41bd3187e6791cae91502a6f474bd8eb3413d7 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 21 Mar 2023 11:15:34 -0400 Subject: [PATCH 13/14] await to catch error --- vue-app/src/lambda/sponsor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue-app/src/lambda/sponsor.js b/vue-app/src/lambda/sponsor.js index 534590063..bfc88f38f 100644 --- a/vue-app/src/lambda/sponsor.js +++ b/vue-app/src/lambda/sponsor.js @@ -118,7 +118,7 @@ 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) } From a1a1a1da03579bf034407289167ba8cbb972ae60 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Tue, 21 Mar 2023 11:59:05 -0400 Subject: [PATCH 14/14] fix up error message --- vue-app/src/lambda/sponsor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vue-app/src/lambda/sponsor.js b/vue-app/src/lambda/sponsor.js index bfc88f38f..2d5e4739b 100644 --- a/vue-app/src/lambda/sponsor.js +++ b/vue-app/src/lambda/sponsor.js @@ -52,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' ) } @@ -120,6 +120,6 @@ exports.handler = async function (event) { return await handleSponsorRequest(jsonBody.userAddress) } catch (err) { - return makeError(err.message + ' ' + event.body) + return makeError(err.message) } }