myde-remote-connect 0.0.1 → 0.0.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.
@@ -19,92 +19,85 @@
19
19
  * - PIN 码:6位数字,用户手动传递,用于初次配对时的 PAKE 握手验证。
20
20
  */
21
21
  export declare class SecureChannel {
22
- constructor(signalAdapter: SignalingAdapter, options?: ChannelOptions);
23
-
24
- /**
25
- * 初始化通道,设置本设备身份。
26
- * 必须在 tryConnect 或 pairInit 之前调用。
27
- *
28
- * @param myDeviceId 本设备 UUID
29
- * @param remoteId 可选的远程设备 UUID
30
- */
31
- init(myDeviceId: string, remoteId?: string): Promise<void>;
32
-
33
- /**
34
- * 主动断开连接并清理会话密钥。
35
- * 通道实例随后可丢弃,不会影响已保存的凭证。
36
- */
37
- disconnect(): void;
38
-
39
- // ================= 连接 =================
40
-
41
- /**
42
- * 尝试使用已保存的凭证建立安全连接。
43
- *
44
- * 应用层应从本地安全存储中读取此前保存的 `Credential`,调用此方法。
45
- * 内部将使用该凭证与对方执行基于长期密钥的握手(如 Noise IK)。
46
- *
47
- * @returns 若成功,返回 `{ success: true, credential }`,其中 credential 可能更新了 lastConnected;
48
- * 若失败(凭证失效、对方无记录等),返回 `{ success: false }`,需重新走配对流程。
49
- */
50
- tryConnect(credential?: CredentialPrivateInfo): Promise<ConnectResult>;
51
-
52
- // ================= 初次配对(需用户传递 PIN) =================
53
-
54
- /**
55
- * 预生成 PIN,可在 pairInit 之前调用以提前展示给用户。
56
- * 重复调用会重新生成 PIN。
57
- * @returns 6 位数字 PIN
58
- */
59
- updatePIN(): string;
60
-
61
- /**
62
- * 在需要身份验证的信道中进行配对,通过 PIN,完成基于 PAKE 的握手,只需要一方输入 PIN。
63
- */
64
- pairInit(credential: CredentialPublicInfo): Promise<{
65
- /** 展示给用户的 6 位数字 PIN,需要用户输入到另一端 */
66
- pin: string;
67
- /** 用户输入对方的 PIN */
68
- inputOtherPin: (pin: string) => void;
69
- /** 等待配对完成 */
70
- waitForPairing: () => Promise<Credential>;
71
- }>;
72
-
73
- // ================= 数据收发(安全通道建立后) =================
74
-
75
- /**
76
- * 发送加密数据(任意可序列化对象)。
77
- * @throws {ChannelNotReadyError} 若安全通道尚未就绪(未调用成功配对/重连)
78
- */
79
- send(payload: string): Promise<void>;
80
-
81
- /**
82
- * 发送二进制数据(高效)。
83
- * @throws {ChannelNotReadyError}
84
- */
85
- sendBinary(data: ArrayBuffer | Uint8Array): Promise<void>;
86
-
87
- // ================= 事件订阅 =================
88
-
89
- /**
90
- * 监听通道事件。
91
- * @param event 事件名
92
- * @param callback 回调函数
93
- */
94
- on<K extends keyof SecureChannelEvents>(event: K, callback: SecureChannelEvents[K]): void;
95
-
96
- /** 移除事件监听 */
97
- off(event: string, callback: Function): void;
98
-
99
- // ================= 主动凭证管理 =================
100
-
101
- /**
102
- * 主动轮换我方长期凭证(生成新密钥对并与对方交换)。
103
- * 完成后会触发 `credentialRotated` 事件,应用层应保存新凭证。
104
- *
105
- * 仅在通道处于 `ready` 状态时可调用。
106
- */
107
- rotateCredential(): Promise<void>;
22
+ constructor(signalAdapter: SignalingAdapter, options?: ChannelOptions);
23
+
24
+ /**
25
+ * 初始化通道,设置本设备身份。
26
+ * 必须在 tryConnect 或 pairInit 之前调用。
27
+ *
28
+ * @param myDeviceId 本设备 UUID
29
+ * @param remoteId 可选的远程设备 UUID
30
+ */
31
+ init(myDeviceId: string, remoteId?: string): Promise<void>;
32
+
33
+ /**
34
+ * 主动断开连接并清理会话密钥。
35
+ * 通道实例随后可丢弃,不会影响已保存的凭证。
36
+ */
37
+ disconnect(): void;
38
+
39
+ // ================= 连接 =================
40
+
41
+ /**
42
+ * 尝试使用已保存的凭证建立安全连接。
43
+ *
44
+ * 应用层应从本地安全存储中读取此前保存的 `Credential`,调用此方法。
45
+ * 内部将使用该凭证与对方执行基于长期密钥的握手(如 Noise IK)。
46
+ *
47
+ * @returns 若成功,返回 `{ success: true, credential }`,其中 credential 可能更新了 lastConnected;
48
+ * 若失败(凭证失效、对方无记录等),返回 `{ success: false }`,需重新走配对流程。
49
+ */
50
+ tryConnect(credential?: CredentialPrivateInfo): Promise<ConnectResult>;
51
+
52
+ // ================= 初次配对(需用户传递 PIN) =================
53
+
54
+ /**
55
+ * 预生成 PIN,可在 pairInit 之前调用以提前展示给用户。
56
+ * 重复调用会重新生成 PIN。
57
+ * @returns 6 位数字 PIN
58
+ */
59
+ updatePIN(): string;
60
+
61
+ /**
62
+ * 在需要身份验证的信道中进行配对,通过 PIN,完成基于 PAKE 的握手,只需要一方输入 PIN。
63
+ */
64
+ pairInit(credential: CredentialPublicInfo): Promise<{
65
+ /** 展示给用户的 6 位数字 PIN,需要用户输入到另一端 */
66
+ pin: string;
67
+ /** 用户输入对方的 PIN */
68
+ inputOtherPin: (pin: string) => void;
69
+ /** 等待配对完成 */
70
+ waitForPairing: () => Promise<Credential>;
71
+ }>;
72
+
73
+ // ================= 数据收发(安全通道建立后) =================
74
+
75
+ /**
76
+ * 发送加密数据(任意可序列化对象)。
77
+ * @throws {ChannelNotReadyError} 若安全通道尚未就绪(未调用成功配对/重连)
78
+ */
79
+ send(payload: string): Promise<void>;
80
+
81
+ /**
82
+ * 发送二进制数据(高效)。
83
+ * @throws {ChannelNotReadyError}
84
+ */
85
+ sendBinary(data: ArrayBuffer | Uint8Array): Promise<void>;
86
+
87
+ // ================= 事件订阅 =================
88
+
89
+ /**
90
+ * 监听通道事件。
91
+ * @param event 事件名
92
+ * @param callback 回调函数
93
+ */
94
+ on<K extends keyof SecureChannelEvents>(
95
+ event: K,
96
+ callback: SecureChannelEvents[K],
97
+ ): void;
98
+
99
+ /** 移除事件监听 */
100
+ off(event: string, callback: Function): void;
108
101
  }
