From 85ae10932917fdfc2626cb0883449d0db7bdab6e Mon Sep 17 00:00:00 2001 From: Believethehype Date: Thu, 15 Feb 2024 09:22:38 +0100 Subject: [PATCH] update amber to work with latest nostr-tools --- ui/noogle/index.html | 2 +- ui/noogle/package.json | 2 +- .../android-signer/AndroidSigner.ts | 48 ++++++++++++++----- .../android-signer/helpers/nip19.ts | 4 ++ 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/ui/noogle/index.html b/ui/noogle/index.html index 08d9647..b64a870 100644 --- a/ui/noogle/index.html +++ b/ui/noogle/index.html @@ -4,7 +4,7 @@ - Nooooooooogle + Nostr decentralized search and other stuff
diff --git a/ui/noogle/package.json b/ui/noogle/package.json index 3d88978..fc9d5e1 100644 --- a/ui/noogle/package.json +++ b/ui/noogle/package.json @@ -19,7 +19,7 @@ "daisyui": "^4.6.0", "mini-toastr": "^0.8.1", "nostr-login": "^1.0.11", - "nostr-tools": "^1.17.0", + "nostr-tools": "^2.1.9", "vue": "^3.4.15", "vue-notifications": "^1.0.2", "vue3-easy-data-table": "^1.5.47", diff --git a/ui/noogle/src/components/android-signer/AndroidSigner.ts b/ui/noogle/src/components/android-signer/AndroidSigner.ts index 8127505..f8a8fe9 100644 --- a/ui/noogle/src/components/android-signer/AndroidSigner.ts +++ b/ui/noogle/src/components/android-signer/AndroidSigner.ts @@ -1,19 +1,28 @@ // taken from https://github.com/hzrd149/nostrudel -import { nip19, verifySignature } from "nostr-tools"; +import { getEventHash, nip19, verifyEvent } from "nostr-tools"; import createDefer, { Deferred } from "./classes/deffered"; -import { getPubkeyFromDecodeResult, isHexKey } from "./helpers/nip19"; -import { NostrEvent } from "./types/nostr-event"; +import { getPubkeyFromDecodeResult, isHex, isHexKey } from "./helpers/nip19"; +import { DraftNostrEvent, NostrEvent } from "./types/nostr-event"; export function createGetPublicKeyIntent() { return `intent:#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=get_public_key;end`; } -export function createSignEventIntent(draft) { +export function createSignEventIntent(draft: DraftNostrEvent) { return `intent:${encodeURIComponent( JSON.stringify(draft), - )}#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=event;S.type=sign_event;end`; + )}#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=sign_event;end`; +} +export function createNip04EncryptIntent(pubkey: string, plainText: string) { + return `intent:${encodeURIComponent( + plainText, + )}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip04_encrypt;end`; +} +export function createNip04DecryptIntent(pubkey: string, data: string) { + return `intent:${encodeURIComponent( + data, + )}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip04_decrypt;end`; } - let pendingRequest: Deferred | null = null; function rejectPending() { @@ -63,18 +72,31 @@ async function getPublicKey() { throw new Error("Expected clipboard to have pubkey"); } -async function signEvent(draft): Promise { - const signedEventJson = await intentRequest(createSignEventIntent(draft)); - const signedEvent = JSON.parse(signedEventJson) as NostrEvent; - - if (!verifySignature(signedEvent)) throw new Error("Invalid signature"); - return signedEvent; +async function signEvent(draft: DraftNostrEvent & { pubkey: string }): Promise { + const draftWithId = { ...draft, id: draft.id || getEventHash(draft) }; + const sig = await intentRequest(createSignEventIntent(draftWithId)); + if (!isHex(sig)) throw new Error("Expected hex signature"); + + const event: NostrEvent = { ...draftWithId, sig }; + if (!verifyEvent(event)) throw new Error("Invalid signature"); + return event; +} + +async function nip04Encrypt(pubkey: string, plaintext: string): Promise { + const data = await intentRequest(createNip04EncryptIntent(pubkey, plaintext)); + return data; +} +async function nip04Decrypt(pubkey: string, data: string): Promise { + const plaintext = await intentRequest(createNip04DecryptIntent(pubkey, data)); + return plaintext; } const amberSignerService = { supported: navigator.userAgent.includes("Android") && navigator.clipboard, getPublicKey, - signEvent + signEvent, + nip04Encrypt, + nip04Decrypt, }; export default amberSignerService; diff --git a/ui/noogle/src/components/android-signer/helpers/nip19.ts b/ui/noogle/src/components/android-signer/helpers/nip19.ts index 395abb6..d3750e5 100644 --- a/ui/noogle/src/components/android-signer/helpers/nip19.ts +++ b/ui/noogle/src/components/android-signer/helpers/nip19.ts @@ -4,6 +4,10 @@ export function isHexKey(key?: string) { if (key?.toLowerCase()?.match(/^[0-9a-f]{64}$/)) return true; return false; } +export function isHex(str?: string) { + if (str?.match(/^[0-9a-f]+$/i)) return true; + return false; +} export function getPubkeyFromDecodeResult(result?: nip19.DecodeResult) { if (!result) return;