@f2a/network 0.1.2

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.
Files changed (234) hide show
  1. package/.github/workflows/ci.yml +113 -0
  2. package/.github/workflows/publish.yml +60 -0
  3. package/LICENSE +21 -0
  4. package/MONOREPO.md +58 -0
  5. package/README.md +280 -0
  6. package/SKILL.md +137 -0
  7. package/dist/adapters/openclaw.d.ts +103 -0
  8. package/dist/adapters/openclaw.d.ts.map +1 -0
  9. package/dist/adapters/openclaw.js +297 -0
  10. package/dist/adapters/openclaw.js.map +1 -0
  11. package/dist/cli/commands.d.ts +17 -0
  12. package/dist/cli/commands.d.ts.map +1 -0
  13. package/dist/cli/commands.js +107 -0
  14. package/dist/cli/commands.js.map +1 -0
  15. package/dist/cli/index.d.ts +6 -0
  16. package/dist/cli/index.d.ts.map +1 -0
  17. package/dist/cli/index.js +203 -0
  18. package/dist/cli/index.js.map +1 -0
  19. package/dist/core/autonomous-economy.d.ts +136 -0
  20. package/dist/core/autonomous-economy.d.ts.map +1 -0
  21. package/dist/core/autonomous-economy.js +255 -0
  22. package/dist/core/autonomous-economy.js.map +1 -0
  23. package/dist/core/connection-manager.d.ts +80 -0
  24. package/dist/core/connection-manager.d.ts.map +1 -0
  25. package/dist/core/connection-manager.js +235 -0
  26. package/dist/core/connection-manager.js.map +1 -0
  27. package/dist/core/connection-manager.test.d.ts +2 -0
  28. package/dist/core/connection-manager.test.d.ts.map +1 -0
  29. package/dist/core/connection-manager.test.js +52 -0
  30. package/dist/core/connection-manager.test.js.map +1 -0
  31. package/dist/core/e2ee-crypto.d.ts +90 -0
  32. package/dist/core/e2ee-crypto.d.ts.map +1 -0
  33. package/dist/core/e2ee-crypto.js +190 -0
  34. package/dist/core/e2ee-crypto.js.map +1 -0
  35. package/dist/core/f2a.d.ts +126 -0
  36. package/dist/core/f2a.d.ts.map +1 -0
  37. package/dist/core/f2a.js +425 -0
  38. package/dist/core/f2a.js.map +1 -0
  39. package/dist/core/identity.d.ts +47 -0
  40. package/dist/core/identity.d.ts.map +1 -0
  41. package/dist/core/identity.js +130 -0
  42. package/dist/core/identity.js.map +1 -0
  43. package/dist/core/identity.test.d.ts +2 -0
  44. package/dist/core/identity.test.d.ts.map +1 -0
  45. package/dist/core/identity.test.js +43 -0
  46. package/dist/core/identity.test.js.map +1 -0
  47. package/dist/core/p2p-network.d.ts +242 -0
  48. package/dist/core/p2p-network.d.ts.map +1 -0
  49. package/dist/core/p2p-network.js +1182 -0
  50. package/dist/core/p2p-network.js.map +1 -0
  51. package/dist/core/reputation-security.d.ts +168 -0
  52. package/dist/core/reputation-security.d.ts.map +1 -0
  53. package/dist/core/reputation-security.js +369 -0
  54. package/dist/core/reputation-security.js.map +1 -0
  55. package/dist/core/reputation.d.ts +179 -0
  56. package/dist/core/reputation.d.ts.map +1 -0
  57. package/dist/core/reputation.js +472 -0
  58. package/dist/core/reputation.js.map +1 -0
  59. package/dist/core/review-committee.d.ts +130 -0
  60. package/dist/core/review-committee.d.ts.map +1 -0
  61. package/dist/core/review-committee.js +251 -0
  62. package/dist/core/review-committee.js.map +1 -0
  63. package/dist/core/serverless.d.ts +155 -0
  64. package/dist/core/serverless.d.ts.map +1 -0
  65. package/dist/core/serverless.js +615 -0
  66. package/dist/core/serverless.js.map +1 -0
  67. package/dist/core/token-manager.d.ts +42 -0
  68. package/dist/core/token-manager.d.ts.map +1 -0
  69. package/dist/core/token-manager.js +122 -0
  70. package/dist/core/token-manager.js.map +1 -0
  71. package/dist/daemon/control-server.d.ts +55 -0
  72. package/dist/daemon/control-server.d.ts.map +1 -0
  73. package/dist/daemon/control-server.js +262 -0
  74. package/dist/daemon/control-server.js.map +1 -0
  75. package/dist/daemon/index.d.ts +35 -0
  76. package/dist/daemon/index.d.ts.map +1 -0
  77. package/dist/daemon/index.js +69 -0
  78. package/dist/daemon/index.js.map +1 -0
  79. package/dist/daemon/main.d.ts +6 -0
  80. package/dist/daemon/main.d.ts.map +1 -0
  81. package/dist/daemon/main.js +38 -0
  82. package/dist/daemon/main.js.map +1 -0
  83. package/dist/daemon/start.d.ts +6 -0
  84. package/dist/daemon/start.d.ts.map +1 -0
  85. package/dist/daemon/start.js +25 -0
  86. package/dist/daemon/start.js.map +1 -0
  87. package/dist/daemon/webhook.d.ts +30 -0
  88. package/dist/daemon/webhook.d.ts.map +1 -0
  89. package/dist/daemon/webhook.js +86 -0
  90. package/dist/daemon/webhook.js.map +1 -0
  91. package/dist/daemon/webhook.test.d.ts +2 -0
  92. package/dist/daemon/webhook.test.d.ts.map +1 -0
  93. package/dist/daemon/webhook.test.js +24 -0
  94. package/dist/daemon/webhook.test.js.map +1 -0
  95. package/dist/index.d.ts +24 -0
  96. package/dist/index.d.ts.map +1 -0
  97. package/dist/index.js +25 -0
  98. package/dist/index.js.map +1 -0
  99. package/dist/protocol/messages.d.ts +739 -0
  100. package/dist/protocol/messages.d.ts.map +1 -0
  101. package/dist/protocol/messages.js +188 -0
  102. package/dist/protocol/messages.js.map +1 -0
  103. package/dist/protocol/messages.test.d.ts +2 -0
  104. package/dist/protocol/messages.test.d.ts.map +1 -0
  105. package/dist/protocol/messages.test.js +55 -0
  106. package/dist/protocol/messages.test.js.map +1 -0
  107. package/dist/types/index.d.ts +247 -0
  108. package/dist/types/index.d.ts.map +1 -0
  109. package/dist/types/index.js +10 -0
  110. package/dist/types/index.js.map +1 -0
  111. package/dist/types/result.d.ts +28 -0
  112. package/dist/types/result.d.ts.map +1 -0
  113. package/dist/types/result.js +16 -0
  114. package/dist/types/result.js.map +1 -0
  115. package/dist/utils/benchmark.d.ts +67 -0
  116. package/dist/utils/benchmark.d.ts.map +1 -0
  117. package/dist/utils/benchmark.js +179 -0
  118. package/dist/utils/benchmark.js.map +1 -0
  119. package/dist/utils/logger.d.ts +105 -0
  120. package/dist/utils/logger.d.ts.map +1 -0
  121. package/dist/utils/logger.js +275 -0
  122. package/dist/utils/logger.js.map +1 -0
  123. package/dist/utils/middleware.d.ts +85 -0
  124. package/dist/utils/middleware.d.ts.map +1 -0
  125. package/dist/utils/middleware.js +173 -0
  126. package/dist/utils/middleware.js.map +1 -0
  127. package/dist/utils/rate-limiter.d.ts +71 -0
  128. package/dist/utils/rate-limiter.d.ts.map +1 -0
  129. package/dist/utils/rate-limiter.js +160 -0
  130. package/dist/utils/rate-limiter.js.map +1 -0
  131. package/dist/utils/signature.d.ts +57 -0
  132. package/dist/utils/signature.d.ts.map +1 -0
  133. package/dist/utils/signature.js +102 -0
  134. package/dist/utils/signature.js.map +1 -0
  135. package/dist/utils/validation.d.ts +504 -0
  136. package/dist/utils/validation.d.ts.map +1 -0
  137. package/dist/utils/validation.js +159 -0
  138. package/dist/utils/validation.js.map +1 -0
  139. package/docs/F2A-PROTOCOL.md +61 -0
  140. package/docs/MOBILE_BOOTSTRAP_DESIGN.md +126 -0
  141. package/docs/a2a-lessons.md +316 -0
  142. package/docs/middleware-guide.md +448 -0
  143. package/docs/readme-update-checklist.md +90 -0
  144. package/docs/reputation-guide.md +396 -0
  145. package/docs/rfcs/001-reputation-system.md +712 -0
  146. package/docs/security-design.md +247 -0
  147. package/install.sh +231 -0
  148. package/package.json +64 -0
  149. package/packages/openclaw-adapter/README.md +510 -0
  150. package/packages/openclaw-adapter/openclaw.plugin.json +106 -0
  151. package/packages/openclaw-adapter/package.json +40 -0
  152. package/packages/openclaw-adapter/src/announcement-queue.test.ts +449 -0
  153. package/packages/openclaw-adapter/src/announcement-queue.ts +403 -0
  154. package/packages/openclaw-adapter/src/capability-detector.test.ts +99 -0
  155. package/packages/openclaw-adapter/src/capability-detector.ts +183 -0
  156. package/packages/openclaw-adapter/src/claim-handlers.test.ts +974 -0
  157. package/packages/openclaw-adapter/src/claim-handlers.ts +482 -0
  158. package/packages/openclaw-adapter/src/connector.business.test.ts +583 -0
  159. package/packages/openclaw-adapter/src/connector.ts +795 -0
  160. package/packages/openclaw-adapter/src/index.test.ts +82 -0
  161. package/packages/openclaw-adapter/src/index.ts +18 -0
  162. package/packages/openclaw-adapter/src/integration.e2e.test.ts +829 -0
  163. package/packages/openclaw-adapter/src/logger.ts +51 -0
  164. package/packages/openclaw-adapter/src/network-client.test.ts +266 -0
  165. package/packages/openclaw-adapter/src/network-client.ts +251 -0
  166. package/packages/openclaw-adapter/src/network-recovery.test.ts +465 -0
  167. package/packages/openclaw-adapter/src/node-manager.test.ts +136 -0
  168. package/packages/openclaw-adapter/src/node-manager.ts +429 -0
  169. package/packages/openclaw-adapter/src/plugin.test.ts +439 -0
  170. package/packages/openclaw-adapter/src/plugin.ts +104 -0
  171. package/packages/openclaw-adapter/src/reputation.test.ts +221 -0
  172. package/packages/openclaw-adapter/src/reputation.ts +368 -0
  173. package/packages/openclaw-adapter/src/task-guard.test.ts +502 -0
  174. package/packages/openclaw-adapter/src/task-guard.ts +860 -0
  175. package/packages/openclaw-adapter/src/task-queue.concurrency.test.ts +462 -0
  176. package/packages/openclaw-adapter/src/task-queue.edge-cases.test.ts +284 -0
  177. package/packages/openclaw-adapter/src/task-queue.persistence.test.ts +408 -0
  178. package/packages/openclaw-adapter/src/task-queue.ts +668 -0
  179. package/packages/openclaw-adapter/src/tool-handlers.test.ts +906 -0
  180. package/packages/openclaw-adapter/src/tool-handlers.ts +574 -0
  181. package/packages/openclaw-adapter/src/types.ts +361 -0
  182. package/packages/openclaw-adapter/src/webhook-pusher.test.ts +188 -0
  183. package/packages/openclaw-adapter/src/webhook-pusher.ts +220 -0
  184. package/packages/openclaw-adapter/src/webhook-server.test.ts +580 -0
  185. package/packages/openclaw-adapter/src/webhook-server.ts +202 -0
  186. package/packages/openclaw-adapter/tsconfig.json +20 -0
  187. package/src/cli/commands.test.ts +157 -0
  188. package/src/cli/commands.ts +129 -0
  189. package/src/cli/index.test.ts +77 -0
  190. package/src/cli/index.ts +234 -0
  191. package/src/core/autonomous-economy.test.ts +291 -0
  192. package/src/core/autonomous-economy.ts +428 -0
  193. package/src/core/e2ee-crypto.test.ts +125 -0
  194. package/src/core/e2ee-crypto.ts +246 -0
  195. package/src/core/f2a.test.ts +269 -0
  196. package/src/core/f2a.ts +618 -0
  197. package/src/core/p2p-network.test.ts +199 -0
  198. package/src/core/p2p-network.ts +1432 -0
  199. package/src/core/reputation-security.test.ts +403 -0
  200. package/src/core/reputation-security.ts +562 -0
  201. package/src/core/reputation.test.ts +260 -0
  202. package/src/core/reputation.ts +576 -0
  203. package/src/core/review-committee.test.ts +380 -0
  204. package/src/core/review-committee.ts +401 -0
  205. package/src/core/token-manager.test.ts +133 -0
  206. package/src/core/token-manager.ts +140 -0
  207. package/src/daemon/control-server.test.ts +216 -0
  208. package/src/daemon/control-server.ts +292 -0
  209. package/src/daemon/index.test.ts +85 -0
  210. package/src/daemon/index.ts +89 -0
  211. package/src/daemon/main.ts +44 -0
  212. package/src/daemon/start.ts +29 -0
  213. package/src/daemon/webhook.test.ts +68 -0
  214. package/src/daemon/webhook.ts +105 -0
  215. package/src/index.test.ts +436 -0
  216. package/src/index.ts +72 -0
  217. package/src/types/index.test.ts +87 -0
  218. package/src/types/index.ts +341 -0
  219. package/src/types/result.ts +68 -0
  220. package/src/utils/benchmark.ts +237 -0
  221. package/src/utils/logger.ts +331 -0
  222. package/src/utils/middleware.ts +229 -0
  223. package/src/utils/rate-limiter.ts +207 -0
  224. package/src/utils/signature.ts +136 -0
  225. package/src/utils/validation.ts +186 -0
  226. package/tests/docker/Dockerfile.node +23 -0
  227. package/tests/docker/Dockerfile.runner +18 -0
  228. package/tests/docker/docker-compose.test.yml +73 -0
  229. package/tests/integration/message-passing.test.ts +109 -0
  230. package/tests/integration/multi-node.test.ts +92 -0
  231. package/tests/integration/p2p-connection.test.ts +83 -0
  232. package/tests/integration/test-config.ts +32 -0
  233. package/tsconfig.json +21 -0
  234. package/vitest.config.ts +26 -0
