Muted words: phase 1
This commit is contained in:
@ -3,7 +3,7 @@ import { getPublicKey } from "@snort/shared";
|
||||
import { EventExt } from "./event-ext";
|
||||
import { Nip4WebCryptoEncryptor } from "./impl/nip4";
|
||||
import { XChaCha20Encryptor } from "./impl/nip44";
|
||||
import { MessageEncryptorPayload, MessageEncryptorVersion } from "./index";
|
||||
import { MessageEncryptorVersion, decodeEncryptionPayload, encodeEncryptionPayload } from "./index";
|
||||
import { NostrEvent } from "./nostr";
|
||||
import { base64 } from "@scure/base";
|
||||
|
||||
@ -74,11 +74,11 @@ export class PrivateKeySigner implements EventSigner {
|
||||
const enc = new XChaCha20Encryptor();
|
||||
const shared = enc.getSharedSecret(this.#privateKey, key);
|
||||
const data = enc.encryptData(content, shared);
|
||||
return this.#encodePayload(data);
|
||||
return encodeEncryptionPayload(data);
|
||||
}
|
||||
|
||||
async nip44Decrypt(content: string, otherKey: string) {
|
||||
const payload = this.#decodePayload(content);
|
||||
const payload = decodeEncryptionPayload(content);
|
||||
if (payload.v !== MessageEncryptorVersion.XChaCha20) throw new Error("Invalid payload version");
|
||||
|
||||
const enc = new XChaCha20Encryptor();
|
||||
@ -86,28 +86,6 @@ export class PrivateKeySigner implements EventSigner {
|
||||
return enc.decryptData(payload, shared);
|
||||
}
|
||||
|
||||
#decodePayload(p: string) {
|
||||
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),
|
||||
} as MessageEncryptorPayload;
|
||||
} else {
|
||||
const buf = base64.decode(p);
|
||||
return {
|
||||
v: buf[0],
|
||||
nonce: buf.subarray(1, 25),
|
||||
ciphertext: buf.subarray(25),
|
||||
} as MessageEncryptorPayload;
|
||||
}
|
||||
}
|
||||
|
||||
#encodePayload(p: MessageEncryptorPayload) {
|
||||
return base64.encode(new Uint8Array([p.v, ...p.nonce, ...p.ciphertext]));
|
||||
}
|
||||
|
||||
sign(ev: NostrEvent): Promise<NostrEvent> {
|
||||
EventExt.sign(ev, this.#privateKey);
|
||||
return Promise.resolve(ev);
|
||||
|
Reference in New Issue
Block a user