commit d6baf6627adbbee5bc59582495ed277515a22feb
parent 950c8c372cbb8b214929b4fe2e4f1b6e2e133238
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Thu, 24 Oct 2024 12:12:59 +0000
client: add iclientlogger. edit http, keystore, nostr, os apis. update packages
Diffstat:
11 files changed, 71 insertions(+), 22 deletions(-)
diff --git a/client/package.json b/client/package.json
@@ -15,14 +15,15 @@
"@radroots/models": "workspace:*",
"@radroots/tauri-plugin-map-display": "workspace:*",
"@radroots/utils": "workspace:*",
- "@tauri-apps/api": "2.0.0-rc.5",
- "@tauri-apps/plugin-dialog": "^2.0.0-rc",
- "@tauri-apps/plugin-geolocation": "^2.0.0-rc",
- "@tauri-apps/plugin-haptics": "^2.0.0-rc",
- "@tauri-apps/plugin-http": "^2.0.0-rc",
- "@tauri-apps/plugin-notification": "^2.0.0-rc",
- "@tauri-apps/plugin-os": "^2.0.0-rc",
- "@tauri-apps/plugin-store": "^2.0.0-rc",
+ "@tauri-apps/api": "2.0.3",
+ "@tauri-apps/plugin-dialog": "^2.0.1",
+ "@tauri-apps/plugin-geolocation": "^2.0.0",
+ "@tauri-apps/plugin-haptics": "^2.0.0",
+ "@tauri-apps/plugin-http": "^2.0.1",
+ "@tauri-apps/plugin-log": "^2.0.0",
+ "@tauri-apps/plugin-notification": "^2.0.0",
+ "@tauri-apps/plugin-os": "^2.0.0",
+ "@tauri-apps/plugin-store": "^2.1.0",
"nostr-tools": "^2.7.2"
},
"devDependencies": {
diff --git a/client/src/http/tauri.ts b/client/src/http/tauri.ts
@@ -1,6 +1,6 @@
import { err_msg, type ErrorMessage, type FieldRecord } from '@radroots/utils';
import { type ClientOptions, fetch } from '@tauri-apps/plugin-http';
-import { IClientDeviceMetadata } from '../device/types';
+import type { IClientDeviceMetadata } from '../device/types';
import type { IClientHttp, IClientHttpOpts, IClientHttpResponse } from './types';
const parse_headers = (headers: Headers): FieldRecord => {
diff --git a/client/src/index.ts b/client/src/index.ts
@@ -15,6 +15,8 @@ export * from "./keyring/tauri"
export * from "./keyring/types"
export * from "./keystore/tauri"
export * from "./keystore/types"
+export * from "./logger/tauri"
+export * from "./logger/types"
export * from "./map/tauri"
export * from "./map/types"
export * from "./nostr/client"
diff --git a/client/src/keystore/tauri.ts b/client/src/keystore/tauri.ts
@@ -1,17 +1,18 @@
-import { err_msg, ResultObj, ResultPass, ResultsList, type ErrorMessage } from '@radroots/utils';
-import { createStore, Store } from '@tauri-apps/plugin-store';
-import type { IClientKeystore, IClientKeystoreUnlisten } from './types';
+import { err_msg, type ErrorMessage, type ResultObj, type ResultPass, type ResultsList } from '@radroots/utils';
+import { load, Store } from '@tauri-apps/plugin-store';
+import type { IClientUnlisten } from '../types';
+import type { IClientKeystore } from './types';
export class TauriClientKeystore implements IClientKeystore {
- private _store: Store | undefined = undefined;
+ private _store: Store | null = null;
private _store_path: string;
- constructor(store_path: string = 'store.bin') {
+ constructor(store_path: string = 'store.json') {
this._store_path = store_path;
}
public async init(): Promise<void> {
- this._store = await createStore(this._store_path);
+ this._store = await load(this._store_path);
}
public async set(key: string, value: string): Promise<ResultPass | ErrorMessage<string>> {
@@ -68,7 +69,7 @@ export class TauriClientKeystore implements IClientKeystore {
};
}
- public async on_key_change(key: string, callback: (value: string | null) => Promise<void>): Promise<IClientKeystoreUnlisten | ErrorMessage<string>> {
+ public async on_key_change(key: string, callback: (value: string | null) => Promise<void>): Promise<IClientUnlisten | ErrorMessage<string>> {
try {
if (!this._store) return err_msg(`*-store`);
const res = await this._store.onKeyChange<{ value: any }>(key, async (res) => await callback(res && `value` in res ? String(res.value) : null));
diff --git a/client/src/keystore/types.ts b/client/src/keystore/types.ts
@@ -1,14 +1,12 @@
import type { ErrorMessage, ResultObj, ResultPass, ResultsList } from "@radroots/utils";
-import type { UnlistenFn } from "@tauri-apps/api/event";
-
-export type IClientKeystoreUnlisten = UnlistenFn;
+import type { IClientUnlisten } from "../types";
export type IClientKeystore = {
- init: () => Promise<void>;
+ init(): Promise<void>;
set(key: string, val: string): Promise<ResultPass | ErrorMessage<string>>;
get(key: string): Promise<ResultObj<string> | ErrorMessage<string>>;
keys(): Promise<ResultsList<string> | ErrorMessage<string>>;
entries(): Promise<ResultsList<[string, unknown]> | ErrorMessage<string>>
remove(key: string): Promise<ResultPass | ErrorMessage<string>>;
- on_key_change(key: string, callback: (value: string | null) => Promise<void>): Promise<IClientKeystoreUnlisten | ErrorMessage<string>>;
+ on_key_change(key: string, callback: (value: string | null) => Promise<void>): Promise<IClientUnlisten | ErrorMessage<string>>;
};
\ No newline at end of file
diff --git a/client/src/logger/tauri.ts b/client/src/logger/tauri.ts
@@ -0,0 +1,9 @@
+import { attachConsole } from '@tauri-apps/plugin-log';
+import type { IClientUnlisten } from '../types';
+import type { IClientLogger } from "./types";
+
+export class TauriClientLogger implements IClientLogger {
+ public async init(): Promise<IClientUnlisten> {
+ return await attachConsole();
+ }
+}
diff --git a/client/src/logger/types.ts b/client/src/logger/types.ts
@@ -0,0 +1,5 @@
+import type { IClientUnlisten } from "../types";
+
+export type IClientLogger = {
+ init(): Promise<IClientUnlisten>;
+};
+\ No newline at end of file
diff --git a/client/src/nostr/lib.ts b/client/src/nostr/lib.ts
@@ -57,6 +57,21 @@ export class ClientNostrLib implements IClientNostrLib {
/**
*
+ * @returns nostr secret key to public key hex
+ */
+ public publickey_decode(secret_key_hex?: string): string | undefined {
+ try {
+ if (secret_key_hex) {
+ return getPublicKey(this.get_key_bytes(secret_key_hex))
+ }
+ return undefined;
+ } catch (e) {
+ return undefined;
+ }
+ }
+
+ /**
+ *
* @returns nostr public key npub
*/
public npub(public_key_hex: string | undefined, fallback_to_hex?: boolean): string {
@@ -122,4 +137,17 @@ export class ClientNostrLib implements IClientNostrLib {
if (decode && decode.type === `nprofile` && decode.data && decode.data.pubkey && decode.data.relays) return [decode.data.pubkey, decode.data.relays]
return undefined;
}
+
+ /**
+ *
+ * @returns nostr public key nprofile
+ */
+ public secretkey_to_publickey(nsec_or_hex: string): string | undefined {
+ if (nsec_or_hex.startsWith(`nsec1`)) {
+ return this.nsec_decode(nsec_or_hex);
+ } else if (nsec_or_hex.length === 64) {
+ return this.publickey_decode(nsec_or_hex)
+ }
+ return undefined;
+ }
};
diff --git a/client/src/nostr/types.ts b/client/src/nostr/types.ts
@@ -19,6 +19,7 @@ export type IClientNostrLib = {
nsec_decode(nsec: string): string | undefined;
nprofile(public_key_hex: string, relays: string[]): string;
nprofile_decode(nprofile: string): [string, string[]] | undefined;
+ secretkey_to_publickey(nsec_or_hex: string): string | undefined;
};
export type IClientNostr = {
diff --git a/client/src/os/tauri.ts b/client/src/os/tauri.ts
@@ -1,4 +1,4 @@
-import { err_msg, ErrorMessage, ResultObj } from '@radroots/utils';
+import { err_msg, type ErrorMessage, type ResultObj } from '@radroots/utils';
import { arch, hostname, platform, version } from '@tauri-apps/plugin-os';
import type { IClientOs } from "./types";
diff --git a/client/src/types.ts b/client/src/types.ts
@@ -1 +1,4 @@
+import type { UnlistenFn } from "@tauri-apps/api/event";
+
export type IClientPlatform = `androiď` | `ios` | `web`;
+export type IClientUnlisten = UnlistenFn;