mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
27 lines
655 B
TypeScript
27 lines
655 B
TypeScript
import * as crypto from 'libp2p-crypto';
|
|
import {KeyPair, PeerId} from "../src/@types/libp2p";
|
|
import {bytes} from "../src/@types/basic";
|
|
|
|
export async function generateEd25519Keys() {
|
|
return await crypto.keys.generateKeyPair('ed25519');
|
|
}
|
|
|
|
export function getKeyPairFromPeerId(peerId: PeerId): KeyPair {
|
|
return {
|
|
privateKey: peerId.privKey.marshal().slice(0, 32),
|
|
publicKey: peerId.marshalPubKey(),
|
|
}
|
|
}
|
|
|
|
export function getRandomBuffer(size: number) : bytes {
|
|
size = Math.max(1, size<<0);
|
|
|
|
const buf = Buffer.alloc(size);
|
|
let i = 0;
|
|
for (; i < size; ++i) {
|
|
buf[i] = (Math.random() * 0xFF) << 0;
|
|
}
|
|
|
|
return buf;
|
|
}
|