@highway1/cli 0.1.37 → 0.1.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +59 -41
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/commands/join.ts +47 -43
- package/src/commands/send.ts +13 -1
package/package.json
CHANGED
package/src/commands/join.ts
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
createDHTOperations,
|
|
8
8
|
createMessageRouter,
|
|
9
9
|
sign,
|
|
10
|
+
verify,
|
|
11
|
+
extractPublicKey,
|
|
10
12
|
} from '@highway1/core';
|
|
11
13
|
import { getIdentity, getAgentCard, getBootstrapPeers } from '../config.js';
|
|
12
14
|
import { success, error, spinner, printHeader, info } from '../ui.js';
|
|
@@ -66,50 +68,41 @@ export function registerJoinCommand(program: Command): void {
|
|
|
66
68
|
}, { once: true });
|
|
67
69
|
});
|
|
68
70
|
|
|
69
|
-
// Phase 2: wait for relay reservation (up to
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
info(`Listen addresses configured: ${JSON.stringify((node.libp2p as any).components?.addressManager?.getListenAddrs?.() || [])}`);
|
|
71
|
+
// Phase 2: wait for relay reservation (up to 15s after connection)
|
|
72
|
+
// Check if we already have relay addresses (might be auto-added by libp2p)
|
|
73
|
+
const countRelayAddrs = () => node.getMultiaddrs().filter(a => a.includes('/p2p-circuit')).length;
|
|
74
|
+
const initialRelayCount = countRelayAddrs();
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
info(`Initial relay addresses: ${initialRelayCount}`);
|
|
77
|
+
info('Waiting for relay reservation...');
|
|
78
|
+
|
|
79
|
+
let reservationSucceeded = false;
|
|
80
|
+
await new Promise<void>((resolve) => {
|
|
81
|
+
const timeout = setTimeout(() => {
|
|
82
|
+
if (!reservationSucceeded) {
|
|
83
|
+
info(`Relay reservation timeout after 15s.`);
|
|
79
84
|
info(`Connected peers: ${node.libp2p.getPeers().map(p => p.toString()).join(', ')}`);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
node.libp2p.addEventListener('relay:reservation', onReservation, { once: true });
|
|
104
|
-
node.libp2p.addEventListener('self:peer:update', onPeerUpdate);
|
|
105
|
-
|
|
106
|
-
setTimeout(() => {
|
|
107
|
-
node.libp2p.removeEventListener('relay:reservation', onReservation);
|
|
108
|
-
node.libp2p.removeEventListener('self:peer:update', onPeerUpdate);
|
|
109
|
-
}, 10000);
|
|
110
|
-
});
|
|
111
|
-
} else {
|
|
112
|
-
info(`Relay address already present: ${node.getMultiaddrs().filter(a => a.includes('/p2p-circuit')).join(', ')}`);
|
|
85
|
+
}
|
|
86
|
+
resolve();
|
|
87
|
+
}, 15000);
|
|
88
|
+
|
|
89
|
+
// Listen for relay:reservation event (fires when reservation succeeds)
|
|
90
|
+
const onReservation = (evt: any) => {
|
|
91
|
+
reservationSucceeded = true;
|
|
92
|
+
info(`✓ Relay reservation successful!`);
|
|
93
|
+
clearTimeout(timeout);
|
|
94
|
+
setTimeout(resolve, 500);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
node.libp2p.addEventListener('relay:reservation', onReservation, { once: true });
|
|
98
|
+
|
|
99
|
+
setTimeout(() => {
|
|
100
|
+
node.libp2p.removeEventListener('relay:reservation', onReservation);
|
|
101
|
+
}, 15000);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (!reservationSucceeded) {
|
|
105
|
+
info('⚠ Relay reservation did not complete - using fallback relay addresses');
|
|
113
106
|
}
|
|
114
107
|
|
|
115
108
|
connectSpin.succeed('Connected to network!');
|
|
@@ -157,10 +150,21 @@ export function registerJoinCommand(program: Command): void {
|
|
|
157
150
|
}
|
|
158
151
|
}, 30000); // ping every 30s
|
|
159
152
|
|
|
153
|
+
const verifyFn = async (signature: Uint8Array, data: Uint8Array): Promise<boolean> => {
|
|
154
|
+
try {
|
|
155
|
+
const decoded = JSON.parse(new TextDecoder().decode(data)) as { from?: string };
|
|
156
|
+
if (!decoded.from || typeof decoded.from !== 'string') return false;
|
|
157
|
+
const senderPublicKey = extractPublicKey(decoded.from);
|
|
158
|
+
return verify(signature, data, senderPublicKey);
|
|
159
|
+
} catch {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
160
164
|
// Register message handlers for incoming messages
|
|
161
165
|
const router = createMessageRouter(
|
|
162
166
|
node.libp2p,
|
|
163
|
-
|
|
167
|
+
verifyFn,
|
|
164
168
|
dht
|
|
165
169
|
);
|
|
166
170
|
|
package/src/commands/send.ts
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
createMessageRouter,
|
|
8
8
|
createDHTOperations,
|
|
9
9
|
sign,
|
|
10
|
+
verify,
|
|
11
|
+
extractPublicKey,
|
|
10
12
|
} from '@highway1/core';
|
|
11
13
|
import { getIdentity, getBootstrapPeers } from '../config.js';
|
|
12
14
|
import { success, error, spinner, printHeader, info } from '../ui.js';
|
|
@@ -70,10 +72,20 @@ export function registerSendCommand(program: Command): void {
|
|
|
70
72
|
await identifyDone;
|
|
71
73
|
|
|
72
74
|
const dht = createDHTOperations(node.libp2p);
|
|
75
|
+
const verifyFn = async (signature: Uint8Array, data: Uint8Array): Promise<boolean> => {
|
|
76
|
+
try {
|
|
77
|
+
const decoded = JSON.parse(new TextDecoder().decode(data)) as { from?: string };
|
|
78
|
+
if (!decoded.from || typeof decoded.from !== 'string') return false;
|
|
79
|
+
const senderPublicKey = extractPublicKey(decoded.from);
|
|
80
|
+
return verify(signature, data, senderPublicKey);
|
|
81
|
+
} catch {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
73
85
|
|
|
74
86
|
const router = createMessageRouter(
|
|
75
87
|
node.libp2p,
|
|
76
|
-
|
|
88
|
+
verifyFn,
|
|
77
89
|
dht,
|
|
78
90
|
bootstrapPeers
|
|
79
91
|
);
|