@coclaw/openclaw-coclaw 0.2.3 → 0.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coclaw/openclaw-coclaw",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "description": "OpenClaw CoClaw channel plugin for remote chat",
@@ -0,0 +1,190 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import nodePath from 'node:path';
5
+
6
+ import { getRuntime } from './runtime.js';
7
+
8
+ const CHANNEL_ID = 'coclaw';
9
+ const IDENTITY_FILENAME = 'device-identity.json';
10
+
11
+ // Ed25519 SPKI 前缀(固定 12 字节),公钥裸字节从 SPKI DER 中截取
12
+ const ED25519_SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex');
13
+
14
+ function base64UrlEncode(buf) {
15
+ return buf.toString('base64').replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/g, '');
16
+ }
17
+
18
+ // 仅处理 ASCII 范围的大写→小写,保持跨运行时确定性
19
+ function toLowerAscii(input) {
20
+ return input.replace(/[A-Z]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) + 32));
21
+ }
22
+
23
+ function normalizeMetadataForAuth(value) {
24
+ if (typeof value !== 'string') return '';
25
+ const trimmed = value.trim();
26
+ return trimmed ? toLowerAscii(trimmed) : '';
27
+ }
28
+
29
+ function resolveStateDir() {
30
+ const rt = getRuntime();
31
+ if (rt?.state?.resolveStateDir) {
32
+ return rt.state.resolveStateDir();
33
+ }
34
+ return process.env.OPENCLAW_STATE_DIR
35
+ ? nodePath.resolve(process.env.OPENCLAW_STATE_DIR)
36
+ : nodePath.join(os.homedir(), '.openclaw');
37
+ }
38
+
39
+ /**
40
+ * 获取身份文件路径
41
+ * @returns {string}
42
+ */
43
+ export function getIdentityPath() {
44
+ return nodePath.join(resolveStateDir(), CHANNEL_ID, IDENTITY_FILENAME);
45
+ }
46
+
47
+ /**
48
+ * 从 PEM 公钥提取裸字节
49
+ * @param {string} publicKeyPem
50
+ * @returns {Buffer}
51
+ */
52
+ function derivePublicKeyRaw(publicKeyPem) {
53
+ const key = crypto.createPublicKey(publicKeyPem);
54
+ const spki = key.export({ type: 'spki', format: 'der' });
55
+ if (
56
+ spki.length === ED25519_SPKI_PREFIX.length + 32
57
+ && spki.subarray(0, ED25519_SPKI_PREFIX.length).equals(ED25519_SPKI_PREFIX)
58
+ ) {
59
+ return spki.subarray(ED25519_SPKI_PREFIX.length);
60
+ }
61
+ /* c8 ignore next -- Ed25519 密钥 SPKI 格式固定,此分支仅防御未知密钥格式 */
62
+ return spki;
63
+ }
64
+
65
+ /**
66
+ * 公钥指纹 = SHA256(裸公钥字节) 的十六进制
67
+ * @param {string} publicKeyPem
68
+ * @returns {string}
69
+ */
70
+ function fingerprintPublicKey(publicKeyPem) {
71
+ const raw = derivePublicKeyRaw(publicKeyPem);
72
+ return crypto.createHash('sha256').update(raw).digest('hex');
73
+ }
74
+
75
+ /**
76
+ * 生成新的 Ed25519 密钥对
77
+ * @returns {{ deviceId: string, publicKeyPem: string, privateKeyPem: string }}
78
+ */
79
+ function generateIdentity() {
80
+ const { publicKey, privateKey } = crypto.generateKeyPairSync('ed25519');
81
+ const publicKeyPem = publicKey.export({ type: 'spki', format: 'pem' }).toString();
82
+ const privateKeyPem = privateKey.export({ type: 'pkcs8', format: 'pem' }).toString();
83
+ const deviceId = fingerprintPublicKey(publicKeyPem);
84
+ return { deviceId, publicKeyPem, privateKeyPem };
85
+ }
86
+
87
+ /**
88
+ * 加载或创建设备身份(Ed25519 密钥对)
89
+ *
90
+ * 存储格式与 OpenClaw device-identity.ts 保持一致。
91
+ * @param {string} [filePath] - 自定义路径,默认 ~/.openclaw/coclaw/device-identity.json
92
+ * @returns {{ deviceId: string, publicKeyPem: string, privateKeyPem: string }}
93
+ */
94
+ export function loadOrCreateDeviceIdentity(filePath) {
95
+ const fp = filePath ?? getIdentityPath();
96
+ try {
97
+ if (fs.existsSync(fp)) {
98
+ const raw = fs.readFileSync(fp, 'utf8');
99
+ const parsed = JSON.parse(raw);
100
+ if (
101
+ parsed?.version === 1
102
+ && typeof parsed.deviceId === 'string'
103
+ && typeof parsed.publicKeyPem === 'string'
104
+ && typeof parsed.privateKeyPem === 'string'
105
+ ) {
106
+ // 校验 deviceId 一致性
107
+ const derivedId = fingerprintPublicKey(parsed.publicKeyPem);
108
+ if (derivedId && derivedId !== parsed.deviceId) {
109
+ const updated = { ...parsed, deviceId: derivedId };
110
+ fs.writeFileSync(fp, `${JSON.stringify(updated, null, 2)}\n`, { mode: 0o600 });
111
+ try { fs.chmodSync(fp, 0o600); } catch { /* best-effort */ }
112
+ return { deviceId: derivedId, publicKeyPem: parsed.publicKeyPem, privateKeyPem: parsed.privateKeyPem };
113
+ }
114
+ return { deviceId: parsed.deviceId, publicKeyPem: parsed.publicKeyPem, privateKeyPem: parsed.privateKeyPem };
115
+ }
116
+ }
117
+ }
118
+ catch {
119
+ // 读取/解析失败时重新生成
120
+ }
121
+
122
+ const identity = generateIdentity();
123
+ fs.mkdirSync(nodePath.dirname(fp), { recursive: true });
124
+ const stored = {
125
+ version: 1,
126
+ deviceId: identity.deviceId,
127
+ publicKeyPem: identity.publicKeyPem,
128
+ privateKeyPem: identity.privateKeyPem,
129
+ createdAtMs: Date.now(),
130
+ };
131
+ fs.writeFileSync(fp, `${JSON.stringify(stored, null, 2)}\n`, { mode: 0o600 });
132
+ try { fs.chmodSync(fp, 0o600); } catch { /* best-effort */ }
133
+ return identity;
134
+ }
135
+
136
+ /**
137
+ * 签名设备认证载荷
138
+ * @param {string} privateKeyPem
139
+ * @param {string} payload
140
+ * @returns {string} base64url 编码签名
141
+ */
142
+ export function signDevicePayload(privateKeyPem, payload) {
143
+ const key = crypto.createPrivateKey(privateKeyPem);
144
+ const sig = crypto.sign(null, Buffer.from(payload, 'utf8'), key);
145
+ return base64UrlEncode(sig);
146
+ }
147
+
148
+ /**
149
+ * 公钥 PEM → base64url 裸字节
150
+ * @param {string} publicKeyPem
151
+ * @returns {string}
152
+ */
153
+ export function publicKeyRawBase64Url(publicKeyPem) {
154
+ return base64UrlEncode(derivePublicKeyRaw(publicKeyPem));
155
+ }
156
+
157
+ /**
158
+ * 构建 v3 版本的设备认证载荷字符串
159
+ * @param {object} params
160
+ * @param {string} params.deviceId
161
+ * @param {string} params.clientId
162
+ * @param {string} params.clientMode
163
+ * @param {string} params.role
164
+ * @param {string[]} params.scopes
165
+ * @param {number} params.signedAtMs
166
+ * @param {string} [params.token]
167
+ * @param {string} params.nonce
168
+ * @param {string} [params.platform]
169
+ * @param {string} [params.deviceFamily]
170
+ * @returns {string}
171
+ */
172
+ export function buildDeviceAuthPayloadV3(params) {
173
+ const scopes = params.scopes.join(',');
174
+ const token = params.token ?? '';
175
+ const platform = normalizeMetadataForAuth(params.platform);
176
+ const deviceFamily = normalizeMetadataForAuth(params.deviceFamily);
177
+ return [
178
+ 'v3',
179
+ params.deviceId,
180
+ params.clientId,
181
+ params.clientMode,
182
+ params.role,
183
+ scopes,
184
+ String(params.signedAtMs),
185
+ token,
186
+ params.nonce,
187
+ platform,
188
+ deviceFamily,
189
+ ].join('|');
190
+ }
@@ -3,6 +3,12 @@ import os from 'node:os';
3
3
  import nodePath from 'node:path';
