js-libp2p-noise/test/xx-fallback-handshake.spec.ts

77 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2020-06-19 12:49:40 +02:00
import Wrap from 'it-pb-rpc'
import { Buffer } from 'buffer'
import Duplex from 'it-pair/duplex'
2020-01-07 10:16:57 +01:00
import {
generateKeypair,
2020-06-19 12:49:40 +02:00
getPayload
} from '../src/utils'
import { XXFallbackHandshake } from '../src/handshake-xx-fallback'
import { createPeerIdsFromFixtures } from './fixtures/peer'
import { assert } from 'chai'
2020-06-19 13:06:31 +02:00
import { encode0 } from '../src/encoder'
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
describe('XX Fallback Handshake', () => {
2020-06-19 13:06:31 +02:00
let peerA, peerB
2020-01-07 10:16:57 +01:00
before(async () => {
2020-06-19 12:49:40 +02:00
[peerA, peerB] = await createPeerIdsFromFixtures(2)
})
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
it('should test that both parties can fallback to XX and finish handshake', async () => {
2020-01-07 10:16:57 +01:00
try {
2020-06-19 12:49:40 +02:00
const duplex = Duplex()
const connectionFrom = Wrap(duplex[0])
const connectionTo = Wrap(duplex[1])
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
const prologue = Buffer.alloc(0)
const staticKeysInitiator = generateKeypair()
const staticKeysResponder = generateKeypair()
const ephemeralKeys = generateKeypair()
2020-01-07 10:16:57 +01:00
2020-01-10 21:25:18 +01:00
// Initial msg for responder is IK first message from initiator
2020-06-19 12:49:40 +02:00
const handshakePayload = await getPayload(peerA, staticKeysInitiator.publicKey)
2020-01-10 21:25:18 +01:00
const initialMsgR = encode0({
2020-01-07 13:05:48 +01:00
ne: ephemeralKeys.publicKey,
ns: Buffer.alloc(0),
2020-06-19 12:49:40 +02:00
ciphertext: handshakePayload
})
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
const respPayload = await getPayload(peerB, staticKeysResponder.publicKey)
2020-01-07 10:16:57 +01:00
const handshakeResp =
2020-06-19 12:49:40 +02:00
new XXFallbackHandshake(false, respPayload, prologue, staticKeysResponder, connectionTo, initialMsgR, peerA)
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
await handshakeResp.propose()
await handshakeResp.exchange()
2020-01-10 21:25:18 +01:00
// Initial message for initiator is XX Message B from responder
// This is the point where initiator falls back from IK
2020-06-19 12:49:40 +02:00
const initialMsgI = await connectionFrom.readLP()
2020-01-10 21:25:18 +01:00
const handshakeInit =
2021-01-26 21:39:37 +00:00
new XXFallbackHandshake(true, handshakePayload, prologue, staticKeysInitiator, connectionFrom, initialMsgI.slice(0), peerB, ephemeralKeys)
2020-01-10 21:25:18 +01:00
2020-06-19 12:49:40 +02:00
await handshakeInit.propose()
await handshakeInit.exchange()
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
await handshakeInit.finish()
await handshakeResp.finish()
2020-01-07 10:16:57 +01:00
2020-06-19 12:49:40 +02:00
const sessionInitator = handshakeInit.session
const sessionResponder = handshakeResp.session
2020-01-07 10:16:57 +01:00
// Test shared key
2021-01-26 21:39:37 +00:00
if (sessionInitator.cs1 !== undefined &&
sessionResponder.cs1 !== undefined &&
sessionInitator.cs2 !== undefined &&
sessionResponder.cs2 !== undefined) {
2020-06-19 12:49:40 +02:00
assert(sessionInitator.cs1.k.equals(sessionResponder.cs1.k))
assert(sessionInitator.cs2.k.equals(sessionResponder.cs2.k))
2020-01-07 10:16:57 +01:00
} else {
2020-06-19 12:49:40 +02:00
assert(false)
2020-01-07 10:16:57 +01:00
}
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-01-07 10:16:57 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-01-07 10:16:57 +01:00
})