-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest-auth-publish.ts
More file actions
executable file
Β·145 lines (116 loc) Β· 4.5 KB
/
test-auth-publish.ts
File metadata and controls
executable file
Β·145 lines (116 loc) Β· 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bun
/**
* Test script to demonstrate auth-required publish retry
* Usage: bun run test-auth-publish.ts --nsec <your-nsec> --msg "your message"
*/
import { NDKEvent } from "./src/events";
import { NDK } from "./src/ndk";
import { NDKPrivateKeySigner } from "./src/signers/private-key";
// Parse command line arguments
const args = process.argv.slice(2);
const nsecIndex = args.indexOf("--nsec");
const msgIndex = args.indexOf("--msg");
if (nsecIndex === -1 || msgIndex === -1) {
console.error('Usage: bun run test-auth-publish.ts --nsec <your-nsec> --msg "your message"');
process.exit(1);
}
const nsec = args[nsecIndex + 1];
const message = args[msgIndex + 1];
if (!nsec || !message) {
console.error("Error: Both --nsec and --msg are required");
process.exit(1);
}
async function main() {
console.log("π Testing auth-required publish retry...\n");
// Create signer from nsec
let signer;
try {
signer = new NDKPrivateKeySigner(nsec);
const user = await signer.user();
console.log("π€ Publishing as:", user.npub);
} catch (error) {
console.error("β Invalid nsec:", error);
process.exit(1);
}
// Create NDK instance with pyramid relay only
const ndk = new NDK({
explicitRelayUrls: ["wss://pyramid.fiatjaf.com"],
signer,
enableOutboxModel: false, // Disable outbox to ensure we only use pyramid
// Auth policy that automatically approves authentication
relayAuthDefaultPolicy: async (relay, challenge) => {
console.log("π Relay requested authentication from:", relay.url);
console.log(" Challenge:", challenge.substring(0, 32) + "...");
return true; // Approve authentication
},
});
// Enable debug logging
ndk.pool.on("relay:connect", (relay) => {
console.log("β
Connected to relay:", relay.url);
});
ndk.pool.on("relay:disconnect", (relay) => {
console.log("β Disconnected from relay:", relay.url);
});
// Track auth events
ndk.pool.relays.forEach((relay) => {
relay.on("auth", (challenge) => {
console.log("π AUTH message received from:", relay.url);
});
relay.on("authed", () => {
console.log("β
Authentication successful for:", relay.url);
});
relay.on("auth:failed", (error) => {
console.log("β Authentication failed for:", relay.url, error);
});
relay.on("publish:failed", (event, error) => {
console.log("π€ Publish failed (will retry after auth):", error.message);
});
});
// Connect to relays
console.log("\nπ‘ Connecting to pyramid.fiatjaf.com...");
await ndk.connect(5000);
// Wait a bit for connection to stabilize
await new Promise((resolve) => setTimeout(resolve, 1000));
// Create and publish event
console.log("\nπ Creating event...");
const event = new NDKEvent(ndk);
event.kind = 1;
event.content = message;
console.log(" Content:", message);
try {
console.log("\nπ€ Publishing event (this will trigger auth-required)...");
const publishStart = Date.now();
const relays = await event.publish();
const publishDuration = Date.now() - publishStart;
console.log("\nβ
EVENT PUBLISHED SUCCESSFULLY!");
console.log(" Event ID:", event.id);
console.log(" Published to", relays.size, "relay(s)");
console.log(" Time taken:", publishDuration, "ms");
console.log("\nπ Auth-required publish retry worked!");
console.log(" The event was initially rejected with auth-required,");
console.log(" but NDK automatically authenticated and retried the publish.");
// Show the relays
for (const relay of relays) {
console.log(" β", relay.url);
}
} catch (error: any) {
console.error("\nβ PUBLISH FAILED");
console.error(" Error:", error.message);
if (error.errors) {
console.error("\n Relay errors:");
for (const [relay, err] of error.errors) {
console.error(" -", relay.url + ":", err.message);
}
}
process.exit(1);
}
// Close connections
console.log("\nπ Closing connections...");
ndk.pool.relays.forEach((relay) => relay.disconnect());
console.log("β
Done!\n");
process.exit(0);
}
main().catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
});