@konemono/nostr-login 1.7.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.prettierrc.json +13 -0
  2. package/README.md +167 -0
  3. package/dist/const/index.d.ts +1 -0
  4. package/dist/iife-module.d.ts +1 -0
  5. package/dist/index.d.ts +27 -0
  6. package/dist/index.esm.js +18 -0
  7. package/dist/index.esm.js.map +1 -0
  8. package/dist/modules/AuthNostrService.d.ts +84 -0
  9. package/dist/modules/BannerManager.d.ts +20 -0
  10. package/dist/modules/ModalManager.d.ts +25 -0
  11. package/dist/modules/Nip46.d.ts +56 -0
  12. package/dist/modules/Nostr.d.ts +34 -0
  13. package/dist/modules/NostrExtensionService.d.ts +17 -0
  14. package/dist/modules/NostrParams.d.ts +8 -0
  15. package/dist/modules/Popup.d.ts +7 -0
  16. package/dist/modules/ProcessManager.d.ts +10 -0
  17. package/dist/modules/Signer.d.ts +9 -0
  18. package/dist/modules/index.d.ts +8 -0
  19. package/dist/types.d.ts +72 -0
  20. package/dist/unpkg.js +17 -0
  21. package/dist/utils/index.d.ts +27 -0
  22. package/dist/utils/nip44.d.ts +9 -0
  23. package/index.html +30 -0
  24. package/package.json +28 -0
  25. package/rollup.config.js +55 -0
  26. package/src/const/index.ts +1 -0
  27. package/src/iife-module.ts +81 -0
  28. package/src/index.ts +347 -0
  29. package/src/modules/AuthNostrService.ts +756 -0
  30. package/src/modules/BannerManager.ts +146 -0
  31. package/src/modules/ModalManager.ts +635 -0
  32. package/src/modules/Nip46.ts +441 -0
  33. package/src/modules/Nostr.ts +107 -0
  34. package/src/modules/NostrExtensionService.ts +99 -0
  35. package/src/modules/NostrParams.ts +18 -0
  36. package/src/modules/Popup.ts +27 -0
  37. package/src/modules/ProcessManager.ts +67 -0
  38. package/src/modules/Signer.ts +25 -0
  39. package/src/modules/index.ts +8 -0
  40. package/src/types.ts +124 -0
  41. package/src/utils/index.ts +326 -0
  42. package/src/utils/nip44.ts +185 -0
  43. package/tsconfig.json +15 -0
