update amber to work with latest nostr-tools

This commit is contained in:
Believethehype 2024-02-15 09:22:38 +01:00
parent af2f4cd8e3
commit 85ae109329
4 changed files with 41 additions and 15 deletions

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nooooooooogle</title>
<title>Nostr decentralized search and other stuff</title>
</head>
<body>
<div id="app"></div>

View File

@ -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",

View File

@ -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<string> | null = null;
function rejectPending() {
@ -63,18 +72,31 @@ async function getPublicKey() {
throw new Error("Expected clipboard to have pubkey");
}
async function signEvent(draft): Promise<NostrEvent> {
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<NostrEvent> {
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<string> {
const data = await intentRequest(createNip04EncryptIntent(pubkey, plaintext));
return data;
}
async function nip04Decrypt(pubkey: string, data: string): Promise<string> {
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;

View File

@ -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;