Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I login with a private key? #223

Closed
ralyodio opened this issue May 10, 2024 · 1 comment
Closed

How do I login with a private key? #223

ralyodio opened this issue May 10, 2024 · 1 comment

Comments

@ralyodio
Copy link
Contributor

ralyodio commented May 10, 2024

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants