@invictus-z/attp 0.1.0-alpha.0
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.cjs +550 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +296 -0
- package/dist/index.d.ts +296 -0
- package/dist/index.js +513 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 消息格式规范化 — RecordedHop / NodeMessage / BackMessage。
|
|
3
|
+
*
|
|
4
|
+
* 从 python/attp/core/message/event.py 翻译而来。
|
|
5
|
+
* 序列化格式 (to_dict/from_dict) 与 Python 端完全一致,
|
|
6
|
+
* 确保跨语言通信时的数据兼容性。
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 单跳记录内容,嵌入在 NodeMessage 和 BackMessage 中。
|
|
10
|
+
*
|
|
11
|
+
* sigContent 是对其余字段哈希后的签名,
|
|
12
|
+
* 由发送方私钥签署,用于内容完整性验证。
|
|
13
|
+
*/
|
|
14
|
+
declare class RecordedHop {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
senderDid: string;
|
|
17
|
+
targetDid: string;
|
|
18
|
+
content: string;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
hopCount: number;
|
|
21
|
+
sigContent: string;
|
|
22
|
+
constructor(data: {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
senderDid: string;
|
|
25
|
+
targetDid: string;
|
|
26
|
+
content: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
hopCount: number;
|
|
29
|
+
sigContent?: string;
|
|
30
|
+
});
|
|
31
|
+
/** 计算除 sigContent 外所有字段的 SHA-256 哈希。 */
|
|
32
|
+
contentHash(): Promise<string>;
|
|
33
|
+
toDict(): Record<string, unknown>;
|
|
34
|
+
static fromDict(data: Record<string, unknown>): RecordedHop;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 转发消息:A 向 B 发送。
|
|
38
|
+
*
|
|
39
|
+
* protocolUrl: 协议节点地址
|
|
40
|
+
* nonce: 唯一标识,用于匹配回传消息
|
|
41
|
+
* recordedHop: 单跳记录内容
|
|
42
|
+
*/
|
|
43
|
+
declare class NodeMessage {
|
|
44
|
+
protocolUrl: string;
|
|
45
|
+
nonce: string;
|
|
46
|
+
recordedHop: RecordedHop;
|
|
47
|
+
constructor(data: {
|
|
48
|
+
protocolUrl: string;
|
|
49
|
+
nonce: string;
|
|
50
|
+
recordedHop: RecordedHop;
|
|
51
|
+
});
|
|
52
|
+
/** 发送方私钥对 recordedHop 内容签名,写入 sigContent。 */
|
|
53
|
+
signContent(privateKey: CryptoKey): Promise<void>;
|
|
54
|
+
/** 验证 recordedHop.sigContent 是否由对应公钥签署。 */
|
|
55
|
+
verifyContent(publicKey: CryptoKey): Promise<boolean>;
|
|
56
|
+
toDict(): Record<string, unknown>;
|
|
57
|
+
static fromDict(data: Record<string, unknown>): NodeMessage;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 节点回传消息:节点(A 或 B)向协议节点回传。
|
|
61
|
+
*
|
|
62
|
+
* protocolUrl: 协议节点地址
|
|
63
|
+
* nodeDid: 回传节点的 DID 身份
|
|
64
|
+
* nonce: 唯一标识,用于匹配回传消息
|
|
65
|
+
* sigIdentity: nodeDid + nonce 的私钥签名,用于身份确认
|
|
66
|
+
* recordedHop: 单跳记录内容
|
|
67
|
+
*/
|
|
68
|
+
declare class BackMessage {
|
|
69
|
+
protocolUrl: string;
|
|
70
|
+
nodeDid: string;
|
|
71
|
+
nonce: string;
|
|
72
|
+
sigIdentity: string;
|
|
73
|
+
recordedHop: RecordedHop;
|
|
74
|
+
constructor(data: {
|
|
75
|
+
protocolUrl: string;
|
|
76
|
+
nodeDid: string;
|
|
77
|
+
nonce: string;
|
|
78
|
+
sigIdentity?: string;
|
|
79
|
+
recordedHop: RecordedHop;
|
|
80
|
+
});
|
|
81
|
+
/** 使用回传节点私钥对 nodeDid+nonce 签名,写入 sigIdentity。 */
|
|
82
|
+
signIdentity(privateKey: CryptoKey): Promise<void>;
|
|
83
|
+
/** 验证 sigIdentity 是否合法。 */
|
|
84
|
+
verifyIdentity(publicKey: CryptoKey): Promise<boolean>;
|
|
85
|
+
/** 使用发送方私钥对 recordedHop 内容签名。 */
|
|
86
|
+
signContent(privateKey: CryptoKey): Promise<void>;
|
|
87
|
+
/** 验证 recordedHop 内容签名(发送方签名)。 */
|
|
88
|
+
verifyContent(publicKey: CryptoKey): Promise<boolean>;
|
|
89
|
+
toDict(): Record<string, unknown>;
|
|
90
|
+
static fromDict(data: Record<string, unknown>): BackMessage;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 密钥管理 — KeyStore 用于缓存公钥/私钥。
|
|
95
|
+
*
|
|
96
|
+
* 从 python/attp/core/authentication/keys.py 翻译而来。
|
|
97
|
+
* 浏览器端使用 CryptoKey 对象替代 Python cryptography 库的密钥对象。
|
|
98
|
+
*/
|
|
99
|
+
/**
|
|
100
|
+
* 密钥存储:管理公钥缓存 (nodeDid → publicKey) 和私钥缓存 (path → privateKey)。
|
|
101
|
+
*
|
|
102
|
+
* 在浏览器环境中,私钥通过 CryptoKey 对象表示,
|
|
103
|
+
* 公钥同样为 CryptoKey 对象。
|
|
104
|
+
*/
|
|
105
|
+
declare class KeyStore {
|
|
106
|
+
private _cache;
|
|
107
|
+
private _privateKeyCache;
|
|
108
|
+
/**
|
|
109
|
+
* 注入公钥到缓存。
|
|
110
|
+
*/
|
|
111
|
+
cachePublicKey(nodeDid: string, publicKey: CryptoKey): void;
|
|
112
|
+
/**
|
|
113
|
+
* 获取缓存的公钥,不存在返回 undefined。
|
|
114
|
+
*/
|
|
115
|
+
get(nodeDid: string): CryptoKey | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* 暴露内部缓存 Map 引用,维持兼容性。
|
|
118
|
+
*/
|
|
119
|
+
get cacheDict(): Map<string, CryptoKey>;
|
|
120
|
+
/**
|
|
121
|
+
* 缓存私钥,后续调用相同标识直接返回缓存。
|
|
122
|
+
*/
|
|
123
|
+
cachePrivateKey(keyId: string, privateKey: CryptoKey): void;
|
|
124
|
+
/**
|
|
125
|
+
* 获取缓存的私钥。
|
|
126
|
+
*/
|
|
127
|
+
getPrivateKey(keyId: string): CryptoKey | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* 检查是否已有缓存的私钥。
|
|
130
|
+
*/
|
|
131
|
+
hasPrivateKey(keyId: string): boolean;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 签名与验签引擎。
|
|
136
|
+
*
|
|
137
|
+
* 从 python/attp/core/authentication/signatures.py 翻译而来。
|
|
138
|
+
* 使用 Web Crypto API 实现,支持:
|
|
139
|
+
* - RSA-PSS (对应 Python rsa.RSAPrivateKey/RSAPublicKey)
|
|
140
|
+
* - ECDSA P-256/384/521 (对应 Python ec.EllipticCurvePrivateKey)
|
|
141
|
+
*
|
|
142
|
+
* 注意:Python 端对 entry_hash 字符串直接签名(UTF-8 编码后),
|
|
143
|
+
* Web Crypto API 签名时也对 UTF-8 编码的字符串签名,保持一致。
|
|
144
|
+
*/
|
|
145
|
+
/**
|
|
146
|
+
* 使用私钥对 entryHash 签名,返回 Base64 编码的签名字符串。
|
|
147
|
+
*
|
|
148
|
+
* @param entryHash SHA-256 hex 字符串
|
|
149
|
+
* @param privateKey CryptoKey 私钥对象 (RSA 或 EC)
|
|
150
|
+
* @returns Base64 编码的签名字符串,失败返回空字符串
|
|
151
|
+
*/
|
|
152
|
+
declare function signHash(entryHash: string, privateKey: CryptoKey): Promise<string>;
|
|
153
|
+
/**
|
|
154
|
+
* 用公钥验证 entryHash 的签名。
|
|
155
|
+
*
|
|
156
|
+
* @param entryHash SHA-256 hex 字符串
|
|
157
|
+
* @param signatureB64 Base64 编码的签名
|
|
158
|
+
* @param publicKey CryptoKey 公钥对象
|
|
159
|
+
* @returns true 签名合法,false 验证失败
|
|
160
|
+
*/
|
|
161
|
+
declare function verifySignature(entryHash: string, signatureB64: string, publicKey: CryptoKey): Promise<boolean>;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 哈希计算:genesis 标识哈希和每跳签名哈希。
|
|
165
|
+
*
|
|
166
|
+
* 从 python/attp/core/provenance/hashing.py 翻译而来。
|
|
167
|
+
* 使用 Web Crypto API (crypto.subtle.digest) 实现 SHA-256,
|
|
168
|
+
* 保证输出与 Python 端 hashlib.sha256 完全一致。
|
|
169
|
+
*/
|
|
170
|
+
/**
|
|
171
|
+
* 计算创世标识的 SHA-256 哈希,用于防篡改验证。
|
|
172
|
+
*/
|
|
173
|
+
declare function calculateGenesisHash(sessionId: string, protocolNodeAddress: string): Promise<string>;
|
|
174
|
+
/**
|
|
175
|
+
* 计算单跳消息字段的 SHA-256 哈希,用于身份验证签名。
|
|
176
|
+
*/
|
|
177
|
+
declare function calculateHopHash(content: string, nodeDid: string, targetDid: string, hopCount: number, timestamp: number, sessionId: string, protocolNodeAddress: string): Promise<string>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* BehaviorEntry — 单条行为记录。
|
|
181
|
+
*
|
|
182
|
+
* 从 python/attp/core/sessions/node_message.py 翻译而来。
|
|
183
|
+
*
|
|
184
|
+
* fieldType 取值:
|
|
185
|
+
* A2T — Agent → Tool (Agent 调用工具)
|
|
186
|
+
* A2U — Agent → User (Agent 发给用户)
|
|
187
|
+
* U2A — User → Agent (用户发给 Agent)
|
|
188
|
+
* A2A — Agent → Agent (Agent 间通信)
|
|
189
|
+
* T2A — Tool → Agent (工具返回结果,预留)
|
|
190
|
+
*/
|
|
191
|
+
/** 行为类型枚举 */
|
|
192
|
+
type FieldType = 'A2T' | 'A2U' | 'U2A' | 'A2A' | 'T2A';
|
|
193
|
+
declare class BehaviorEntry {
|
|
194
|
+
fieldType: FieldType;
|
|
195
|
+
content: string;
|
|
196
|
+
timestamp: number;
|
|
197
|
+
target: string;
|
|
198
|
+
extra: Record<string, unknown>;
|
|
199
|
+
constructor(data: {
|
|
200
|
+
fieldType: FieldType;
|
|
201
|
+
content: string;
|
|
202
|
+
timestamp?: number;
|
|
203
|
+
target?: string;
|
|
204
|
+
extra?: Record<string, unknown>;
|
|
205
|
+
});
|
|
206
|
+
toDict(): Record<string, unknown>;
|
|
207
|
+
static fromDict(data: Record<string, unknown>): BehaviorEntry;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* UserSession / UserSessionManager — 用户端会话管理。
|
|
212
|
+
*
|
|
213
|
+
* 面向 User 客户端,管理 ATTP 协议层的会话元数据:
|
|
214
|
+
* - session_id / protocol_node_address / hop 元数据
|
|
215
|
+
* - 用户身份 (userDid) 和密钥标识
|
|
216
|
+
*
|
|
217
|
+
* 参考 Python 端 AppSession 的结构,但针对用户端场景简化。
|
|
218
|
+
*/
|
|
219
|
+
/** Hop 记录的元数据结构 */
|
|
220
|
+
interface HopMetadata {
|
|
221
|
+
Hop_Count: number;
|
|
222
|
+
Timestamp: number;
|
|
223
|
+
Signature: string;
|
|
224
|
+
Content: string;
|
|
225
|
+
node_did: string;
|
|
226
|
+
target_did: string;
|
|
227
|
+
session_id?: string;
|
|
228
|
+
protocol_node_address?: string;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* UserSession — 用户端单个会话的协议层状态容器。
|
|
232
|
+
*/
|
|
233
|
+
declare class UserSession {
|
|
234
|
+
/** 会话标识(通常是 chatId) */
|
|
235
|
+
key: string;
|
|
236
|
+
/** ATTP 协议层元数据 */
|
|
237
|
+
metadata: Record<string, unknown>;
|
|
238
|
+
/** 最后更新时间(Unix 时间戳,秒) */
|
|
239
|
+
updatedAt: number;
|
|
240
|
+
constructor(data: {
|
|
241
|
+
key: string;
|
|
242
|
+
metadata?: Record<string, unknown>;
|
|
243
|
+
updatedAt?: number;
|
|
244
|
+
});
|
|
245
|
+
/** 提取追踪相关的元数据。 */
|
|
246
|
+
getTraceMetadata(): Record<string, unknown>;
|
|
247
|
+
/** 合并追踪元数据。 */
|
|
248
|
+
setTraceMetadata(traceData: Record<string, unknown>): void;
|
|
249
|
+
get sessionId(): string | undefined;
|
|
250
|
+
set sessionId(value: string | undefined);
|
|
251
|
+
get protocolNodeAddress(): string | undefined;
|
|
252
|
+
set protocolNodeAddress(value: string | undefined);
|
|
253
|
+
get hop(): HopMetadata | undefined;
|
|
254
|
+
get hopCount(): number;
|
|
255
|
+
/** 用户 DID 标识 */
|
|
256
|
+
get userDid(): string | undefined;
|
|
257
|
+
set userDid(value: string | undefined);
|
|
258
|
+
/** 密钥标识(用于从 KeyStore 查找私钥) */
|
|
259
|
+
get keyId(): string | undefined;
|
|
260
|
+
set keyId(value: string | undefined);
|
|
261
|
+
setMetadata(key: string, value: unknown): void;
|
|
262
|
+
getMetadata(key: string, defaultValue?: unknown): unknown;
|
|
263
|
+
updateMetadata(data: Record<string, unknown>): void;
|
|
264
|
+
toDict(): Record<string, unknown>;
|
|
265
|
+
static fromDict(data: Record<string, unknown>): UserSession;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* UserSessionManager — 用户端会话存储,按 chatId 索引。
|
|
269
|
+
*
|
|
270
|
+
* 内存中维护所有活跃会话,可选持久化到 localStorage。
|
|
271
|
+
*/
|
|
272
|
+
declare class UserSessionManager {
|
|
273
|
+
private _sessions;
|
|
274
|
+
/** 获取或创建会话。 */
|
|
275
|
+
getOrCreate(chatId: string): UserSession;
|
|
276
|
+
/** 获取已有会话,不存在返回 undefined。 */
|
|
277
|
+
get(chatId: string): UserSession | undefined;
|
|
278
|
+
/** 保存/更新会话。 */
|
|
279
|
+
save(session: UserSession): void;
|
|
280
|
+
/** 删除会话。 */
|
|
281
|
+
delete(chatId: string): void;
|
|
282
|
+
/** 所有会话条目。 */
|
|
283
|
+
entries(): IterableIterator<[string, UserSession]>;
|
|
284
|
+
/** 会话数量。 */
|
|
285
|
+
get size(): number;
|
|
286
|
+
/** 序列化所有会话为 JSON 字符串。 */
|
|
287
|
+
serialize(): string;
|
|
288
|
+
/** 从 JSON 字符串反序列化恢复所有会话。 */
|
|
289
|
+
deserialize(json: string): void;
|
|
290
|
+
/** 保存到 localStorage。 */
|
|
291
|
+
saveToStorage(storageKey?: string): void;
|
|
292
|
+
/** 从 localStorage 加载。 */
|
|
293
|
+
loadFromStorage(storageKey?: string): void;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export { BackMessage, BehaviorEntry, type FieldType, type HopMetadata, KeyStore, NodeMessage, RecordedHop, UserSession, UserSessionManager, calculateGenesisHash, calculateHopHash, signHash, verifySignature };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 消息格式规范化 — RecordedHop / NodeMessage / BackMessage。
|
|
3
|
+
*
|
|
4
|
+
* 从 python/attp/core/message/event.py 翻译而来。
|
|
5
|
+
* 序列化格式 (to_dict/from_dict) 与 Python 端完全一致,
|
|
6
|
+
* 确保跨语言通信时的数据兼容性。
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 单跳记录内容,嵌入在 NodeMessage 和 BackMessage 中。
|
|
10
|
+
*
|
|
11
|
+
* sigContent 是对其余字段哈希后的签名,
|
|
12
|
+
* 由发送方私钥签署,用于内容完整性验证。
|
|
13
|
+
*/
|
|
14
|
+
declare class RecordedHop {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
senderDid: string;
|
|
17
|
+
targetDid: string;
|
|
18
|
+
content: string;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
hopCount: number;
|
|
21
|
+
sigContent: string;
|
|
22
|
+
constructor(data: {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
senderDid: string;
|
|
25
|
+
targetDid: string;
|
|
26
|
+
content: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
hopCount: number;
|
|
29
|
+
sigContent?: string;
|
|
30
|
+
});
|
|
31
|
+
/** 计算除 sigContent 外所有字段的 SHA-256 哈希。 */
|
|
32
|
+
contentHash(): Promise<string>;
|
|
33
|
+
toDict(): Record<string, unknown>;
|
|
34
|
+
static fromDict(data: Record<string, unknown>): RecordedHop;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 转发消息:A 向 B 发送。
|
|
38
|
+
*
|
|
39
|
+
* protocolUrl: 协议节点地址
|
|
40
|
+
* nonce: 唯一标识,用于匹配回传消息
|
|
41
|
+
* recordedHop: 单跳记录内容
|
|
42
|
+
*/
|
|
43
|
+
declare class NodeMessage {
|
|
44
|
+
protocolUrl: string;
|
|
45
|
+
nonce: string;
|
|
46
|
+
recordedHop: RecordedHop;
|
|
47
|
+
constructor(data: {
|
|
48
|
+
protocolUrl: string;
|
|
49
|
+
nonce: string;
|
|
50
|
+
recordedHop: RecordedHop;
|
|
51
|
+
});
|
|
52
|
+
/** 发送方私钥对 recordedHop 内容签名,写入 sigContent。 */
|
|
53
|
+
signContent(privateKey: CryptoKey): Promise<void>;
|
|
54
|
+
/** 验证 recordedHop.sigContent 是否由对应公钥签署。 */
|
|
55
|
+
verifyContent(publicKey: CryptoKey): Promise<boolean>;
|
|
56
|
+
toDict(): Record<string, unknown>;
|
|
57
|
+
static fromDict(data: Record<string, unknown>): NodeMessage;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 节点回传消息:节点(A 或 B)向协议节点回传。
|
|
61
|
+
*
|
|
62
|
+
* protocolUrl: 协议节点地址
|
|
63
|
+
* nodeDid: 回传节点的 DID 身份
|
|
64
|
+
* nonce: 唯一标识,用于匹配回传消息
|
|
65
|
+
* sigIdentity: nodeDid + nonce 的私钥签名,用于身份确认
|
|
66
|
+
* recordedHop: 单跳记录内容
|
|
67
|
+
*/
|
|
68
|
+
declare class BackMessage {
|
|
69
|
+
protocolUrl: string;
|
|
70
|
+
nodeDid: string;
|
|
71
|
+
nonce: string;
|
|
72
|
+
sigIdentity: string;
|
|
73
|
+
recordedHop: RecordedHop;
|
|
74
|
+
constructor(data: {
|
|
75
|
+
protocolUrl: string;
|
|
76
|
+
nodeDid: string;
|
|
77
|
+
nonce: string;
|
|
78
|
+
sigIdentity?: string;
|
|
79
|
+
recordedHop: RecordedHop;
|
|
80
|
+
});
|
|
81
|
+
/** 使用回传节点私钥对 nodeDid+nonce 签名,写入 sigIdentity。 */
|
|
82
|
+
signIdentity(privateKey: CryptoKey): Promise<void>;
|
|
83
|
+
/** 验证 sigIdentity 是否合法。 */
|
|
84
|
+
verifyIdentity(publicKey: CryptoKey): Promise<boolean>;
|
|
85
|
+
/** 使用发送方私钥对 recordedHop 内容签名。 */
|
|
86
|
+
signContent(privateKey: CryptoKey): Promise<void>;
|
|
87
|
+
/** 验证 recordedHop 内容签名(发送方签名)。 */
|
|
88
|
+
verifyContent(publicKey: CryptoKey): Promise<boolean>;
|
|
89
|
+
toDict(): Record<string, unknown>;
|
|
90
|
+
static fromDict(data: Record<string, unknown>): BackMessage;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 密钥管理 — KeyStore 用于缓存公钥/私钥。
|
|
95
|
+
*
|
|
96
|
+
* 从 python/attp/core/authentication/keys.py 翻译而来。
|
|
97
|
+
* 浏览器端使用 CryptoKey 对象替代 Python cryptography 库的密钥对象。
|
|
98
|
+
*/
|
|
99
|
+
/**
|
|
100
|
+
* 密钥存储:管理公钥缓存 (nodeDid → publicKey) 和私钥缓存 (path → privateKey)。
|
|
101
|
+
*
|
|
102
|
+
* 在浏览器环境中,私钥通过 CryptoKey 对象表示,
|
|
103
|
+
* 公钥同样为 CryptoKey 对象。
|
|
104
|
+
*/
|
|
105
|
+
declare class KeyStore {
|
|
106
|
+
private _cache;
|
|
107
|
+
private _privateKeyCache;
|
|
108
|
+
/**
|
|
109
|
+
* 注入公钥到缓存。
|
|
110
|
+
*/
|
|
111
|
+
cachePublicKey(nodeDid: string, publicKey: CryptoKey): void;
|
|
112
|
+
/**
|
|
113
|
+
* 获取缓存的公钥,不存在返回 undefined。
|
|
114
|
+
*/
|
|
115
|
+
get(nodeDid: string): CryptoKey | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* 暴露内部缓存 Map 引用,维持兼容性。
|
|
118
|
+
*/
|
|
119
|
+
get cacheDict(): Map<string, CryptoKey>;
|
|
120
|
+
/**
|
|
121
|
+
* 缓存私钥,后续调用相同标识直接返回缓存。
|
|
122
|
+
*/
|
|
123
|
+
cachePrivateKey(keyId: string, privateKey: CryptoKey): void;
|
|
124
|
+
/**
|
|
125
|
+
* 获取缓存的私钥。
|
|
126
|
+
*/
|
|
127
|
+
getPrivateKey(keyId: string): CryptoKey | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* 检查是否已有缓存的私钥。
|
|
130
|
+
*/
|
|
131
|
+
hasPrivateKey(keyId: string): boolean;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 签名与验签引擎。
|
|
136
|
+
*
|
|
137
|
+
* 从 python/attp/core/authentication/signatures.py 翻译而来。
|
|
138
|
+
* 使用 Web Crypto API 实现,支持:
|
|
139
|
+
* - RSA-PSS (对应 Python rsa.RSAPrivateKey/RSAPublicKey)
|
|
140
|
+
* - ECDSA P-256/384/521 (对应 Python ec.EllipticCurvePrivateKey)
|
|
141
|
+
*
|
|
142
|
+
* 注意:Python 端对 entry_hash 字符串直接签名(UTF-8 编码后),
|
|
143
|
+
* Web Crypto API 签名时也对 UTF-8 编码的字符串签名,保持一致。
|
|
144
|
+
*/
|
|
145
|
+
/**
|
|
146
|
+
* 使用私钥对 entryHash 签名,返回 Base64 编码的签名字符串。
|
|
147
|
+
*
|
|
148
|
+
* @param entryHash SHA-256 hex 字符串
|
|
149
|
+
* @param privateKey CryptoKey 私钥对象 (RSA 或 EC)
|
|
150
|
+
* @returns Base64 编码的签名字符串,失败返回空字符串
|
|
151
|
+
*/
|
|
152
|
+
declare function signHash(entryHash: string, privateKey: CryptoKey): Promise<string>;
|
|
153
|
+
/**
|
|
154
|
+
* 用公钥验证 entryHash 的签名。
|
|
155
|
+
*
|
|
156
|
+
* @param entryHash SHA-256 hex 字符串
|
|
157
|
+
* @param signatureB64 Base64 编码的签名
|
|
158
|
+
* @param publicKey CryptoKey 公钥对象
|
|
159
|
+
* @returns true 签名合法,false 验证失败
|
|
160
|
+
*/
|
|
161
|
+
declare function verifySignature(entryHash: string, signatureB64: string, publicKey: CryptoKey): Promise<boolean>;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 哈希计算:genesis 标识哈希和每跳签名哈希。
|
|
165
|
+
*
|
|
166
|
+
* 从 python/attp/core/provenance/hashing.py 翻译而来。
|
|
167
|
+
* 使用 Web Crypto API (crypto.subtle.digest) 实现 SHA-256,
|
|
168
|
+
* 保证输出与 Python 端 hashlib.sha256 完全一致。
|
|
169
|
+
*/
|
|
170
|
+
/**
|
|
171
|
+
* 计算创世标识的 SHA-256 哈希,用于防篡改验证。
|
|
172
|
+
*/
|
|
173
|
+
declare function calculateGenesisHash(sessionId: string, protocolNodeAddress: string): Promise<string>;
|
|
174
|
+
/**
|
|
175
|
+
* 计算单跳消息字段的 SHA-256 哈希,用于身份验证签名。
|
|
176
|
+
*/
|
|
177
|
+
declare function calculateHopHash(content: string, nodeDid: string, targetDid: string, hopCount: number, timestamp: number, sessionId: string, protocolNodeAddress: string): Promise<string>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* BehaviorEntry — 单条行为记录。
|
|
181
|
+
*
|
|
182
|
+
* 从 python/attp/core/sessions/node_message.py 翻译而来。
|
|
183
|
+
*
|
|
184
|
+
* fieldType 取值:
|
|
185
|
+
* A2T — Agent → Tool (Agent 调用工具)
|
|
186
|
+
* A2U — Agent → User (Agent 发给用户)
|
|
187
|
+
* U2A — User → Agent (用户发给 Agent)
|
|
188
|
+
* A2A — Agent → Agent (Agent 间通信)
|
|
189
|
+
* T2A — Tool → Agent (工具返回结果,预留)
|
|
190
|
+
*/
|
|
191
|
+
/** 行为类型枚举 */
|
|
192
|
+
type FieldType = 'A2T' | 'A2U' | 'U2A' | 'A2A' | 'T2A';
|
|
193
|
+
declare class BehaviorEntry {
|
|
194
|
+
fieldType: FieldType;
|
|
195
|
+
content: string;
|
|
196
|
+
timestamp: number;
|
|
197
|
+
target: string;
|
|
198
|
+
extra: Record<string, unknown>;
|
|
199
|
+
constructor(data: {
|
|
200
|
+
fieldType: FieldType;
|
|
201
|
+
content: string;
|
|
202
|
+
timestamp?: number;
|
|
203
|
+
target?: string;
|
|
204
|
+
extra?: Record<string, unknown>;
|
|
205
|
+
});
|
|
206
|
+
toDict(): Record<string, unknown>;
|
|
207
|
+
static fromDict(data: Record<string, unknown>): BehaviorEntry;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* UserSession / UserSessionManager — 用户端会话管理。
|
|
212
|
+
*
|
|
213
|
+
* 面向 User 客户端,管理 ATTP 协议层的会话元数据:
|
|
214
|
+
* - session_id / protocol_node_address / hop 元数据
|
|
215
|
+
* - 用户身份 (userDid) 和密钥标识
|
|
216
|
+
*
|
|
217
|
+
* 参考 Python 端 AppSession 的结构,但针对用户端场景简化。
|
|
218
|
+
*/
|
|
219
|
+
/** Hop 记录的元数据结构 */
|
|
220
|
+
interface HopMetadata {
|
|
221
|
+
Hop_Count: number;
|
|
222
|
+
Timestamp: number;
|
|
223
|
+
Signature: string;
|
|
224
|
+
Content: string;
|
|
225
|
+
node_did: string;
|
|
226
|
+
target_did: string;
|
|
227
|
+
session_id?: string;
|
|
228
|
+
protocol_node_address?: string;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* UserSession — 用户端单个会话的协议层状态容器。
|
|
232
|
+
*/
|
|
233
|
+
declare class UserSession {
|
|
234
|
+
/** 会话标识(通常是 chatId) */
|
|
235
|
+
key: string;
|
|
236
|
+
/** ATTP 协议层元数据 */
|
|
237
|
+
metadata: Record<string, unknown>;
|
|
238
|
+
/** 最后更新时间(Unix 时间戳,秒) */
|
|
239
|
+
updatedAt: number;
|
|
240
|
+
constructor(data: {
|
|
241
|
+
key: string;
|
|
242
|
+
metadata?: Record<string, unknown>;
|
|
243
|
+
updatedAt?: number;
|
|
244
|
+
});
|
|
245
|
+
/** 提取追踪相关的元数据。 */
|
|
246
|
+
getTraceMetadata(): Record<string, unknown>;
|
|
247
|
+
/** 合并追踪元数据。 */
|
|
248
|
+
setTraceMetadata(traceData: Record<string, unknown>): void;
|
|
249
|
+
get sessionId(): string | undefined;
|
|
250
|
+
set sessionId(value: string | undefined);
|
|
251
|
+
get protocolNodeAddress(): string | undefined;
|
|
252
|
+
set protocolNodeAddress(value: string | undefined);
|
|
253
|
+
get hop(): HopMetadata | undefined;
|
|
254
|
+
get hopCount(): number;
|
|
255
|
+
/** 用户 DID 标识 */
|
|
256
|
+
get userDid(): string | undefined;
|
|
257
|
+
set userDid(value: string | undefined);
|
|
258
|
+
/** 密钥标识(用于从 KeyStore 查找私钥) */
|
|
259
|
+
get keyId(): string | undefined;
|
|
260
|
+
set keyId(value: string | undefined);
|
|
261
|
+
setMetadata(key: string, value: unknown): void;
|
|
262
|
+
getMetadata(key: string, defaultValue?: unknown): unknown;
|
|
263
|
+
updateMetadata(data: Record<string, unknown>): void;
|
|
264
|
+
toDict(): Record<string, unknown>;
|
|
265
|
+
static fromDict(data: Record<string, unknown>): UserSession;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* UserSessionManager — 用户端会话存储,按 chatId 索引。
|
|
269
|
+
*
|
|
270
|
+
* 内存中维护所有活跃会话,可选持久化到 localStorage。
|
|
271
|
+
*/
|
|
272
|
+
declare class UserSessionManager {
|
|
273
|
+
private _sessions;
|
|
274
|
+
/** 获取或创建会话。 */
|
|
275
|
+
getOrCreate(chatId: string): UserSession;
|
|
276
|
+
/** 获取已有会话,不存在返回 undefined。 */
|
|
277
|
+
get(chatId: string): UserSession | undefined;
|
|
278
|
+
/** 保存/更新会话。 */
|
|
279
|
+
save(session: UserSession): void;
|
|
280
|
+
/** 删除会话。 */
|
|
281
|
+
delete(chatId: string): void;
|
|
282
|
+
/** 所有会话条目。 */
|
|
283
|
+
entries(): IterableIterator<[string, UserSession]>;
|
|
284
|
+
/** 会话数量。 */
|
|
285
|
+
get size(): number;
|
|
286
|
+
/** 序列化所有会话为 JSON 字符串。 */
|
|
287
|
+
serialize(): string;
|
|
288
|
+
/** 从 JSON 字符串反序列化恢复所有会话。 */
|
|
289
|
+
deserialize(json: string): void;
|
|
290
|
+
/** 保存到 localStorage。 */
|
|
291
|
+
saveToStorage(storageKey?: string): void;
|
|
292
|
+
/** 从 localStorage 加载。 */
|
|
293
|
+
loadFromStorage(storageKey?: string): void;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export { BackMessage, BehaviorEntry, type FieldType, type HopMetadata, KeyStore, NodeMessage, RecordedHop, UserSession, UserSessionManager, calculateGenesisHash, calculateHopHash, signHash, verifySignature };
|