@@ -0,0 +1,199 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { P2PNetwork } from './p2p-network.js';
3
+ import { AgentInfo } from '../types/index.js';
4
+
5
+ describe('P2PNetwork', () => {
6
+ let network: P2PNetwork;
7
+ let mockAgentInfo: AgentInfo;
8
+
9
+ beforeEach(() => {
10
+ mockAgentInfo = {
11
+ peerId: '',
12
+ displayName: 'Test Agent',
13
+ agentType: 'openclaw',
14
+ version: '1.0.0',
15
+ capabilities: [],
16
+ protocolVersion: 'f2a/1.0',
17
+ lastSeen: Date.now(),
18
+ multiaddrs: []
19
+ };
20
+ network = new P2PNetwork(mockAgentInfo);
21
+ });
22
+
23
+ afterEach(async () => {
24
+ await network.stop();
25
+ });
26
+
27
+ describe('initialization', () => {
28
+ it('should initialize with correct default config', () => {
29
+ expect(network).toBeDefined();
30
+ expect(network.getPeerId()).toBe('');
31
+ });
32
+
33
+ it('should handle multiple stop calls gracefully', async () => {
34
+ await network.stop();
35
+ await network.stop(); // Should not throw
36
+ expect(network.getPeerId()).toBe('');
37
+ });
38
+ });
39
+
40
+ describe('peer management', () => {
41
+ it('should return empty array when no peers connected', () => {
42
+ const peers = network.getConnectedPeers();
43
+ expect(peers).toEqual([]);
44
+ });
45
+
46
+ it('should return empty array for all peers initially', () => {
47
+ const peers = network.getAllPeers();
48
+ expect(peers).toEqual([]);
49
+ });
50
+ });
51
+
52
+ describe('capability checking', () => {
53
+ it('should correctly identify agent capabilities', () => {
54
+ const agentWithCaps: AgentInfo = {
55
+ ...mockAgentInfo,
56
+ capabilities: [
57
+ { name: 'file-operation', description: 'File ops', tools: ['read'] }
58
+ ]
59
+ };
60
+
61
+ const hasCap = (network as any).hasCapability(agentWithCaps, 'file-operation');
62
+ expect(hasCap).toBe(true);
63
+ });
64
+
65
+ it('should return false for missing capabilities', () => {
66
+ const agentWithCaps: AgentInfo = {
67
+ ...mockAgentInfo,
68
+ capabilities: []
69
+ };
70
+
71
+ const hasCap = (network as any).hasCapability(agentWithCaps, 'file-operation');
72
+ expect(hasCap).toBe(false);
73
+ });
74
+ });
75
+
76
+ describe('DHT features', () => {
77
+ it('should return false for isDHTEnabled when not started', () => {
78
+ expect(network.isDHTEnabled()).toBe(false);
79
+ });
80
+
81
+ it('should return 0 for getDHTPeerCount when not started', () => {
82
+ expect(network.getDHTPeerCount()).toBe(0);
83
+ });
84
+
85
+ it('should return error when findPeerViaDHT called before start', async () => {
86
+ const result = await network.findPeerViaDHT('test-peer-id');
87
+ expect(result.success).toBe(false);
88
+ expect(result.error?.code).toBe('NETWORK_NOT_STARTED');
89
+ });
90
+ });
91
+
92
+ describe('E2EE features', () => {
93
+ it('should return null for getEncryptionPublicKey when not started', () => {
94
+ expect(network.getEncryptionPublicKey()).toBeNull();
95
+ });
96
+
97
+ it('should return 0 for getEncryptedPeerCount when not started', () => {
98
+ expect(network.getEncryptedPeerCount()).toBe(0);
99
+ });
100
+ });
101
+
102
+ describe('events', () => {
103
+ it('should emit error event', async () => {
104
+ const errorPromise = new Promise<Error>((resolve) => {
105
+ network.on('error', (error) => {
106
+ resolve(error);
107
+ });
108
+ });
109
+
110
+ (network as any).emit('error', new Error('Test error'));
111
+
112
+ const error = await errorPromise;
113
+ expect(error).toBeInstanceOf(Error);
114
+ expect(error.message).toBe('Test error');
115
+ });
116
+ });
117
+
118
+ describe('message handling', () => {
119
+ it('should process DISCOVER_RESP and upsert peer', async () => {
120
+ const agentInfo: AgentInfo = {
121
+ ...mockAgentInfo,
122
+ peerId: 'peer-remote',
123
+ multiaddrs: ['/ip4/127.0.0.1/tcp/9002']
124
+ };
125
+
126
+ await (network as any).handleMessage(
127
+ {
128
+ id: '00000000-0000-4000-8000-000000000001',
129
+ type: 'DISCOVER_RESP',
130
+ from: 'peer-remote',
131
+ timestamp: Date.now(),
132
+ payload: { agentInfo }
133
+ },
134
+ 'peer-remote'
135
+ );
136
+
137
+ const peers = network.getAllPeers();
138
+ expect(peers).toHaveLength(1);
139
+ expect(peers[0].agentInfo?.peerId).toBe('peer-remote');
140
+ expect(peers[0].multiaddrs[0].toString()).toContain('/tcp/9002');
141
+ });
142
+
143
+ it('should process CAPABILITY_RESPONSE and upsert peer', async () => {
144
+ const agentInfo: AgentInfo = {
145
+ ...mockAgentInfo,
146
+ peerId: 'peer-cap',
147
+ capabilities: [{ name: 'code-gen', description: 'Code Gen', tools: ['generate'] }],
148
+ multiaddrs: ['/ip4/127.0.0.1/tcp/9003']
149
+ };
150
+
151
+ await (network as any).handleMessage(
152
+ {
153
+ id: '00000000-0000-4000-8000-000000000002',
154
+ type: 'CAPABILITY_RESPONSE',
155
+ from: 'peer-cap',
156
+ timestamp: Date.now(),
157
+ payload: { agentInfo }
158
+ },
159
+ 'peer-cap'
160
+ );
161
+
162
+ const peers = network.getAllPeers();
163
+ expect(peers).toHaveLength(1);
164
+ expect(peers[0].agentInfo?.capabilities[0].name).toBe('code-gen');
165
+ });
166
+ });
167
+
168
+ describe('broadcast', () => {
169
+ it('should count fulfilled failures in broadcast warning', async () => {
170
+ const warnSpy = vi.spyOn((network as any).logger, 'warn');
171
+
172
+ (network as any).node = {
173
+ getPeers: vi.fn().mockReturnValue([
174
+ { toString: () => 'peer-a' },
175
+ { toString: () => 'peer-b' }
176
+ ]),
177
+ stop: vi.fn().mockResolvedValue(undefined)
178
+ };
179
+
180
+ const sendSpy = vi.spyOn(network as any, 'sendMessage')
181
+ .mockResolvedValueOnce({ success: true, data: undefined })
182
+ .mockResolvedValueOnce({ success: false, error: { code: 'PEER_NOT_FOUND', message: 'Peer not found' } });
183
+
184
+ await (network as any).broadcast({
185
+ id: 'msg-broadcast',
186
+ type: 'DISCOVER',
187
+ from: 'self',
188
+ timestamp: Date.now(),
189
+ payload: { agentInfo: mockAgentInfo }
190
+ });
191
+
192
+ expect(sendSpy).toHaveBeenCalledTimes(2);
193
+ expect(warnSpy).toHaveBeenCalledWith('Broadcast failed to some peers', {
194
+ failed: 1,
195
+ total: 2
196
+ });
197
+ });
198
+ });
199
+ });