Written handshake exchange

This commit is contained in:
morrigan
2019-11-21 13:38:39 +01:00
parent f6bc40baf4
commit b2d058291c
3 changed files with 47 additions and 24 deletions

View File

@ -21,15 +21,16 @@ export async function generateKeypair() : Promise<KeyPair> {
}
export async function createHandshakePayload(
libp2pKeys: KeyPair,
libp2pPublicKey: bytes,
signedPayload: bytes,
earlyData?: bytes,
libp2pPrivateKey?: bytes,
) : Promise<bytes> {
const NoiseHandshakePayload = await loadPayloadProto();
const payloadInit = NoiseHandshakePayload.create({
libp2pKey: libp2pKeys.publicKey,
libp2pKey: libp2pPublicKey,
noiseStaticKeySignature: signedPayload,
...resolveEarlyDataPayload(libp2pKeys.privateKey, earlyData),
...resolveEarlyDataPayload(libp2pPrivateKey, earlyData),
});
return Buffer.from(NoiseHandshakePayload.encode(payloadInit).finish());
@ -44,8 +45,8 @@ export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.
export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
function resolveEarlyDataPayload(privateKey: bytes, earlyData?: bytes) : Object {
if (!earlyData) {
function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes) : Object {
if (!earlyData || !privateKey) {
return {};
}