myde-remote-connect 0.0.3 → 0.0.5

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/sconnect.js CHANGED
@@ -79,12 +79,19 @@ var SConnect = class {
79
79
  try {
80
80
  await this.signalAdapter.connect(this.remoteId);
81
81
  if (this.signalAdapter.trustIdentity) {
82
- this.setState("Connected");
83
- this.emit("ready");
84
- return {
85
- success: true,
86
- credential: this.buildCredential(credential)
87
- };
82
+ this.setState("Handshaking", "connect-request");
83
+ await this.sendTypeMessage(MSG_CONNECT_REQUEST, new TextEncoder().encode(this.myDeviceId));
84
+ if ((await this.getTypeMessage(MSG_CONNECT_ACCEPT))[0] === 1) {
85
+ this.setState("Connected");
86
+ this.emit("ready");
87
+ return {
88
+ success: true,
89
+ credential: this.buildCredential(credential)
90
+ };
91
+ } else {
92
+ this.setState("Ready");
93
+ return { success: false };
94
+ }
88
95
  }
89
96
  if (credential && "myPrivateKey" in credential && credential.myPrivateKey) {
90
97
  this.setState("Handshaking", "connect-request");
@@ -151,6 +158,13 @@ var SConnect = class {
151
158
  const request = {
152
159
  remoteDeviceId: senderId,
153
160
  accept: (credential) => {
161
+ if (this.signalAdapter.trustIdentity) {
162
+ this.setState("Connected");
163
+ this.emit("ready");
164
+ this.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([1])).catch(() => {});
165
+ } else if (!credential) throw new SConnectError("CREDENTIAL_INVALID", "Credential is required");
166
+ },
167
+ acceptWithCre: (credential) => {
154
168
  this.setState("Handshaking", "connect-response");
155
169
  this.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([1])).catch(() => {});
156
170
  return this.connectWithCredential(credential);
@@ -1 +1 @@
1
- {"version":3,"file":"sconnect.js","names":[],"sources":["../sconnect.ts"],"sourcesContent":["import type {\n\tChannelOptions,\n\tChannelState,\n\tConnectRequest,\n\tConnectResult,\n\tCredential,\n\tCredentialPrivateInfo,\n\tCredentialPublicInfo,\n\tHandshakeType,\n\tPairRequest,\n\tSecureChannel,\n\tSecureChannelEvents,\n\tSignalingAdapter,\n} from \"./sconnect_type\";\n\ninterface KeyPair {\n\tpublicKey: Uint8Array;\n\tprivateKey: Uint8Array;\n}\n\n// 协议消息类型\nconst MSG_PAIR_REQUEST = 1;\nconst MSG_USE_YOUR_PIN = 2; // 通知对方使用自己的 PIN\nconst MSG_PAIR_REJECT = 7; // 拒绝配对请求\nconst MSG_CONNECT_REQUEST = 3;\nconst MSG_CONNECT_ACCEPT = 4;\nconst MSG_ERROR = 6;\nconst MSG_SPAKE_DATA = 11;\nconst MSG_BLIND_PUBLIC_KEY = 12;\nconst MSG_CONNECT_PUBLIC_KEY = 13;\nconst MSG_CONNECT_MAC_VER = 14;\nconst MSG_SIGNING_PUBLIC_KEY = 15;\n\n// 错误类\nclass SConnectError extends Error {\n\tcode: ErrorCode;\n\trecoverable: boolean;\n\n\tconstructor(code: ErrorCode, message: string, recoverable = true) {\n\t\tsuper(message);\n\t\tthis.name = \"SConnectError\";\n\t\tthis.code = code;\n\t\tthis.recoverable = recoverable;\n\t}\n}\n\ntype ErrorCode =\n\t// 配对错误\n\t| \"PIN_INVALID\"\n\t| \"PIN_MISMATCH\"\n\t| \"PAKE_FAILED\"\n\t| \"PAIRING_FAILED\"\n\t// 连接错误\n\t| \"TIMEOUT\"\n\t| \"ADAPTER_ERROR\"\n\t| \"PEER_DISCONNECTED\"\n\t// 验证错误\n\t| \"CREDENTIAL_INVALID\"\n\t| \"CREDENTIAL_EXPIRED\"\n\t| \"IK_HANDSHAKE_FAILED\"\n\t// 状态错误\n\t| \"NOT_INITIALIZED\"\n\t| \"ALREADY_CONNECTING\"\n\t| \"CHANNEL_NOT_READY\"\n\t| \"REMOTE_ID_UNKNOWN\"\n\t// 对端错误\n\t| \"UNEXPECTED_MESSAGE\"\n\t| \"NOT_CONNECTED\"\n\t| \"ALREADY_CONNECTED\"\n\t// 致命错误\n\t| \"ADAPTER_INIT_FAILED\"\n\t| \"CRYPTO_UNAVAILABLE\";\n\nclass Limiter {\n\tprivate lastTime = 0;\n\tconstructor(private interval: number) {}\n\tcanExecute(): boolean {\n\t\tconst now = Date.now();\n\t\tif (now - this.lastTime >= this.interval) {\n\t\t\tthis.lastTime = now;\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\tcleanup() {\n\t\tthis.lastTime = 0;\n\t}\n}\n\nexport class SConnect implements SecureChannel {\n\tprivate signalAdapter: SignalingAdapter;\n\tprivate options: Required<ChannelOptions>;\n\tprivate remoteId = \"\";\n\tprivate cipher: cipher | null = null;\n\tprivate PIN = \"\";\n\tprivate eventHandlers: Map<string, Set<(...args: unknown[]) => void>> =\n\t\tnew Map();\n\n\t// 状态机\n\tprivate state: ChannelState = \"Idle\";\n\tprivate handshakeType: HandshakeType = null;\n\tprivate typeMessageQueue: { type: number; payload: Uint8Array }[] = [];\n\tprivate typeMessageResolvers: Map<\n\t\tnumber,\n\t\t{ resolve: (data: Uint8Array) => void; reject: (err: Error) => void }\n\t> = new Map();\n\n\t// 设备身份\n\tprivate myDeviceId = \"\";\n\n\t// 计时器管理\n\tprivate activeTimers: Set<ReturnType<typeof setTimeout>> = new Set();\n\n\tprivate pairLimiter: Limiter;\n\tprivate connectLimiter: Limiter;\n\n\tprivate textDecoder = new TextDecoder();\n\n\tconstructor(signalAdapter: SignalingAdapter, options?: ChannelOptions) {\n\t\tthis.signalAdapter = signalAdapter;\n\t\tthis.options = {\n\t\t\tconnectTimeout: options?.connectTimeout ?? 30000,\n\t\t\tpairingTimeout: options?.pairingTimeout ?? 60000,\n\t\t\thandshakeTimeout: options?.handshakeTimeout ?? 30000,\n\t\t\tmaxPinAttempts: options?.maxPinAttempts ?? 5,\n\t\t\tpairInterval: options?.pairInterval ?? 1000,\n\t\t\tconnectInterval: options?.connectInterval ?? 20,\n\t\t};\n\n\t\tthis.pairLimiter = new Limiter(this.options.pairInterval);\n\t\tthis.connectLimiter = new Limiter(this.options.connectInterval);\n\n\t\tthis.signalAdapter.onMessage((data) => this.handleRawMessage(data));\n\t\tthis.signalAdapter.onClose(() => this.handleDisconnect());\n\t\tthis.signalAdapter.onError((err) =>\n\t\t\tthis.emit(\"error\", this.wrapError(err)),\n\t\t);\n\t}\n\n\t// ================= 初始化 =================\n\n\tasync init(myDeviceId: string, remoteId?: string) {\n\t\tif (this.state !== \"Idle\") {\n\t\t\tthrow new SConnectError(\"NOT_INITIALIZED\", \"Already initialized\");\n\t\t}\n\n\t\tthis.myDeviceId = myDeviceId;\n\t\tif (remoteId) {\n\t\t\tthis.remoteId = remoteId;\n\t\t}\n\t\tawait this.signalAdapter.init(myDeviceId);\n\n\t\tthis.setState(\"Ready\");\n\t}\n\n\t// ================= 连接 =================\n\n\tasync tryConnect(credential?: CredentialPrivateInfo): Promise<ConnectResult> {\n\t\tif (this.state !== \"Ready\") {\n\t\t\treturn { success: false };\n\t\t}\n\n\t\tif (!this.remoteId) {\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.signalAdapter.connect(this.remoteId);\n\n\t\t\tif (this.signalAdapter.trustIdentity) {\n\t\t\t\tthis.setState(\"Connected\");\n\t\t\t\tthis.emit(\"ready\");\n\t\t\t\treturn { success: true, credential: this.buildCredential(credential) };\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tcredential &&\n\t\t\t\t\"myPrivateKey\" in credential &&\n\t\t\t\tcredential.myPrivateKey\n\t\t\t) {\n\t\t\t\tthis.setState(\"Handshaking\", \"connect-request\");\n\t\t\t\tawait this.sendTypeMessage(\n\t\t\t\t\tMSG_CONNECT_REQUEST,\n\t\t\t\t\tnew TextEncoder().encode(this.myDeviceId),\n\t\t\t\t);\n\t\t\t\tconst r = (await this.getTypeMessage(MSG_CONNECT_ACCEPT))[0];\n\t\t\t\tif (r === 1) return this.connectWithCredential(credential);\n\t\t\t}\n\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t} catch (error) {\n\t\t\tthis.setState(\"Ready\");\n\t\t\tthis.emit(\"error\", this.wrapError(error));\n\t\t\treturn { success: false };\n\t\t}\n\t}\n\n\tprivate async connectWithCredential(\n\t\tcredential: CredentialPrivateInfo,\n\t): Promise<ConnectResult> {\n\t\tconst eph = await generateKeyPair();\n\t\tconst ephSig = await sigh(credential.myPrivateKey, eph.publicKey);\n\t\tconst m = concatUint8Arrays([eph.publicKey, ephSig]);\n\t\tawait this.sendTypeMessage(MSG_CONNECT_PUBLIC_KEY, m);\n\t\tconst otherM = await this.getTypeMessage(MSG_CONNECT_PUBLIC_KEY);\n\t\tconst otherPubKey = otherM.subarray(0, eph.publicKey.length);\n\t\tconst otherSig = otherM.subarray(eph.publicKey.length);\n\t\tconst sigValid = await verifySignature(\n\t\t\tcredential.remotePublicKey,\n\t\t\totherPubKey,\n\t\t\totherSig,\n\t\t);\n\t\tif (!sigValid) {\n\t\t\tthis.setState(\"Ready\");\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t}\n\n\t\tconst dhSecret = await dh(eph.privateKey, otherPubKey);\n\n\t\tconst info = buildConnectionInfo(\n\t\t\tthis.myDeviceId,\n\t\t\tthis.remoteId,\n\t\t\teph.publicKey,\n\t\t\totherPubKey,\n\t\t);\n\t\tconst derivedKey = await derive(dhSecret, info, new Uint8Array(0));\n\t\tconst myMac = await mac(derivedKey, info);\n\t\tawait this.sendTypeMessage(MSG_CONNECT_MAC_VER, myMac);\n\t\tconst otherMac = await this.getTypeMessage(MSG_CONNECT_MAC_VER);\n\n\t\tconst otherInfo = buildConnectionInfo(\n\t\t\tthis.remoteId,\n\t\t\tthis.myDeviceId,\n\t\t\totherPubKey,\n\t\t\teph.publicKey,\n\t\t);\n\t\tconst otherDerivedKey = await derive(\n\t\t\tdhSecret,\n\t\t\totherInfo,\n\t\t\tnew Uint8Array(0),\n\t\t);\n\t\tconst otherMacCheck = await mac(otherDerivedKey, otherInfo);\n\n\t\tconst macValid = verify(otherMac, otherMacCheck);\n\t\tif (!macValid) {\n\t\t\tthis.setState(\"Ready\");\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t}\n\n\t\tif (!this.signalAdapter.supportNativeEncryption) {\n\t\t\tthis.cipher = new cipher(derivedKey, derivedKey);\n\t\t}\n\t\tthis.setState(\"Connected\");\n\t\tthis.emit(\"ready\");\n\t\treturn { success: true, credential: this.buildCredential(credential) };\n\t}\n\n\tprivate handleConnectRequest(payload: Uint8Array): void {\n\t\tconst senderId = new TextDecoder().decode(payload);\n\n\t\tif (senderId !== this.remoteId) {\n\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([0])).catch(\n\t\t\t\t() => {},\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.connectLimiter.canExecute()) {\n\t\t\t// todo 提示被频繁占用\n\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([0])).catch(\n\t\t\t\t() => {},\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst request: ConnectRequest = {\n\t\t\tremoteDeviceId: senderId,\n\t\t\taccept: (credential: Credential): Promise<ConnectResult> => {\n\t\t\t\tthis.setState(\"Handshaking\", \"connect-response\");\n\t\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([1])).catch(\n\t\t\t\t\t() => {},\n\t\t\t\t);\n\t\t\t\treturn this.connectWithCredential(credential);\n\t\t\t},\n\t\t\treject: () => {\n\t\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([0])).catch(\n\t\t\t\t\t() => {},\n\t\t\t\t);\n\t\t\t},\n\t\t};\n\n\t\tthis.emit(\"connectRequest\", request);\n\t}\n\n\t// ================= 配对 =================\n\n\tupdatePIN(): string {\n\t\tthis.PIN = this.generatePin();\n\t\treturn this.PIN;\n\t}\n\n\tasync pairInit(credential: CredentialPublicInfo): Promise<{\n\t\tpin: string;\n\t\tinputOtherPin: (pin: string) => void;\n\t\twaitForPairing: () => Promise<Credential>;\n\t}> {\n\t\tif (this.state !== \"Ready\") {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"CHANNEL_NOT_READY\",\n\t\t\t\t`Cannot pair in ${this.state} state`,\n\t\t\t);\n\t\t}\n\n\t\tconst pin = this.PIN || this.updatePIN();\n\n\t\tawait this.signalAdapter.connect(credential.remoteDeviceId);\n\n\t\tconst otherPin = Promise.withResolvers<string>();\n\n\t\tconst inputOtherPin = (remotePin: string) => {\n\t\t\tif (!this.validatePin(remotePin)) {\n\t\t\t\totherPin.reject(\n\t\t\t\t\tnew SConnectError(\"PIN_INVALID\", \"PIN must be 6 digits\"),\n\t\t\t\t);\n\t\t\t\tthrow new SConnectError(\"PIN_INVALID\", \"PIN must be 6 digits\");\n\t\t\t}\n\n\t\t\totherPin.resolve(remotePin);\n\t\t};\n\n\t\tconst waitForPairing = Promise.withResolvers<Credential>();\n\n\t\t// 发送配对请求\n\t\tthis.sendTypeMessage(\n\t\t\tMSG_PAIR_REQUEST,\n\t\t\tnew TextEncoder().encode(this.myDeviceId),\n\t\t).catch(() => {});\n\n\t\tthis.getTypeMessage(MSG_PAIR_REJECT).then(() => {\n\t\t\twaitForPairing.reject(\n\t\t\t\tnew SConnectError(\"PAIRING_FAILED\", \"Pairing request was rejected\"),\n\t\t\t);\n\t\t});\n\n\t\treturn {\n\t\t\tpin,\n\t\t\tinputOtherPin,\n\t\t\twaitForPairing: () => {\n\t\t\t\tthis.genWaitForPairing({\n\t\t\t\t\tremoteDeviceId: credential.remoteDeviceId,\n\t\t\t\t\totherPin: otherPin.promise,\n\t\t\t\t\tPIN: pin,\n\t\t\t\t})\n\t\t\t\t\t.then(waitForPairing.resolve)\n\t\t\t\t\t.catch(waitForPairing.reject);\n\t\t\t\treturn waitForPairing.promise;\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate async genWaitForPairing(op: {\n\t\tremoteDeviceId: string;\n\t\totherPin: Promise<string>;\n\t\tPIN: string;\n\t}): Promise<Credential> {\n\t\tconst keyPair = await generateKeyPair();\n\t\tconst finalPin = await Promise.race([\n\t\t\t(async () => {\n\t\t\t\tconst pin = await op.otherPin;\n\t\t\t\tawait this.sendTypeMessage(MSG_USE_YOUR_PIN);\n\t\t\t\treturn pin;\n\t\t\t})(),\n\t\t\t(async () => {\n\t\t\t\tawait this.getTypeMessage(MSG_USE_YOUR_PIN);\n\t\t\t\treturn op.PIN;\n\t\t\t})(),\n\t\t]);\n\t\tconst bindPublicKey = blind(finalPin, keyPair.publicKey);\n\t\tawait this.sendTypeMessage(MSG_BLIND_PUBLIC_KEY, bindPublicKey);\n\t\tconst otherBindPublicKey = await this.getTypeMessage(MSG_BLIND_PUBLIC_KEY);\n\t\tif (otherBindPublicKey === undefined) {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"PAIRING_FAILED\",\n\t\t\t\t\"Failed to receive blinded public key from peer\",\n\t\t\t);\n\t\t}\n\t\tconst otherPublicKey = deBlind(otherBindPublicKey, finalPin);\n\t\tconst s = await dh(keyPair.privateKey, otherPublicKey);\n\n\t\tconst info = buildPairingInfo(\n\t\t\tfinalPin,\n\t\t\tthis.myDeviceId,\n\t\t\top.remoteDeviceId,\n\t\t\tkeyPair.publicKey,\n\t\t\totherPublicKey,\n\t\t);\n\t\tconst derivedKey = await derive(\n\t\t\ts,\n\t\t\tinfo,\n\t\t\tnew TextEncoder().encode(finalPin),\n\t\t);\n\t\tconst myMac = await mac(derivedKey, info);\n\t\tawait this.sendTypeMessage(MSG_SPAKE_DATA, myMac);\n\t\tconst otherMac = await this.getTypeMessage(MSG_SPAKE_DATA);\n\n\t\tconst otherInfo = buildPairingInfo(\n\t\t\tfinalPin,\n\t\t\top.remoteDeviceId,\n\t\t\tthis.myDeviceId,\n\t\t\totherPublicKey,\n\t\t\tkeyPair.publicKey,\n\t\t);\n\t\tconst otherDerivedKey = await derive(\n\t\t\ts,\n\t\t\totherInfo,\n\t\t\tnew TextEncoder().encode(finalPin),\n\t\t);\n\t\tconst otherMacCheck = await mac(otherDerivedKey, otherInfo);\n\n\t\tif (!verify(otherMac, otherMacCheck)) {\n\t\t\tthrow new SConnectError(\"PAIRING_FAILED\", \"Pairing verification failed\");\n\t\t}\n\n\t\t// todo 派生\n\t\tconst signingKeyPair = await generateSigningKeyPair();\n\t\tawait this.sendTypeMessage(\n\t\t\tMSG_SIGNING_PUBLIC_KEY,\n\t\t\tsigningKeyPair.publicKey,\n\t\t);\n\t\tconst remoteSigningPublicKey = await this.getTypeMessage(\n\t\t\tMSG_SIGNING_PUBLIC_KEY,\n\t\t);\n\n\t\tif (!remoteSigningPublicKey) {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"PAIRING_FAILED\",\n\t\t\t\t\"Failed to receive signing public key\",\n\t\t\t);\n\t\t}\n\n\t\tif (!this.signalAdapter.supportNativeEncryption) {\n\t\t\t// todo 派生\n\t\t\tthis.cipher = new cipher(\n\t\t\t\tawait dh(keyPair.privateKey, otherPublicKey),\n\t\t\t\tawait dh(keyPair.privateKey, otherPublicKey),\n\t\t\t);\n\t\t}\n\n\t\tthis.setState(\"Connected\");\n\n\t\treturn {\n\t\t\tmyPrivateKey: signingKeyPair.privateKey,\n\t\t\tmyPublicKey: signingKeyPair.publicKey,\n\t\t\tremotePublicKey: remoteSigningPublicKey,\n\t\t\tcreatedAt: Date.now(),\n\t\t\tmyDeviceId: this.myDeviceId,\n\t\t\tremoteDeviceId: this.remoteId,\n\t\t};\n\t}\n\n\tprivate handlePairRequest(payload: Uint8Array): void {\n\t\tif (!this.pairLimiter.canExecute()) {\n\t\t\t// 考虑到id都是临时可变的,无法区分真实设备,所以全部限制\n\t\t\t// todo 中间信息攻击,比如useyourpin dos,或者其它,需要状态机限制顺序和个数\n\t\t\t// todo 提示被频繁占用\n\t\t\t// todo 其他可能的配对方式\n\t\t\tthis.sendTypeMessage(MSG_PAIR_REJECT).catch(() => {}); // todo 区分拒绝原因\n\t\t\treturn;\n\t\t}\n\n\t\tconst senderId = new TextDecoder().decode(payload);\n\n\t\tconst otherPin = Promise.withResolvers<string>();\n\n\t\tconst request: PairRequest = {\n\t\t\tremoteDeviceId: senderId,\n\t\t\tinputOtherPin: (pin: string): void => {\n\t\t\t\tif (!this.validatePin(pin)) {\n\t\t\t\t\totherPin.reject();\n\t\t\t\t\tthrow new SConnectError(\"PIN_INVALID\", \"PIN must be 6 digits\");\n\t\t\t\t}\n\n\t\t\t\totherPin.resolve(pin);\n\t\t\t},\n\t\t\twaitForPairing: () => {\n\t\t\t\treturn this.genWaitForPairing({\n\t\t\t\t\tremoteDeviceId: senderId,\n\t\t\t\t\totherPin: otherPin.promise,\n\t\t\t\t\tPIN: this.PIN,\n\t\t\t\t});\n\t\t\t},\n\t\t\treject: () => {\n\t\t\t\t// 通知对方配对被拒绝\n\t\t\t\tthis.sendTypeMessage(MSG_PAIR_REJECT).catch(() => {});\n\t\t\t},\n\t\t};\n\n\t\tthis.emit(\"pairRequest\", request);\n\t}\n\n\t// ================= 断开 =================\n\n\tdisconnect(): void {\n\t\tif (this.state === \"Idle\") return;\n\n\t\tthis.cipher = null;\n\n\t\tthis.signalAdapter.close();\n\t\tthis.setState(\"Ready\");\n\t\tthis.emit(\"disconnect\");\n\t}\n\n\tdestroy(): void {\n\t\tthis.cipher = null;\n\n\t\tthis.signalAdapter.close();\n\t\tthis.myDeviceId = \"\";\n\t\tthis.setState(\"Idle\");\n\t}\n\n\t// ================= 数据发送 =================\n\n\tasync send(payload: string): Promise<void> {\n\t\tif (this.state !== \"Connected\") {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"CHANNEL_NOT_READY\",\n\t\t\t\t`Cannot send in ${this.state} state`,\n\t\t\t);\n\t\t}\n\n\t\tconst data = new TextEncoder().encode(payload);\n\t\tawait this.sendData(data);\n\t}\n\n\tasync sendBinary(data: ArrayBuffer | Uint8Array): Promise<void> {\n\t\tif (this.state !== \"Connected\") {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"CHANNEL_NOT_READY\",\n\t\t\t\t`Cannot send in ${this.state} state`,\n\t\t\t);\n\t\t}\n\n\t\tconst buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;\n\t\tawait this.sendData(buffer);\n\t}\n\n\tprivate async sendData(data: Uint8Array): Promise<void> {\n\t\tif (this.cipher) {\n\t\t\tconst encrypted = await this.cipher.encrypt(data);\n\t\t\tawait this.signalAdapter.send(encrypted);\n\t\t} else {\n\t\t\tawait this.signalAdapter.send(data);\n\t\t}\n\t}\n\n\t// ================= 事件 =================\n\n\ton<K extends keyof SecureChannelEvents>(\n\t\tevent: K,\n\t\tcallback: SecureChannelEvents[K],\n\t): void {\n\t\tif (!this.eventHandlers.has(event)) {\n\t\t\tthis.eventHandlers.set(event, new Set());\n\t\t}\n\t\tconst handlers = this.eventHandlers.get(event);\n\t\tif (handlers) {\n\t\t\thandlers.add(callback as (...args: unknown[]) => void);\n\t\t}\n\t}\n\n\toff(event: string, callback: (...args: unknown[]) => void): void {\n\t\tconst handlers = this.eventHandlers.get(event);\n\t\tif (handlers) {\n\t\t\thandlers.delete(callback);\n\t\t}\n\t}\n\n\t// ================= 状态机 =================\n\n\tprivate setState(\n\t\tnewState: ChannelState,\n\t\thandshakeType?: HandshakeType,\n\t): void {\n\t\tconst oldState = this.state;\n\t\tthis.state = newState;\n\t\tthis.handshakeType =\n\t\t\thandshakeType ?? (newState === \"Handshaking\" ? this.handshakeType : null);\n\n\t\t// 状态改变时清理所有计时器\n\t\tif (oldState !== newState) {\n\t\t\tthis.clearAllTimers();\n\t\t}\n\t}\n\n\t// ================= 计时器管理 =================\n\n\tprivate clearAllTimers(): void {\n\t\tfor (const timer of this.activeTimers) {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t\tthis.activeTimers.clear();\n\t}\n\n\t// ================= 消息处理 =================\n\n\tprivate handleRawMessage(data: Uint8Array): void {\n\t\tif (this.state === \"Connected\") {\n\t\t\tthis.handleAppData(data).catch(() => {});\n\t\t} else {\n\t\t\tconst type = data[0];\n\t\t\tconst payload = data.subarray(1);\n\n\t\t\t// 错误消息任何状态都处理\n\t\t\tif (type === MSG_ERROR) {\n\t\t\t\tthis.handleErrorFromPeer(payload);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst handlers = this.typeMessageResolvers.get(type);\n\t\t\tif (handlers) {\n\t\t\t\thandlers.resolve(payload);\n\t\t\t} else {\n\t\t\t\tthis.typeMessageQueue.push({ type, payload });\n\t\t\t}\n\n\t\t\tswitch (this.state) {\n\t\t\t\tcase \"Idle\":\n\t\t\t\t\treturn;\n\n\t\t\t\tcase \"Ready\":\n\t\t\t\t\tswitch (type) {\n\t\t\t\t\t\tcase MSG_PAIR_REQUEST:\n\t\t\t\t\t\t\tthis.handlePairRequest(payload);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase MSG_CONNECT_REQUEST:\n\t\t\t\t\t\t\tthis.handleConnectRequest(payload);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async handleAppData(data: Uint8Array): Promise<void> {\n\t\tlet payload = data;\n\n\t\tif (this.cipher) {\n\t\t\ttry {\n\t\t\t\tpayload = new Uint8Array(await this.cipher.decrypt(payload));\n\t\t\t} catch {\n\t\t\t\tthis.emit(\"data\", new Uint8Array(payload).buffer as ArrayBuffer, () =>\n\t\t\t\t\tthis.textDecoder.decode(payload),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.emit(\"data\", payload.buffer as ArrayBuffer, () =>\n\t\t\tthis.textDecoder.decode(payload),\n\t\t);\n\t}\n\n\t// ================= 错误消息 =================\n\n\tprivate handleErrorFromPeer(payload: Uint8Array): void {\n\t\tconst text = new TextDecoder().decode(payload);\n\t\ttry {\n\t\t\tconst error = JSON.parse(text);\n\t\t\tthis.emit(\"error\", new SConnectError(error.code, error.message));\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\n\t// ================= 工具方法 =================\n\n\tprivate async sendTypeMessage(\n\t\ttype: number,\n\t\tpayload?: Uint8Array,\n\t): Promise<void> {\n\t\tconst message = new Uint8Array(1 + (payload ? payload.length : 0));\n\t\tmessage[0] = type;\n\t\tif (payload) {\n\t\t\tmessage.set(payload, 1);\n\t\t}\n\t\tawait this.signalAdapter.send(message);\n\t}\n\n\tprivate getTypeMessage(type: number) {\n\t\tconst p = Promise.withResolvers<Uint8Array>();\n\t\tconst recived = this.typeMessageQueue.findIndex((msg) => msg.type === type);\n\t\tif (recived !== -1) {\n\t\t\tconst msg = this.typeMessageQueue.splice(recived, 1)[0];\n\t\t\tp.resolve(msg.payload);\n\t\t\treturn p.promise;\n\t\t}\n\t\tthis.typeMessageResolvers.set(type, {\n\t\t\tresolve: (data: Uint8Array) => {\n\t\t\t\tthis.typeMessageResolvers.delete(type);\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tp.resolve(data);\n\t\t\t},\n\t\t\treject: (err: Error) => {\n\t\t\t\tthis.typeMessageResolvers.delete(type);\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tp.reject(err);\n\t\t\t},\n\t\t});\n\t\tconst timer = setTimeout(() => {\n\t\t\tthis.typeMessageResolvers.delete(type);\n\t\t\tp.reject(new SConnectError(\"TIMEOUT\", `Timeout waiting for message type ${type}`));\n\t\t}, this.options.handshakeTimeout);\n\t\tthis.activeTimers.add(timer);\n\t\treturn p.promise;\n\t}\n\n\tprivate buildCredential(\n\t\tcredential: CredentialPrivateInfo | undefined,\n\t): Credential {\n\t\treturn {\n\t\t\tmyDeviceId: this.myDeviceId,\n\t\t\tremoteDeviceId: this.remoteId,\n\t\t\tmyPrivateKey: new Uint8Array(),\n\t\t\tmyPublicKey: new Uint8Array(),\n\t\t\tremotePublicKey: new Uint8Array(),\n\t\t\tcreatedAt: Date.now(),\n\t\t\tlastConnected: Date.now(),\n\t\t\t...credential,\n\t\t};\n\t}\n\n\tprivate handleDisconnect(): void {\n\t\tif (this.state === \"Idle\") return;\n\t\tthis.cipher = null;\n\t\tthis.setState(\"Ready\");\n\t\tthis.emit(\"disconnect\");\n\t}\n\n\tprivate emit<K extends keyof SecureChannelEvents>(\n\t\tevent: K,\n\t\t...args: Parameters<SecureChannelEvents[K]>\n\t): void {\n\t\tconst handlers = this.eventHandlers.get(event);\n\t\tif (handlers) {\n\t\t\tfor (const handler of handlers) {\n\t\t\t\ttry {\n\t\t\t\t\thandler(...args);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconsole.error(`Error in event handler for ${event}:`, err);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate generatePin(): string {\n\t\treturn Math.floor(100000 + Math.random() * 900000).toString();\n\t}\n\n\tprivate validatePin(pin: string): boolean {\n\t\treturn /^\\d{6}$/.test(pin);\n\t}\n\n\tprivate wrapError(error: unknown): SConnectError {\n\t\tif (error instanceof SConnectError) return error;\n\t\treturn new SConnectError(\n\t\t\t\"ADAPTER_ERROR\",\n\t\t\terror instanceof Error ? error.message : String(error),\n\t\t);\n\t}\n}\n\nexport async function generateKeyPair(): Promise<KeyPair> {\n\tconst keyPair = await crypto.subtle.generateKey(\n\t\t{ name: \"X25519\" } as AlgorithmIdentifier,\n\t\ttrue,\n\t\t[\"deriveBits\"],\n\t);\n\n\tconst publicKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"raw\",\n\t\t(keyPair as CryptoKeyPair).publicKey,\n\t);\n\tconst privateKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"pkcs8\",\n\t\t(keyPair as CryptoKeyPair).privateKey,\n\t);\n\n\treturn {\n\t\tpublicKey: new Uint8Array(publicKeyBuffer),\n\t\tprivateKey: new Uint8Array(privateKeyBuffer).subarray(16),\n\t};\n}\n\nexport async function generateSigningKeyPair(): Promise<KeyPair> {\n\tconst keyPair = await crypto.subtle.generateKey(\"Ed25519\", true, [\n\t\t\"sign\",\n\t\t\"verify\",\n\t]);\n\tconst publicKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"raw\",\n\t\t(keyPair as CryptoKeyPair).publicKey,\n\t);\n\tconst privateKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"pkcs8\",\n\t\t(keyPair as CryptoKeyPair).privateKey,\n\t);\n\treturn {\n\t\tpublicKey: new Uint8Array(publicKeyBuffer),\n\t\tprivateKey: new Uint8Array(privateKeyBuffer).subarray(16),\n\t};\n}\n\nexport class cipher {\n\tprivate sendCryptoKey: CryptoKey | null = null;\n\tprivate receiveCryptoKey: CryptoKey | null = null;\n\n\tconstructor(\n\t\tprivate sendKey: Uint8Array,\n\t\tprivate receiveKey: Uint8Array,\n\t) {}\n\n\tprivate async getSendKey(): Promise<CryptoKey> {\n\t\tif (!this.sendCryptoKey) {\n\t\t\tthis.sendCryptoKey = await crypto.subtle.importKey(\n\t\t\t\t\"raw\",\n\t\t\t\tthis.sendKey as unknown as BufferSource,\n\t\t\t\t{ name: \"AES-GCM\" },\n\t\t\t\tfalse,\n\t\t\t\t[\"encrypt\"],\n\t\t\t);\n\t\t}\n\t\treturn this.sendCryptoKey;\n\t}\n\n\tprivate async getReceiveKey(): Promise<CryptoKey> {\n\t\tif (!this.receiveCryptoKey) {\n\t\t\tthis.receiveCryptoKey = await crypto.subtle.importKey(\n\t\t\t\t\"raw\",\n\t\t\t\tthis.receiveKey as unknown as BufferSource,\n\t\t\t\t{ name: \"AES-GCM\" },\n\t\t\t\tfalse,\n\t\t\t\t[\"decrypt\"],\n\t\t\t);\n\t\t}\n\t\treturn this.receiveCryptoKey;\n\t}\n\n\tasync encrypt(data: Uint8Array): Promise<Uint8Array> {\n\t\tconst key = await this.getSendKey();\n\t\tconst iv = crypto.getRandomValues(new Uint8Array(12));\n\t\tconst encrypted = await crypto.subtle.encrypt(\n\t\t\t{ name: \"AES-GCM\", iv },\n\t\t\tkey,\n\t\t\tdata as unknown as BufferSource,\n\t\t);\n\t\tconst result = new Uint8Array(iv.length + encrypted.byteLength);\n\t\tresult.set(iv);\n\t\tresult.set(new Uint8Array(encrypted), iv.length);\n\t\treturn result;\n\t}\n\n\tasync decrypt(data: Uint8Array): Promise<Uint8Array> {\n\t\tconst key = await this.getReceiveKey();\n\t\tconst iv = new Uint8Array(data.buffer, data.byteOffset, 12);\n\t\tconst ciphertext = new Uint8Array(data.buffer, data.byteOffset + 12);\n\t\tconst decrypted = await crypto.subtle.decrypt(\n\t\t\t{ name: \"AES-GCM\", iv: iv as unknown as BufferSource },\n\t\t\tkey,\n\t\t\tciphertext as unknown as BufferSource,\n\t\t);\n\t\treturn new Uint8Array(decrypted);\n\t}\n}\n\n// todo 更安全做法\nexport function blind(pin: string, publicKey: Uint8Array): Uint8Array {\n\t// 将 PIN 转换为 Uint8Array\n\tconst pinBytes = new TextEncoder().encode(pin);\n\n\t// 创建一个新的 Uint8Array 来存储盲化后的公钥\n\tconst blindedKey = new Uint8Array(publicKey.length);\n\n\t// 对每个字节进行盲化处理\n\tfor (let i = 0; i < publicKey.length; i++) {\n\t\tblindedKey[i] = publicKey[i] ^ pinBytes[i % pinBytes.length];\n\t}\n\n\treturn blindedKey;\n}\n\nexport function deBlind(blindedKey: Uint8Array, pin: string): Uint8Array {\n\t// 将 PIN 转换为 Uint8Array\n\tconst pinBytes = new TextEncoder().encode(pin);\n\n\t// 创建一个新的 Uint8Array 来存储去盲化后的公钥\n\tconst originalKey = new Uint8Array(blindedKey.length);\n\n\t// 对每个字节进行去盲化处理\n\tfor (let i = 0; i < blindedKey.length; i++) {\n\t\toriginalKey[i] = blindedKey[i] ^ pinBytes[i % pinBytes.length];\n\t}\n\n\treturn originalKey;\n}\n\nexport async function dh(\n\tprivateKey: Uint8Array,\n\tpublicKey: Uint8Array,\n): Promise<Uint8Array> {\n\tconst privKey = await crypto.subtle.importKey(\n\t\t\"pkcs8\",\n\t\tbuildX25519Pkcs8(privateKey),\n\t\t\"X25519\",\n\t\tfalse,\n\t\t[\"deriveBits\"],\n\t);\n\tconst pubKey = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tpublicKey as unknown as BufferSource,\n\t\t\"X25519\",\n\t\tfalse,\n\t\t[],\n\t);\n\tconst bits = await crypto.subtle.deriveBits(\n\t\t{ name: \"X25519\", public: pubKey },\n\t\tprivKey,\n\t\t256,\n\t);\n\treturn new Uint8Array(bits);\n}\n\nfunction buildX25519Pkcs8(rawPrivKey: Uint8Array): ArrayBuffer {\n\tconst header = new Uint8Array([\n\t\t0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,\n\t\t0x04, 0x22, 0x04, 0x20,\n\t]);\n\tconst buf = new Uint8Array(header.length + rawPrivKey.length);\n\tbuf.set(header, 0);\n\tbuf.set(rawPrivKey, header.length);\n\treturn buf.buffer;\n}\n\nfunction buildEd25519Pkcs8(rawPrivKey: Uint8Array): ArrayBuffer {\n\tconst header = new Uint8Array([\n\t\t0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,\n\t\t0x04, 0x22, 0x04, 0x20,\n\t]);\n\tconst buf = new Uint8Array(header.length + rawPrivKey.length);\n\tbuf.set(header, 0);\n\tbuf.set(rawPrivKey, header.length);\n\treturn buf.buffer;\n}\n\nfunction buildPairingInfo(\n\tpin: string,\n\tlocalId: string,\n\tremoteId: string,\n\tlocalEphemeralPk: Uint8Array,\n\tremoteEphemeralPk: Uint8Array,\n): ArrayBuffer {\n\tconst enc = new TextEncoder();\n\tconst pinBytes = enc.encode(pin);\n\tconst localIdBytes = enc.encode(localId);\n\tconst remoteIdBytes = enc.encode(remoteId);\n\n\tconst buf = new Uint8Array(\n\t\t[\n\t\t\tpinBytes,\n\t\t\tlocalIdBytes,\n\t\t\tremoteIdBytes,\n\t\t\tlocalEphemeralPk,\n\t\t\tremoteEphemeralPk,\n\t\t].flatMap((arr) => [...arr]),\n\t);\n\n\treturn buf.buffer;\n}\n\nasync function derive(\n\tikm: Uint8Array,\n\tinfo: ArrayBuffer,\n\tsalt: Uint8Array,\n): Promise<Uint8Array> {\n\tconst key = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tikm as unknown as BufferSource,\n\t\t\"HKDF\",\n\t\tfalse,\n\t\t[\"deriveBits\"],\n\t);\n\tconst bits = await crypto.subtle.deriveBits(\n\t\t{\n\t\t\tname: \"HKDF\",\n\t\t\thash: \"SHA-256\",\n\t\t\tsalt: salt as unknown as BufferSource,\n\t\t\tinfo,\n\t\t},\n\t\tkey,\n\t\t256,\n\t);\n\treturn new Uint8Array(bits);\n}\n\nasync function mac(key: Uint8Array, data: ArrayBuffer): Promise<Uint8Array> {\n\tconst cryptoKey = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tkey as unknown as BufferSource,\n\t\t{ name: \"HMAC\", hash: \"SHA-256\" },\n\t\tfalse,\n\t\t[\"sign\"],\n\t);\n\tconst sig = await crypto.subtle.sign(\"HMAC\", cryptoKey, data);\n\treturn new Uint8Array(sig);\n}\n\nfunction verify(key: Uint8Array, key2: Uint8Array) {\n\tif (key.byteLength !== key2.byteLength) return false;\n\tconst a = key;\n\tconst b = key2;\n\tlet diff = 0;\n\tfor (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];\n\treturn diff === 0;\n}\n\nfunction concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {\n\tconst totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);\n\tconst result = new Uint8Array(totalLength);\n\tlet offset = 0;\n\tfor (const arr of arrays) {\n\t\tresult.set(arr, offset);\n\t\toffset += arr.length;\n\t}\n\treturn result;\n}\n\nexport async function sigh(\n\tprivateKey: Uint8Array,\n\tdata: Uint8Array,\n): Promise<Uint8Array> {\n\tconst key = await crypto.subtle.importKey(\n\t\t\"pkcs8\",\n\t\tbuildEd25519Pkcs8(privateKey),\n\t\t\"Ed25519\",\n\t\tfalse,\n\t\t[\"sign\"],\n\t);\n\tconst sig = await crypto.subtle.sign(\n\t\t\"Ed25519\",\n\t\tkey,\n\t\tdata as unknown as BufferSource,\n\t);\n\treturn new Uint8Array(sig);\n}\n\nexport async function verifySignature(\n\tpublicKey: Uint8Array,\n\tdata: Uint8Array,\n\tsignature: Uint8Array,\n): Promise<boolean> {\n\tconst key = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tpublicKey as unknown as BufferSource,\n\t\t\"Ed25519\",\n\t\tfalse,\n\t\t[\"verify\"],\n\t);\n\treturn crypto.subtle.verify(\n\t\t\"Ed25519\",\n\t\tkey,\n\t\tsignature as unknown as BufferSource,\n\t\tdata as unknown as BufferSource,\n\t);\n}\n\nfunction buildConnectionInfo(\n\tlocalId: string,\n\tremoteId: string,\n\tlocalEphemeralPk: Uint8Array,\n\tremoteEphemeralPk: Uint8Array,\n): ArrayBuffer {\n\tconst enc = new TextEncoder();\n\tconst localIdBytes = enc.encode(localId);\n\tconst remoteIdBytes = enc.encode(remoteId);\n\treturn concatUint8Arrays([\n\t\tlocalIdBytes,\n\t\tremoteIdBytes,\n\t\tlocalEphemeralPk,\n\t\tremoteEphemeralPk,\n\t]).buffer as ArrayBuffer;\n}\n"],"mappings":";AAqBA,IAAM,mBAAmB;AACzB,IAAM,mBAAmB;AACzB,IAAM,kBAAkB;AACxB,IAAM,sBAAsB;AAC5B,IAAM,qBAAqB;AAC3B,IAAM,YAAY;AAClB,IAAM,iBAAiB;AACvB,IAAM,uBAAuB;AAC7B,IAAM,yBAAyB;AAC/B,IAAM,sBAAsB;AAC5B,IAAM,yBAAyB;AAG/B,IAAM,gBAAN,cAA4B,MAAM;CAIjC,YAAY,MAAiB,SAAiB,cAAc,MAAM;EACjE,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,cAAc;CACpB;AACD;AA6BA,IAAM,UAAN,MAAc;CAEb,YAAY,UAA0B;EAAlB,KAAA,WAAA;kBADD;CACoB;CACvC,aAAsB;EACrB,MAAM,MAAM,KAAK,IAAI;EACrB,IAAI,MAAM,KAAK,YAAY,KAAK,UAAU;GACzC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ACA,UACA,kBACA,mBACc;CACd,MAAM,MAAM,IAAI,YAAY;CAG5B,OAAO,kBAAkB;EAFJ,IAAI,OAAO,OAG/B;EAFqB,IAAI,OAAO,QAGhC;EACA;EACA;CACD,CAAC,CAAC,CAAC;AACJ"}
1
+ {"version":3,"file":"sconnect.js","names":[],"sources":["../sconnect.ts"],"sourcesContent":["import type {\n\tChannelOptions,\n\tChannelState,\n\tConnectRequest,\n\tConnectResult,\n\tCredential,\n\tCredentialPrivateInfo,\n\tCredentialPublicInfo,\n\tHandshakeType,\n\tPairRequest,\n\tSecureChannel,\n\tSecureChannelEvents,\n\tSignalingAdapter,\n} from \"./sconnect_type\";\n\ninterface KeyPair {\n\tpublicKey: Uint8Array;\n\tprivateKey: Uint8Array;\n}\n\n// 协议消息类型\nconst MSG_PAIR_REQUEST = 1;\nconst MSG_USE_YOUR_PIN = 2; // 通知对方使用自己的 PIN\nconst MSG_PAIR_REJECT = 7; // 拒绝配对请求\nconst MSG_CONNECT_REQUEST = 3;\nconst MSG_CONNECT_ACCEPT = 4;\nconst MSG_ERROR = 6;\nconst MSG_SPAKE_DATA = 11;\nconst MSG_BLIND_PUBLIC_KEY = 12;\nconst MSG_CONNECT_PUBLIC_KEY = 13;\nconst MSG_CONNECT_MAC_VER = 14;\nconst MSG_SIGNING_PUBLIC_KEY = 15;\n\n// 错误类\nclass SConnectError extends Error {\n\tcode: ErrorCode;\n\trecoverable: boolean;\n\n\tconstructor(code: ErrorCode, message: string, recoverable = true) {\n\t\tsuper(message);\n\t\tthis.name = \"SConnectError\";\n\t\tthis.code = code;\n\t\tthis.recoverable = recoverable;\n\t}\n}\n\ntype ErrorCode =\n\t// 配对错误\n\t| \"PIN_INVALID\"\n\t| \"PIN_MISMATCH\"\n\t| \"PAKE_FAILED\"\n\t| \"PAIRING_FAILED\"\n\t// 连接错误\n\t| \"TIMEOUT\"\n\t| \"ADAPTER_ERROR\"\n\t| \"PEER_DISCONNECTED\"\n\t// 验证错误\n\t| \"CREDENTIAL_INVALID\"\n\t| \"CREDENTIAL_EXPIRED\"\n\t| \"IK_HANDSHAKE_FAILED\"\n\t// 状态错误\n\t| \"NOT_INITIALIZED\"\n\t| \"ALREADY_CONNECTING\"\n\t| \"CHANNEL_NOT_READY\"\n\t| \"REMOTE_ID_UNKNOWN\"\n\t// 对端错误\n\t| \"UNEXPECTED_MESSAGE\"\n\t| \"NOT_CONNECTED\"\n\t| \"ALREADY_CONNECTED\"\n\t// 致命错误\n\t| \"ADAPTER_INIT_FAILED\"\n\t| \"CRYPTO_UNAVAILABLE\";\n\nclass Limiter {\n\tprivate lastTime = 0;\n\tconstructor(private interval: number) {}\n\tcanExecute(): boolean {\n\t\tconst now = Date.now();\n\t\tif (now - this.lastTime >= this.interval) {\n\t\t\tthis.lastTime = now;\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\tcleanup() {\n\t\tthis.lastTime = 0;\n\t}\n}\n\nexport class SConnect implements SecureChannel {\n\tprivate signalAdapter: SignalingAdapter;\n\tprivate options: Required<ChannelOptions>;\n\tprivate remoteId = \"\";\n\tprivate cipher: cipher | null = null;\n\tprivate PIN = \"\";\n\tprivate eventHandlers: Map<string, Set<(...args: unknown[]) => void>> =\n\t\tnew Map();\n\n\t// 状态机\n\tprivate state: ChannelState = \"Idle\";\n\tprivate handshakeType: HandshakeType = null;\n\tprivate typeMessageQueue: { type: number; payload: Uint8Array }[] = [];\n\tprivate typeMessageResolvers: Map<\n\t\tnumber,\n\t\t{ resolve: (data: Uint8Array) => void; reject: (err: Error) => void }\n\t> = new Map();\n\n\t// 设备身份\n\tprivate myDeviceId = \"\";\n\n\t// 计时器管理\n\tprivate activeTimers: Set<ReturnType<typeof setTimeout>> = new Set();\n\n\tprivate pairLimiter: Limiter;\n\tprivate connectLimiter: Limiter;\n\n\tprivate textDecoder = new TextDecoder();\n\n\tconstructor(signalAdapter: SignalingAdapter, options?: ChannelOptions) {\n\t\tthis.signalAdapter = signalAdapter;\n\t\tthis.options = {\n\t\t\tconnectTimeout: options?.connectTimeout ?? 30000,\n\t\t\tpairingTimeout: options?.pairingTimeout ?? 60000,\n\t\t\thandshakeTimeout: options?.handshakeTimeout ?? 30000,\n\t\t\tmaxPinAttempts: options?.maxPinAttempts ?? 5,\n\t\t\tpairInterval: options?.pairInterval ?? 1000,\n\t\t\tconnectInterval: options?.connectInterval ?? 20,\n\t\t};\n\n\t\tthis.pairLimiter = new Limiter(this.options.pairInterval);\n\t\tthis.connectLimiter = new Limiter(this.options.connectInterval);\n\n\t\tthis.signalAdapter.onMessage((data) => this.handleRawMessage(data));\n\t\tthis.signalAdapter.onClose(() => this.handleDisconnect());\n\t\tthis.signalAdapter.onError((err) =>\n\t\t\tthis.emit(\"error\", this.wrapError(err)),\n\t\t);\n\t}\n\n\t// ================= 初始化 =================\n\n\tasync init(myDeviceId: string, remoteId?: string) {\n\t\tif (this.state !== \"Idle\") {\n\t\t\tthrow new SConnectError(\"NOT_INITIALIZED\", \"Already initialized\");\n\t\t}\n\n\t\tthis.myDeviceId = myDeviceId;\n\t\tif (remoteId) {\n\t\t\tthis.remoteId = remoteId;\n\t\t}\n\t\tawait this.signalAdapter.init(myDeviceId);\n\n\t\tthis.setState(\"Ready\");\n\t}\n\n\t// ================= 连接 =================\n\n\tasync tryConnect(credential?: CredentialPrivateInfo): Promise<ConnectResult> {\n\t\tif (this.state !== \"Ready\") {\n\t\t\treturn { success: false };\n\t\t}\n\n\t\tif (!this.remoteId) {\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.signalAdapter.connect(this.remoteId);\n\n\t\t\tif (this.signalAdapter.trustIdentity) {\n\t\t\t\tthis.setState(\"Handshaking\", \"connect-request\");\n\t\t\t\tawait this.sendTypeMessage(\n\t\t\t\t\tMSG_CONNECT_REQUEST,\n\t\t\t\t\tnew TextEncoder().encode(this.myDeviceId),\n\t\t\t\t);\n\t\t\t\tconst r = (await this.getTypeMessage(MSG_CONNECT_ACCEPT))[0];\n\t\t\t\tif (r === 1) {\n\t\t\t\t\tthis.setState(\"Connected\");\n\t\t\t\t\tthis.emit(\"ready\");\n\t\t\t\t\treturn {\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\tcredential: this.buildCredential(credential),\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tthis.setState(\"Ready\");\n\t\t\t\t\treturn { success: false };\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tcredential &&\n\t\t\t\t\"myPrivateKey\" in credential &&\n\t\t\t\tcredential.myPrivateKey\n\t\t\t) {\n\t\t\t\tthis.setState(\"Handshaking\", \"connect-request\");\n\t\t\t\tawait this.sendTypeMessage(\n\t\t\t\t\tMSG_CONNECT_REQUEST,\n\t\t\t\t\tnew TextEncoder().encode(this.myDeviceId),\n\t\t\t\t);\n\t\t\t\tconst r = (await this.getTypeMessage(MSG_CONNECT_ACCEPT))[0];\n\t\t\t\tif (r === 1) return this.connectWithCredential(credential);\n\t\t\t}\n\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t} catch (error) {\n\t\t\tthis.setState(\"Ready\");\n\t\t\tthis.emit(\"error\", this.wrapError(error));\n\t\t\treturn { success: false };\n\t\t}\n\t}\n\n\tprivate async connectWithCredential(\n\t\tcredential: CredentialPrivateInfo,\n\t): Promise<ConnectResult> {\n\t\tconst eph = await generateKeyPair();\n\t\tconst ephSig = await sigh(credential.myPrivateKey, eph.publicKey);\n\t\tconst m = concatUint8Arrays([eph.publicKey, ephSig]);\n\t\tawait this.sendTypeMessage(MSG_CONNECT_PUBLIC_KEY, m);\n\t\tconst otherM = await this.getTypeMessage(MSG_CONNECT_PUBLIC_KEY);\n\t\tconst otherPubKey = otherM.subarray(0, eph.publicKey.length);\n\t\tconst otherSig = otherM.subarray(eph.publicKey.length);\n\t\tconst sigValid = await verifySignature(\n\t\t\tcredential.remotePublicKey,\n\t\t\totherPubKey,\n\t\t\totherSig,\n\t\t);\n\t\tif (!sigValid) {\n\t\t\tthis.setState(\"Ready\");\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t}\n\n\t\tconst dhSecret = await dh(eph.privateKey, otherPubKey);\n\n\t\tconst info = buildConnectionInfo(\n\t\t\tthis.myDeviceId,\n\t\t\tthis.remoteId,\n\t\t\teph.publicKey,\n\t\t\totherPubKey,\n\t\t);\n\t\tconst derivedKey = await derive(dhSecret, info, new Uint8Array(0));\n\t\tconst myMac = await mac(derivedKey, info);\n\t\tawait this.sendTypeMessage(MSG_CONNECT_MAC_VER, myMac);\n\t\tconst otherMac = await this.getTypeMessage(MSG_CONNECT_MAC_VER);\n\n\t\tconst otherInfo = buildConnectionInfo(\n\t\t\tthis.remoteId,\n\t\t\tthis.myDeviceId,\n\t\t\totherPubKey,\n\t\t\teph.publicKey,\n\t\t);\n\t\tconst otherDerivedKey = await derive(\n\t\t\tdhSecret,\n\t\t\totherInfo,\n\t\t\tnew Uint8Array(0),\n\t\t);\n\t\tconst otherMacCheck = await mac(otherDerivedKey, otherInfo);\n\n\t\tconst macValid = verify(otherMac, otherMacCheck);\n\t\tif (!macValid) {\n\t\t\tthis.setState(\"Ready\");\n\t\t\treturn { success: false, reason: \"NEEDS_PAIRING\" };\n\t\t}\n\n\t\tif (!this.signalAdapter.supportNativeEncryption) {\n\t\t\tthis.cipher = new cipher(derivedKey, derivedKey);\n\t\t}\n\t\tthis.setState(\"Connected\");\n\t\tthis.emit(\"ready\");\n\t\treturn { success: true, credential: this.buildCredential(credential) };\n\t}\n\n\tprivate handleConnectRequest(payload: Uint8Array): void {\n\t\tconst senderId = new TextDecoder().decode(payload);\n\n\t\tif (senderId !== this.remoteId) {\n\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([0])).catch(\n\t\t\t\t() => {},\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.connectLimiter.canExecute()) {\n\t\t\t// todo 提示被频繁占用\n\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([0])).catch(\n\t\t\t\t() => {},\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst request: ConnectRequest = {\n\t\t\tremoteDeviceId: senderId,\n\t\t\taccept: (credential?: Credential) => {\n\t\t\t\tif (this.signalAdapter.trustIdentity) {\n\t\t\t\t\tthis.setState(\"Connected\");\n\t\t\t\t\tthis.emit(\"ready\");\n\t\t\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([1])).catch(\n\t\t\t\t\t\t() => {},\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tif (!credential) {\n\t\t\t\t\t\tthrow new SConnectError(\n\t\t\t\t\t\t\t\"CREDENTIAL_INVALID\",\n\t\t\t\t\t\t\t\"Credential is required\",\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\tacceptWithCre: (\n\t\t\t\tcredential: CredentialPrivateInfo,\n\t\t\t): Promise<ConnectResult> => {\n\t\t\t\tthis.setState(\"Handshaking\", \"connect-response\");\n\t\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([1])).catch(\n\t\t\t\t\t() => {},\n\t\t\t\t);\n\t\t\t\treturn this.connectWithCredential(credential);\n\t\t\t},\n\t\t\treject: () => {\n\t\t\t\tthis.sendTypeMessage(MSG_CONNECT_ACCEPT, new Uint8Array([0])).catch(\n\t\t\t\t\t() => {},\n\t\t\t\t);\n\t\t\t},\n\t\t};\n\n\t\tthis.emit(\"connectRequest\", request);\n\t}\n\n\t// ================= 配对 =================\n\n\tupdatePIN(): string {\n\t\tthis.PIN = this.generatePin();\n\t\treturn this.PIN;\n\t}\n\n\tasync pairInit(credential: CredentialPublicInfo): Promise<{\n\t\tpin: string;\n\t\tinputOtherPin: (pin: string) => void;\n\t\twaitForPairing: () => Promise<Credential>;\n\t}> {\n\t\tif (this.state !== \"Ready\") {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"CHANNEL_NOT_READY\",\n\t\t\t\t`Cannot pair in ${this.state} state`,\n\t\t\t);\n\t\t}\n\n\t\tconst pin = this.PIN || this.updatePIN();\n\n\t\tawait this.signalAdapter.connect(credential.remoteDeviceId);\n\n\t\tconst otherPin = Promise.withResolvers<string>();\n\n\t\tconst inputOtherPin = (remotePin: string) => {\n\t\t\tif (!this.validatePin(remotePin)) {\n\t\t\t\totherPin.reject(\n\t\t\t\t\tnew SConnectError(\"PIN_INVALID\", \"PIN must be 6 digits\"),\n\t\t\t\t);\n\t\t\t\tthrow new SConnectError(\"PIN_INVALID\", \"PIN must be 6 digits\");\n\t\t\t}\n\n\t\t\totherPin.resolve(remotePin);\n\t\t};\n\n\t\tconst waitForPairing = Promise.withResolvers<Credential>();\n\n\t\t// 发送配对请求\n\t\tthis.sendTypeMessage(\n\t\t\tMSG_PAIR_REQUEST,\n\t\t\tnew TextEncoder().encode(this.myDeviceId),\n\t\t).catch(() => {});\n\n\t\tthis.getTypeMessage(MSG_PAIR_REJECT).then(() => {\n\t\t\twaitForPairing.reject(\n\t\t\t\tnew SConnectError(\"PAIRING_FAILED\", \"Pairing request was rejected\"),\n\t\t\t);\n\t\t});\n\n\t\treturn {\n\t\t\tpin,\n\t\t\tinputOtherPin,\n\t\t\twaitForPairing: () => {\n\t\t\t\tthis.genWaitForPairing({\n\t\t\t\t\tremoteDeviceId: credential.remoteDeviceId,\n\t\t\t\t\totherPin: otherPin.promise,\n\t\t\t\t\tPIN: pin,\n\t\t\t\t})\n\t\t\t\t\t.then(waitForPairing.resolve)\n\t\t\t\t\t.catch(waitForPairing.reject);\n\t\t\t\treturn waitForPairing.promise;\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate async genWaitForPairing(op: {\n\t\tremoteDeviceId: string;\n\t\totherPin: Promise<string>;\n\t\tPIN: string;\n\t}): Promise<Credential> {\n\t\tconst keyPair = await generateKeyPair();\n\t\tconst finalPin = await Promise.race([\n\t\t\t(async () => {\n\t\t\t\tconst pin = await op.otherPin;\n\t\t\t\tawait this.sendTypeMessage(MSG_USE_YOUR_PIN);\n\t\t\t\treturn pin;\n\t\t\t})(),\n\t\t\t(async () => {\n\t\t\t\tawait this.getTypeMessage(MSG_USE_YOUR_PIN);\n\t\t\t\treturn op.PIN;\n\t\t\t})(),\n\t\t]);\n\t\tconst bindPublicKey = blind(finalPin, keyPair.publicKey);\n\t\tawait this.sendTypeMessage(MSG_BLIND_PUBLIC_KEY, bindPublicKey);\n\t\tconst otherBindPublicKey = await this.getTypeMessage(MSG_BLIND_PUBLIC_KEY);\n\t\tif (otherBindPublicKey === undefined) {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"PAIRING_FAILED\",\n\t\t\t\t\"Failed to receive blinded public key from peer\",\n\t\t\t);\n\t\t}\n\t\tconst otherPublicKey = deBlind(otherBindPublicKey, finalPin);\n\t\tconst s = await dh(keyPair.privateKey, otherPublicKey);\n\n\t\tconst info = buildPairingInfo(\n\t\t\tfinalPin,\n\t\t\tthis.myDeviceId,\n\t\t\top.remoteDeviceId,\n\t\t\tkeyPair.publicKey,\n\t\t\totherPublicKey,\n\t\t);\n\t\tconst derivedKey = await derive(\n\t\t\ts,\n\t\t\tinfo,\n\t\t\tnew TextEncoder().encode(finalPin),\n\t\t);\n\t\tconst myMac = await mac(derivedKey, info);\n\t\tawait this.sendTypeMessage(MSG_SPAKE_DATA, myMac);\n\t\tconst otherMac = await this.getTypeMessage(MSG_SPAKE_DATA);\n\n\t\tconst otherInfo = buildPairingInfo(\n\t\t\tfinalPin,\n\t\t\top.remoteDeviceId,\n\t\t\tthis.myDeviceId,\n\t\t\totherPublicKey,\n\t\t\tkeyPair.publicKey,\n\t\t);\n\t\tconst otherDerivedKey = await derive(\n\t\t\ts,\n\t\t\totherInfo,\n\t\t\tnew TextEncoder().encode(finalPin),\n\t\t);\n\t\tconst otherMacCheck = await mac(otherDerivedKey, otherInfo);\n\n\t\tif (!verify(otherMac, otherMacCheck)) {\n\t\t\tthrow new SConnectError(\"PAIRING_FAILED\", \"Pairing verification failed\");\n\t\t}\n\n\t\t// todo 派生\n\t\tconst signingKeyPair = await generateSigningKeyPair();\n\t\tawait this.sendTypeMessage(\n\t\t\tMSG_SIGNING_PUBLIC_KEY,\n\t\t\tsigningKeyPair.publicKey,\n\t\t);\n\t\tconst remoteSigningPublicKey = await this.getTypeMessage(\n\t\t\tMSG_SIGNING_PUBLIC_KEY,\n\t\t);\n\n\t\tif (!remoteSigningPublicKey) {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"PAIRING_FAILED\",\n\t\t\t\t\"Failed to receive signing public key\",\n\t\t\t);\n\t\t}\n\n\t\tif (!this.signalAdapter.supportNativeEncryption) {\n\t\t\t// todo 派生\n\t\t\tthis.cipher = new cipher(\n\t\t\t\tawait dh(keyPair.privateKey, otherPublicKey),\n\t\t\t\tawait dh(keyPair.privateKey, otherPublicKey),\n\t\t\t);\n\t\t}\n\n\t\tthis.setState(\"Connected\");\n\n\t\treturn {\n\t\t\tmyPrivateKey: signingKeyPair.privateKey,\n\t\t\tmyPublicKey: signingKeyPair.publicKey,\n\t\t\tremotePublicKey: remoteSigningPublicKey,\n\t\t\tcreatedAt: Date.now(),\n\t\t\tmyDeviceId: this.myDeviceId,\n\t\t\tremoteDeviceId: this.remoteId,\n\t\t};\n\t}\n\n\tprivate handlePairRequest(payload: Uint8Array): void {\n\t\tif (!this.pairLimiter.canExecute()) {\n\t\t\t// 考虑到id都是临时可变的,无法区分真实设备,所以全部限制\n\t\t\t// todo 中间信息攻击,比如useyourpin dos,或者其它,需要状态机限制顺序和个数\n\t\t\t// todo 提示被频繁占用\n\t\t\t// todo 其他可能的配对方式\n\t\t\tthis.sendTypeMessage(MSG_PAIR_REJECT).catch(() => {}); // todo 区分拒绝原因\n\t\t\treturn;\n\t\t}\n\n\t\tconst senderId = new TextDecoder().decode(payload);\n\n\t\tconst otherPin = Promise.withResolvers<string>();\n\n\t\tconst request: PairRequest = {\n\t\t\tremoteDeviceId: senderId,\n\t\t\tinputOtherPin: (pin: string): void => {\n\t\t\t\tif (!this.validatePin(pin)) {\n\t\t\t\t\totherPin.reject();\n\t\t\t\t\tthrow new SConnectError(\"PIN_INVALID\", \"PIN must be 6 digits\");\n\t\t\t\t}\n\n\t\t\t\totherPin.resolve(pin);\n\t\t\t},\n\t\t\twaitForPairing: () => {\n\t\t\t\treturn this.genWaitForPairing({\n\t\t\t\t\tremoteDeviceId: senderId,\n\t\t\t\t\totherPin: otherPin.promise,\n\t\t\t\t\tPIN: this.PIN,\n\t\t\t\t});\n\t\t\t},\n\t\t\treject: () => {\n\t\t\t\t// 通知对方配对被拒绝\n\t\t\t\tthis.sendTypeMessage(MSG_PAIR_REJECT).catch(() => {});\n\t\t\t},\n\t\t};\n\n\t\tthis.emit(\"pairRequest\", request);\n\t}\n\n\t// ================= 断开 =================\n\n\tdisconnect(): void {\n\t\tif (this.state === \"Idle\") return;\n\n\t\tthis.cipher = null;\n\n\t\tthis.signalAdapter.close();\n\t\tthis.setState(\"Ready\");\n\t\tthis.emit(\"disconnect\");\n\t}\n\n\tdestroy(): void {\n\t\tthis.cipher = null;\n\n\t\tthis.signalAdapter.close();\n\t\tthis.myDeviceId = \"\";\n\t\tthis.setState(\"Idle\");\n\t}\n\n\t// ================= 数据发送 =================\n\n\tasync send(payload: string): Promise<void> {\n\t\tif (this.state !== \"Connected\") {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"CHANNEL_NOT_READY\",\n\t\t\t\t`Cannot send in ${this.state} state`,\n\t\t\t);\n\t\t}\n\n\t\tconst data = new TextEncoder().encode(payload);\n\t\tawait this.sendData(data);\n\t}\n\n\tasync sendBinary(data: ArrayBuffer | Uint8Array): Promise<void> {\n\t\tif (this.state !== \"Connected\") {\n\t\t\tthrow new SConnectError(\n\t\t\t\t\"CHANNEL_NOT_READY\",\n\t\t\t\t`Cannot send in ${this.state} state`,\n\t\t\t);\n\t\t}\n\n\t\tconst buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;\n\t\tawait this.sendData(buffer);\n\t}\n\n\tprivate async sendData(data: Uint8Array): Promise<void> {\n\t\tif (this.cipher) {\n\t\t\tconst encrypted = await this.cipher.encrypt(data);\n\t\t\tawait this.signalAdapter.send(encrypted);\n\t\t} else {\n\t\t\tawait this.signalAdapter.send(data);\n\t\t}\n\t}\n\n\t// ================= 事件 =================\n\n\ton<K extends keyof SecureChannelEvents>(\n\t\tevent: K,\n\t\tcallback: SecureChannelEvents[K],\n\t): void {\n\t\tif (!this.eventHandlers.has(event)) {\n\t\t\tthis.eventHandlers.set(event, new Set());\n\t\t}\n\t\tconst handlers = this.eventHandlers.get(event);\n\t\tif (handlers) {\n\t\t\thandlers.add(callback as (...args: unknown[]) => void);\n\t\t}\n\t}\n\n\toff(event: string, callback: (...args: unknown[]) => void): void {\n\t\tconst handlers = this.eventHandlers.get(event);\n\t\tif (handlers) {\n\t\t\thandlers.delete(callback);\n\t\t}\n\t}\n\n\t// ================= 状态机 =================\n\n\tprivate setState(\n\t\tnewState: ChannelState,\n\t\thandshakeType?: HandshakeType,\n\t): void {\n\t\tconst oldState = this.state;\n\t\tthis.state = newState;\n\t\tthis.handshakeType =\n\t\t\thandshakeType ?? (newState === \"Handshaking\" ? this.handshakeType : null);\n\n\t\t// 状态改变时清理所有计时器\n\t\tif (oldState !== newState) {\n\t\t\tthis.clearAllTimers();\n\t\t}\n\t}\n\n\t// ================= 计时器管理 =================\n\n\tprivate clearAllTimers(): void {\n\t\tfor (const timer of this.activeTimers) {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t\tthis.activeTimers.clear();\n\t}\n\n\t// ================= 消息处理 =================\n\n\tprivate handleRawMessage(data: Uint8Array): void {\n\t\tif (this.state === \"Connected\") {\n\t\t\tthis.handleAppData(data).catch(() => {});\n\t\t} else {\n\t\t\tconst type = data[0];\n\t\t\tconst payload = data.subarray(1);\n\n\t\t\t// 错误消息任何状态都处理\n\t\t\tif (type === MSG_ERROR) {\n\t\t\t\tthis.handleErrorFromPeer(payload);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst handlers = this.typeMessageResolvers.get(type);\n\t\t\tif (handlers) {\n\t\t\t\thandlers.resolve(payload);\n\t\t\t} else {\n\t\t\t\tthis.typeMessageQueue.push({ type, payload });\n\t\t\t}\n\n\t\t\tswitch (this.state) {\n\t\t\t\tcase \"Idle\":\n\t\t\t\t\treturn;\n\n\t\t\t\tcase \"Ready\":\n\t\t\t\t\tswitch (type) {\n\t\t\t\t\t\tcase MSG_PAIR_REQUEST:\n\t\t\t\t\t\t\tthis.handlePairRequest(payload);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase MSG_CONNECT_REQUEST:\n\t\t\t\t\t\t\tthis.handleConnectRequest(payload);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async handleAppData(data: Uint8Array): Promise<void> {\n\t\tlet payload = data;\n\n\t\tif (this.cipher) {\n\t\t\ttry {\n\t\t\t\tpayload = new Uint8Array(await this.cipher.decrypt(payload));\n\t\t\t} catch {\n\t\t\t\tthis.emit(\"data\", new Uint8Array(payload).buffer as ArrayBuffer, () =>\n\t\t\t\t\tthis.textDecoder.decode(payload),\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.emit(\"data\", payload.buffer as ArrayBuffer, () =>\n\t\t\tthis.textDecoder.decode(payload),\n\t\t);\n\t}\n\n\t// ================= 错误消息 =================\n\n\tprivate handleErrorFromPeer(payload: Uint8Array): void {\n\t\tconst text = new TextDecoder().decode(payload);\n\t\ttry {\n\t\t\tconst error = JSON.parse(text);\n\t\t\tthis.emit(\"error\", new SConnectError(error.code, error.message));\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\n\t// ================= 工具方法 =================\n\n\tprivate async sendTypeMessage(\n\t\ttype: number,\n\t\tpayload?: Uint8Array,\n\t): Promise<void> {\n\t\tconst message = new Uint8Array(1 + (payload ? payload.length : 0));\n\t\tmessage[0] = type;\n\t\tif (payload) {\n\t\t\tmessage.set(payload, 1);\n\t\t}\n\t\tawait this.signalAdapter.send(message);\n\t}\n\n\tprivate getTypeMessage(type: number) {\n\t\tconst p = Promise.withResolvers<Uint8Array>();\n\t\tconst recived = this.typeMessageQueue.findIndex((msg) => msg.type === type);\n\t\tif (recived !== -1) {\n\t\t\tconst msg = this.typeMessageQueue.splice(recived, 1)[0];\n\t\t\tp.resolve(msg.payload);\n\t\t\treturn p.promise;\n\t\t}\n\t\tthis.typeMessageResolvers.set(type, {\n\t\t\tresolve: (data: Uint8Array) => {\n\t\t\t\tthis.typeMessageResolvers.delete(type);\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tp.resolve(data);\n\t\t\t},\n\t\t\treject: (err: Error) => {\n\t\t\t\tthis.typeMessageResolvers.delete(type);\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tp.reject(err);\n\t\t\t},\n\t\t});\n\t\tconst timer = setTimeout(() => {\n\t\t\tthis.typeMessageResolvers.delete(type);\n\t\t\tp.reject(\n\t\t\t\tnew SConnectError(\n\t\t\t\t\t\"TIMEOUT\",\n\t\t\t\t\t`Timeout waiting for message type ${type}`,\n\t\t\t\t),\n\t\t\t);\n\t\t}, this.options.handshakeTimeout);\n\t\tthis.activeTimers.add(timer);\n\t\treturn p.promise;\n\t}\n\n\tprivate buildCredential(\n\t\tcredential: CredentialPrivateInfo | undefined,\n\t): Credential {\n\t\treturn {\n\t\t\tmyDeviceId: this.myDeviceId,\n\t\t\tremoteDeviceId: this.remoteId,\n\t\t\tmyPrivateKey: new Uint8Array(),\n\t\t\tmyPublicKey: new Uint8Array(),\n\t\t\tremotePublicKey: new Uint8Array(),\n\t\t\tcreatedAt: Date.now(),\n\t\t\tlastConnected: Date.now(),\n\t\t\t...credential,\n\t\t};\n\t}\n\n\tprivate handleDisconnect(): void {\n\t\tif (this.state === \"Idle\") return;\n\t\tthis.cipher = null;\n\t\tthis.setState(\"Ready\");\n\t\tthis.emit(\"disconnect\");\n\t}\n\n\tprivate emit<K extends keyof SecureChannelEvents>(\n\t\tevent: K,\n\t\t...args: Parameters<SecureChannelEvents[K]>\n\t): void {\n\t\tconst handlers = this.eventHandlers.get(event);\n\t\tif (handlers) {\n\t\t\tfor (const handler of handlers) {\n\t\t\t\ttry {\n\t\t\t\t\thandler(...args);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconsole.error(`Error in event handler for ${event}:`, err);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate generatePin(): string {\n\t\treturn Math.floor(100000 + Math.random() * 900000).toString();\n\t}\n\n\tprivate validatePin(pin: string): boolean {\n\t\treturn /^\\d{6}$/.test(pin);\n\t}\n\n\tprivate wrapError(error: unknown): SConnectError {\n\t\tif (error instanceof SConnectError) return error;\n\t\treturn new SConnectError(\n\t\t\t\"ADAPTER_ERROR\",\n\t\t\terror instanceof Error ? error.message : String(error),\n\t\t);\n\t}\n}\n\nexport async function generateKeyPair(): Promise<KeyPair> {\n\tconst keyPair = await crypto.subtle.generateKey(\n\t\t{ name: \"X25519\" } as AlgorithmIdentifier,\n\t\ttrue,\n\t\t[\"deriveBits\"],\n\t);\n\n\tconst publicKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"raw\",\n\t\t(keyPair as CryptoKeyPair).publicKey,\n\t);\n\tconst privateKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"pkcs8\",\n\t\t(keyPair as CryptoKeyPair).privateKey,\n\t);\n\n\treturn {\n\t\tpublicKey: new Uint8Array(publicKeyBuffer),\n\t\tprivateKey: new Uint8Array(privateKeyBuffer).subarray(16),\n\t};\n}\n\nexport async function generateSigningKeyPair(): Promise<KeyPair> {\n\tconst keyPair = await crypto.subtle.generateKey(\"Ed25519\", true, [\n\t\t\"sign\",\n\t\t\"verify\",\n\t]);\n\tconst publicKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"raw\",\n\t\t(keyPair as CryptoKeyPair).publicKey,\n\t);\n\tconst privateKeyBuffer = await crypto.subtle.exportKey(\n\t\t\"pkcs8\",\n\t\t(keyPair as CryptoKeyPair).privateKey,\n\t);\n\treturn {\n\t\tpublicKey: new Uint8Array(publicKeyBuffer),\n\t\tprivateKey: new Uint8Array(privateKeyBuffer).subarray(16),\n\t};\n}\n\nexport class cipher {\n\tprivate sendCryptoKey: CryptoKey | null = null;\n\tprivate receiveCryptoKey: CryptoKey | null = null;\n\n\tconstructor(\n\t\tprivate sendKey: Uint8Array,\n\t\tprivate receiveKey: Uint8Array,\n\t) {}\n\n\tprivate async getSendKey(): Promise<CryptoKey> {\n\t\tif (!this.sendCryptoKey) {\n\t\t\tthis.sendCryptoKey = await crypto.subtle.importKey(\n\t\t\t\t\"raw\",\n\t\t\t\tthis.sendKey as unknown as BufferSource,\n\t\t\t\t{ name: \"AES-GCM\" },\n\t\t\t\tfalse,\n\t\t\t\t[\"encrypt\"],\n\t\t\t);\n\t\t}\n\t\treturn this.sendCryptoKey;\n\t}\n\n\tprivate async getReceiveKey(): Promise<CryptoKey> {\n\t\tif (!this.receiveCryptoKey) {\n\t\t\tthis.receiveCryptoKey = await crypto.subtle.importKey(\n\t\t\t\t\"raw\",\n\t\t\t\tthis.receiveKey as unknown as BufferSource,\n\t\t\t\t{ name: \"AES-GCM\" },\n\t\t\t\tfalse,\n\t\t\t\t[\"decrypt\"],\n\t\t\t);\n\t\t}\n\t\treturn this.receiveCryptoKey;\n\t}\n\n\tasync encrypt(data: Uint8Array): Promise<Uint8Array> {\n\t\tconst key = await this.getSendKey();\n\t\tconst iv = crypto.getRandomValues(new Uint8Array(12));\n\t\tconst encrypted = await crypto.subtle.encrypt(\n\t\t\t{ name: \"AES-GCM\", iv },\n\t\t\tkey,\n\t\t\tdata as unknown as BufferSource,\n\t\t);\n\t\tconst result = new Uint8Array(iv.length + encrypted.byteLength);\n\t\tresult.set(iv);\n\t\tresult.set(new Uint8Array(encrypted), iv.length);\n\t\treturn result;\n\t}\n\n\tasync decrypt(data: Uint8Array): Promise<Uint8Array> {\n\t\tconst key = await this.getReceiveKey();\n\t\tconst iv = new Uint8Array(data.buffer, data.byteOffset, 12);\n\t\tconst ciphertext = new Uint8Array(data.buffer, data.byteOffset + 12);\n\t\tconst decrypted = await crypto.subtle.decrypt(\n\t\t\t{ name: \"AES-GCM\", iv: iv as unknown as BufferSource },\n\t\t\tkey,\n\t\t\tciphertext as unknown as BufferSource,\n\t\t);\n\t\treturn new Uint8Array(decrypted);\n\t}\n}\n\n// todo 更安全做法\nexport function blind(pin: string, publicKey: Uint8Array): Uint8Array {\n\t// 将 PIN 转换为 Uint8Array\n\tconst pinBytes = new TextEncoder().encode(pin);\n\n\t// 创建一个新的 Uint8Array 来存储盲化后的公钥\n\tconst blindedKey = new Uint8Array(publicKey.length);\n\n\t// 对每个字节进行盲化处理\n\tfor (let i = 0; i < publicKey.length; i++) {\n\t\tblindedKey[i] = publicKey[i] ^ pinBytes[i % pinBytes.length];\n\t}\n\n\treturn blindedKey;\n}\n\nexport function deBlind(blindedKey: Uint8Array, pin: string): Uint8Array {\n\t// 将 PIN 转换为 Uint8Array\n\tconst pinBytes = new TextEncoder().encode(pin);\n\n\t// 创建一个新的 Uint8Array 来存储去盲化后的公钥\n\tconst originalKey = new Uint8Array(blindedKey.length);\n\n\t// 对每个字节进行去盲化处理\n\tfor (let i = 0; i < blindedKey.length; i++) {\n\t\toriginalKey[i] = blindedKey[i] ^ pinBytes[i % pinBytes.length];\n\t}\n\n\treturn originalKey;\n}\n\nexport async function dh(\n\tprivateKey: Uint8Array,\n\tpublicKey: Uint8Array,\n): Promise<Uint8Array> {\n\tconst privKey = await crypto.subtle.importKey(\n\t\t\"pkcs8\",\n\t\tbuildX25519Pkcs8(privateKey),\n\t\t\"X25519\",\n\t\tfalse,\n\t\t[\"deriveBits\"],\n\t);\n\tconst pubKey = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tpublicKey as unknown as BufferSource,\n\t\t\"X25519\",\n\t\tfalse,\n\t\t[],\n\t);\n\tconst bits = await crypto.subtle.deriveBits(\n\t\t{ name: \"X25519\", public: pubKey },\n\t\tprivKey,\n\t\t256,\n\t);\n\treturn new Uint8Array(bits);\n}\n\nfunction buildX25519Pkcs8(rawPrivKey: Uint8Array): ArrayBuffer {\n\tconst header = new Uint8Array([\n\t\t0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,\n\t\t0x04, 0x22, 0x04, 0x20,\n\t]);\n\tconst buf = new Uint8Array(header.length + rawPrivKey.length);\n\tbuf.set(header, 0);\n\tbuf.set(rawPrivKey, header.length);\n\treturn buf.buffer;\n}\n\nfunction buildEd25519Pkcs8(rawPrivKey: Uint8Array): ArrayBuffer {\n\tconst header = new Uint8Array([\n\t\t0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,\n\t\t0x04, 0x22, 0x04, 0x20,\n\t]);\n\tconst buf = new Uint8Array(header.length + rawPrivKey.length);\n\tbuf.set(header, 0);\n\tbuf.set(rawPrivKey, header.length);\n\treturn buf.buffer;\n}\n\nfunction buildPairingInfo(\n\tpin: string,\n\tlocalId: string,\n\tremoteId: string,\n\tlocalEphemeralPk: Uint8Array,\n\tremoteEphemeralPk: Uint8Array,\n): ArrayBuffer {\n\tconst enc = new TextEncoder();\n\tconst pinBytes = enc.encode(pin);\n\tconst localIdBytes = enc.encode(localId);\n\tconst remoteIdBytes = enc.encode(remoteId);\n\n\tconst buf = new Uint8Array(\n\t\t[\n\t\t\tpinBytes,\n\t\t\tlocalIdBytes,\n\t\t\tremoteIdBytes,\n\t\t\tlocalEphemeralPk,\n\t\t\tremoteEphemeralPk,\n\t\t].flatMap((arr) => [...arr]),\n\t);\n\n\treturn buf.buffer;\n}\n\nasync function derive(\n\tikm: Uint8Array,\n\tinfo: ArrayBuffer,\n\tsalt: Uint8Array,\n): Promise<Uint8Array> {\n\tconst key = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tikm as unknown as BufferSource,\n\t\t\"HKDF\",\n\t\tfalse,\n\t\t[\"deriveBits\"],\n\t);\n\tconst bits = await crypto.subtle.deriveBits(\n\t\t{\n\t\t\tname: \"HKDF\",\n\t\t\thash: \"SHA-256\",\n\t\t\tsalt: salt as unknown as BufferSource,\n\t\t\tinfo,\n\t\t},\n\t\tkey,\n\t\t256,\n\t);\n\treturn new Uint8Array(bits);\n}\n\nasync function mac(key: Uint8Array, data: ArrayBuffer): Promise<Uint8Array> {\n\tconst cryptoKey = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tkey as unknown as BufferSource,\n\t\t{ name: \"HMAC\", hash: \"SHA-256\" },\n\t\tfalse,\n\t\t[\"sign\"],\n\t);\n\tconst sig = await crypto.subtle.sign(\"HMAC\", cryptoKey, data);\n\treturn new Uint8Array(sig);\n}\n\nfunction verify(key: Uint8Array, key2: Uint8Array) {\n\tif (key.byteLength !== key2.byteLength) return false;\n\tconst a = key;\n\tconst b = key2;\n\tlet diff = 0;\n\tfor (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];\n\treturn diff === 0;\n}\n\nfunction concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {\n\tconst totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);\n\tconst result = new Uint8Array(totalLength);\n\tlet offset = 0;\n\tfor (const arr of arrays) {\n\t\tresult.set(arr, offset);\n\t\toffset += arr.length;\n\t}\n\treturn result;\n}\n\nexport async function sigh(\n\tprivateKey: Uint8Array,\n\tdata: Uint8Array,\n): Promise<Uint8Array> {\n\tconst key = await crypto.subtle.importKey(\n\t\t\"pkcs8\",\n\t\tbuildEd25519Pkcs8(privateKey),\n\t\t\"Ed25519\",\n\t\tfalse,\n\t\t[\"sign\"],\n\t);\n\tconst sig = await crypto.subtle.sign(\n\t\t\"Ed25519\",\n\t\tkey,\n\t\tdata as unknown as BufferSource,\n\t);\n\treturn new Uint8Array(sig);\n}\n\nexport async function verifySignature(\n\tpublicKey: Uint8Array,\n\tdata: Uint8Array,\n\tsignature: Uint8Array,\n): Promise<boolean> {\n\tconst key = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tpublicKey as unknown as BufferSource,\n\t\t\"Ed25519\",\n\t\tfalse,\n\t\t[\"verify\"],\n\t);\n\treturn crypto.subtle.verify(\n\t\t\"Ed25519\",\n\t\tkey,\n\t\tsignature as unknown as BufferSource,\n\t\tdata as unknown as BufferSource,\n\t);\n}\n\nfunction buildConnectionInfo(\n\tlocalId: string,\n\tremoteId: string,\n\tlocalEphemeralPk: Uint8Array,\n\tremoteEphemeralPk: Uint8Array,\n): ArrayBuffer {\n\tconst enc = new TextEncoder();\n\tconst localIdBytes = enc.encode(localId);\n\tconst remoteIdBytes = enc.encode(remoteId);\n\treturn concatUint8Arrays([\n\t\tlocalIdBytes,\n\t\tremoteIdBytes,\n\t\tlocalEphemeralPk,\n\t\tremoteEphemeralPk,\n\t]).buffer as ArrayBuffer;\n}\n"],"mappings":";AAqBA,IAAM,mBAAmB;AACzB,IAAM,mBAAmB;AACzB,IAAM,kBAAkB;AACxB,IAAM,sBAAsB;AAC5B,IAAM,qBAAqB;AAC3B,IAAM,YAAY;AAClB,IAAM,iBAAiB;AACvB,IAAM,uBAAuB;AAC7B,IAAM,yBAAyB;AAC/B,IAAM,sBAAsB;AAC5B,IAAM,yBAAyB;AAG/B,IAAM,gBAAN,cAA4B,MAAM;CAIjC,YAAY,MAAiB,SAAiB,cAAc,MAAM;EACjE,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,cAAc;CACpB;AACD;AA6BA,IAAM,UAAN,MAAc;CAEb,YAAY,UAA0B;EAAlB,KAAA,WAAA;kBADD;CACoB;CACvC,aAAsB;EACrB,MAAM,MAAM,KAAK,IAAI;EACrB,IAAI,MAAM,KAAK,YAAY,KAAK,UAAU;GACzC,KAAK,WAA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EACzB,OAAO,IAAI,KAAK,MAAM;EACtB,UAAU,IAAI;CACf;CACA,OAAO;AACR;AAEA,eAAsB,KACrB,YACA,MACsB;CACtB,MAAM,MAAM,MAAM,OAAO,OAAO,UAC/B,SACA,kBAAkB,UAAU,GAC5B,WACA,OACA,CAAC,MAAM,CACR;CACA,MAAM,MAAM,MAAM,OAAO,OAAO,KAC/B,WACA,KACA,IACD;CACA,OAAO,IAAI,WAAW,GAAG;AAC1B;AAEA,eAAsB,gBACrB,WACA,MACA,WACmB;CACnB,MAAM,MAAM,MAAM,OAAO,OAAO,UAC/B,OACA,WACA,WACA,OACA,CAAC,QAAQ,CACV;CACA,OAAO,OAAO,OAAO,OACpB,WACA,KACA,WACA,IACD;AACD;AAEA,SAAS,oBACR,SACA,UACA,kBACA,mBACc;CACd,MAAM,MAAM,IAAI,YAAY;CAG5B,OAAO,kBAAkB;EAFJ,IAAI,OAAO,OAG/B;EAFqB,IAAI,OAAO,QAGhC;EACA;EACA;CACD,CAAC,CAAC,CAAC;AACJ"}
@@ -144,8 +144,6 @@ interface UntrustedSignalingAdapter {
144
144
  interface PairRequest {
145
145
  /** 发起方的设备 ID */
146
146
  remoteDeviceId: string;
147
- /** 发起方的显示名称(可选) */
148
- remoteDisplayName?: string;
149
147
  /** 输入对方的 PIN(触发 PAKE 交换) */
150
148
  inputOtherPin: (pin: string) => void;
151
149
  /** 等待配对完成 */
@@ -159,9 +157,9 @@ interface ConnectRequest {
159
157
  /** 发起方的设备 ID */
160
158
  remoteDeviceId: string;
161
159
  /** 发起方的显示名称(可选) */
162
- remoteDisplayName?: string;
163
160
  /** 接受连接请求,传入本地保存的关于对方的 Credential */
164
- accept: (credential: Credential) => Promise<ConnectResult>;
161
+ accept: () => void;
162
+ acceptWithCre: (credential: CredentialPrivateInfo) => Promise<ConnectResult>;
165
163
  /** 拒绝连接请求 */
166
164
  reject: () => void;
167
165
  }
@@ -185,12 +183,8 @@ interface ChannelOptions {
185
183
  interface CredentialPublicInfo {
186
184
  /** 我方设备 UUID */
187
185
  myDeviceId: string;
188
- /** 我方可读名称(可选) */
189
- myDisplayName?: string;
190
186
  /** 对方设备 UUID */
191
187
  remoteDeviceId: string;
192
- /** 对方可读名称(可选) */
193
- remoteDisplayName?: string;
194
188
  }
195
189
 
196
190
  interface CredentialPrivateInfo {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myde-remote-connect",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Secure remote connection module with PAKE pairing and Noise IK handshake",
5
5
  "type": "module",
6
6
  "main": "./dist/sconnect.js",
@@ -144,8 +144,6 @@ interface UntrustedSignalingAdapter {
144
144
  interface PairRequest {
145
145
  /** 发起方的设备 ID */
146
146
  remoteDeviceId: string;
147
- /** 发起方的显示名称(可选) */
148
- remoteDisplayName?: string;
149
147
  /** 输入对方的 PIN(触发 PAKE 交换) */
150
148
  inputOtherPin: (pin: string) => void;
151
149
  /** 等待配对完成 */
@@ -159,9 +157,9 @@ interface ConnectRequest {
159
157
  /** 发起方的设备 ID */
160
158
  remoteDeviceId: string;
161
159
  /** 发起方的显示名称(可选) */
162
- remoteDisplayName?: string;
163
160
  /** 接受连接请求,传入本地保存的关于对方的 Credential */
164
- accept: (credential: Credential) => Promise<ConnectResult>;
161
+ accept: () => void;
162
+ acceptWithCre: (credential: CredentialPrivateInfo) => Promise<ConnectResult>;
165
163
  /** 拒绝连接请求 */
166
164
  reject: () => void;
167
165
  }
@@ -185,12 +183,8 @@ interface ChannelOptions {
185
183
  interface CredentialPublicInfo {
186
184
  /** 我方设备 UUID */
187
185
  myDeviceId: string;
188
- /** 我方可读名称(可选) */
189
- myDisplayName?: string;
190
186
  /** 对方设备 UUID */
191
187
  remoteDeviceId: string;
192
- /** 对方可读名称(可选) */
193
- remoteDisplayName?: string;
194
188
  }
195
189
 
196
190
  interface CredentialPrivateInfo {