Skip to content

Commit

Permalink
Merge pull request #578 from gitroomhq/feat/nostr
Browse files Browse the repository at this point in the history
Nostr provider
  • Loading branch information
nevo-david authored Jan 25, 2025
2 parents 8b9f060 + b048dbd commit 7951f46
Show file tree
Hide file tree
Showing 9 changed files with 944 additions and 565 deletions.
Binary file added apps/frontend/public/icons/platforms/nostr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { withProvider } from '@gitroom/frontend/components/launches/providers/high.order.provider';

export default withProvider(
null,
undefined,
undefined,
async () => {
return true;
},
undefined,
);
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import BlueskyProvider from '@gitroom/frontend/components/launches/providers/blu
import LemmyProvider from '@gitroom/frontend/components/launches/providers/lemmy/lemmy.provider';
import WarpcastProvider from '@gitroom/frontend/components/launches/providers/warpcast/warpcast.provider';
import TelegramProvider from '@gitroom/frontend/components/launches/providers/telegram/telegram.provider';
import NostrProvider from '@gitroom/frontend/components/launches/providers/nostr/nostr.provider';

export const Providers = [
{identifier: 'devto', component: DevtoProvider},
Expand All @@ -44,6 +45,7 @@ export const Providers = [
{identifier: 'lemmy', component: LemmyProvider},
{identifier: 'wrapcast', component: WarpcastProvider},
{identifier: 'telegram', component: TelegramProvider},
{identifier: 'nostr', component: NostrProvider},
];


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use client';
import '@neynar/react/dist/style.css';
import React, { FC, useMemo, useState, useCallback, useEffect } from 'react';
import { Web3ProviderInterface } from '@gitroom/frontend/components/launches/web3/web3.provider.interface';
import { useVariables } from '@gitroom/react/helpers/variable.context';
import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component';
import { useModals } from '@mantine/modals';
import { LoadingComponent } from '@gitroom/frontend/components/layout/loading';
import {
NeynarAuthButton,
NeynarContextProvider,
Theme,
useNeynarContext,
} from '@neynar/react';
import { INeynarAuthenticatedUser } from '@neynar/react/dist/types/common';
import { ButtonCaster } from '@gitroom/frontend/components/auth/providers/farcaster.provider';

export const WrapcasterProvider: FC<Web3ProviderInterface> = (props) => {
const [_, state] = props.nonce.split('||');
const modal = useModals();
const [hide, setHide] = useState(false);

const auth = useCallback((code: string) => {
setHide(true);
return props.onComplete(code, state);
}, [state]);

return (
<div className="rounded-[4px] border border-customColor6 bg-sixth px-[16px] pb-[16px] relative w-full">
<TopTitle title={`Add Wrapcast`} />
<button
className="outline-none absolute right-[20px] top-[20px] mantine-UnstyledButton-root mantine-ActionIcon-root hover:bg-tableBorder cursor-pointer mantine-Modal-close mantine-1dcetaa"
type="button"
onClick={() => modal.closeAll()}
>
<svg
viewBox="0 0 15 15"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
>
<path
d="M11.7816 4.03157C12.0062 3.80702 12.0062 3.44295 11.7816 3.2184C11.5571 2.99385 11.193 2.99385 10.9685 3.2184L7.50005 6.68682L4.03164 3.2184C3.80708 2.99385 3.44301 2.99385 3.21846 3.2184C2.99391 3.44295 2.99391 3.80702 3.21846 4.03157L6.68688 7.49999L3.21846 10.9684C2.99391 11.193 2.99391 11.557 3.21846 11.7816C3.44301 12.0061 3.80708 12.0061 4.03164 11.7816L7.50005 8.31316L10.9685 11.7816C11.193 12.0061 11.5571 12.0061 11.7816 11.7816C12.0062 11.557 12.0062 11.193 11.7816 10.9684L8.31322 7.49999L11.7816 4.03157Z"
fill="currentColor"
fillRule="evenodd"
clipRule="evenodd"
></path>
</svg>
</button>
<div className="justify-center items-center flex">
{hide ? (
<div className="justify-center items-center flex -mt-[90px]">
<LoadingComponent width={100} height={100} />
</div>
) : (
<div className="justify-center items-center py-[20px] flex-col w-[500px]">
<ButtonCaster login={auth} />
</div>
)}
</div>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { LemmyProvider } from '@gitroom/nestjs-libraries/integrations/social/lem
import { InstagramStandaloneProvider } from '@gitroom/nestjs-libraries/integrations/social/instagram.standalone.provider';
import { FarcasterProvider } from '@gitroom/nestjs-libraries/integrations/social/farcaster.provider';
import { TelegramProvider } from '@gitroom/nestjs-libraries/integrations/social/telegram.provider';
import { NostrProvider } from '@gitroom/nestjs-libraries/integrations/social/nostr.provider';

const socialIntegrationList: SocialProvider[] = [
new XProvider(),
Expand All @@ -46,6 +47,7 @@ const socialIntegrationList: SocialProvider[] = [
new LemmyProvider(),
new FarcasterProvider(),
new TelegramProvider(),
new NostrProvider(),
// new MastodonCustomProvider(),
];

Expand Down
196 changes: 196 additions & 0 deletions libraries/nestjs-libraries/src/integrations/social/nostr.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import {
AuthTokenDetails,
PostDetails,
PostResponse,
SocialProvider,
} from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import dayjs from 'dayjs';
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
import { getPublicKey, Relay, finalizeEvent } from 'nostr-tools';
import WebSocket from 'ws';
import { AuthService } from '@gitroom/helpers/auth/auth.service';

// @ts-ignore
global.WebSocket = WebSocket;

const list = [
'wss://relay.primal.net',
'wss://relay.damus.io',
'wss://relay.snort.social',
'wss://nostr.wine',
'wss://nos.lol',
'wss://relay.primal.net',
];

export class NostrProvider extends SocialAbstract implements SocialProvider {
identifier = 'nostr';
name = 'Nostr';
isBetweenSteps = false;
scopes = [];

async customFields() {
return [
{
key: 'password',
label: 'Nostr private key',
validation: `/^.{3,}$/`,
type: 'password' as const,
},
];
}

async refreshToken(refresh_token: string): Promise<AuthTokenDetails> {
return {
refreshToken: '',
expiresIn: 0,
accessToken: '',
id: '',
name: '',
picture: '',
username: '',
};
}

async generateAuthUrl() {
const state = makeId(17);
return {
url: '',
codeVerifier: makeId(10),
state,
};
}

private async findRelayInformation(pubkey: string) {
for (const relay of list) {
const relayInstance = await Relay.connect(relay);
const value = await new Promise<any>((resolve) => {
console.log('connecting');
relayInstance.subscribe([{ kinds: [0], authors: [pubkey] }], {
eoseTimeout: 6000,
onevent: (event) => {
resolve(event);
},
oneose: () => {
resolve({});
},
onclose: () => {
resolve({});
},
});
});

relayInstance.close();
const content = JSON.parse(value?.content || '{}');
if (content.name || content.displayName || content.display_name) {
return content;
}
}

return {};
}

private async publish(pubkey: string, event: any) {
let id = '';
for (const relay of list) {
try {
const relayInstance = await Relay.connect(relay);
const value = new Promise<any>((resolve) => {
relayInstance.subscribe([{ kinds: [1], authors: [pubkey] }], {
eoseTimeout: 6000,
onevent: (event) => {
resolve(event);
},
oneose: () => {
resolve({});
},
onclose: () => {
resolve({});
},
});
});

await relayInstance.publish(event);
const all = await value;
relayInstance.close();
// relayInstance.close();
id = id || all?.id;
} catch (err) {
/**empty**/
}
}

return id;
}

async authenticate(params: {
code: string;
codeVerifier: string;
refresh?: string;
}) {
try {
const body = JSON.parse(Buffer.from(params.code, 'base64').toString());

const pubkey = getPublicKey(
Uint8Array.from(
body.password.match(/.{1,2}/g).map((byte: any) => parseInt(byte, 16))
)
);

const user = await this.findRelayInformation(pubkey);

return {
id: String(user.pubkey),
name: user.display_name || user.displayName || 'No Name',
accessToken: AuthService.signJWT({ password: body.password }),
refreshToken: '',
expiresIn: dayjs().add(200, 'year').unix() - dayjs().unix(),
picture: user.picture,
username: user.name || 'nousername',
};
} catch (e) {
console.log(e);
return 'Invalid credentials';
}
}

async post(
id: string,
accessToken: string,
postDetails: PostDetails[]
): Promise<PostResponse[]> {
const { password } = AuthService.verifyJWT(accessToken) as any;

let lastId = '';
const ids: PostResponse[] = [];
for (const post of postDetails) {
const textEvent = finalizeEvent(
{
kind: 1, // Text note
content:
post.message + '\n\n' + post.media?.map((m) => m.url).join('\n\n'),
tags: [
...(lastId
? [
['e', lastId, '', 'reply'],
['p', id],
]
: []),
], // Include delegation token in the event
created_at: Math.floor(Date.now() / 1000),
},
password
);

lastId = await this.publish(id, textEvent);
ids.push({
id: post.id,
postId: String(lastId),
releaseURL: `https://primal.net/e/${lastId}`,
status: 'completed',
});
}

return ids;
}
}
Loading

0 comments on commit 7951f46

Please sign in to comment.