diff --git a/src/@types/it-pb-rpc/index.d.ts b/src/@types/it-pb-rpc/index.d.ts index 9366760..012bc39 100644 --- a/src/@types/it-pb-rpc/index.d.ts +++ b/src/@types/it-pb-rpc/index.d.ts @@ -2,8 +2,8 @@ declare module "it-pb-rpc" { import { Buffer } from "buffer"; import { Duplex } from "it-pair"; type WrappedDuplex = { - read(bytes: number): Buffer, - readLP(): Buffer, + read(bytes: number): Promise, + readLP(): Promise, write(input: Buffer): void, writeLP(input: Buffer): void, unwrap(): Duplex diff --git a/src/crypto.ts b/src/crypto.ts index d3a8a06..8385595 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -10,7 +10,9 @@ interface IReturnEncryptionWrapper { export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper { return async function * (source) { for await (const chunk of source) { + console.log("chunk: ", chunk); const data = await handshake.encrypt(chunk, session); + console.log("encrypted: ", data); yield data; } } diff --git a/src/noise.ts b/src/noise.ts index d216446..ebc89c1 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -83,18 +83,16 @@ export class Noise implements NoiseConnection { const prologue = Buffer.from(this.protocol); const handshake = new Handshake('XX', isInitiator, remotePublicKey, prologue, this.staticKeys, connection); - console.log("Starting with handshake in createSecureConnection") - const session = await handshake.propose(this.earlyData); await handshake.exchange(session); await handshake.finish(session); - console.log("Finished handshake in createSecureConnection") - // Create encryption box/unbox wrapper const [secure, user] = DuplexPair(); const network = connection.unwrap(); + console.log("Unwrapped network: ", network) + pipe( secure, // write to wrapper ensureBuffer, // ensure any type of data is converted to buffer diff --git a/test/fixtures/peer.ts b/test/fixtures/peer.ts index 431e4b6..585b37b 100644 --- a/test/fixtures/peer.ts +++ b/test/fixtures/peer.ts @@ -28,7 +28,7 @@ export async function createPeerIdsFromFixtures (length) { export async function createPeerIds (length) { const peerIds: any[] = []; for (let i = 0; i < length; i++) { - const id = await PeerId.create({ keyType: 'ed25519'}); + const id = await PeerId.create({ keyType: 'ed25519', bits: 256 }); peerIds.push(id); } diff --git a/test/noise.test.ts b/test/noise.test.ts index 3542388..c54b8e6 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -21,35 +21,64 @@ describe("Noise", () => { let remotePeer, localPeer; before(async () => { - [remotePeer, localPeer] = await createPeerIds(2); + // [remotePeer, localPeer] = await createPeerIds(2); + // TODO: Handle Peer ID received ed25519 keys + + const pair1 = generateKeypair(); + remotePeer = { + id: "id-1", + pubKey: { bytes: pair1.publicKey }, + privKey: { bytes: pair1.privateKey }, + } + const pair2 = generateKeypair(); + localPeer = { + id: "id-2", + pubKey: { bytes: pair2.publicKey }, + privKey: { bytes: pair2.privateKey }, + } }); - it("should test that secureOutbound is spec compliant", async() => { + it("should test that secureOutbound is spec compliant", async(done) => { const libp2pKeys = await generateEd25519Keys(); const noise = new Noise(libp2pKeys._key); const [inboundConnection, outboundConnection] = DuplexPair(); - await Promise.all([ + const [outbound, inbound] = await Promise.all([ noise.secureOutbound(localPeer, outboundConnection, remotePeer), - (async () => { - const wrapped = Wrap(inboundConnection); - const prologue = Buffer.from('/noise'); - const staticKeys = { - privateKey: remotePeer.privKey.bytes, - publicKey: remotePeer.pubKey.bytes, - }; - const handshake = new Handshake('XX', false, localPeer.pubKey.bytes, prologue, staticKeys, wrapped); + noise.secureInbound(remotePeer, inboundConnection, localPeer), - // Finish handshake - console.log("Starting with handshake as responder in test..."); - const sessionResponder = await handshake.propose(Buffer.alloc(0)); - await handshake.exchange(sessionResponder); - await handshake.finish(sessionResponder); - console.log("Finished handshake as responder in test..."); - - // Create the encrypted streams - })(), + // (async () => { + // const wrapped = Wrap(inboundConnection); + // const prologue = Buffer.from('/noise'); + // const staticKeys = { + // privateKey: remotePeer.privKey.bytes, + // publicKey: remotePeer.pubKey.bytes, + // }; + // const handshake = new Handshake('XX', false, localPeer.pubKey.bytes, prologue, staticKeys, wrapped); + // + // // Finish handshake + // const sessionResponder = await handshake.propose(Buffer.alloc(0)); + // await handshake.exchange(sessionResponder); + // await handshake.finish(sessionResponder); + // + // // Create the encrypted streams + // console.log(sessionResponder); + // })(), ]); + const wrappedInbound = Wrap(inbound.conn); + const wrappedOutbound = Wrap(outbound.conn); + + wrappedInbound.readLP().then((log) => { + console.log("Read this thing: ", log); + done(); + }); + + wrappedOutbound.writeLP(Buffer.from("test")); + + console.log("Payload is: ", Buffer.from("test")) + + // const response = (await inbound.conn.source.next()).value; + // console.log(response); }) });