tests
This commit is contained in:
parent
31b0538337
commit
3ffe4d5b19
|
@ -4,8 +4,9 @@
|
||||||
"main": "dist/lib.js",
|
"main": "dist/lib.js",
|
||||||
"types": "dist/src/index.d.ts",
|
"types": "dist/src/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "webpack",
|
"build": "rm -rf dist && webpack",
|
||||||
"watch": "webpack -w",
|
"watch": "rm -rf dist && webpack -w",
|
||||||
|
"clean": "rm -rf dist",
|
||||||
"test": "ts-mocha --type-check -j 1 --timeout 5s test/test.*.ts",
|
"test": "ts-mocha --type-check -j 1 --timeout 5s test/test.*.ts",
|
||||||
"test-browser": "ts-node test/browser/server.ts",
|
"test-browser": "ts-node test/browser/server.ts",
|
||||||
"lint": "eslint ."
|
"lint": "eslint ."
|
||||||
|
|
|
@ -139,7 +139,19 @@ export async function signEvent<T extends RawEvent>(
|
||||||
if (typeof window === "undefined" || window.nostr === undefined) {
|
if (typeof window === "undefined" || window.nostr === undefined) {
|
||||||
throw new NostrError("no private key provided")
|
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>
|
getPublicKey: () => Promise<PublicKey>
|
||||||
signEvent: <T extends RawEvent>(event: Unsigned<T>) => Promise<T>
|
signEvent: <T extends RawEvent>(event: Unsigned<T>) => Promise<T>
|
||||||
|
|
||||||
|
// TODO It's unclear to me if I should use this anywhere.
|
||||||
getRelays?: () => Promise<{
|
getRelays?: () => Promise<{
|
||||||
[url: string]: { read: boolean; write: boolean }
|
[url: string]: { read: boolean; write: boolean }
|
||||||
}>
|
}>
|
||||||
|
|
|
@ -7,6 +7,21 @@
|
||||||
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
|
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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>
|
<div id="mocha"></div>
|
||||||
|
|
||||||
<script src="https://unpkg.com/mocha/mocha.js"></script>
|
<script src="https://unpkg.com/mocha/mocha.js"></script>
|
||||||
|
@ -15,6 +30,7 @@
|
||||||
mocha.setup({
|
mocha.setup({
|
||||||
ui: "bdd",
|
ui: "bdd",
|
||||||
timeout: "5s",
|
timeout: "5s",
|
||||||
|
global: ["nostr"],
|
||||||
})
|
})
|
||||||
mocha.checkLeaks()
|
mocha.checkLeaks()
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -10,7 +10,7 @@ const port = 33543
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
app.use("/", (req: express.Request, res: express.Response) => {
|
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"), {
|
const index = fs.readFileSync(path.join(__dirname, "index.html"), {
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,11 +1,21 @@
|
||||||
|
import "../src/nostr-object"
|
||||||
import { Nostr } from "../src/client"
|
import { Nostr } from "../src/client"
|
||||||
import { Timestamp, unixTimestamp } from "../src/common"
|
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 const relayUrl = new URL("ws://localhost:12648")
|
||||||
|
|
||||||
export interface Setup {
|
export interface Setup {
|
||||||
publisher: Nostr
|
publisher: Nostr
|
||||||
publisherSecret: string
|
publisherSecret?: string
|
||||||
publisherPubkey: string
|
publisherPubkey: string
|
||||||
subscriber: Nostr
|
subscriber: Nostr
|
||||||
subscriberSecret: string
|
subscriberSecret: string
|
||||||
|
@ -25,6 +35,50 @@ export async function setup(
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await restartRelay()
|
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 publisher = new Nostr()
|
||||||
const subscriber = new Nostr()
|
const subscriber = new Nostr()
|
||||||
|
|
||||||
|
@ -44,9 +98,10 @@ export async function setup(
|
||||||
const result = test({
|
const result = test({
|
||||||
publisher,
|
publisher,
|
||||||
publisherSecret:
|
publisherSecret:
|
||||||
"nsec15fnff4uxlgyu79ua3l7327w0wstrd6x565cx6zze78zgkktmr8vs90j363",
|
typeof window === "undefined" || window.nostr === undefined
|
||||||
publisherPubkey:
|
? publisherSecret
|
||||||
"npub1he978sxy7tgc7yfp2zra05v045kfuqnfl3gwr82jd00mzxjj9fjqzw2dg7",
|
: undefined,
|
||||||
|
publisherPubkey,
|
||||||
subscriber,
|
subscriber,
|
||||||
subscriberSecret:
|
subscriberSecret:
|
||||||
"nsec1fxvlyqn3rugvxwaz6dr5h8jcfn0fe0lxyp7pl4mgntxfzqr7dmgst7z9ps",
|
"nsec1fxvlyqn3rugvxwaz6dr5h8jcfn0fe0lxyp7pl4mgntxfzqr7dmgst7z9ps",
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { parsePublicKey } from "../src/crypto"
|
||||||
import { setup } from "./setup"
|
import { setup } from "./setup"
|
||||||
import { createDirectMessage } from "../src/event/direct-message"
|
import { createDirectMessage } from "../src/event/direct-message"
|
||||||
|
|
||||||
describe("dm", () => {
|
describe("direct-message", () => {
|
||||||
const message = "for your eyes only"
|
const message = "for your eyes only"
|
||||||
|
|
||||||
// Test that the intended recipient can receive and decrypt the direct message.
|
// Test that the intended recipient can receive and decrypt the direct message.
|
Loading…
Reference in New Issue