Skip to content

Commit

Permalink
Merge pull request #14 from bob2402/fix-gen-nsec
Browse files Browse the repository at this point in the history
problem: no temporary nsec is generated
  • Loading branch information
gsovereignty authored Sep 20, 2024
2 parents 700fd18 + cfc9040 commit 04242d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
8 changes: 5 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions src/components/UseTemporaryAccount.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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() {
Expand Down
11 changes: 7 additions & 4 deletions src/lib/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 04242d1

Please sign in to comment.