2023-02-10 12:23:51 +00:00
|
|
|
<script>
|
|
|
|
|
import { onMount } from "svelte";
|
2023-06-05 21:50:16 +00:00
|
|
|
// import QR from 'svelte-qr';
|
2023-02-10 12:23:51 +00:00
|
|
|
import { chatAdapter } from './lib/store';
|
|
|
|
|
import NstrAdapterNip07 from './lib/adapters/nip07.js';
|
|
|
|
|
import NstrAdapterNip46 from './lib/adapters/nip46.js';
|
|
|
|
|
import NstrAdapterDiscadableKeys from './lib/adapters/discardable-keys.js';
|
|
|
|
|
|
|
|
|
|
export let websiteOwnerPubkey;
|
|
|
|
|
export let chatConfiguration;
|
|
|
|
|
export let relays;
|
|
|
|
|
|
|
|
|
|
let hasNostrNip07 = true;
|
|
|
|
|
let publicKey = null;
|
|
|
|
|
let nip46URI;
|
|
|
|
|
let adapterConfig;
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
// hasNostrNip07 = !!window.nostr;
|
|
|
|
|
|
|
|
|
|
const type = localStorage.getItem('nostrichat-type');
|
|
|
|
|
|
|
|
|
|
if (type === 'nip07') {
|
|
|
|
|
useNip07();
|
|
|
|
|
} else if (type === 'nip-46') {
|
|
|
|
|
useNip46();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
adapterConfig = {
|
|
|
|
|
type: chatConfiguration.chatType,
|
|
|
|
|
tags: chatConfiguration.chatTags,
|
|
|
|
|
referenceTags: chatConfiguration.chatReferenceTags,
|
|
|
|
|
websiteOwnerPubkey,
|
|
|
|
|
relays
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function useNip07() {
|
|
|
|
|
window.nostr.getPublicKey().then((pubkey) => {
|
|
|
|
|
localStorage.setItem('nostrichat-type', 'nip07');
|
|
|
|
|
chatAdapter.set(new NstrAdapterNip07(pubkey, adapterConfig))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import { generatePrivateKey, getPublicKey } from 'nostr-tools';
|
|
|
|
|
import { Connect, ConnectURI } from '@nostr-connect/connect';
|
|
|
|
|
|
|
|
|
|
async function useDiscardableKeys() {
|
|
|
|
|
chatAdapter.set(new NstrAdapterDiscadableKeys(adapterConfig))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function useNip46() {
|
|
|
|
|
let key = localStorage.getItem('nostrichat-nostr-connect-key');
|
|
|
|
|
let publicKey = localStorage.getItem('nostrichat-nostr-connect-public-key');
|
|
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
|
chatAdapter.set(new NstrAdapterNip46(publicKey, key, adapterConfig))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = generatePrivateKey();
|
|
|
|
|
|
|
|
|
|
const connect = new Connect({ secretKey: key, relay: 'wss://nostr.vulpem.com' });
|
|
|
|
|
connect.events.on('connect', (connectedPubKey) => {
|
|
|
|
|
localStorage.setItem('nostrichat-nostr-connect-key', key);
|
|
|
|
|
localStorage.setItem('nostrichat-nostr-connect-public-key', connectedPubKey);
|
|
|
|
|
localStorage.setItem('nostrichat-type', 'nip-46');
|
2023-06-05 21:50:16 +00:00
|
|
|
|
2023-02-10 12:23:51 +00:00
|
|
|
console.log('connected to nostr connect relay')
|
|
|
|
|
publicKey = connectedPubKey;
|
|
|
|
|
chatAdapter.set(new NstrAdapterNip46(publicKey, key))
|
|
|
|
|
nip46URI = null;
|
|
|
|
|
});
|
|
|
|
|
connect.events.on('disconnect', () => {
|
|
|
|
|
console.log('disconnected from nostr connect relay')
|
|
|
|
|
})
|
|
|
|
|
await connect.init();
|
|
|
|
|
|
2023-03-10 15:51:55 +00:00
|
|
|
let windowTitle, currentUrl, currentDomain;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
windowTitle = window.document.title || 'Nostrichat';
|
|
|
|
|
currentUrl = new URL(window.location.href);
|
|
|
|
|
currentDomain = currentUrl.hostname;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
currentUrl = window.location.href;
|
|
|
|
|
currentDomain = currentUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 12:23:51 +00:00
|
|
|
const connectURI = new ConnectURI({
|
|
|
|
|
target: getPublicKey(key),
|
|
|
|
|
relay: 'wss://nostr.vulpem.com',
|
|
|
|
|
metadata: {
|
2023-03-10 15:51:55 +00:00
|
|
|
name: windowTitle,
|
2023-02-10 12:23:51 +00:00
|
|
|
description: '🔉🔉🔉',
|
2023-03-10 15:51:55 +00:00
|
|
|
url: currentUrl,
|
2023-02-10 12:23:51 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
nip46URI = connectURI.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Nip46Copy() {
|
|
|
|
|
navigator.clipboard.writeText(nip46URI);
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2023-08-29 12:30:41 +00:00
|
|
|
<h1 class="">
|
2023-09-13 14:28:28 +00:00
|
|
|
Welcome to DiSseNT
|
2023-02-10 12:23:51 +00:00
|
|
|
</h1>
|
|
|
|
|
|
2023-09-13 14:28:28 +00:00
|
|
|
<h2 class="">
|
|
|
|
|
How would you like to connect?
|
|
|
|
|
</h2>
|
|
|
|
|
|
2023-02-10 12:23:51 +00:00
|
|
|
{#if publicKey}
|
2023-08-29 12:30:41 +00:00
|
|
|
<p class="">
|
2023-02-10 12:23:51 +00:00
|
|
|
Nostr Connect is a WIP, not fully implemented yet!
|
|
|
|
|
</p>
|
|
|
|
|
|
2023-08-29 12:30:41 +00:00
|
|
|
<p class="">
|
2023-02-10 12:23:51 +00:00
|
|
|
You are currently connected with the following public key:
|
|
|
|
|
<span>{publicKey}</span>
|
|
|
|
|
</p>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#if nip46URI}
|
2023-08-29 12:30:41 +00:00
|
|
|
<p class="">
|
2023-02-10 12:23:51 +00:00
|
|
|
Scan this with your Nostr Connect (click to copy to clipboard)
|
|
|
|
|
</p>
|
|
|
|
|
|
2023-08-29 12:30:41 +00:00
|
|
|
<div class=""
|
2023-02-10 12:23:51 +00:00
|
|
|
on:click|preventDefault={Nip46Copy}>
|
2023-06-05 21:50:16 +00:00
|
|
|
<!-- <QR text={nip46URI} /> -->
|
2023-02-10 12:23:51 +00:00
|
|
|
</div>
|
|
|
|
|
|
2023-08-29 12:30:41 +00:00
|
|
|
<button class="" on:click|preventDefault={() => { nip46URI = null; }}>
|
2023-02-10 12:23:51 +00:00
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
{:else if !publicKey}
|
2023-09-13 14:28:28 +00:00
|
|
|
<div class="btn-list">
|
2023-02-10 12:23:51 +00:00
|
|
|
{#if hasNostrNip07}
|
2023-09-13 14:28:28 +00:00
|
|
|
<button class="btn" on:click|preventDefault={useNip07}>
|
|
|
|
|
Browser Extension <span class="btn__subheading">(NIP-07)</span>
|
2023-02-10 12:23:51 +00:00
|
|
|
</button>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
|
2023-09-13 14:28:28 +00:00
|
|
|
<button class="btn" on:click|preventDefault={useNip46}>
|
|
|
|
|
Nostr Connect<span class="btn__subheading">(NIP-46)</span>
|
2023-02-10 12:23:51 +00:00
|
|
|
</button>
|
|
|
|
|
|
2023-09-13 14:28:28 +00:00
|
|
|
<button class="btn" on:click|preventDefault={useDiscardableKeys}>
|
2023-02-10 12:23:51 +00:00
|
|
|
Anonymous
|
2023-09-13 14:28:28 +00:00
|
|
|
<span class="btn__subheading">
|
2023-02-10 12:23:51 +00:00
|
|
|
(Ephemeral Keys)
|
|
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2023-08-29 12:30:41 +00:00
|
|
|
{/if}
|