feat: appdata

This commit is contained in:
2023-11-13 16:51:29 +00:00
parent 540f29dd69
commit 24978f4e62
29 changed files with 181 additions and 136 deletions

View File

@ -330,6 +330,13 @@ export class EventPublisher {
return await this.#sign(eb);
}
async appData(data: object, id: string) {
const eb = this.#eb(EventKind.AppData);
eb.content(await this.nip4Encrypt(JSON.stringify(data), this.#pubKey));
eb.tag(["d", id]);
return await this.#sign(eb);
}
/**
* NIP-59 Gift Wrap event with ephemeral key
*/

View File

@ -139,21 +139,28 @@ export interface MessageEncryptor {
decryptData(payload: MessageEncryptorPayload, sharedSecet: Uint8Array): Promise<string> | string;
}
export function decodeEncryptionPayload(p: 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),
} as MessageEncryptorPayload;
};
} 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),
} as MessageEncryptorPayload;
};
}
}