window.nostr.js 0.4.0 → 0.4.2

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.
package/index.html CHANGED
@@ -15,7 +15,7 @@
15
15
  "
16
16
  >
17
17
  <main>
18
- <h1>App Title</h1>
18
+ <h1>Lorem Ipsum</h1>
19
19
  <p>
20
20
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et ex dui.
21
21
  Donec eu dui sed massa auctor rhoncus. Nunc mollis, metus nec
@@ -34,31 +34,6 @@
34
34
  mollis sit amet. Cras sit amet mi sit amet eros convallis pulvinar.
35
35
  Donec condimentum condimentum tincidunt.
36
36
  </p>
37
-
38
- <p>
39
- Suspendisse eu ipsum vel lorem congue mattis in at neque. Phasellus nec
40
- felis sit amet nibh bibendum tristique a quis urna. Pellentesque laoreet
41
- porta libero eget pellentesque. Praesent tempor, nisi et efficitur
42
- cursus, leo nunc ullamcorper ante, quis eleifend velit justo sed enim.
43
- Maecenas laoreet dictum lorem rhoncus tempus. Integer ipsum augue,
44
- sagittis a facilisis et, mollis ut ipsum. Proin elementum consectetur
45
- tincidunt.
46
- </p>
47
-
48
- <p>
49
- Mauris nec mi et quam semper volutpat. Morbi suscipit felis lectus, in
50
- ornare purus pretium in. Maecenas et ex metus. Ut aliquam, felis vel
51
- mollis dapibus, tellus velit blandit dui, eu hendrerit tellus magna at
52
- eros. Donec scelerisque hendrerit auctor. Aenean aliquet sapien
53
- elementum dui sagittis eleifend. Donec hendrerit odio et dui lacinia
54
- aliquet. Morbi auctor ultrices dui fringilla elementum. Curabitur
55
- scelerisque lorem vitae placerat vulputate. Donec vestibulum urna sem,
56
- at sodales ligula laoreet id. Etiam vehicula, lorem id tincidunt
57
- sodales, sem odio vehicula ex, vel condimentum quam augue id augue.
58
- Integer a varius nunc. Mauris rhoncus leo sed facilisis bibendum. Nullam
59
- consectetur pellentesque vestibulum. Cras hendrerit feugiat orci, in
60
- molestie risus rutrum ut. Proin dictum nunc at rutrum vehicula.
61
- </p>
62
37
  </main>
63
38
  <!-- <script>
