tests
This commit is contained in:
parent
31b0538337
commit
3ffe4d5b19
@ -4,8 +4,9 @@
|
||||
"main": "dist/lib.js",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
"watch": "webpack -w",
|
||||
"build": "rm -rf dist && webpack",
|
||||
"watch": "rm -rf dist && webpack -w",
|
||||
"clean": "rm -rf dist",
|
||||
"test": "ts-mocha --type-check -j 1 --timeout 5s test/test.*.ts",
|
||||
"test-browser": "ts-node test/browser/server.ts",
|
||||
"lint": "eslint ."
|
||||
|
@ -139,7 +139,19 @@ export async function signEvent<T extends RawEvent>(
|
||||
if (typeof window === "undefined" || window.nostr === undefined) {
|
||||
throw new NostrError("no private key provided")
|
||||
}
|
||||
return await window.nostr.signEvent(event)
|
||||
// Extensions like nos2x expect to receive only the event data, without any of the methods.
|
||||
const methods: { [key: string]: unknown } = {}
|
||||
for (const [key, value] of Object.entries(event)) {
|
||||
if (typeof value === "function") {
|
||||
methods[key] = value
|
||||
delete event[key]
|
||||
}
|
||||
}
|
||||
const signed = await window.nostr.signEvent(event)
|
||||
return {
|
||||
...signed,
|
||||
...methods,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ declare global {
|
||||
getPublicKey: () => Promise<PublicKey>
|
||||
signEvent: <T extends RawEvent>(event: Unsigned<T>) => Promise<T>
|
||||
|
||||
// TODO It's unclear to me if I should use this anywhere.
|
||||
getRelays?: () => Promise<{
|
||||
[url: string]: { read: boolean; write: boolean }
|
||||
}>
|
||||
|
@ -7,6 +7,21 @@
|
||||
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div style="margin-left: 5px; margin-top: 15px">
|
||||
<a style="color: var(--mocha-color)" id="window.nostr"></a>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const a = document.getElementById("window.nostr")
|
||||
if (window.location.pathname === "/nostr-object") {
|
||||
a.href = "/"
|
||||
a.innerHTML = "disable window.nostr"
|
||||
} else {
|
||||
a.href = "/nostr-object"
|
||||
a.innerHTML = "enable window.nostr"
|
||||
}
|
||||
</script>
|
||||
|
||||
<div id="mocha"></div>
|
||||
|
||||
<script src="https://unpkg.com/mocha/mocha.js"></script>
|
||||
@ -15,6 +30,7 @@
|
||||
mocha.setup({
|
||||
ui: "bdd",
|
||||
timeout: "5s",
|
||||
global: ["nostr"],
|
||||
})
|
||||
mocha.checkLeaks()
|
||||
</script>
|
||||
|
@ -10,7 +10,7 @@ const port = 33543
|
||||
const app = express()
|
||||
|
||||
app.use("/", (req: express.Request, res: express.Response) => {
|
||||
if (req.path === "/") {
|
||||
if (req.path === "/" || req.path === "/nostr-object") {
|
||||
const index = fs.readFileSync(path.join(__dirname, "index.html"), {
|
||||
encoding: "utf8",
|
||||
})
|
||||
|
@ -1,11 +1,21 @@
|
||||
import "../src/nostr-object"
|
||||
import { Nostr } from "../src/client"
|
||||
import { Timestamp, unixTimestamp } from "../src/common"
|
||||
import {
|
||||
aesDecryptBase64,
|
||||
aesEncryptBase64,
|
||||
parsePrivateKey,
|
||||
parsePublicKey,
|
||||
PublicKey,
|
||||
} from "../src/crypto"
|
||||
import { RawEvent } from "../src"
|
||||
import { signEvent, Unsigned } from "../src/event"
|
||||
|
||||
export const relayUrl = new URL("ws://localhost:12648")
|
||||
|
||||
export interface Setup {
|
||||
publisher: Nostr
|
||||
publisherSecret: string
|
||||
publisherSecret?: string
|
||||
publisherPubkey: string
|
||||
subscriber: Nostr
|
||||
subscriberSecret: string
|
||||
@ -25,6 +35,50 @@ export async function setup(
|
||||
) {
|
||||
try {
|
||||
await restartRelay()
|
||||
|
||||
const publisherPubkey =
|
||||
"npub1he978sxy7tgc7yfp2zra05v045kfuqnfl3gwr82jd00mzxjj9fjqzw2dg7"
|
||||
const publisherSecret =
|
||||
"nsec15fnff4uxlgyu79ua3l7327w0wstrd6x565cx6zze78zgkktmr8vs90j363"
|
||||
|
||||
// Set up the global window.nostr object for the publisher.
|
||||
if (typeof window !== "undefined") {
|
||||
if (window.location.pathname === "/nostr-object") {
|
||||
window.nostr = {
|
||||
getPublicKey: () => Promise.resolve(parsePublicKey(publisherPubkey)),
|
||||
signEvent: <T extends RawEvent>(event: Unsigned<T>) =>
|
||||
signEvent(event, publisherSecret),
|
||||
|
||||
getRelays: () => Promise.resolve({}),
|
||||
|
||||
nip04: {
|
||||
encrypt: async (pubkey: PublicKey, plaintext: string) => {
|
||||
const { data, iv } = await aesEncryptBase64(
|
||||
parsePrivateKey(publisherSecret),
|
||||
pubkey,
|
||||
plaintext
|
||||
)
|
||||
return `${data}?iv=${iv}`
|
||||
},
|
||||
decrypt: async (pubkey: PublicKey, ciphertext: string) => {
|
||||
const [data, iv] = ciphertext.split("?iv=")
|
||||
return await aesDecryptBase64(
|
||||
pubkey,
|
||||
parsePrivateKey(publisherSecret),
|
||||
{
|
||||
data,
|
||||
iv,
|
||||
}
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
// Otherwise, disable the user's nostr extension if they have one.
|
||||
window.nostr = undefined
|
||||
}
|
||||
}
|
||||
|
||||
const publisher = new Nostr()
|
||||
const subscriber = new Nostr()
|
||||
|
||||
@ -44,9 +98,10 @@ export async function setup(
|
||||
const result = test({
|
||||
publisher,
|
||||
publisherSecret:
|
||||
"nsec15fnff4uxlgyu79ua3l7327w0wstrd6x565cx6zze78zgkktmr8vs90j363",
|
||||
publisherPubkey:
|
||||
"npub1he978sxy7tgc7yfp2zra05v045kfuqnfl3gwr82jd00mzxjj9fjqzw2dg7",
|
||||
typeof window === "undefined" || window.nostr === undefined
|
||||
? publisherSecret
|
||||
: undefined,
|
||||
publisherPubkey,
|
||||
subscriber,
|
||||
subscriberSecret:
|
||||
"nsec1fxvlyqn3rugvxwaz6dr5h8jcfn0fe0lxyp7pl4mgntxfzqr7dmgst7z9ps",
|
||||
|
@ -4,7 +4,7 @@ import { parsePublicKey } from "../src/crypto"
|
||||
import { setup } from "./setup"
|
||||
import { createDirectMessage } from "../src/event/direct-message"
|
||||
|
||||
describe("dm", () => {
|
||||
describe("direct-message", () => {
|
||||
const message = "for your eyes only"
|
||||
|
||||
// Test that the intended recipient can receive and decrypt the direct message.
|
Loading…
x
Reference in New Issue
Block a user