diff --git a/package.json b/package.json index 4232435..a298049 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nsecbunkerd", - "version": "0.10.5", + "version": "0.10.6", "description": "nsecbunker daemon", "main": "dist/index.js", "bin": { @@ -39,7 +39,7 @@ "@fastify/view": "^8.2.0", "@inquirer/password": "^1.1.2", "@inquirer/prompts": "^1.2.3", - "@nostr-dev-kit/ndk": "workspace:*", + "@nostr-dev-kit/ndk": "^2.18.1", "@prisma/client": "^5.4.1", "@scure/base": "^1.1.1", "@types/yargs": "^17.0.24", @@ -57,7 +57,7 @@ "isomorphic-ws": "^5.0.0", "lnbits": "^1.1.5", "lnbits-ts": "^0.0.2", - "nostr-tools": "^1.17.0", + "nostr-tools": "^2.17.0", "websocket-polyfill": "^0.0.3", "ws": "^8.13.0", "yargs": "^17.7.2" diff --git a/src/daemon/admin/commands/create_account.ts b/src/daemon/admin/commands/create_account.ts index e4632d6..311f0af 100644 --- a/src/daemon/admin/commands/create_account.ts +++ b/src/daemon/admin/commands/create_account.ts @@ -1,6 +1,7 @@ import { Hexpubkey, NDKKind, NDKPrivateKeySigner, NDKRpcRequest, NDKUserProfile } from "@nostr-dev-kit/ndk"; import AdminInterface from ".."; import { nip19 } from 'nostr-tools'; +import { hexToBytes } from "../../../utils/hex.js"; import { setupSkeletonProfile } from "../../lib/profile"; import { IConfig, getCurrentConfig, saveCurrentConfig } from "../../../config"; import { readFileSync, writeFileSync } from "fs"; @@ -195,7 +196,7 @@ export async function createAccountReal( } const keyName = nip05; - const nsec = nip19.nsecEncode(key.privateKey!); + const nsec = nip19.nsecEncode(hexToBytes(key.privateKey!)); currentConfig.keys[keyName] = { key: key.privateKey }; saveCurrentConfig(admin.configFile, currentConfig); diff --git a/src/daemon/admin/commands/create_new_key.ts b/src/daemon/admin/commands/create_new_key.ts index 306962c..0d0fbe9 100644 --- a/src/daemon/admin/commands/create_new_key.ts +++ b/src/daemon/admin/commands/create_new_key.ts @@ -3,6 +3,7 @@ import AdminInterface from "../index.js"; import { saveEncrypted } from "../../../commands/add.js"; import { nip19 } from 'nostr-tools'; import { setupSkeletonProfile } from "../../lib/profile.js"; +import { bytesToHex, hexToBytes } from "../../../utils/hex.js"; export default async function createNewKey(admin: AdminInterface, req: NDKRpcRequest) { const [ keyName, passphrase, _nsec ] = req.params as [ string, string, string? ]; @@ -13,7 +14,7 @@ export default async function createNewKey(admin: AdminInterface, req: NDKRpcReq let key; if (_nsec) { - key = new NDKPrivateKeySigner(nip19.decode(_nsec).data as string); + key = new NDKPrivateKeySigner(bytesToHex(nip19.decode(_nsec).data as Uint8Array)); } else { key = NDKPrivateKeySigner.generate(); @@ -23,7 +24,7 @@ export default async function createNewKey(admin: AdminInterface, req: NDKRpcReq } const user = await key.user(); - const nsec = nip19.nsecEncode(key.privateKey!); + const nsec = nip19.nsecEncode(hexToBytes(key.privateKey!)); await saveEncrypted( admin.configFile, diff --git a/src/daemon/admin/commands/create_new_policy.ts b/src/daemon/admin/commands/create_new_policy.ts index e924c43..a40d1ee 100644 --- a/src/daemon/admin/commands/create_new_policy.ts +++ b/src/daemon/admin/commands/create_new_policy.ts @@ -19,9 +19,9 @@ export default async function createNewPolicy(admin: AdminInterface, req: NDKRpc for (const rule of policy.rules) { await prisma.policyRule.create({ data: { - policyId: policyRecord.id, - kind: rule.kind.toString(), - method: rule.method, + Policy: { connect: { id: policyRecord.id } }, + kind: rule.kind != null ? rule.kind.toString() : null, + method: rule.method ?? "sign_event", maxUsageCount: rule.use_count, currentUsageCount: 0, } diff --git a/src/daemon/run.ts b/src/daemon/run.ts index 262a150..b09b734 100644 --- a/src/daemon/run.ts +++ b/src/daemon/run.ts @@ -1,5 +1,6 @@ import NDK, { NDKPrivateKeySigner, Nip46PermitCallback, Nip46PermitCallbackParams } from '@nostr-dev-kit/ndk'; import { nip19 } from 'nostr-tools'; +import { bytesToHex, hexToBytes } from '../utils/hex.js'; import { Backend } from './backend/index.js'; import { IMethod, @@ -38,7 +39,7 @@ function getKeys(config: DaemonConfig) { const keys: Key[] = []; for (const [name, nsec] of Object.entries(config.keys)) { - const hexpk = nip19.decode(nsec).data as string; + const hexpk = bytesToHex(nip19.decode(nsec).data as Uint8Array); const user = await new NDKPrivateKeySigner(hexpk).user(); const key = { name, @@ -206,7 +207,7 @@ class Daemon { continue; } - const nsec = nip19.nsecEncode(settings.key); + const nsec = nip19.nsecEncode(hexToBytes(settings.key)); this.loadNsec(keyName, nsec); } } diff --git a/src/utils/hex.ts b/src/utils/hex.ts new file mode 100644 index 0000000..eb30612 --- /dev/null +++ b/src/utils/hex.ts @@ -0,0 +1,17 @@ +/** + * Convert Uint8Array to hex string + */ +export function bytesToHex(bytes: Uint8Array): string { + return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join(''); +} + +/** + * Convert hex string to Uint8Array + */ +export function hexToBytes(hex: string): Uint8Array { + const bytes = new Uint8Array(hex.length / 2); + for (let i = 0; i < hex.length; i += 2) { + bytes[i / 2] = parseInt(hex.substr(i, 2), 16); + } + return bytes; +}