feat: nip44 v2

This commit is contained in:
2024-08-26 15:57:46 +03:00
parent 1ed36352a8
commit 644bd57094
12 changed files with 223 additions and 142 deletions

View File

@ -1,5 +1,3 @@
import { base64 } from "@scure/base";
export { NostrSystem } from "./nostr-system";
export { NDKSystem } from "./ndk-system";
export { default as EventKind } from "./event-kind";
@ -24,12 +22,13 @@ export * from "./text";
export * from "./pow";
export * from "./pow-util";
export * from "./query-optimizer";
export * from "./encrypted";
export * from "./encryption/pin-encrypted";
export * from "./outbox";
export * from "./sync";
export * from "./user-state";
export * from "./cache-relay";
export * from "./connection-cache-relay";
export * from "./encryption";
export * from "./impl/nip4";
export * from "./impl/nip7";
@ -42,49 +41,3 @@ export * from "./cache/index";
export * from "./cache/user-relays";
export * from "./cache/user-metadata";
export * from "./cache/relay-metric";
export const enum MessageEncryptorVersion {
Nip4 = 0,
XChaCha20 = 1,
}
export interface MessageEncryptorPayload {
ciphertext: Uint8Array;
nonce: Uint8Array;
v: MessageEncryptorVersion;
}
export interface MessageEncryptor {
getSharedSecret(privateKey: string, publicKey: string): Promise<Uint8Array> | Uint8Array;
encryptData(plaintext: string, sharedSecet: Uint8Array): Promise<MessageEncryptorPayload> | MessageEncryptorPayload;
decryptData(payload: MessageEncryptorPayload, sharedSecet: Uint8Array): Promise<string> | string;
}
export function decodeEncryptionPayload(p: string): MessageEncryptorPayload {
if (p.startsWith("{") && p.endsWith("}")) {
const pj = JSON.parse(p) as { v: number; nonce: string; ciphertext: string };
return {
v: pj.v,
nonce: base64.decode(pj.nonce),
ciphertext: base64.decode(pj.ciphertext),
};
} else if (p.includes("?iv=")) {
const [ciphertext, nonce] = p.split("?iv=");
return {
v: MessageEncryptorVersion.Nip4,
nonce: base64.decode(nonce),
ciphertext: base64.decode(ciphertext),
};
} else {
const buf = base64.decode(p);
return {
v: buf[0],
nonce: buf.subarray(1, 25),
ciphertext: buf.subarray(25),
};
}
}
export function encodeEncryptionPayload(p: MessageEncryptorPayload) {
return base64.encode(new Uint8Array([p.v, ...p.nonce, ...p.ciphertext]));
}