Bitcoin

Lightning Network – Provides npub for user nostr. How can I follow my followers?

I’m trying to create a function on the JavaScript client side that allows a user to enter someone else’s npub and then follow their followers. However, using nostr-tools seems to require the user’s private key to sign follow events.

If not, what should I do? Obviously I won’t have anyone’s private keys.

// Your Nostr private key
//const privateKey = 'your_private_key'; // Replace with your actual private key

// Function to get followers for a given npub
async function getAndFollowUsersFollowers(targetNpub) {
        //const  relayInit, finishEvent, generatePrivateKey, getPublicKey  = window.nostr;
    const relayUrl="wss://relay.primal.net"; // Replace with your chosen relay URL
    const client = new nostr.Client();

    // Connect to the relay
    await client.connect(relayUrl);

    // Subscription to get 'meta' events
    const subscription = 
        filter: 
            kinds: (4), // Kind 4 for 'meta' events
            authors: (targetNpub) // The npub of the target user
        ,
        cb: (event) => 
            // Handle the event - extract followers
            if (event.content && event.content.follow) 
                event.content.follow.forEach(async (followerNpub) => 
                    // Follow each follower
                    await followUser(followerNpub, client);
                );
            
        
    ;

    client.subscribe(subscription);
}

// Function to follow a user
async function followUser(followerNpub, client) 
        const  pubkey  = (await window.webln.getInfo()).node;
    const event = 
        pubkey,
        kind: 4, // Kind 4 for 'meta' events
        content: 
            follow: (followerNpub)
        ,
        tags: (),
        created_at: Math.floor(new Date().getTime() / 1000),
    ;

    // Sign the event with your private key
    event.id = nostr.getEventId(event, privateKey);
    event.sig = nostr.getEventSignature(event, privateKey);

    // Publish the event
    await client.publish(event);


Related Articles

Back to top button