publicstring commited on
Commit
996d653
·
verified ·
1 Parent(s): dba8820

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +22 -13
app.js CHANGED
@@ -160,9 +160,9 @@ async function generateUniquePin() {
160
  }
161
 
162
  // ============================================================================
163
- // PUBLIC ROUTE: LEGACY DIRECT REGISTRATION (CURL SUPPORT)
164
  // ============================================================================
165
- app.post('/anp/v1/register-handle', async (req, res) => {
166
  const { handle, session_id, session_pub_key, callback_token } = req.body;
167
  if (!handle || !session_id || !session_pub_key) return res.status(400).json({ error: "Missing required parameters." });
168
 
@@ -177,7 +177,7 @@ app.post('/anp/v1/register-handle', async (req, res) => {
177
  });
178
 
179
  if (error) throw error;
180
- console.log(`[Handle Registry] Bound permanent handle (Direct API): ${normalizedHandle} ➔ ${session_pub_key.substring(0,8)}`);
181
  return res.json({ status: "BOUND", handle: normalizedHandle });
182
  } catch (err) {
183
  console.error("[-] Handle registration error:", err.message);
@@ -188,18 +188,16 @@ app.post('/anp/v1/register-handle', async (req, res) => {
188
  // ============================================================================
189
  // ANP HANDSHAKE ROUTE (Protocol Registration, Room Creation & Joining)
190
  // ============================================================================
191
- app.post('/anp/v1/handshake', async (req, res) => {
192
  const { session_id, buyer_pub_key, constraints, blind_auth_token, callback_token } = req.body;
193
 
194
  if (blind_auth_token) {
195
  console.log(`[Auth] Blind key token received | session: ${session_id} | buyer: ${buyer_pub_key?.substring(0, 8)}...`);
196
  }
197
 
198
- // Explicitly sync seller's local state machine
199
  core.registerIncomingSession(session_id, buyer_pub_key, constraints);
200
 
201
  try {
202
- // Support strict JSON objects generated by the AI extracting constraints
203
  const targetHandle = constraints?.terms?.target_handle || constraints?.target_handle;
204
  const p2pPin = constraints?.terms?.p2p_pin || constraints?.p2p_pin;
205
  const intent = constraints?.intent;
@@ -208,7 +206,6 @@ app.post('/anp/v1/handshake', async (req, res) => {
208
  if (intent === 'register' && targetHandle) {
209
  const handle = targetHandle.toLowerCase().trim();
210
 
211
- // Check if handle exists and is owned by someone else
212
  const { data: existing } = await supabase.from('ginger_handles').select('pub_key').eq('handle', handle).maybeSingle();
213
  if (existing && existing.pub_key !== buyer_pub_key) {
214
  return res.json({ type: 'CANCEL', reason: `Handle '${handle}' is already registered to another cryptographic key.` });
@@ -260,6 +257,12 @@ app.post('/anp/v1/handshake', async (req, res) => {
260
  if (roomErr) throw roomErr;
261
  localRoomCache.set(room.room_id, room);
262
 
 
 
 
 
 
 
263
  // Notify the Host asynchronously
264
  await core.request('/api/route/async-callback', 'POST', {
265
  buyer_pub_key: persistentHost.pub_key,
@@ -268,12 +271,12 @@ app.post('/anp/v1/handshake', async (req, res) => {
268
  session_id: persistentHost.session_id,
269
  price: constraints.max_budget || 0,
270
  turn: 2,
271
- message: `Peer targeting your handle '${handle}' has joined. Room active.`
272
  }
273
  }).catch(() => {});
274
 
275
  core.updateSessionStateLocally(session_id, NegotiationState.COUNTERED, constraints.max_budget || 0, 2);
276
- return res.json({ type: "COUNTER", price: constraints.max_budget || 0, turn: 2, message: `Connected to ${handle}.` });
277
  }
278
 
279
  // --- CASE 2: JOINING VIA 6-DIGIT PIN ---
@@ -296,9 +299,15 @@ app.post('/anp/v1/handshake', async (req, res) => {
296
  room.status = 'active';
297
  localRoomCache.set(room.room_id, room);
298
 
 
 
 
 
 
 
299
  await core.request('/api/route/async-callback', 'POST', {
300
  buyer_pub_key: room.initiator_pub_key,
301
- payload: { type: "COUNTER", session_id: room.initiator_session_id, price: constraints.max_budget || 0, turn: 2, message: "Peer has entered the room. Bargaining active." }
302
  }).catch(() => {});
303
 
304
  core.updateSessionStateLocally(session_id, NegotiationState.COUNTERED, constraints.max_budget || 0, 2);
@@ -330,7 +339,7 @@ app.post('/anp/v1/handshake', async (req, res) => {
330
  // ============================================================================
331
  // ANP COUNTER ROUTE (Real-time Message Tunneling + AI Mediation)
332
  // ============================================================================
333
- app.post('/anp/v1/counter', async (req, res) => {
334
  const { session_id, turn, price, reason, signature } = req.body;
335
  const currentTurn = turn || 2;
336
  const nextTurn = currentTurn + 1;
@@ -400,7 +409,7 @@ app.post('/anp/v1/counter', async (req, res) => {
400
  // ============================================================================
401
  // BILATERAL SIGNATURE HANDLER
402
  // ============================================================================
403
- app.post('/anp/v1/sign_request', (req, res) => {
404
  try {
405
  const sig = core.signAsSeller(req.body.artifact);
406
  res.json({ sellerSignature: sig });
@@ -424,7 +433,7 @@ app.listen(PORT, async () => {
424
 
425
  await core.registerNode(
426
  AGENT_ID,
427
- `${PUBLIC_ENDPOINT}/anp/v1`,
428
  ['p2p_bridge', 'mediation', 'scheduling'],
429
  ['p2p_bridge', 'room_routing', 'ai_mediator'],
430
  { supported_modes: ['ANP/C'] }
 
160
  }
161
 
162
  // ============================================================================
163
+ // PUBLIC ROUTE: LEGACY DIRECT REGISTRATION
164
  // ============================================================================
165
+ app.post('/register-handle', async (req, res) => {
166
  const { handle, session_id, session_pub_key, callback_token } = req.body;
167
  if (!handle || !session_id || !session_pub_key) return res.status(400).json({ error: "Missing required parameters." });
168
 
 
177
  });
178
 
179
  if (error) throw error;
180
+ console.log(`[Handle Registry] Bound permanent handle: ${normalizedHandle} ➔ ${session_pub_key.substring(0,8)}`);
181
  return res.json({ status: "BOUND", handle: normalizedHandle });
182
  } catch (err) {
183
  console.error("[-] Handle registration error:", err.message);
 
188
  // ============================================================================
189
  // ANP HANDSHAKE ROUTE (Protocol Registration, Room Creation & Joining)
190
  // ============================================================================
191
+ app.post('/handshake', async (req, res) => {
192
  const { session_id, buyer_pub_key, constraints, blind_auth_token, callback_token } = req.body;
193
 
194
  if (blind_auth_token) {
195
  console.log(`[Auth] Blind key token received | session: ${session_id} | buyer: ${buyer_pub_key?.substring(0, 8)}...`);
196
  }
197
 
 
198
  core.registerIncomingSession(session_id, buyer_pub_key, constraints);
199
 
200
  try {
 
201
  const targetHandle = constraints?.terms?.target_handle || constraints?.target_handle;
202
  const p2pPin = constraints?.terms?.p2p_pin || constraints?.p2p_pin;
203
  const intent = constraints?.intent;
 
206
  if (intent === 'register' && targetHandle) {
207
  const handle = targetHandle.toLowerCase().trim();
208
 
 
209
  const { data: existing } = await supabase.from('ginger_handles').select('pub_key').eq('handle', handle).maybeSingle();
210
  if (existing && existing.pub_key !== buyer_pub_key) {
211
  return res.json({ type: 'CANCEL', reason: `Handle '${handle}' is already registered to another cryptographic key.` });
 
257
  if (roomErr) throw roomErr;
258
  localRoomCache.set(room.room_id, room);
259
 
260
+ // Extract custom message
261
+ const customMsg = constraints?.terms?.message || constraints?.message;
262
+ const notificationMsg = customMsg
263
+ ? `[Incoming from ${handle}]: ${customMsg}`
264
+ : `Peer targeting your handle '${handle}' has joined. Room active.`;
265
+
266
  // Notify the Host asynchronously
267
  await core.request('/api/route/async-callback', 'POST', {
268
  buyer_pub_key: persistentHost.pub_key,
 
271
  session_id: persistentHost.session_id,
272
  price: constraints.max_budget || 0,
273
  turn: 2,
274
+ message: notificationMsg
275
  }
276
  }).catch(() => {});
277
 
278
  core.updateSessionStateLocally(session_id, NegotiationState.COUNTERED, constraints.max_budget || 0, 2);
279
+ return res.json({ type: "COUNTER", price: constraints.max_budget || 0, turn: 2, message: `Connected to ${handle}. Message delivered.` });
280
  }
281
 
282
  // --- CASE 2: JOINING VIA 6-DIGIT PIN ---
 
299
  room.status = 'active';
300
  localRoomCache.set(room.room_id, room);
301
 
302
+ // Extract custom message
303
+ const customMsg = constraints?.terms?.message || constraints?.message;
304
+ const notificationMsg = customMsg
305
+ ? `[Incoming via PIN ${pin}]: ${customMsg}`
306
+ : "Peer has entered the room. Bargaining active.";
307
+
308
  await core.request('/api/route/async-callback', 'POST', {
309
  buyer_pub_key: room.initiator_pub_key,
310
+ payload: { type: "COUNTER", session_id: room.initiator_session_id, price: constraints.max_budget || 0, turn: 2, message: notificationMsg }
311
  }).catch(() => {});
312
 
313
  core.updateSessionStateLocally(session_id, NegotiationState.COUNTERED, constraints.max_budget || 0, 2);
 
339
  // ============================================================================
340
  // ANP COUNTER ROUTE (Real-time Message Tunneling + AI Mediation)
341
  // ============================================================================
342
+ app.post('/counter', async (req, res) => {
343
  const { session_id, turn, price, reason, signature } = req.body;
344
  const currentTurn = turn || 2;
345
  const nextTurn = currentTurn + 1;
 
409
  // ============================================================================
410
  // BILATERAL SIGNATURE HANDLER
411
  // ============================================================================
412
+ app.post('/sign_request', (req, res) => {
413
  try {
414
  const sig = core.signAsSeller(req.body.artifact);
415
  res.json({ sellerSignature: sig });
 
433
 
434
  await core.registerNode(
435
  AGENT_ID,
436
+ `${PUBLIC_ENDPOINT}`,
437
  ['p2p_bridge', 'mediation', 'scheduling'],
438
  ['p2p_bridge', 'room_routing', 'ai_mediator'],
439
  { supported_modes: ['ANP/C'] }