@hyperfrontend/network-protocol/node/v2v2
Node.js-side V2 protocol: pre-shared-key handshake encryption with time-based packet obfuscation.
V2 assumes both ends already share a long-lived secret (a PSK), so there is no first-message key-exchange round-trip; the encryption key is derived directly from the PSK at channel construction time. The obfuscation layer still rotates a time-derived password (getTimeBasedPassword / getTimeBasedPasswords) on a configurable interval. This entry point composes the lib/protocol/v2 createPSKHandshakeProtocolFactory with @hyperfrontend/cryptography/node and exposes createProtocol. Wire-compatible with /browser/v2, so a Node service and a browser client can negotiate a V2 channel end-to-end as long as both have the PSK. Use V2 when both endpoints can be provisioned with the same secret; use /node/v1 when you need zero-config dynamic key exchange.
API Reference
ƒ Functions
Returns
ProtocolProviderStoreExample
Creating and using a protocol provider store
const store = createProtocolProviderStore()
store.add('websocket', myProtocolProvider)
const provider = store.getByName('websocket')createPSKHandshakeProtocolFactory<T>(encryptPacket: PacketEncrypter, decryptPacket: PacketDecrypter, createTimeIntervalObfuscation: (refreshRate: number) => ObfuscationSuite): (logger: Logger, sharedKey: string, refreshRate: number) => ProtocolProvider<T>
This is the Pre-Shared Key (PSK) Handshake protocol model (V2):
- First message: Encrypted with PSK (both endpoints share key beforehand)
- Key capture: Dynamic key extracted from
packet.data.keyon receive - Subsequent messages: Encrypted with dynamically captured keys
- V1: First message has NO encryption (obfuscation only) - key visible after deobfuscation
- V2: First message IS encrypted with PSK - key never exposed, even after deobfuscation
Both protocols use time-based obfuscation as an additional security layer.
Parameters
Returns
(logger: Logger, sharedKey: string, refreshRate: number) => ProtocolProvider<T>Example
Creating a protocol provider with PSK handshake
const createProvider = createPSKHandshakeProtocolFactory(
encryptPacket,
decryptPacket,
createTimeIntervalObfuscation
)
const protocolProvider = createProvider(logger, 'shared-secret-key', 5)
const protocol = protocolProvider(sendFn, receiveFn)Parameters
| Name | Type | Description |
|---|---|---|
§name | string | The name to validate |
Returns
booleanExample
Validating protocol names
isValidName('websocket')
// => true
isValidName('')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§protocol | unknown | The value to validate as a protocol |
Returns
ValidProtocolResultExample
Validating a protocol object
const result = isValidProtocol(myProtocol)
// => { packetEncryption: true, packetDecryption: true, ... }
const invalid = isValidProtocol({})
// => { packetEncryption: false, packetDecryption: undefined, ... }Parameters
| Name | Type | Description |
|---|---|---|
§protocolProvider | unknown | The value to validate as a protocol provider |
Returns
booleanExample
Validating a protocol provider function
isValidProtocolProvider(() => protocol)
// => true
isValidProtocolProvider('not-a-function')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§receive | unknown | The value to validate as a receive function |
Returns
booleanExample
Validating a receive function
isValidReceiveFn((packet) => console.log(packet))
// => true
isValidReceiveFn(null)
// => falseParameters
| Name | Type | Description |
|---|---|---|
§send | unknown | The value to validate as a send function |
Returns
booleanExample
Validating a send function
isValidSendFn((packet) => websocket.send(packet))
// => true
isValidSendFn('not-a-function')
// => false◈ Interfaces
Properties
readonly add:(name: string, protocolProvider: ProtocolProvider<T>) => void— ◆ Types
type ValidProtocolResult = mapped● Variables
V2 Protocol - PSK Handshake:
- First message: Encrypted with pre-shared key (PSK)
- Key capture: Dynamic key extracted from
packet.data.key - Subsequent messages: Encrypted with dynamically captured keys
All messages also use time-based obfuscation for an additional security layer.