@@ -0,0 +1,13 @@
1
+ {
2
+ "arrowParens": "avoid",
3
+ "bracketSpacing": true,
4
+ "jsxBracketSameLine": false,
5
+ "jsxSingleQuote": false,
6
+ "quoteProps": "consistent",
7
+ "printWidth": 180,
8
+ "semi": true,
9
+ "singleQuote": true,
10
+ "tabWidth": 2,
11
+ "trailingComma": "all",
12
+ "useTabs": false
13
+ }
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ Nostr-Login
2
+ ===========
3
+
4
+ This library is a powerful `window.nostr` provider.
5
+
6
+ ```
7
+ <script src='https://www.unpkg.com/nostr-login@latest/dist/unpkg.js'></script>
8
+ ```
9
+
10
+ Just add the above script to your HTML and
11
+ get a nice UI for users to login with Nostr Connect (nip46), with an extension, read-only login,
12
+ account switching, OAuth-like sign up, etc. Your app just talks to the `window.nostr`, the rest is handled by `nostr-login`.
13
+
14
+ See it in action on [nostr.band](https://nostr.band).
15
+
16
+ ## Options
17
+
18
+ You can set these attributes to the `script` tag to customize the behavior:
19
+ - `data-dark-mode` - `true`/`false`, default will use the browser's color theme
20
+ - `data-bunkers` - the comma-separated list of domain names of Nostr Connect (nip46) providers for sign up, i.e. `nsec.app,highlighter.com`
21
+ - `data-perms` - the comma-separated list of [permissions](https://github.com/nostr-protocol/nips/blob/master/46.md#requested-permissions) requested by the app over Nostr Connect, i.e. `sign_event:1,nip04_encrypt`
22
+ - `data-theme` - color themes, one of `default`, `ocean`, `lemonade`, `purple`
23
+ - `data-no-banner` - if `true`, do not show the `nostr-login` banner, will need to launch the modals using event dispatch, see below
24
+ - `data-methods` - comma-separated list of allowed auth methods, method names: `connect`, `extension`, `readOnly`, `local`, all allowed by default.
25
+ - `data-otp-request-url` - URL for requesting OTP code
26
+ - `data-otp-reply-url` - URL for replying with OTP code
27
+ - `data-title` - title for the welcome screen
28
+ - `data-description` - description for the welcome screen
29
+ - `data-start-screen` - screen shown by default (banner click, window.nostr.* call), options: `welcome`, `welcome-login`, `welcome-signup`, `signup`, `local-signup`, `login`, `otp`, `connect`, `login-bunker-url`, `login-read-only`, `connection-string`, `switch-account`, `import`
30
+ - `data-signup-relays` - comma-separated list of relays where nip65 event will be published on local signup
31
+ - `data-outbox-relays` - comma-separated list of relays that will be added to nip65 event on local signup
32
+ - `data-signup-nstart` - "true" to use start.njump.me instead of local signup
33
+ - `data-follow-npubs` - comma-separated list of npubs to follow if njump.me signup is used
34
+
35
+ Example:
36
+ ```
37
+ <script src='https://www.unpkg.com/nostr-login@latest/dist/unpkg.js' data-perms="sign_event:1,sign_event:0" data-theme="ocean"></script>
38
+ ```
39
+
40
+ ## Updating the UI
41
+
42
+ Whenever user performs an auth-related action using `nostr-login`, a `nlAuth` event will be dispatched on the `document`, which you can listen
43
+ to in order to update your UI (show user profile, etc):
44
+
45
+ ```
46
+ document.addEventListener('nlAuth', (e) => {
47
+ // type is login, signup or logout
48
+ if (e.detail.type === 'login' || e.detail.type === 'signup') {
49
+ onLogin(); // get pubkey with window.nostr and show user profile
50
+ } else {
51
+ onLogout() // clear local user data, hide profile info
52
+ }
53
+ })
54
+ ```
55
+
56
+ ## Launching, logout, etc
57
+
58
+ The `nostr-login` auth modals will be automatically launched whenever you
59
+ make a call to `window.nostr` if user isn't authed yet. However, you can also launch the auth flow by dispatching a custom `nlLaunch` event:
60
+
61
+ ```
62
+ document.dispatchEvent(new CustomEvent('nlLaunch', { detail: 'welcome' }));
63
+ ```
64
+
65
+ The `detail` event payload can be empty, or can be one of `welcome`, `signup`, `login`, `login-bunker-url`, `login-read-only`, `switch-account`.
66
+
67
+ To trigger logout in the `nostr-login`, you can dispatch a `nlLogout` event:
68
+
69
+ ```
70
+ document.dispatchEvent(new Event("nlLogout"));
71
+ ```
72
+
73
+ To change dark mode in the `nostr-login`, you can dispatch a `nlDarkMode` event, with detail as `darkMode` boolean:
74
+
75
+ ```
76
+ document.dispatchEvent(new CustomEvent("nlDarkMode", { detail: true }));
77
+ ```
78
+
79
+ ## Use as a package
80
+
81
+ Install `nostr-login` package with `npm` and then:
82
+
83
+ ```
84
+ import { init as initNostrLogin } from "nostr-login"
85
+
86
+ // make sure this is called before any
87
+ // window.nostr calls are made
88
+ initNostrLogin({/*options*/})
89
+
90
+ ```
91
+
92
+ Now the `window.nostr` will be initialized and on your first call
93
+ to it the auth flow will be launched if user isn't authed yet.
94
+
95
+ You can also launch the auth flow yourself:
96
+
97
+ ```
98
+ import { launch as launchNostrLoginDialog } from "nostr-login"
99
+
100
+ // make sure init() was called
101
+
102
+ // on your signup button click
103
+ function onSignupClick() {
104
+ // launch signup screen
105
+ launchNostrLoginDialog({
106
+ startScreen: 'signup'
107
+ })
108
+ }
109
+ ```
110
+
111
+ ### Next.js Fix for Server Side Rendering (SSR)
112
+
113
+ `nostr-login` calls `document` which is unavailable for server-side rendering. You will have build errors. To fix this, you can import `nostr-login` on the client side in your component with a `useEffect` like this:
114
+
115
+ ```javascript
116
+ useEffect(() => {
117
+ import('nostr-login')
118
+ .then(async ({ init }) => {
119
+ init({
120
+ // options
121
+ })
122
+ })
123
+ .catch((error) => console.log('Failed to load nostr-login', error));
124
+ }, []);
125
+ ```
126
+ Note: even if your component has `"use client"` in the first line, this fix still may be necessary.
127
+
128
+ ---
129
+
130
+ API:
131
+ - `init(opts)` - set mapping of window.nostr to nostr-login
132
+ - `launch(startScreen)` - launch nostr-login UI
133
+ - `logout()` - drop the current nip46 connection
134
+
135
+ Options:
136
+ - `theme` - same as `data-theme` above
137
+ - `startScreen` - same as `startScreen` for `nlLaunch` event above
138
+ - `bunkers` - same as `data-bunkers` above
139
+ - `devOverrideBunkerOrigin` - for testing, overrides the bunker origin for local setup
140
+ - `onAuth: (npub: string, options: NostrLoginAuthOptions)` - a callback to provide instead of listening to `nlAuth` event
141
+ - `perms` - same as `data-perms` above
142
+ - `darkMode` - same as `data-dark-mode` above
143
+ - `noBanner` - same as `data-no-banner` above
144
+ - `isSignInWithExtension` - `true` to bring the *Sign in with exception* button into main list of options, `false` to hide to the *Advanced*, default will behave as `true` if extension is detected.
145
+
146
+ ## OTP login
147
+
148
+ If you supply both `data-otp-request-url` and `data-otp-reply-url` then "Login with DM" button will appear on the welcome screen.
149
+
150
+ When user enters their nip05 or npub, a GET request is made to `<data-otp-request-url>[?&]pubkey=<user-pubkey>`. Server should send
151
+ a DM with one-time code to that pubkey and should return 200.
152
+
153
+ After user enters the code, a GET request is made to `<data-otp-reply-url>[?&]pubkey=<user-pubkey>&code=<code>`. Server should check that code matches the pubkey and hasn't expired, and should return 200 status and an optional payload. Nostr-login will deliver the payload as `otpData` field in `nlAuth` event, and will save the payload in localstore and will deliver it again as `nlAuth` on page reload.
154
+
155
+ The reply payload may be used to supply the session token. If token is sent by the server as a cookie then payload might be empty, otherwise the payload should be used by the app to extract the token and use it in future API calls to the server.
156
+
157
+ ## Examples
158
+
159
+ * [Basic HTML Example](./examples/usage.html)
160
+
161
+ ## TODO
162
+
163
+ - fetch bunker list using NIP-89
164
+ - Amber support
165
+ - allow use without the UIs
166
+ - add timeout handling
167
+ - more at [issues](https://github.com/nostrband/nostr-login/issues)
@@ -0,0 +1 @@
1
+ export declare const CALL_TIMEOUT = 5000;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import 'nostr-login-components';
2
+ import { AuthNostrService, NostrExtensionService, Popup, NostrParams, Nostr, ProcessManager, BannerManager, ModalManager } from './modules';
3
+ import { NostrLoginAuthOptions, NostrLoginOptions, StartScreens } from './types';
4
+ export declare class NostrLoginInitializer {
5
+ extensionService: NostrExtensionService;
6
+ params: NostrParams;
7
+ authNostrService: AuthNostrService;
8
+ nostr: Nostr;
9
+ processManager: ProcessManager;
10
+ popupManager: Popup;
11
+ bannerManager: BannerManager;
12
+ modalManager: ModalManager;
13
+ private customLaunchCallback?;
14
+ constructor();
15
+ private openPopup;
16
+ private switchAccount;
17
+ private updateAccounts;
18
+ launchCustomNostrConnect(): Promise<void>;
19
+ private fulfillCustomLaunchPromise;
20
+ launch: (startScreen?: StartScreens | 'default') => Promise<void>;
21
+ init: (opt: NostrLoginOptions) => Promise<void>;
22
+ logout: () => Promise<void>;
23
+ setDarkMode: (dark: boolean) => void;
24
+ setAuth: (o: NostrLoginAuthOptions) => Promise<void>;
25
+ cancelNeedAuth: () => void;
26
+ }
27
+ export declare const init: (opt: NostrLoginOptions) => Promise<void>, launch: (startScreen?: StartScreens | 'default') => Promise<void>, logout: () => Promise<void>, setDarkMode: (dark: boolean) => void, setAuth: (o: NostrLoginAuthOptions) => Promise<void>, cancelNeedAuth: () => void;