From 213ae79bf52da893dccc4576dd690cddbb0e4bd8 Mon Sep 17 00:00:00 2001 From: Davinci Date: Wed, 22 May 2024 12:08:02 +0200 Subject: [PATCH 1/4] fix: login with hex key does not work, missing proper error when nsec or private key is wrong --- src/pages/login/index.tsx | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 9c224b7..9539ee7 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -20,6 +20,7 @@ import { LoginMethods } from '../../store/auth/types' import { Dispatch } from '../../store/store' import { npubToHex, queryNip05 } from '../../utils' import styles from './style.module.scss' +import { hexToBytes } from '@noble/hashes/utils' export const Login = () => { const [searchParams] = useSearchParams() @@ -92,9 +93,24 @@ export const Login = () => { }) } - const loginWithNsec = async () => { - const nsec = inputValue - const privateKey = nip19.decode(nsec).data as Uint8Array + /** + * Login with NSEC or HEX private key + * @param privateKey in HEX format + */ + const loginWithNsec = async (privateKey?: Uint8Array) => { + let nsec = '' + + if (privateKey) { + nsec = nip19.nsecEncode(privateKey) + } else { + nsec = inputValue + + try { + privateKey = nip19.decode(nsec).data as Uint8Array + } catch(err) { + toast.error(`Error decoding the nsec. ${err}`) + } + } if (!privateKey) { toast.error( @@ -307,7 +323,19 @@ export const Login = () => { return loginWithNsecBunker() } - toast.error('Invalid Input!') + // Check if maybe hex nsec + try { + // const privateKey = nip19.decode(inputValue).data as Uint8Array + const privateKey = hexToBytes(inputValue) + const publickey = getPublicKey(privateKey) + + if (publickey) return loginWithNsec(privateKey) + + } catch(err) { + console.warn('err', err) + } + + toast.error('Invalid format, please use: private key (hex), nsec..., bunker:// or nip05 format.') return } @@ -329,7 +357,7 @@ export const Login = () => { Welcome to Sigit setInputValue(e.target.value)} sx={{ width: '100%', mt: 2 }} -- 2.34.1 From 35bd6d3688b44c769999806600e8f211cc03e030 Mon Sep 17 00:00:00 2001 From: Davinci Date: Wed, 22 May 2024 12:08:12 +0200 Subject: [PATCH 2/4] style: lint --- src/pages/login/index.tsx | 11 ++++++----- src/pages/profile/index.tsx | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 9539ee7..7bf8b80 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -107,7 +107,7 @@ export const Login = () => { try { privateKey = nip19.decode(nsec).data as Uint8Array - } catch(err) { + } catch (err) { toast.error(`Error decoding the nsec. ${err}`) } } @@ -328,14 +328,15 @@ export const Login = () => { // const privateKey = nip19.decode(inputValue).data as Uint8Array const privateKey = hexToBytes(inputValue) const publickey = getPublicKey(privateKey) - - if (publickey) return loginWithNsec(privateKey) - } catch(err) { + if (publickey) return loginWithNsec(privateKey) + } catch (err) { console.warn('err', err) } - toast.error('Invalid format, please use: private key (hex), nsec..., bunker:// or nip05 format.') + toast.error( + 'Invalid format, please use: private key (hex), nsec..., bunker:// or nip05 format.' + ) return } diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index caa5387..146dda8 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -208,7 +208,7 @@ export const ProfilePage = () => { - + { > {profileMetadata && ( @@ -240,11 +240,19 @@ export const ProfilePage = () => { {profileMetadata?.nip05 && - textElementWithCopyIcon(profileMetadata.nip05, undefined, 15)} + textElementWithCopyIcon( + profileMetadata.nip05, + undefined, + 15 + )} {profileMetadata?.lud16 && - textElementWithCopyIcon(profileMetadata.lud16, undefined, 15)} + textElementWithCopyIcon( + profileMetadata.lud16, + undefined, + 15 + )} -- 2.34.1 From dada1eebc5039b678419c3fd8ee8cdfdc3448272 Mon Sep 17 00:00:00 2001 From: Davinci Date: Wed, 22 May 2024 12:31:00 +0200 Subject: [PATCH 3/4] chore: label fix --- src/pages/login/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 7bf8b80..3846c57 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -358,7 +358,7 @@ export const Login = () => { Welcome to Sigit setInputValue(e.target.value)} sx={{ width: '100%', mt: 2 }} -- 2.34.1 From e501f99729d0cd07a5fd3d4e4869c814ca709326 Mon Sep 17 00:00:00 2001 From: Davinci Date: Fri, 24 May 2024 08:51:59 +0200 Subject: [PATCH 4/4] chore: removed commented code --- src/pages/login/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 3846c57..8c2f702 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -325,7 +325,6 @@ export const Login = () => { // Check if maybe hex nsec try { - // const privateKey = nip19.decode(inputValue).data as Uint8Array const privateKey = hexToBytes(inputValue) const publickey = getPublicKey(privateKey) -- 2.34.1