Skip to content

Commit

Permalink
fix-test
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohaodu committed Jun 1, 2024
1 parent 26137fd commit 8f16e6e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 198 deletions.
190 changes: 0 additions & 190 deletions packages/local-chat-x-core/next/test.ts

This file was deleted.

22 changes: 22 additions & 0 deletions packages/local-chat-x-core/secret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const storePeerId = [
{
secretEd25519Base64String:
"CAESQPFSqZDwq92F8FsrKRS0TrVcqb+1NutEmaCh2LRP1p2RaaL3ymqBlvXKxXC7WqtDh9XNeONnyKaJnP5B+91Xu1g=",
peerId: "12D3KooWGvj79L57Dp8uvLxdZ4PQcEQSoyhh8hgDJBdcHeBW7P11",
},
{
secretEd25519Base64String:
"CAESQN8IvbShR6VAHTYQeviBHndw2j3npljAyk4RzkxYR96qgEPkjSWAdszcOpaxevW8vJYB7GWm6nEubDOnxzeAtRw=",
peerId: "12D3KooWJT4Q5xQwNF9dKnVXk67W8TXbrHRzq2a6qhCxgj67Vd1m",
},
{
secretEd25519Base64String:
"CAESQGNFFJLmATroQ0yDo4QVYK7/7kbG2Inllo8qerUKaRj/lc5mlgdwX4TqQ6jXXTtMDa2O5nNwgLkiqFhun/L4d54=",
peerId: "12D3KooWKu9TVhx8rcTW8GUA6B7oEKXfGrv4YBWSToocS8sk8kZB",
},
{
secretEd25519Base64String:
"CAESQISjdzj8e7FC+1898XsPrK0s4bC9Ri1t+XCM+GyhSRco5x9GdIwn7uUBeNNjKZeWlKojJ2upQs5XEp13sb9+3c8=",
peerId: "12D3KooWRNa1MeCqjtGctYT2ebhVvxwyJ82dWvXkCMWfzBE2hqRp",
},
];
3 changes: 3 additions & 0 deletions packages/local-chat-x-core/tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { generateAndExportPeerInfo } from "../util.js";

console.log(await generateAndExportPeerInfo());
2 changes: 1 addition & 1 deletion packages/local-chat-x-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["*.ts", "next/test.ts"]
"include": ["*.ts", "tests/*.ts"]
}
56 changes: 56 additions & 0 deletions packages/local-chat-x-core/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { peerIdFromKeys } from "@libp2p/peer-id";
import { unmarshalPrivateKey } from "@libp2p/crypto/keys";
import { createEd25519PeerId } from "@libp2p/peer-id-factory";
import sshpk from "sshpk";

// 解析私钥
export function parsePrivateKey(sshPrivateKey: string) {
try {
const privateKey = sshpk.parsePrivateKey(sshPrivateKey, "auto");
return privateKey;
} catch (error) {
console.error("Failed to parse private key:", error);
return null;
}
}

// 解析公钥
export function parsePublicKey(sshPublicKey: string) {
try {
const publicKey = sshpk.parseKey(sshPublicKey, "auto");
return publicKey;
} catch (error) {
console.error("Failed to parse public key:", error);
return null;
}
}

export async function parsePrivateKeySecret(
secretEd25519Base64StringReg: string
) {
const keyPairImport = Uint8Array.from(
Buffer.from(secretEd25519Base64StringReg, "base64")
);
const keyPairU = await unmarshalPrivateKey(keyPairImport);
return keyPairU;
}
export async function parsePrivateKeySecretToPeerId(
secretEd25519Base64StringReg: string
) {
const keyPairImport = Uint8Array.from(
Buffer.from(secretEd25519Base64StringReg, "base64")
);
const keyPairU = await unmarshalPrivateKey(keyPairImport);
const peerId = await peerIdFromKeys(keyPairU.public.bytes, keyPairU.bytes);
return peerId;
}

export const generateAndExportPeerInfo = async () => {
const peerId = await createEd25519PeerId();
return {
secretEd25519Base64String: Buffer.from(peerId.privateKey!).toString(
"base64"
),
peerId: peerId.toString(),
};
};
6 changes: 6 additions & 0 deletions packages/web-chat-x-vue/src/classes/PeerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ export class PeerManager {

// 释放媒体流资源
releaseMediaStream = () => {
if (this.mediaConnect.value) {
this.mediaConnect.value.close();
console.log("close mediaConnect and clear mediaStream");
} else {
console.log("only clear mediaStream");
}
this.communication.value.await = true;
this.communication.value.accepted = false;
if (this.mediaStream.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ onUnmounted(() => {
});
function hungUp() {
elDialogVisible.value = false;
nextTick(() => {
if (peerManager.mediaConnect.value) {
peerManager.mediaConnect.value.close();
} else {
console.log(peerManager.mediaConnect, peerManager.mediaConnect.value);
}
});
peerManager.releaseMediaStream();
}
function reject() {
Expand Down

0 comments on commit 8f16e6e

Please sign in to comment.