4
4
 
5
5
  import { clearConfig, getBindingsPath, readConfig } from './config.js';
6
+ import {
7
+ loadOrCreateDeviceIdentity,
8
+ signDevicePayload,
9
+ publicKeyRawBase64Url,
10
+ buildDeviceAuthPayloadV3,
11
+ } from './device-identity.js';
6
12
  import { getRuntime } from './runtime.js';
7
13
 
8
14
  const DEFAULT_GATEWAY_WS_URL = 'ws://127.0.0.1:18789';
@@ -65,12 +71,14 @@ export class RealtimeBridge {
65
71
  * @param {Function} [deps.clearConfig] - 清除绑定配置
66
72
  * @param {Function} [deps.getBindingsPath] - 获取绑定文件路径
67
73
  * @param {Function} [deps.resolveGatewayAuthToken] - 获取 gateway 认证 token
74
+ * @param {Function} [deps.loadDeviceIdentity] - 加载设备身份
68
75
  */
69
76
  constructor(deps = {}) {
70
77
  this.__readConfig = deps.readConfig ?? readConfig;
71
78
  this.__clearConfig = deps.clearConfig ?? clearConfig;
72
79
  this.__getBindingsPath = deps.getBindingsPath ?? getBindingsPath;
73
80
  this.__resolveGatewayAuthToken = deps.resolveGatewayAuthToken ?? defaultResolveGatewayAuthToken;
81
+ this.__loadDeviceIdentity = deps.loadDeviceIdentity ?? loadOrCreateDeviceIdentity;
74
82
  this.__WebSocket = deps.WebSocket ?? null;
75
83
 
76
84
  this.serverWs = null;
@@ -90,6 +98,7 @@ export class RealtimeBridge {
90
98
  this.serverHbInterval = null;
91
99
  this.serverHbTimer = null;
92
100
  this.__serverHbMissCount = 0;
101
+ this.__deviceIdentity = null;
93
102
  }
94
103
 
95
104
  __resolveWebSocket() {
@@ -281,25 +290,63 @@ export class RealtimeBridge {
281
290
  }
282
291
  }
283
292
 
284
- __sendGatewayConnectRequest(ws) {
293
+ __ensureDeviceIdentity() {
294
+ if (!this.__deviceIdentity) {
295
+ this.__deviceIdentity = this.__loadDeviceIdentity();
296
+ }
297
+ return this.__deviceIdentity;
298
+ }
299
+
300
+ __buildDeviceField(nonce, authToken) {
301
+ const identity = this.__ensureDeviceIdentity();
302
+ const clientId = 'gateway-client';
303
+ const clientMode = 'backend';
304
+ const role = 'operator';
305
+ const scopes = ['operator.admin'];
306
+ const signedAtMs = Date.now();
307
+ const payload = buildDeviceAuthPayloadV3({
308
+ deviceId: identity.deviceId,
309
+ clientId,
310
+ clientMode,
311
+ role,
312
+ scopes,
313
+ signedAtMs,
314
+ token: authToken ?? '',
315
+ nonce: nonce ?? '',
316
+ platform: process.platform,
317
+ deviceFamily: '',
318
+ });
319
+ const signature = signDevicePayload(identity.privateKeyPem, payload);
320
+ return {
321
+ id: identity.deviceId,
322
+ publicKey: publicKeyRawBase64Url(identity.publicKeyPem),
323
+ signature,
324
+ signedAt: signedAtMs,
325
+ nonce: nonce ?? '',
326
+ };
327
+ }
328
+
329
+ __sendGatewayConnectRequest(ws, nonce) {
285
330
  this.gatewayConnectReqId = `coclaw-connect-${Date.now()}`;
286
331
  this.__logDebug(`gateway connect request -> id=${this.gatewayConnectReqId}`);
287
- const authToken = this.__resolveGatewayAuthToken();
288
- const params = {
289
- minProtocol: 3,
290
- maxProtocol: 3,
291
- client: {
292
- id: 'gateway-client',
293
- version: 'dev',
294
- platform: process.platform,
295
- mode: 'backend',
296
- },
297
- caps: [],
298
- role: 'operator',
299
- scopes: ['operator.admin'],
300
- auth: authToken ? { token: authToken } : undefined,
301
- };
302
332
  try {
333
+ const authToken = this.__resolveGatewayAuthToken();
334
+ const device = this.__buildDeviceField(nonce, authToken);
335
+ const params = {
336
+ minProtocol: 3,
337
+ maxProtocol: 3,
338
+ client: {
339
+ id: 'gateway-client',
340
+ version: 'dev',
341
+ platform: process.platform,
342
+ mode: 'backend',
343
+ },
344
+ caps: [],
345
+ role: 'operator',
346
+ scopes: ['operator.admin'],
347
+ auth: authToken ? { token: authToken } : undefined,
348
+ device,
349
+ };
303
350
  ws.send(JSON.stringify({
304
351
  type: 'req',
305
352
  id: this.gatewayConnectReqId,
@@ -338,8 +385,9 @@ export class RealtimeBridge {
338
385
  return;
339
386
  }
340
387
  if (payload.type === 'event' && payload.event === 'connect.challenge') {
388
+ const nonce = payload?.payload?.nonce ?? '';
341
389
  this.__logDebug('gateway event <- connect.challenge');
342
- this.__sendGatewayConnectRequest(ws);
390
+ this.__sendGatewayConnectRequest(ws, nonce);
343
391
  return;
344
392
  }
345
393
  if (payload.type === 'res' && this.gatewayConnectReqId && payload.id === this.gatewayConnectReqId) {