aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-11 17:09:39 -0800
committerAdam Soltys <[email protected]>2026-02-11 17:09:39 -0800
commit3aa9ce1b5985397d7f568887ec6f881df13c1034 (patch)
tree2c459692dc5315e92924de6be9f0999a835e0f54 /src/lib
parent8a4b0087cbfe8b43d9b43d19af6d991a646ef3d7 (diff)
downloadcoinos-ui-3aa9ce1b5985397d7f568887ec6f881df13c1034.tar.gz
coinos-ui-3aa9ce1b5985397d7f568887ec6f881df13c1034.zip
fix ts errors
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/locales.ts38
-rw-r--r--src/lib/nip07.ts25
-rw-r--r--src/lib/nip17.ts69
-rw-r--r--src/lib/nip51.ts17
-rw-r--r--src/lib/nostr.ts30
5 files changed, 84 insertions, 95 deletions
diff --git a/src/lib/locales.ts b/src/lib/locales.ts
index 68a28abd..282eac22 100644
--- a/src/lib/locales.ts
+++ b/src/lib/locales.ts
@@ -1,22 +1,22 @@
-import de from "date-fns/locale/de";
-import el from "date-fns/locale/el";
-import en from "date-fns/locale/en-US";
-import es from "date-fns/locale/es";
-import fa from "date-fns/locale/fa-IR";
-import fr from "date-fns/locale/fr";
-import ja from "date-fns/locale/ja";
-import pt from "date-fns/locale/pt";
-import ru from "date-fns/locale/ru";
-import zh from "date-fns/locale/zh-CN";
-import ar from "date-fns/locale/ar-SA";
-import hi from "date-fns/locale/hi";
-import bn from "date-fns/locale/bn";
-import tr from "date-fns/locale/tr";
-import ko from "date-fns/locale/ko";
-import it from "date-fns/locale/it";
-import nl from "date-fns/locale/nl";
-import th from "date-fns/locale/th";
-import pl from "date-fns/locale/pl";
+import { de } from "date-fns/locale/de";
+import { el } from "date-fns/locale/el";
+import { enUS as en } from "date-fns/locale/en-US";
+import { es } from "date-fns/locale/es";
+import { faIR as fa } from "date-fns/locale/fa-IR";
+import { fr } from "date-fns/locale/fr";
+import { ja } from "date-fns/locale/ja";
+import { pt } from "date-fns/locale/pt";
+import { ru } from "date-fns/locale/ru";
+import { zhCN as zh } from "date-fns/locale/zh-CN";
+import { arSA as ar } from "date-fns/locale/ar-SA";
+import { hi } from "date-fns/locale/hi";
+import { bn } from "date-fns/locale/bn";
+import { tr } from "date-fns/locale/tr";
+import { ko } from "date-fns/locale/ko";
+import { it } from "date-fns/locale/it";
+import { nl } from "date-fns/locale/nl";
+import { th } from "date-fns/locale/th";
+import { pl } from "date-fns/locale/pl";
export default {
de,
diff --git a/src/lib/nip07.ts b/src/lib/nip07.ts
index 4c2048e2..85f68377 100644
--- a/src/lib/nip07.ts
+++ b/src/lib/nip07.ts
@@ -1,27 +1,26 @@
import { bytesToHex } from '@noble/hashes/utils.js';
import { finalizeEvent } from 'nostr-tools/pure';
-import { unwrapEvent } from 'nostr-tools/nip17';
import { getPrivateKey } from '$lib/nostr';
import * as libnip44 from '$lib/nip44';
import { u } from '$lib/nip44';
// Signs an event using NIP-07 if available, otherwise getPrivateKey()
-export const signEvent = async (event: object, user: object): object => {
- if (await window.nostr.getPublicKey() === user.pubkey) {
- return window.nostr.signEvent(event);
+export const signEvent = async (event: any, user: any): Promise<any> => {
+ if (await (window as any).nostr.getPublicKey() === user.pubkey) {
+ return (window as any).nostr.signEvent(event);
} else {
- const sk = await getPrivateKey();
+ const sk = await getPrivateKey(user);
return finalizeEvent(event, sk);
}
}
// Encrypts an event in NIP-44 using NIP-07 if available, otherwise getPrivateKey()
-export const encrypt = async (json: object, user: object, receiverPK: string): string => {
- if (await window.nostr.getPublicKey() === user.pubkey) {
- return window.nostr.nip44.encrypt(
+export const encrypt = async (json: any, user: any, receiverPK: string): Promise<string> => {
+ if (await (window as any).nostr.getPublicKey() === user.pubkey) {
+ return (window as any).nostr.nip44.encrypt(
receiverPK, JSON.stringify(json));
} else {
- const senderSK = await getPrivateKey();
+ const senderSK = await getPrivateKey(user);
const senderSKString = bytesToHex(senderSK);
const ckey = u.getConversationKey(senderSKString, receiverPK);
return libnip44.encrypt(JSON.stringify(json), ckey);
@@ -29,12 +28,12 @@ export const encrypt = async (json: object, user: object, receiverPK: string): s
}
// Decrypts an event in NIP-44 using NIP-07 if available, otherwise getPrivateKey()
-export const decrypt = async (encrypted: string, user: object, receiverPK: string): object => {
- if (await window.nostr.getPublicKey() === user.pubkey) {
- return JSON.parse(await window.nostr.nip44.decrypt(
+export const decrypt = async (encrypted: string, user: any, receiverPK: string): Promise<any> => {
+ if (await (window as any).nostr.getPublicKey() === user.pubkey) {
+ return JSON.parse(await (window as any).nostr.nip44.decrypt(
receiverPK, encrypted));
} else {
- const senderSK = await getPrivateKey();
+ const senderSK = await getPrivateKey(user);
const senderSKString = bytesToHex(senderSK);
const ckey = u.getConversationKey(senderSKString, receiverPK);
return JSON.parse(libnip44.decrypt(encrypted, ckey));
diff --git a/src/lib/nip17.ts b/src/lib/nip17.ts
index d7ce181d..b039ebf8 100644
--- a/src/lib/nip17.ts
+++ b/src/lib/nip17.ts
@@ -25,9 +25,9 @@ const DM_FETCH_LIMIT = 256;
// Returns the sk as Uint8Array, or null if using NIP-07 extension.
const ensureSigner = async (userPubkey: string): Promise<Uint8Array | null> => {
// Check if NIP-07 extension is available and matches user
- if (window.nostr) {
+ if ((window as any).nostr) {
try {
- const extPubkey = await window.nostr.getPublicKey();
+ const extPubkey = await (window as any).nostr.getPublicKey();
if (extPubkey === userPubkey) {
return null; // Use NIP-07
}
@@ -37,7 +37,7 @@ const ensureSigner = async (userPubkey: string): Promise<Uint8Array | null> => {
}
// Check if signer already has a valid sk (64 hex chars = 32 bytes)
- const currentSigner = get(signer);
+ const currentSigner: any = get(signer);
if (currentSigner?.ready && currentSigner?.params?.sk && currentSigner.params.sk.length === 64) {
return hexToBytes(currentSigner.params.sk);
}
@@ -54,7 +54,7 @@ const ensureSigner = async (userPubkey: string): Promise<Uint8Array | null> => {
// Wait for signer to be ready
const signerValue = await new Promise<any>((resolve, reject) => {
let unsubscribe: () => void;
- unsubscribe = signer.subscribe((v) => {
+ unsubscribe = signer.subscribe((v: any) => {
if (v === 'cancel') {
setTimeout(() => unsubscribe?.());
eventToSign.set(null);
@@ -87,7 +87,7 @@ const ensureSigner = async (userPubkey: string): Promise<Uint8Array | null> => {
// If expiryDays is specified, the message will expire in that many days,
// plus a random amount from 0 to 2 for security.
// (expiration time = random creation time + expiryDays + 2 days)
-export const createNIP17MessageSK = (text: string, senderSK: Uint8Array, receiverPK: string, wrapPK: string = null, expiryDays: number = null) => {
+export const createNIP17MessageSK = (text: string, senderSK: Uint8Array, receiverPK: string, wrapPK: string | null = null, expiryDays: number | null = null) => {
const rumour = createRumour(text, getPublicKey(senderSK), receiverPK);
const sealed = sealRumourSK(rumour, senderSK, wrapPK || receiverPK, expiryDays);
return giftWrap(sealed, wrapPK || receiverPK, expiryDays);
@@ -100,14 +100,14 @@ export const createNIP17MessageSK = (text: string, senderSK: Uint8Array, receive
// If expiryDays is specified, the message will expire in that many days,
// plus a random amount from 0 to 2 for security.
// (expiration time = random creation time + expiryDays + 2 days)
-export const createNIP17MessageNIP07 = async (text: string, senderPK: string, receiverPK: string, wrapPK: string = null, expiryDays: number = null) => {
+export const createNIP17MessageNIP07 = async (text: string, senderPK: string, receiverPK: string, wrapPK: string | null = null, expiryDays: number | null = null) => {
const rumour = createRumour(text, senderPK, receiverPK);
const sealed = await sealRumourNip07(rumour, wrapPK || receiverPK, expiryDays);
return giftWrap(sealed, wrapPK || receiverPK, expiryDays);
}
const createRumour = (text: string, senderPK: string, receiverPK: string) => {
- const rumour = {
+ const rumour: any = {
kind: 14, // rumour
created_at: Math.floor(Date.now() / 1000),
tags: [["p", receiverPK]],
@@ -118,13 +118,13 @@ const createRumour = (text: string, senderPK: string, receiverPK: string) => {
return rumour;
}
-const sealRumourSK = (rumour: object, senderSK: Uint8Array, receiverPK: string, expiryDays: number = null) => {
+const sealRumourSK = (rumour: any, senderSK: Uint8Array, receiverPK: string, expiryDays: number | null = null) => {
const senderSKString = bytesToHex(senderSK);
const conversationKey = u.getConversationKey(senderSKString, receiverPK);
const encryptedRumour = encrypt(JSON.stringify(rumour), conversationKey);
const created = randomTimestamp();
- const sealEvent = {
+ const sealEvent: any = {
kind: 13, // seal
created_at: created,
tags: [],
@@ -139,12 +139,12 @@ const sealRumourSK = (rumour: object, senderSK: Uint8Array, receiverPK: string,
return finalizeEvent(sealEvent, senderSK);
}
-const sealRumourNip07 = async (rumour: object, receiverPK: string, expiryDays: number = null) => {
- const encryptedRumour = await window.nostr.nip44.encrypt(
+const sealRumourNip07 = async (rumour: any, receiverPK: string, expiryDays: number | null = null) => {
+ const encryptedRumour = await (window as any).nostr.nip44.encrypt(
receiverPK, JSON.stringify(rumour));
const created = randomTimestamp();
- const sealEvent = {
+ const sealEvent: any = {
kind: 13, // seal
created_at: created,
tags: [],
@@ -155,17 +155,16 @@ const sealRumourNip07 = async (rumour: object, receiverPK: string, expiryDays: n
sealEvent.tags.push(["expiration", expiration.toString()]);
}
- return window.nostr.signEvent(sealEvent);
+ return (window as any).nostr.signEvent(sealEvent);
}
-const giftWrap = (event: object, receiverPK: string, expiryDays: number = null) => {
+const giftWrap = (event: any, receiverPK: string, expiryDays: number | null = null) => {
const secretKey = generateSecretKey();
- const SKString = bytesToHex(secretKey);
- const conversationKey = u.getConversationKey(secretKey, receiverPK);
+ const conversationKey = u.getConversationKey(bytesToHex(secretKey), receiverPK);
const encryptedEvent = encrypt(JSON.stringify(event), conversationKey);
const created = randomTimestamp();
- const giftWrapEvent = {
+ const giftWrapEvent: any = {
kind: 1059, // gift wrap
created_at: created,
tags: [["p", receiverPK]],
@@ -180,18 +179,18 @@ const giftWrap = (event: object, receiverPK: string, expiryDays: number = null)
return finalizeEvent(giftWrapEvent, secretKey);
}
-const decryptNIP17MessageNIP07 = async (wrapped: object): object => {
- const sealedText = await window.nostr.nip44.decrypt(
+const decryptNIP17MessageNIP07 = async (wrapped: any): Promise<any> => {
+ const sealedText = await (window as any).nostr.nip44.decrypt(
wrapped.pubkey, wrapped.content);
const sealed = JSON.parse(sealedText);
- const rumourText = await window.nostr.nip44.decrypt(
+ const rumourText = await (window as any).nostr.nip44.decrypt(
sealed.pubkey, sealed.content);
return JSON.parse(rumourText);
}
// Decrypts the provided message, addressed to the user.
// If NIP-07 is available, it uses that, otherwise it triggers the signer modal.
-export const decrypt = async (wrapped: object, user: object): object => {
+export const decrypt = async (wrapped: any, user: any): Promise<any> => {
const sk = await ensureSigner(user.pubkey);
if (sk === null) {
@@ -204,15 +203,15 @@ export const decrypt = async (wrapped: object, user: object): object => {
// Returns the rumours of the messages the provided user has sent or received.
// If the events have an expiration timestamp,
// that timestamp is added to its rumour object as the property `expiration`.
-export const getMessageRumours = async (user: object): object[] => {
+export const getMessageRumours = async (user: any): Promise<any[]> => {
const preferredRelays = await getPreferredRelays(user.pubkey);
// Always include default relays to catch messages that might be there
- const relays = [...new Set([...preferredRelays, ...DM_RELAYS_LIST])];
+ const relays = Array.from(new Set([...preferredRelays, ...DM_RELAYS_LIST]));
const wrapped = await pool.querySync(
relays, { kinds: [1059], "#p": [user.pubkey], limit: DM_FETCH_LIMIT }
);
- let rumours = [];
+ let rumours: any[] = [];
// intentionally decrypting sequentially to avoid having a bunch of popups
for (const event of wrapped) {
if (expired(event)) {
@@ -221,7 +220,7 @@ export const getMessageRumours = async (user: object): object[] => {
}
try {
- const rumour = await decrypt(event, user);
+ const rumour: any = await decrypt(event, user);
const expires = expiration(event);
if (expires) {
@@ -239,7 +238,7 @@ export const getMessageRumours = async (user: object): object[] => {
// Returns the preferred relays of the user with the provided pubkey,
// according to their kind-10050 event.
-export const getPreferredRelays = async (pubkey: string): string[] => {
+export const getPreferredRelays = async (pubkey: string): Promise<string[]> => {
if (relayListCache.has(pubkey))
return relayListCache.get(pubkey);
@@ -247,7 +246,7 @@ export const getPreferredRelays = async (pubkey: string): string[] => {
DM_RELAYS_LIST, { kinds: [10050], limit: 1, authors: [pubkey] }
);
- let relays = [];
+ let relays: string[] = [];
for (const event of events) {
for (const tag of event.tags) {
if (tag.length >= 2 && tag[0] == "relay") {
@@ -266,21 +265,21 @@ export const getPreferredRelays = async (pubkey: string): string[] => {
}
// Publish to a single relay with NIP-42 auth support
-const publishWithAuth = async (relayUrl: string, event: object, sk: Uint8Array | null): Promise<string> => {
+const publishWithAuth = async (relayUrl: string, event: any, sk: Uint8Array | null): Promise<string> => {
const relay = await Relay.connect(relayUrl);
- const signAuthEvent = async (authEvent: object) => {
+ const signAuthEvent = async (authEvent: any) => {
if (sk) {
return finalizeEvent(authEvent, sk);
- } else if (window.nostr) {
- return await window.nostr.signEvent(authEvent);
+ } else if ((window as any).nostr) {
+ return await (window as any).nostr.signEvent(authEvent);
}
throw new Error('No signing method for auth');
};
try {
return await relay.publish(event);
- } catch (e) {
+ } catch (e: any) {
// Check if it's an auth error
if (e?.message?.includes('auth-required')) {
// Authenticate and retry
@@ -294,7 +293,7 @@ const publishWithAuth = async (relayUrl: string, event: object, sk: Uint8Array |
// Store the current sk for use in publish
let currentSk: Uint8Array | null = null;
-const publishToPreferred = async (event: object, pubkey: string, expiryEnabled: boolean): Promise<string> => {
+const publishToPreferred = async (event: any, pubkey: string, expiryEnabled: boolean): Promise<string> => {
let preferredRelays = await getPreferredRelays(pubkey);
if (expiryEnabled) {
preferredRelays = await relaysSupporting(preferredRelays, [40]);
@@ -305,7 +304,7 @@ const publishToPreferred = async (event: object, pubkey: string, expiryEnabled:
const promises = preferredRelays.map(url => publishWithAuth(url, event, currentSk));
try {
- return await Promise.any(promises);
+ return await (Promise as any).any(promises);
} catch (e) {
const results = await Promise.allSettled(promises);
console.error('All relay publishes failed:', results);
@@ -315,7 +314,7 @@ const publishToPreferred = async (event: object, pubkey: string, expiryEnabled:
// Sends a NIP-17 message from `user` to `recipient`.
// If expiryDays is set, messages will expire after that many days.
-export const send = async (message: string, user: object, recipient: object, expiryDays?: number) => {
+export const send = async (message: string, user: any, recipient: any, expiryDays?: number) => {
let event1, event2;
const sk = await ensureSigner(user.pubkey);
diff --git a/src/lib/nip51.ts b/src/lib/nip51.ts
index b1b0b613..0210bf8b 100644
--- a/src/lib/nip51.ts
+++ b/src/lib/nip51.ts
@@ -1,5 +1,4 @@
import { SimplePool } from 'nostr-tools/pool';
-import { verifyEvent } from 'nostr-tools/pure';
import { signEvent, encrypt, decrypt } from '$lib/nip07';
const pool = new SimplePool();
@@ -7,7 +6,7 @@ import { PUBLIC_DM_RELAYS } from '$env/static/public';
const DM_RELAYS_LIST = PUBLIC_DM_RELAYS.split(',');
// Mute the provided pubkey, as the provided user.
-export const mute = async (user: object, pubkey: string, hide: bool) => {
+export const mute = async (user: any, pubkey: string, hide: boolean) => {
let { shown, hidden } = await getMuteLists(user);
if (hide) {
hidden.push(["p", pubkey]);
@@ -17,19 +16,19 @@ export const mute = async (user: object, pubkey: string, hide: bool) => {
await publishMuteList(user, shown, hidden);
}
-const tagMatches = (tag: string[], pubkey: string): bool =>
+const tagMatches = (tag: string[], pubkey: string): boolean =>
tag.length >= 2 && tag[0] === "p" && tag[1] === pubkey;
// Unmute the provided pubkey, as the provided user.
-export const unmute = async (user: object, pubkey: string) => {
+export const unmute = async (user: any, pubkey: string) => {
let { shown, hidden } = await getMuteLists(user);
- shown = shown.filter(tag => !tagMatches(tag, pubkey));
- hidden = hidden.filter(tag => !tagMatches(tag, pubkey));
+ shown = shown.filter((tag: string[]) => !tagMatches(tag, pubkey));
+ hidden = hidden.filter((tag: string[]) => !tagMatches(tag, pubkey));
await publishMuteList(user, shown, hidden);
}
// Returns a set of the provided user's muted accounts.
-export const mutedAccounts = async (user: object): Set<string> => {
+export const mutedAccounts = async (user: any): Promise<Set<string>> => {
const { shown, hidden } = await getMuteLists(user);
let muted = new Set<string>();
for (const tag of shown) {
@@ -45,7 +44,7 @@ export const mutedAccounts = async (user: object): Set<string> => {
return muted;
}
-const getMuteLists = async (user: object): object => {
+const getMuteLists = async (user: any): Promise<any> => {
const listEvents = await pool.querySync(
DM_RELAYS_LIST, { kinds: [10000], "authors": [user.pubkey], limit: 1 }
);
@@ -60,7 +59,7 @@ const getMuteLists = async (user: object): object => {
};
}
-const publishMuteList = async (user: object, shown: string[], hidden: string[]) => {
+const publishMuteList = async (user: any, shown: string[], hidden: string[]) => {
const event = await signEvent({
kind: 10000,
created_at: Math.floor(Date.now() / 1000),
diff --git a/src/lib/nostr.ts b/src/lib/nostr.ts
index 99efb697..dd283987 100644
--- a/src/lib/nostr.ts
+++ b/src/lib/nostr.ts
@@ -9,15 +9,7 @@ import {
} from "$lib/store";
import { post, wait } from "$lib/utils";
import { bytesToHex } from "@noble/hashes/utils.js";
-import {
- type EventTemplate,
- finalizeEvent,
- getEventHash,
- getPublicKey,
- nip04,
- nip19,
-} from "nostr-tools";
-import { Relay } from "nostr-tools/relay";
+import type { EventTemplate } from "nostr-tools";
import { get } from "svelte/store";
let nostrToolsPromise:
@@ -49,7 +41,7 @@ export const encrypt = async ({ message, recipient, user }: EncryptParams) => {
return nip04.encrypt(sk, recipient, message);
};
-export const decrypt = async ({ event, user }) => {
+export const decrypt = async ({ event, user }: { event: any; user: any }) => {
const cache = get(decrypted);
try {
let { content, pubkey, id } = event;
@@ -120,7 +112,7 @@ export const encryptNsec = async (nsec: string) => {
return nip49encrypt(d, await getPassword());
};
-export const sign = async (event, user) => {
+export const sign = async (event: any, user?: any) => {
if (user?.nsec && !get($signer)?.ready) {
const sk = await getPrivateKey(user);
const { getPublicKey } = await loadNostrTools();
@@ -162,8 +154,8 @@ const getPassword = async (): Promise<string> => {
export const nostrConnectRelay = "wss://relay.nsec.app";
-const signingMethods = {
- async connect(event, { sk, pk, pubkey }) {
+const signingMethods: Record<string, (event: any, params: any) => Promise<any>> = {
+ async connect(event: any, { sk, pk, pubkey }: any) {
const id = crypto.randomUUID();
const signEvent = {
id,
@@ -214,16 +206,16 @@ const signingMethods = {
});
},
- async nsec(event, { sk }) {
+ async nsec(event: any, { sk }: any) {
const { finalizeEvent } = await loadNostrTools();
return finalizeEvent(event, sk);
},
- async extension(event) {
- return window.nostr.signEvent(event);
+ async extension(event: any) {
+ return window.nostr!.signEvent(event);
},
- async signer(event, { pubkey, sig }) {
+ async signer(event: any, { pubkey, sig }: any) {
event.pubkey = pubkey;
const { getEventHash } = await loadNostrTools();
const signedEvent = {
@@ -236,8 +228,8 @@ const signingMethods = {
};
// Returns all the keys mentioned in the provided event's 'p' tags.
-export const pTagKeys = (event: object): string[] => {
- let keys = [];
+export const pTagKeys = (event: any): string[] => {
+ let keys: string[] = [];
for (const tag of event.tags) {
if (tag[0] === "p") {
keys.push(tag[1]);