mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-28 18:41:43 +01:00
update amber to work with latest nostr-tools
This commit is contained in:
parent
af2f4cd8e3
commit
85ae109329
@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<link rel="icon" href="/favicon.ico">
|
<link rel="icon" href="/favicon.ico">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Nooooooooogle</title>
|
<title>Nostr decentralized search and other stuff</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
"daisyui": "^4.6.0",
|
"daisyui": "^4.6.0",
|
||||||
"mini-toastr": "^0.8.1",
|
"mini-toastr": "^0.8.1",
|
||||||
"nostr-login": "^1.0.11",
|
"nostr-login": "^1.0.11",
|
||||||
"nostr-tools": "^1.17.0",
|
"nostr-tools": "^2.1.9",
|
||||||
"vue": "^3.4.15",
|
"vue": "^3.4.15",
|
||||||
"vue-notifications": "^1.0.2",
|
"vue-notifications": "^1.0.2",
|
||||||
"vue3-easy-data-table": "^1.5.47",
|
"vue3-easy-data-table": "^1.5.47",
|
||||||
|
@ -1,19 +1,28 @@
|
|||||||
// taken from https://github.com/hzrd149/nostrudel
|
// 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 createDefer, { Deferred } from "./classes/deffered";
|
||||||
import { getPubkeyFromDecodeResult, isHexKey } from "./helpers/nip19";
|
import { getPubkeyFromDecodeResult, isHex, isHexKey } from "./helpers/nip19";
|
||||||
import { NostrEvent } from "./types/nostr-event";
|
import { DraftNostrEvent, NostrEvent } from "./types/nostr-event";
|
||||||
|
|
||||||
export function createGetPublicKeyIntent() {
|
export function createGetPublicKeyIntent() {
|
||||||
return `intent:#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=get_public_key;end`;
|
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(
|
return `intent:${encodeURIComponent(
|
||||||
JSON.stringify(draft),
|
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;
|
let pendingRequest: Deferred<string> | null = null;
|
||||||
|
|
||||||
function rejectPending() {
|
function rejectPending() {
|
||||||
@ -63,18 +72,31 @@ async function getPublicKey() {
|
|||||||
throw new Error("Expected clipboard to have pubkey");
|
throw new Error("Expected clipboard to have pubkey");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function signEvent(draft): Promise<NostrEvent> {
|
async function signEvent(draft: DraftNostrEvent & { pubkey: string }): Promise<NostrEvent> {
|
||||||
const signedEventJson = await intentRequest(createSignEventIntent(draft));
|
const draftWithId = { ...draft, id: draft.id || getEventHash(draft) };
|
||||||
const signedEvent = JSON.parse(signedEventJson) as NostrEvent;
|
const sig = await intentRequest(createSignEventIntent(draftWithId));
|
||||||
|
if (!isHex(sig)) throw new Error("Expected hex signature");
|
||||||
if (!verifySignature(signedEvent)) throw new Error("Invalid signature");
|
|
||||||
return signedEvent;
|
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 = {
|
const amberSignerService = {
|
||||||
supported: navigator.userAgent.includes("Android") && navigator.clipboard,
|
supported: navigator.userAgent.includes("Android") && navigator.clipboard,
|
||||||
getPublicKey,
|
getPublicKey,
|
||||||
signEvent
|
signEvent,
|
||||||
|
nip04Encrypt,
|
||||||
|
nip04Decrypt,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default amberSignerService;
|
export default amberSignerService;
|
||||||
|
@ -4,6 +4,10 @@ export function isHexKey(key?: string) {
|
|||||||
if (key?.toLowerCase()?.match(/^[0-9a-f]{64}$/)) return true;
|
if (key?.toLowerCase()?.match(/^[0-9a-f]{64}$/)) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
export function isHex(str?: string) {
|
||||||
|
if (str?.match(/^[0-9a-f]+$/i)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
export function getPubkeyFromDecodeResult(result?: nip19.DecodeResult) {
|
export function getPubkeyFromDecodeResult(result?: nip19.DecodeResult) {
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user