109
102
 
110
103
  // ================= 相关类型定义 =================
@@ -121,94 +114,94 @@ type SignalingAdapter = TrustedSignalingAdapter | UntrustedSignalingAdapter;
121
114
 
122
115
  /** 受信任的信令适配器(物理信道等),外部已验证身份,不加密 */
123
116
  interface TrustedSignalingAdapter {
124
- trustIdentity: true;
125
- /** 受信任信道不使用加密,固定为 false */
126
- supportNativeEncryption: false;
127
- init(myId: string): Promise<void>;
128
- connect(id: string): Promise<void>;
129
- send(data: Uint8Array): Promise<void>;
130
- close(): void;
131
- onMessage: (handler: (data: Uint8Array) => void) => void;
132
- onClose: (handler: () => void) => void;
133
- onError: (handler: (err: Error) => void) => void;
117
+ trustIdentity: true;
118
+ /** 受信任信道不使用加密,固定为 false */
119
+ supportNativeEncryption: false;
120
+ init(myId: string): Promise<void>;
121
+ connect(id: string): Promise<void>;
122
+ send(data: Uint8Array): Promise<void>;
123
+ close(): void;
124
+ onMessage: (handler: (data: Uint8Array) => void) => void;
125
+ onClose: (handler: () => void) => void;
126
+ onError: (handler: (err: Error) => void) => void;
134
127
  }
135
128
 
136
129
  /** 不受信任的信令适配器(网络信道等),需要内部 PAKE 验证,可选加密 */
137
130
  interface UntrustedSignalingAdapter {
138
- trustIdentity: false;
139
- /** PAKE 验证后是否启用应用层加密(如 Noise) */
140
- supportNativeEncryption: boolean;
141
- init(myId: string): Promise<void>;
142
- connect(id: string): Promise<void>;
143
- send(data: Uint8Array): Promise<void>;
144
- close(): void;
145
- onMessage: (handler: (data: Uint8Array) => void) => void;
146
- onClose: (handler: () => void) => void;
147
- onError: (handler: (err: Error) => void) => void;
131
+ trustIdentity: false;
132
+ /** PAKE 验证后是否启用应用层加密(如 Noise) */
133
+ supportNativeEncryption: boolean;
134
+ init(myId: string): Promise<void>;
135
+ connect(id: string): Promise<void>;
136
+ send(data: Uint8Array): Promise<void>;
137
+ close(): void;
138
+ onMessage: (handler: (data: Uint8Array) => void) => void;
139
+ onClose: (handler: () => void) => void;
140
+ onError: (handler: (err: Error) => void) => void;
148
141
  }
