-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
198 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { generateAndExportPeerInfo } from "../util.js"; | ||
|
||
console.log(await generateAndExportPeerInfo()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters