create static method

Future<NoiseXXPattern> create(
  1. bool isInitiator,
  2. SimpleKeyPair staticKeys
)

Creates a new NoiseXXPattern instance

Implementation

static Future<NoiseXXPattern> create(
    bool isInitiator, SimpleKeyPair staticKeys) async {
  // Generate ephemeral keys
  final ephemeralKeys = await X25519().newKeyPair();

  // Initialize symmetric state per Noise spec:
  // If len(protocol_name) <= HASHLEN: h = protocol_name (zero-padded to HASHLEN)
  // Else: h = HASH(protocol_name)
  // ck = h
  // k = empty (no cipher key yet)
  final protocolName = utf8.encode(PROTOCOL_NAME);
  _validateProtocolName(protocolName);

  // Step 1: h = protocol_name (zero-padded to HASHLEN if shorter)
  Uint8List initialH;
  if (protocolName.length <= 32) {
    initialH = Uint8List(32);
    initialH.setAll(0, protocolName);
  } else {
    final hash = await Sha256().hash(protocolName);
    initialH = Uint8List.fromList(hash.bytes);
  }

  // Step 2: ck = h
  final ck = Uint8List.fromList(initialH);

  // Step 3: MixHash(prologue) where prologue is empty for libp2p
  // h = HASH(h || prologue) = HASH(h || "") = HASH(h)
  final hAfterPrologue = await Sha256().hash(initialH);
  final state = HandshakeState(
    chainKey: ck,
    handshakeHash: Uint8List.fromList(hAfterPrologue.bytes),
    state: XXHandshakeState.initial,
  );

  return NoiseXXPattern._(isInitiator, staticKeys, ephemeralKeys, state);
}