64
39
  window.wnjParams = {
package/justfile CHANGED
@@ -4,16 +4,10 @@ set unstable
4
4
  dev:
5
5
  vite --port 13471
6
6
 
7
- build: wnj iframe
8
-
9
- wnj:
7
+ build:
10
8
  vite build
11
9
  mv dist/assets/*.js dist/window.nostr.js
12
10
 
13
- iframe:
14
- node ./iframe/build.js
15
- cp ./iframe/iframe.html dist/
16
-
17
11
  demo:
18
12
  xdg-open demo/index.html
19
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "window.nostr.js",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "type": "module",
5
5
  "main": "dist/window.nostr.js",
6
6
  "devDependencies": {
@@ -23,7 +23,7 @@
23
23
  "vite": "^5.1.0"
24
24
  },
25
25
  "dependencies": {
26
- "debounce": "^2.0.0",
27
- "nostr-tools": "^2.8.1"
26
+ "debounce": "2.0.0",
27
+ "nostr-tools": "^2.9.1"
28
28
  }
29
29
  }
package/src/App.svelte CHANGED
@@ -18,7 +18,6 @@
18
18
  type BunkerProfile,
19
19
  BUNKER_REGEX
20
20
  } from 'nostr-tools/nip46'
21
- import {localStorageKeys} from './lib'
22
21
  import {NIP05_REGEX, queryProfile} from 'nostr-tools/nip05'
23
22
  import {npubEncode} from 'nostr-tools/nip19'
24
23
  import {onMount} from 'svelte'
@@ -26,36 +25,42 @@
26
25
  import Spinner from './Spinner.svelte'
27
26
 
28
27
  const mobileMode = mediaQueryStore('only screen and (max-width: 640px)')
28
+ const lskeys = {
29
+ ORIGIN: 'wnj:origin',
30
+ CLIENT_SECRET: 'wnj:clientSecret',
31
+ Y_POS: 'wnj:ypos',
32
+ CALLBACK_TOKEN: 'wnj:callbackToken',
33
+ BUNKER_POINTER: 'wnj:bunkerPointer',
34
+ CACHED_PUBKEY: 'wnj:cachedPubKey',
35
+ IGNORE_IFRAME: 'wnj:ignoreIframe'
36
+ }
29
37
 
30
38
  let myself: HTMLDivElement
31
39
  export let accent: string
32
40
  export let position: 'top' | 'bottom' = 'top'
33
41
  $: origin = $mobileMode
34
42
  ? 'bottom'
35
- : (localStorage.getItem(localStorageKeys.ORIGIN) as
36
- | 'top'
37
- | 'bottom'
38
- | null) || position
43
+ : (localStorage.getItem(lskeys.ORIGIN) as 'top' | 'bottom' | null) ||
44
+ position
39
45
  export let startHidden: boolean
40
46
  export let compactMode: boolean
41
47
 
42
48
  const win = window as any
43
49
  const pool = new SimplePool()
50
+ let useIframe = false
51
+ let iframe: HTMLIFrameElement | undefined
44
52
  let bunkerInput: HTMLInputElement
45
53
  let bunkerInputValue: string
46
54
  let nameInput: HTMLInputElement
47
55
  let nameInputValue: string
48
56
  let chosenProvider: BunkerProfile | undefined
49
57
  let clientSecret: Uint8Array
50
- const local = localStorage.getItem(localStorageKeys.CLIENT_SECRET)
58
+ const local = localStorage.getItem(lskeys.CLIENT_SECRET)
51
59
  if (local) {
52
60
  clientSecret = hexToBytes(local)
53
61
  } else {
54
62
  clientSecret = generateSecretKey()
55
- localStorage.setItem(
56
- localStorageKeys.CLIENT_SECRET,
57
- bytesToHex(clientSecret)
58
- )
63
+ localStorage.setItem(lskeys.CLIENT_SECRET, bytesToHex(clientSecret))
59
64
  }
60
65
 
61
66
  let state: 'opened' | 'closed' | 'justopened' | 'justclosed' = 'closed'
@@ -91,7 +96,7 @@
91
96
  export let right = 20
92
97
  $: ypos = $mobileMode
93
98
  ? BASE_YPOS
94
- : parseInt(localStorage.getItem(localStorageKeys.Y_POS) || '0') || BASE_YPOS
99
+ : parseInt(localStorage.getItem(lskeys.Y_POS) || '0') || BASE_YPOS
95
100
  let dragStarted = false
96
101
  let hasMoved = false
97
102
  let insidePosition: number
@@ -163,9 +168,8 @@
163
168
  let windowNostr = {
164
169
  isWnj: true,
165
170
  async getPublicKey(): Promise<string> {
166
- if (bunkerPointer) return bunkerPointer.pubkey
167
171
  if (!connecting && !connected) open()
168
- return (await bunker).bp.pubkey
172
+ return (await bunker).getPublicKey()
169
173
  },
170
174
  async signEvent(event: NostrEvent): Promise<VerifiedEvent> {
171
175
  try {
@@ -212,12 +216,22 @@
212
216
 
213
217
  onMount(() => {
214
218
  if (!bunkerPointer) {
215
- let data = localStorage.getItem(localStorageKeys.BUNKER_POINTER)
219
+ let data = localStorage.getItem(lskeys.BUNKER_POINTER)
216
220
  if (data) {
217
221
  bunkerPointer = JSON.parse(data)
218
- // we have a pointer, which means we can get the public key right away
219
- // but we will only try to connect when any other method is called on window.nostr
220
222
  identify()
223
+
224
+ // we must connect here so identify() works because we can't rely on the bunker params to read our pubkey
225
+ // however we may first check if we have it cached locally before doing the expensive connection
226
+ let cachedPubkey = localStorage.getItem(lskeys.CACHED_PUBKEY)
227
+ if (!cachedPubkey) {
228
+ connect()
229
+ }
230
+ } else {
231
+ // when we don't have any bunker data stored, we can still check the iframe for it
232
+ if (!localStorage.getItem(lskeys.IGNORE_IFRAME)) {
233
+ useIframe = true
234
+ }
221
235
  }
222
236
  }
223
237
 
@@ -250,6 +264,21 @@
250
264
  }
251
265
  })
252
266
 
267
+ function onIframeLoaded() {
268
+ window.addEventListener('message', handleMessage)
269
+ iframe?.contentWindow?.postMessage({getbunker: true}, '*')
270
+
271
+ async function handleMessage(ev: MessageEvent) {
272
+ let {bunker} = ev.data
273
+ if (bunker) {
274
+ bunkerPointer = await parseBunkerInput(bunker)
275
+ identify()
276
+ connect()
277
+ window.removeEventListener('message', handleMessage)
278
+ }
279
+ }
280
+ }
281
+
253
282
  function handleClick(ev: MouseEvent) {
254
283
  if (Math.abs(ypos - yposStart) > 6 || Date.now() - clickStart > 600) {
255
284
  return
@@ -312,7 +341,9 @@
312
341
 
313
342
  async function handleDisconnect(ev: MouseEvent) {
314
343
  ev.preventDefault()
315
- localStorage.removeItem(localStorageKeys.BUNKER_POINTER)
344
+ localStorage.removeItem(lskeys.BUNKER_POINTER)
345
+ localStorage.removeItem(lskeys.CACHED_PUBKEY)
346
+ localStorage.setItem(lskeys.IGNORE_IFRAME, '')
316
347
  reset()
317
348
  }
318
349
 
@@ -385,10 +416,7 @@
385
416
  try {
386
417
  await b.connect()
387
418
  connected = true
388
- localStorage.setItem(
389
- localStorageKeys.BUNKER_POINTER,
390
- JSON.stringify(bunkerPointer)
391
- )
419
+ localStorage.setItem(lskeys.BUNKER_POINTER, JSON.stringify(bunkerPointer))
392
420
  close()
393
421
  resolveBunker(b)
394
422
  } catch (err: any) {
@@ -404,8 +432,16 @@
404
432
  }
405
433
  }
406
434
 
407
- function identify(onFirstMetadata: (() => void) | null = null) {
408
- let pubkey = bunkerPointer!.pubkey
435
+ // identify() is what gives a name and picture to our floating widget
436
+ async function identify() {
437
+ let pubkey = localStorage.getItem(lskeys.CACHED_PUBKEY)
438
+ if (!pubkey) {
439
+ pubkey = await (await bunker).getPublicKey()
440
+
441
+ // store this pubkey here so we don't have to connect and get our pubkey immediately
442
+ // the next time we open this page
443
+ localStorage.setItem(lskeys.CACHED_PUBKEY, pubkey)
444
+ }
409
445
 
410
446
  identity = {
411
447
  pubkey: pubkey,
@@ -428,8 +464,6 @@
428
464
  identity!.event = evt
429
465
  identity!.name = name
430
466
  identity!.picture = picture
431
- onFirstMetadata?.()
432
- onFirstMetadata = null
433
467
  } catch (err) {
434
468
  /***/
435
469
  }
@@ -491,8 +525,8 @@
491
525
  ypos = BASE_YPOS
492
526
  }
493
527
 
494
- localStorage.setItem(localStorageKeys.ORIGIN, origin)
495
- localStorage.setItem(localStorageKeys.Y_POS, ypos.toString())
528
+ localStorage.setItem(lskeys.ORIGIN, origin)
529
+ localStorage.setItem(lskeys.Y_POS, ypos.toString())
496
530
  }
497
531
  }
498
532
  </script>
@@ -806,3 +840,11 @@
806
840
  </div>
807
841
  {/if}
808
842
  </div>
843
+ {#if useIframe}
844
+ <iframe
845
+ title="~"
846
+ bind:this={iframe}
847
+ on:load={onIframeLoaded}
848
+ src="https://join.the-nostr.org/iframe.html"
849
+ ></iframe>
850
+ {/if}
package/dist/iframe.html DELETED
@@ -1 +0,0 @@
1
- <script src="iframe.js"></script>