nostr-dev-kit / ndk

Nostr Development Kit with outbox-model support
https://nostr-dev-kit.github.io/ndk/
MIT License
366 stars 99 forks source link

How do I login with a private key? #223

Closed ralyodio closed 6 months ago

ralyodio commented 6 months ago

I have my nsec1... key from getalby.com wallet...but I can't signin with the following code:

I'm getting the error Error: Invalid private key. Must be 32 bytes in hexadecimal format. Getalby.com nostr private keys are only 63 bytes.

<script>
  import { onMount } from "svelte";
  import NDK, { NDKPrivateKeySigner, NDKUser } from "@nostr-dev-kit/ndk";

  // State variables
  let privateKey = "";
  let userProfile = null;
  let error = "";
  let ndk = null;

  // Initialize NDK
  function initializeNDK() {
    ndk = new NDK({
      explicitRelayUrls: [
        "wss://relay.damus.io",
        "wss://relay.nostr.band",
        "wss://relay.primal.net",
        "wss://pablof7z.nostr1.com",
        "wss://offchain.pub",
        "wss://relay.f7z.io",
        "wss://relay.damus.io",
        "wss://relay.snort.social",
        "wss://offchain.pub/",
        "wss://nostr.mom",
        "wss://nostr-pub.wellorder.net",
        "wss://purplepag.es",
        "wss://brb.io/",
      ],
    });
  }

  // Log in and fetch the user's profile using the private key
  async function login() {
    try {
      error = "";
      if (!ndk) initializeNDK();

      // Ensure the private key is a valid hex string and pass it directly
      if (privateKey.length !== 64) {
        throw new Error(
          "Invalid private key. Must be 32 bytes in hexadecimal format."
        );
      }

      // Create a signer using NDKPrivateKeySigner with a hex string
      const signer = new NDKPrivateKeySigner(privateKey);

      // Create an NDKUser instance using the signer
      const user = new NDKUser({ signer });
      user.ndk = ndk; // Associate the user with NDK

      // Connect NDK to relays
      await ndk.connect();

      // Fetch the user's profile
      await user.fetchProfile();

      // Display the profile
      userProfile = {
        pubkey: user.pubkey,
        name: user.profile?.name || "No name provided",
        bio: user.profile?.about || "No bio available",
      };
    } catch (e) {
      error = `Error: ${e.message}`;
      userProfile = null;
    }
  }

  onMount(() => {
    initializeNDK(); // Initialize NDK when the component mounts
  });
</script>

<!-- Login form and profile display -->
<div class="login-form">
  <input
    type="password"
    bind:value={privateKey}
    placeholder="Enter Private Key"
  />
  <button on:click={login}>Log In</button>

  {#if error}
    <p class="error">{error}</p>
  {/if}

  {#if userProfile}
    <div class="profile">
      <h3>User Profile</h3>
      <p>Public Key: {userProfile.pubkey}</p>
      <p>Name: {userProfile.name}</p>
      <p>Bio: {userProfile.bio}</p>
      <!-- Add more profile fields as needed -->
    </div>
  {/if}
</div>

<style>
  /* Simple styles for layout */
  .login-form {
    display: flex;
    flex-direction: column;
    max-width: 300px;
    margin: auto;
  }
  input,
  button {
    margin: 5px 0;
    padding: 8px;
  }
  .error {
    color: red;
  }
  .profile {
    margin-top: 15px;
  }
</style>
erskingardner commented 6 months ago

If you're trying to use the nsec1... value (the bech32 encoded value) it won't work. you need to use the hex value instead. you can use a site like nak to convert it.