|
| 1 | +import { |
| 2 | + AuthTokenDetails, |
| 3 | + PostDetails, |
| 4 | + PostResponse, |
| 5 | + SocialProvider, |
| 6 | +} from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface'; |
| 7 | +import { makeId } from '@gitroom/nestjs-libraries/services/make.is'; |
| 8 | +import dayjs from 'dayjs'; |
| 9 | +import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract'; |
| 10 | +import { getPublicKey, Relay, finalizeEvent } from 'nostr-tools'; |
| 11 | +import WebSocket from 'ws'; |
| 12 | +import { AuthService } from '@gitroom/helpers/auth/auth.service'; |
| 13 | + |
| 14 | +// @ts-ignore |
| 15 | +global.WebSocket = WebSocket; |
| 16 | + |
| 17 | +const list = [ |
| 18 | + 'wss://relay.primal.net', |
| 19 | + 'wss://relay.damus.io', |
| 20 | + 'wss://relay.snort.social', |
| 21 | + 'wss://nostr.wine', |
| 22 | + 'wss://nos.lol', |
| 23 | + 'wss://relay.primal.net', |
| 24 | +]; |
| 25 | + |
| 26 | +export class NostrProvider extends SocialAbstract implements SocialProvider { |
| 27 | + identifier = 'nostr'; |
| 28 | + name = 'Nostr'; |
| 29 | + isBetweenSteps = false; |
| 30 | + scopes = []; |
| 31 | + |
| 32 | + async customFields() { |
| 33 | + return [ |
| 34 | + { |
| 35 | + key: 'password', |
| 36 | + label: 'Nostr private key', |
| 37 | + validation: `/^.{3,}$/`, |
| 38 | + type: 'password' as const, |
| 39 | + }, |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + async refreshToken(refresh_token: string): Promise<AuthTokenDetails> { |
| 44 | + return { |
| 45 | + refreshToken: '', |
| 46 | + expiresIn: 0, |
| 47 | + accessToken: '', |
| 48 | + id: '', |
| 49 | + name: '', |
| 50 | + picture: '', |
| 51 | + username: '', |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + async generateAuthUrl() { |
| 56 | + const state = makeId(17); |
| 57 | + return { |
| 58 | + url: '', |
| 59 | + codeVerifier: makeId(10), |
| 60 | + state, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + private async findRelayInformation(pubkey: string) { |
| 65 | + for (const relay of list) { |
| 66 | + const relayInstance = await Relay.connect(relay); |
| 67 | + const value = await new Promise<any>((resolve) => { |
| 68 | + console.log('connecting'); |
| 69 | + relayInstance.subscribe([{ kinds: [0], authors: [pubkey] }], { |
| 70 | + eoseTimeout: 6000, |
| 71 | + onevent: (event) => { |
| 72 | + resolve(event); |
| 73 | + }, |
| 74 | + oneose: () => { |
| 75 | + resolve({}); |
| 76 | + }, |
| 77 | + onclose: () => { |
| 78 | + resolve({}); |
| 79 | + }, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + relayInstance.close(); |
| 84 | + const content = JSON.parse(value?.content || '{}'); |
| 85 | + if (content.name || content.displayName || content.display_name) { |
| 86 | + return content; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return {}; |
| 91 | + } |
| 92 | + |
| 93 | + private async publish(pubkey: string, event: any) { |
| 94 | + let id = ''; |
| 95 | + for (const relay of list) { |
| 96 | + try { |
| 97 | + const relayInstance = await Relay.connect(relay); |
| 98 | + const value = new Promise<any>((resolve) => { |
| 99 | + relayInstance.subscribe([{ kinds: [1], authors: [pubkey] }], { |
| 100 | + eoseTimeout: 6000, |
| 101 | + onevent: (event) => { |
| 102 | + resolve(event); |
| 103 | + }, |
| 104 | + oneose: () => { |
| 105 | + resolve({}); |
| 106 | + }, |
| 107 | + onclose: () => { |
| 108 | + resolve({}); |
| 109 | + }, |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + await relayInstance.publish(event); |
| 114 | + const all = await value; |
| 115 | + relayInstance.close(); |
| 116 | + // relayInstance.close(); |
| 117 | + id = id || all?.id; |
| 118 | + } catch (err) { |
| 119 | + /**empty**/ |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return id; |
| 124 | + } |
| 125 | + |
| 126 | + async authenticate(params: { |
| 127 | + code: string; |
| 128 | + codeVerifier: string; |
| 129 | + refresh?: string; |
| 130 | + }) { |
| 131 | + try { |
| 132 | + const body = JSON.parse(Buffer.from(params.code, 'base64').toString()); |
| 133 | + |
| 134 | + const pubkey = getPublicKey( |
| 135 | + Uint8Array.from( |
| 136 | + body.password.match(/.{1,2}/g).map((byte: any) => parseInt(byte, 16)) |
| 137 | + ) |
| 138 | + ); |
| 139 | + |
| 140 | + const user = await this.findRelayInformation(pubkey); |
| 141 | + |
| 142 | + return { |
| 143 | + id: String(user.pubkey), |
| 144 | + name: user.display_name || user.displayName || 'No Name', |
| 145 | + accessToken: AuthService.signJWT({ password: body.password }), |
| 146 | + refreshToken: '', |
| 147 | + expiresIn: dayjs().add(200, 'year').unix() - dayjs().unix(), |
| 148 | + picture: user.picture, |
| 149 | + username: user.name || 'nousername', |
| 150 | + }; |
| 151 | + } catch (e) { |
| 152 | + console.log(e); |
| 153 | + return 'Invalid credentials'; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + async post( |
| 158 | + id: string, |
| 159 | + accessToken: string, |
| 160 | + postDetails: PostDetails[] |
| 161 | + ): Promise<PostResponse[]> { |
| 162 | + const { password } = AuthService.verifyJWT(accessToken) as any; |
| 163 | + |
| 164 | + let lastId = ''; |
| 165 | + const ids: PostResponse[] = []; |
| 166 | + for (const post of postDetails) { |
| 167 | + const textEvent = finalizeEvent( |
| 168 | + { |
| 169 | + kind: 1, // Text note |
| 170 | + content: |
| 171 | + post.message + '\n\n' + post.media?.map((m) => m.url).join('\n\n'), |
| 172 | + tags: [ |
| 173 | + ...(lastId |
| 174 | + ? [ |
| 175 | + ['e', lastId, '', 'reply'], |
| 176 | + ['p', id], |
| 177 | + ] |
| 178 | + : []), |
| 179 | + ], // Include delegation token in the event |
| 180 | + created_at: Math.floor(Date.now() / 1000), |
| 181 | + }, |
| 182 | + password |
| 183 | + ); |
| 184 | + |
| 185 | + lastId = await this.publish(id, textEvent); |
| 186 | + ids.push({ |
| 187 | + id: post.id, |
| 188 | + postId: String(lastId), |
| 189 | + releaseURL: `https://primal.net/e/${lastId}`, |
| 190 | + status: 'completed', |
| 191 | + }); |
| 192 | + } |
| 193 | + |
| 194 | + return ids; |
| 195 | + } |
| 196 | +} |
0 commit comments