mirror of
https://github.com/dergigi/boris.git
synced 2025-12-19 15:44:20 +01:00
- Add project structure with TypeScript, React, and Vite - Implement nostr authentication using browser extension (NIP-07) - Add NIP-51 compliant bookmark fetching and display - Create minimal UI with login and bookmark components - Integrate applesauce-core and applesauce-react libraries - Add responsive styling with dark/light mode support - Include comprehensive README with setup instructions This is a minimal MVP for a nostr bookmark client that allows users to view their bookmarks according to NIP-51 specification.
41 lines
1.5 KiB
JavaScript
41 lines
1.5 KiB
JavaScript
// nip04.ts
|
|
import { bytesToHex as bytesToHex2, randomBytes } from "@noble/hashes/utils";
|
|
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
import { cbc } from "@noble/ciphers/aes";
|
|
import { base64 } from "@scure/base";
|
|
|
|
// utils.ts
|
|
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
|
|
var utf8Decoder = new TextDecoder("utf-8");
|
|
var utf8Encoder = new TextEncoder();
|
|
|
|
// nip04.ts
|
|
function encrypt(secretKey, pubkey, text) {
|
|
const privkey = secretKey instanceof Uint8Array ? bytesToHex2(secretKey) : secretKey;
|
|
const key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
|
|
const normalizedKey = getNormalizedX(key);
|
|
let iv = Uint8Array.from(randomBytes(16));
|
|
let plaintext = utf8Encoder.encode(text);
|
|
let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext);
|
|
let ctb64 = base64.encode(new Uint8Array(ciphertext));
|
|
let ivb64 = base64.encode(new Uint8Array(iv.buffer));
|
|
return `${ctb64}?iv=${ivb64}`;
|
|
}
|
|
function decrypt(secretKey, pubkey, data) {
|
|
const privkey = secretKey instanceof Uint8Array ? bytesToHex2(secretKey) : secretKey;
|
|
let [ctb64, ivb64] = data.split("?iv=");
|
|
let key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
|
|
let normalizedKey = getNormalizedX(key);
|
|
let iv = base64.decode(ivb64);
|
|
let ciphertext = base64.decode(ctb64);
|
|
let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext);
|
|
return utf8Decoder.decode(plaintext);
|
|
}
|
|
function getNormalizedX(key) {
|
|
return key.slice(1, 33);
|
|
}
|
|
export {
|
|
decrypt,
|
|
encrypt
|
|
};
|