From cfc9040f283653a88a15ed69f157078124ee0d98 Mon Sep 17 00:00:00 2001 From: Bob Date: Fri, 20 Sep 2024 15:17:23 +0800 Subject: [PATCH] problem: no temporary nsec is generated --- package-lock.json | 8 +++++--- package.json | 1 + src/components/UseTemporaryAccount.svelte | 11 ++++++++--- src/lib/login.ts | 11 +++++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index cd64e7a..45c96b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@nostr-dev-kit/ndk-cache-dexie": "^2.5.1", "@nostr-dev-kit/ndk-svelte": "^2.2.18", "@nostr-dev-kit/ndk-svelte-components": "^2.2.19", + "@scure/base": "^1.1.9", "@sveltejs/adapter-static": "^3.0.4", "bitcoin-address-validation": "^2.2.3", "bits-ui": "^0.21.13", @@ -967,9 +968,10 @@ ] }, "node_modules/@scure/base": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.8.tgz", - "integrity": "sha512-6CyAclxj3Nb0XT7GHK6K4zK6k2xJm6E4Ft0Ohjt4WgegiFUHEtFb2CGzmPmGBwoIhrLsqNLYfLr04Y1GePrzZg==", + "version": "1.1.9", + "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.9.tgz", + "integrity": "sha512-8YKhl8GHiNI/pU2VMaofa2Tor7PJRAjwQLBBuilkJ9L5+13yVbC7JO/wS7piioAvPSwR3JKM1IJ/u4xQzbcXKg==", + "license": "MIT", "funding": { "url": "https://paulmillr.com/funding/" } diff --git a/package.json b/package.json index 6fead52..f9f18b4 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@nostr-dev-kit/ndk-cache-dexie": "^2.5.1", "@nostr-dev-kit/ndk-svelte": "^2.2.18", "@nostr-dev-kit/ndk-svelte-components": "^2.2.19", + "@scure/base": "^1.1.9", "@sveltejs/adapter-static": "^3.0.4", "bitcoin-address-validation": "^2.2.3", "bits-ui": "^0.21.13", diff --git a/src/components/UseTemporaryAccount.svelte b/src/components/UseTemporaryAccount.svelte index d8207a0..239f471 100644 --- a/src/components/UseTemporaryAccount.svelte +++ b/src/components/UseTemporaryAccount.svelte @@ -3,7 +3,7 @@ import { Button } from '@/components/ui/button'; import * as Dialog from '$lib/components/ui/dialog'; import { NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'; - import { nip19 } from 'nostr-tools'; + import { bech32 } from '@scure/base'; import CopyButton from './CopyButton.svelte'; import { Input } from '$lib/components/ui/input'; import { pubkey } from '@/stores/session'; @@ -13,8 +13,13 @@ async function createTemporaryAccount() { temporaryAccount = NDKPrivateKeySigner.generate(); - nsec = nip19.nsecEncode(temporaryAccount.privateKey!); - console.log(nsec); + const privateKeyHex = temporaryAccount.privateKey!; + const privateKeyBytes = new Uint8Array( + privateKeyHex.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16)) + ); + + const words = bech32.toWords(privateKeyBytes); + nsec = bech32.encode('nsec', words, 1000); } async function useTemporaryAccount() { diff --git a/src/lib/login.ts b/src/lib/login.ts index a42e62b..86ca8fd 100644 --- a/src/lib/login.ts +++ b/src/lib/login.ts @@ -12,13 +12,16 @@ const STORAGE_KEYS = { export function loginWithNsec(nsec: string): NDKPrivateKeySigner | Error { try { const { type, data } = nip19.decode(nsec); - if (type !== 'nsec' || typeof data !== 'string') { + console.log(typeof type, typeof data, data); + + if (type !== 'nsec' || !(typeof data === 'string' || data instanceof Uint8Array)) { throw new Error('Invalid nsec format'); } + return new NDKPrivateKeySigner(data); - } catch (e) { - console.error('Error logging in with nsec:', e); - return e instanceof Error ? e : new Error(String(e)); + } catch (error) { + console.error('Error logging in with nsec:', error); + return error instanceof Error ? error : new Error(String(error)); } }