149
142
 
150
143
  /** 配对请求信息(接收方通过 pairRequest 事件获取) */
151
144
  interface PairRequest {
152
- /** 发起方的设备 ID */
153
- remoteDeviceId: string;
154
- /** 发起方的显示名称(可选) */
155
- remoteDisplayName?: string;
156
- /** 输入对方的 PIN(触发 PAKE 交换) */
157
- inputOtherPin: (pin: string) => void;
158
- /** 等待配对完成 */
159
- waitForPairing: () => Promise<Credential>;
160
- /** 拒绝配对请求 */
161
- reject: () => void;
145
+ /** 发起方的设备 ID */
146
+ remoteDeviceId: string;
147
+ /** 发起方的显示名称(可选) */
148
+ remoteDisplayName?: string;
149
+ /** 输入对方的 PIN(触发 PAKE 交换) */
150
+ inputOtherPin: (pin: string) => void;
151
+ /** 等待配对完成 */
152
+ waitForPairing: () => Promise<Credential>;
153
+ /** 拒绝配对请求 */
154
+ reject: () => void;
162
155
  }
163
156
 
164
157
  /** 连接请求信息(接收方通过 connectRequest 事件获取,用于 Credential 重连) */
165
158
  interface ConnectRequest {
166
- /** 发起方的设备 ID */
167
- remoteDeviceId: string;
168
- /** 发起方的显示名称(可选) */
169
- remoteDisplayName?: string;
170
- /** 接受连接请求,传入本地保存的关于对方的 Credential */
171
- accept: (credential: Credential) => Promise<ConnectResult>;
172
- /** 拒绝连接请求 */
173
- reject: () => void;
159
+ /** 发起方的设备 ID */
160
+ remoteDeviceId: string;
161
+ /** 发起方的显示名称(可选) */
162
+ remoteDisplayName?: string;
163
+ /** 接受连接请求,传入本地保存的关于对方的 Credential */
164
+ accept: (credential: Credential) => Promise<ConnectResult>;
165
+ /** 拒绝连接请求 */
166
+ reject: () => void;
174
167
  }
175
168
 
176
169
  /** 通道配置选项 */
177
170
  interface ChannelOptions {
178
- /** 连接请求超时时间(毫秒),默认 30000 */
179
- connectTimeout?: number;
180
- /** 配对超时时间(毫秒),默认 60000(PIN 输入需要更多时间) */
181
- pairingTimeout?: number;
182
- /** 握手超时时间(毫秒),默认 30000 */
183
- handshakeTimeout?: number;
184
- /** 允许的最大 PIN 错误尝试次数,默认 5,超出后自动断开并触发 error 事件 */
185
- maxPinAttempts?: number;
186
- /** 配对请求间隔限制(毫秒),默认 1000 */
187
- pairInterval?: number;
188
- /** 连接请求间隔限制(毫秒),默认 20 */
189
- connectInterval?: number;
171
+ /** 连接请求超时时间(毫秒),默认 30000 */
172
+ connectTimeout?: number;
173
+ /** 配对超时时间(毫秒),默认 60000(PIN 输入需要更多时间) */
174
+ pairingTimeout?: number;
175
+ /** 握手超时时间(毫秒),默认 30000 */
176
+ handshakeTimeout?: number;
177
+ /** 允许的最大 PIN 错误尝试次数,默认 5,超出后自动断开并触发 error 事件 */
178
+ maxPinAttempts?: number;
179
+ /** 配对请求间隔限制(毫秒),默认 1000 */
180
+ pairInterval?: number;
181
+ /** 连接请求间隔限制(毫秒),默认 20 */
182
+ connectInterval?: number;
190
183
  }
191
184
 
192
185
  interface CredentialPublicInfo {
193
- /** 我方设备 UUID */
194
- myDeviceId: string;
195
- /** 我方可读名称(可选) */
196
- myDisplayName?: string;
197
- /** 对方设备 UUID */
198
- remoteDeviceId: string;
199
- /** 对方可读名称(可选) */
200
- remoteDisplayName?: string;
186
+ /** 我方设备 UUID */
187
+ myDeviceId: string;
188
+ /** 我方可读名称(可选) */
189
+ myDisplayName?: string;
190
+ /** 对方设备 UUID */
191
+ remoteDeviceId: string;
192
+ /** 对方可读名称(可选) */
193
+ remoteDisplayName?: string;
201
194
  }
202
195
 
