allow publishing without a private key
This commit is contained in:
parent
02a7418d47
commit
6565aa6fa0
@ -139,7 +139,7 @@ export const enum OutgoingKind {
|
||||
*/
|
||||
export interface OutgoingEvent {
|
||||
kind: OutgoingKind.Event
|
||||
signed: SignedEvent
|
||||
event: SignedEvent | RawEvent
|
||||
}
|
||||
|
||||
/**
|
||||
@ -221,7 +221,9 @@ async function parseIncomingMessage(data: string): Promise<IncomingMessage> {
|
||||
|
||||
function serializeOutgoingMessage(msg: OutgoingMessage): string {
|
||||
if (msg.kind === OutgoingKind.Event) {
|
||||
return JSON.stringify(["EVENT", msg.signed.serialize()])
|
||||
const raw =
|
||||
msg.event instanceof SignedEvent ? msg.event.serialize() : msg.event
|
||||
return JSON.stringify(["EVENT", raw])
|
||||
} else if (msg.kind === OutgoingKind.Subscription) {
|
||||
return JSON.stringify([
|
||||
"REQ",
|
||||
|
@ -202,18 +202,42 @@ export class Nostr {
|
||||
/**
|
||||
* Publish an event.
|
||||
*/
|
||||
async publish(event: Event, key: PrivateKey): Promise<void> {
|
||||
if (event.pubkey.toString() !== key.pubkey.toString()) {
|
||||
throw new Error("invalid private key")
|
||||
async publish(event: SignedEvent): Promise<void>
|
||||
async publish(event: RawEvent): Promise<void>
|
||||
// TODO This will need to change when I add NIP-44 AUTH support - the key should be optional
|
||||
async publish(event: Event, key: PrivateKey): Promise<void>
|
||||
async publish(
|
||||
event: SignedEvent | RawEvent | Event,
|
||||
key?: PrivateKey
|
||||
): Promise<void> {
|
||||
// Validate the parameters.
|
||||
if (event instanceof SignedEvent || "sig" in event) {
|
||||
if (key !== undefined) {
|
||||
throw new Error(
|
||||
"when calling publish with a SignedEvent, private key should not be specified"
|
||||
)
|
||||
}
|
||||
} else {
|
||||
if (key === undefined) {
|
||||
throw new Error(
|
||||
"publish called with an unsigned Event, private key must be specified"
|
||||
)
|
||||
}
|
||||
if (event.pubkey.toString() !== key.pubkey.toString()) {
|
||||
throw new Error("invalid private key")
|
||||
}
|
||||
}
|
||||
|
||||
for (const { conn, write } of this.#conns.values()) {
|
||||
if (!write) {
|
||||
continue
|
||||
}
|
||||
const signed = await SignedEvent.sign(event, key)
|
||||
if (!(event instanceof SignedEvent) && !("sig" in event)) {
|
||||
event = await SignedEvent.sign(event, key)
|
||||
}
|
||||
conn.send({
|
||||
kind: OutgoingKind.Event,
|
||||
signed: signed,
|
||||
event,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user