2023-10-21 11:29:47 +00:00
|
|
|
import { assertEquals, fail } from "https://deno.land/std@0.176.0/testing/asserts.ts";
|
2023-06-30 14:05:57 +00:00
|
|
|
import {
|
|
|
|
blobToBase64,
|
|
|
|
decryptNostrEvent,
|
|
|
|
InMemoryAccountContext,
|
|
|
|
NostrEvent,
|
|
|
|
NostrKind,
|
2023-08-28 17:58:05 +00:00
|
|
|
} from "./lib/nostr-ts/nostr.ts";
|
2023-10-21 11:29:47 +00:00
|
|
|
import { prepareNostrImageEvent, prepareReplyEvent } from "./nostr.ts";
|
|
|
|
import { PrivateKey } from "./lib/nostr-ts/key.ts";
|
2023-08-28 17:58:05 +00:00
|
|
|
import { utf8Decode } from "./lib/nostr-ts/ende.ts";
|
2023-09-06 21:04:25 +00:00
|
|
|
import { prepareNormalNostrEvent } from "./lib/nostr-ts/event.ts";
|
2023-06-30 14:05:57 +00:00
|
|
|
|
2023-09-11 20:40:56 +00:00
|
|
|
Deno.test("prepareNostrImageEvent", async (t) => {
|
2023-06-30 14:05:57 +00:00
|
|
|
const pri = PrivateKey.Generate();
|
2023-07-11 09:19:57 +00:00
|
|
|
const pub = pri.toPublicKey();
|
2023-09-11 20:40:56 +00:00
|
|
|
const ctx = InMemoryAccountContext.New(pri);
|
2023-06-30 14:05:57 +00:00
|
|
|
|
2023-09-11 20:40:56 +00:00
|
|
|
let randomData = new Uint8Array(1024 * 32); // 48KB raw data
|
2023-06-30 14:05:57 +00:00
|
|
|
for (let i = 0; i < randomData.length; i++) {
|
|
|
|
randomData.fill(Math.floor(Math.random() * 128), i); // https://en.wikipedia.org/wiki/UTF-8
|
|
|
|
}
|
|
|
|
let randomStr = utf8Decode(randomData);
|
|
|
|
|
|
|
|
const blob = new Blob([randomStr]);
|
2023-10-21 11:29:47 +00:00
|
|
|
const imgEvent = await prepareNostrImageEvent(
|
2023-09-11 20:40:56 +00:00
|
|
|
ctx,
|
2023-06-30 14:05:57 +00:00
|
|
|
pub,
|
|
|
|
blob,
|
|
|
|
NostrKind.DIRECT_MESSAGE,
|
|
|
|
);
|
2023-10-21 11:29:47 +00:00
|
|
|
if (imgEvent instanceof Error) {
|
|
|
|
fail(imgEvent.message);
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|
2023-10-21 11:29:47 +00:00
|
|
|
|
2023-06-30 14:05:57 +00:00
|
|
|
await t.step("full", async () => {
|
|
|
|
const decryptedEvents = [];
|
2023-10-21 11:29:47 +00:00
|
|
|
const decryptedEvent = await decryptNostrEvent(imgEvent, ctx, pub.hex);
|
2023-09-11 20:40:56 +00:00
|
|
|
if (decryptedEvent instanceof Error) {
|
|
|
|
fail(decryptedEvent.message);
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|
2023-09-11 20:40:56 +00:00
|
|
|
decryptedEvents.push(decryptedEvent);
|
2023-10-21 11:29:47 +00:00
|
|
|
assertEquals(await blobToBase64(blob), decryptedEvent.content);
|
2023-06-30 14:05:57 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Generate reply event", async () => {
|
|
|
|
const userAPrivateKey = PrivateKey.Generate();
|
|
|
|
const userAContext = InMemoryAccountContext.New(userAPrivateKey);
|
|
|
|
|
|
|
|
const message1 = await prepareNormalNostrEvent(
|
|
|
|
userAContext,
|
2023-10-26 08:42:04 +00:00
|
|
|
{
|
|
|
|
kind: NostrKind.DIRECT_MESSAGE,
|
|
|
|
|
|
|
|
content: "text message 1",
|
|
|
|
},
|
2023-06-30 14:05:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const replyMessage1WithText = await prepareReplyEvent(
|
|
|
|
userAContext,
|
|
|
|
message1,
|
|
|
|
[],
|
|
|
|
"aaaa",
|
|
|
|
) as NostrEvent;
|
|
|
|
|
|
|
|
assertEquals(replyMessage1WithText.kind, message1.kind);
|
2023-07-01 14:51:28 +00:00
|
|
|
assertEquals(replyMessage1WithText.pubkey, userAPrivateKey.toPublicKey().hex);
|
2023-06-30 14:05:57 +00:00
|
|
|
assertEquals(replyMessage1WithText.tags, [[
|
|
|
|
"e",
|
|
|
|
message1.id,
|
|
|
|
"",
|
|
|
|
"reply",
|
|
|
|
]]);
|
|
|
|
});
|