mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-12 14:11:35 +00:00
Create part of XX test and update code accordingly
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { expect } from "chai";
|
||||
import { expect, assert } from "chai";
|
||||
import { Buffer } from 'buffer';
|
||||
import * as crypto from 'libp2p-crypto';
|
||||
import protobuf from 'protobufjs';
|
||||
|
||||
import { XXHandshake, KeyPair } from "../src/xx";
|
||||
|
||||
@ -28,4 +30,50 @@ describe("Index", () => {
|
||||
expect(k2.toString('hex')).to.equal('a16ada915e551ab623f38be674bb4ef15d428ae9d80688899c9ef9b62ef208fa');
|
||||
expect(k3.toString('hex')).to.equal('ff67bf9727e31b06efc203907e6786667d2c7a74ac412b4d31a80ba3fd766f68');
|
||||
})
|
||||
|
||||
async function generateKeypair() {
|
||||
return await crypto.keys.generateKeyPair('ed25519');;
|
||||
}
|
||||
|
||||
async function doHandshake() {
|
||||
const xx = new XXHandshake();
|
||||
const kpInit = await xx.generateKeypair();
|
||||
const kpResp = await xx.generateKeypair();
|
||||
const payloadString = Buffer.from("noise-libp2p-static-key:");
|
||||
|
||||
// initiator setup
|
||||
const libp2pInitKeys = await generateKeypair();
|
||||
const initSignedPayload = await libp2pInitKeys.sign(Buffer.concat([payloadString, kpInit.publicKey]));
|
||||
|
||||
// responder setup
|
||||
const libp2pRespKeys = await generateKeypair();
|
||||
const respSignedPayload = await libp2pRespKeys.sign(Buffer.concat([payloadString, kpResp.publicKey]));
|
||||
|
||||
// initiator: new XX noise session
|
||||
const nsInit = await xx.initSession(true, prologue, kpInit, kpResp.publicKey);
|
||||
// responder: new XX noise session
|
||||
const nsResp = await xx.initSession(false, prologue, kpResp, kpInit.publicKey);
|
||||
|
||||
/* stage 0: initiator */
|
||||
|
||||
// initiator creates payload
|
||||
const payloadProtoBuf = await protobuf.load("payload.proto");
|
||||
const NoiseHandshakePayload = payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
||||
const payloadInit = NoiseHandshakePayload.create({
|
||||
libp2pKey: libp2pInitKeys.bytes.toString('hex'),
|
||||
noiseStaticKeySignature: initSignedPayload,
|
||||
});
|
||||
|
||||
const payloadInitEnc = NoiseHandshakePayload.encode(payloadInit).finish();
|
||||
|
||||
// initiator sends message
|
||||
const message = Buffer.concat([Buffer.alloc(0), payloadInitEnc]);
|
||||
const messageBuffer = await xx.sendMessage(nsInit, message);
|
||||
|
||||
expect(messageBuffer.ne.length).not.equal(0);
|
||||
}
|
||||
|
||||
it("Test handshake", async () => {
|
||||
await doHandshake();
|
||||
})
|
||||
});
|
||||
|
Reference in New Issue
Block a user