203
196
  interface CredentialPrivateInfo {
204
- /** 我方长期私钥(格式取决于底层实现,可能为 CryptoKey 或 Uint8Array) */
205
- myPrivateKey: Uint8Array; // todo 使用 Web Crypto API
206
- /** 我方长期公钥(用于对方验证我方身份) */
207
- myPublicKey: Uint8Array;
208
- /** 对方长期公钥(用于验证对方身份) */
209
- remotePublicKey: Uint8Array;
210
- /** 凭证创建时间戳(毫秒) */
211
- createdAt: number;
197
+ /** 我方长期私钥(格式取决于底层实现,可能为 CryptoKey 或 Uint8Array) */
198
+ myPrivateKey: Uint8Array; // todo 使用 Web Crypto API
199
+ /** 我方长期公钥(用于对方验证我方身份) */
200
+ myPublicKey: Uint8Array;
201
+ /** 对方长期公钥(用于验证对方身份) */
202
+ remotePublicKey: Uint8Array;
203
+ /** 凭证创建时间戳(毫秒) */
204
+ createdAt: number;
212
205
  }
213
206
 
214
207
  /**
@@ -217,37 +210,35 @@ interface CredentialPrivateInfo {
217
210
  * 私钥部分(myPrivateKey)绝不应离开本地设备。
218
211
  */
219
212
  interface Credential extends CredentialPublicInfo, CredentialPrivateInfo {
220
- /** 上次成功连接时间戳(毫秒),用于 UI 排序 */
221
- lastConnected?: number;
213
+ /** 上次成功连接时间戳(毫秒),用于 UI 排序 */
214
+ lastConnected?: number;
222
215
  }
223
216
 
224
217
  /** `tryConnect` 返回的结果类型 */
225
218
  interface ConnectSuccess {
226
- success: true;
227
- /** 重连成功,可能更新了 lastConnected 等字段的凭证 */
228
- credential: Credential;
219
+ success: true;
220
+ /** 重连成功,可能更新了 lastConnected 等字段的凭证 */
221
+ credential: Credential;
229
222
  }
230
223
 
231
224
  interface ConnectFailed {
232
- success: false;
225
+ success: false;
233
226
  }
234
227
 
235
228
  interface ConnectNeedsPairing {
236
- success: false;
237
- reason: "NEEDS_PAIRING";
229
+ success: false;
230
+ reason: "NEEDS_PAIRING";
238
231
  }
239
232
 
240
233
  type ConnectResult = ConnectSuccess | ConnectFailed | ConnectNeedsPairing;
241
234
 
242
235
  export type SecureChannelEvents = {
243
- ready: () => void;
244
- data: (data: ArrayBuffer, text: () => string) => void;
245
- disconnect: () => void;
246
- error: (err: SConnectError) => void;
247
- pairRequest: (request: PairRequest) => void;
248
- connectRequest: (request: ConnectRequest) => void;
249
- credentialRotated: (updatedCredential: Credential) => void;
250
- credentialInvalidated: (remoteDeviceId: string) => void;
236
+ ready: () => void;
237
+ data: (data: ArrayBuffer, text: () => string) => void;
238
+ disconnect: () => void;
239
+ error: (err: SConnectError) => void;
240
+ pairRequest: (request: PairRequest) => void;
241
+ connectRequest: (request: ConnectRequest) => void;
251
242
  };
252
243
 
253
244
  // ================= 状态类型 =================
@@ -256,4 +247,9 @@ export type SecureChannelEvents = {
256
247
  type ChannelState = "Idle" | "Ready" | "Handshaking" | "Connected";
257
248
 
258
249
  /** 握手类型 */
259
- type HandshakeType = "pake" | "ik" | "connect-request" | "connect-response" | null;
250
+ type HandshakeType =
251
+ | "pake"
252
+ | "ik"
253
+ | "connect-request"
254
+ | "connect-response"
255
+ | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myde-remote-connect",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Secure remote connection module with PAKE pairing and Noise IK handshake",
5
5
  "type": "module",
6
6
  "main": "./dist/sconnect.js",
@@ -47,20 +47,12 @@
47
47
  "license": "AGPL-3.0-only",
48
48
  "repository": {
49
49
  "type": "git",
50
- "url": "https://github.com/xushengfeng/myde/",
51
- "directory": "src/remote_connect"
50
+ "url": "https://github.com/xushengfeng/sconnect"
52
51
  },
53
52
  "devDependencies": {
54
- "@types/b4a": "^1.6.5",
55
- "@types/bn.js": "^5.2.0",
56
- "@types/elliptic": "^6.4.18",
53
+ "biome": "^0.3.3",
54
+ "typescript": "^6.0.3",
57
55
  "vite": "^8.1.3",
58
56
  "vitest": "^4.1.9"
59
- },
60
- "dependencies": {
61
- "b4a": "^1.8.0",
62
- "bn.js": "^5.2.3",
63
- "elliptic": "^6.6.1",
64
- "noise-handshake": "^4.2.0"
65
57
  }
66
58
  }