27 lines
655 B
TypeScript
Raw Normal View History

2019-11-07 10:55:18 +01:00
import * as crypto from 'libp2p-crypto';
2019-12-03 13:52:44 +01:00
import {KeyPair, PeerId} from "../src/@types/libp2p";
2019-12-24 16:25:49 +01:00
import {bytes} from "../src/@types/basic";
2019-11-07 10:55:18 +01:00
export async function generateEd25519Keys() {
return await crypto.keys.generateKeyPair('ed25519');
}
2019-12-03 13:52:44 +01:00
export function getKeyPairFromPeerId(peerId: PeerId): KeyPair {
return {
privateKey: peerId.privKey.marshal().slice(0, 32),
publicKey: peerId.marshalPubKey(),
}
}
2019-12-24 16:25:49 +01:00
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;
}