bip324 0.10.0

Encrypted transport for the bitcoin P2P protocol as specified by BIP 324
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// SPDX-License-Identifier: CC0-1.0

#[cfg(feature = "std")]
const PORT: u16 = 18444;

#[test]
#[cfg(feature = "std")]
fn hello_world_happy_path() {
    use bip324::{
        GarbageResult, Handshake, Initialized, PacketType, ReceivedKey, Role, VersionResult,
        NUM_LENGTH_BYTES,
    };
    use bitcoin::Network;

    // Create initiator handshake
    let init_handshake = Handshake::<Initialized>::new(Network::Bitcoin, Role::Initiator).unwrap();

    // Send initiator key
    let mut init_key_buffer = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
    let init_handshake = init_handshake.send_key(None, &mut init_key_buffer).unwrap();

    // Create responder handshake
    let resp_handshake = Handshake::<Initialized>::new(Network::Bitcoin, Role::Responder).unwrap();

    // Send responder key
    let mut resp_key_buffer = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
    let resp_handshake = resp_handshake.send_key(None, &mut resp_key_buffer).unwrap();

    // Initiator receives responder's key
    let init_handshake = init_handshake
        .receive_key(resp_key_buffer[..64].try_into().unwrap())
        .unwrap();

    // Responder receives initiator's key
    let resp_handshake = resp_handshake
        .receive_key(init_key_buffer[..64].try_into().unwrap())
        .unwrap();

    // Initiator sends version
    let mut init_version_buffer = vec![0u8; Handshake::<ReceivedKey>::send_version_len(None)];
    let init_handshake = init_handshake
        .send_version(&mut init_version_buffer, None)
        .unwrap();

    // Responder sends version
    let mut resp_version_buffer = vec![0u8; Handshake::<ReceivedKey>::send_version_len(None)];
    let resp_handshake = resp_handshake
        .send_version(&mut resp_version_buffer, None)
        .unwrap();

    // Initiator receives responder's garbage and version
    let (mut init_handshake, consumed) = match init_handshake
        .receive_garbage(&resp_version_buffer)
        .unwrap()
    {
        GarbageResult::FoundGarbage {
            handshake,
            consumed_bytes,
        } => (handshake, consumed_bytes),
        GarbageResult::NeedMoreData(_) => panic!("Should have found garbage"),
    };

    // Process the version packet properly
    let remaining = &resp_version_buffer[consumed..];
    let packet_len = init_handshake
        .decrypt_packet_len(remaining[..NUM_LENGTH_BYTES].try_into().unwrap())
        .unwrap();
    let mut version_packet = remaining[NUM_LENGTH_BYTES..NUM_LENGTH_BYTES + packet_len].to_vec();
    let mut alice = match init_handshake.receive_version(&mut version_packet).unwrap() {
        VersionResult::Complete { cipher } => cipher,
        VersionResult::Decoy(_) => panic!("Should have completed"),
    };

    // Responder receives initiator's garbage and version
    let (mut resp_handshake, consumed) = match resp_handshake
        .receive_garbage(&init_version_buffer)
        .unwrap()
    {
        GarbageResult::FoundGarbage {
            handshake,
            consumed_bytes,
        } => (handshake, consumed_bytes),
        GarbageResult::NeedMoreData(_) => panic!("Should have found garbage"),
    };

    // Process the version packet properly
    let remaining = &init_version_buffer[consumed..];
    let packet_len = resp_handshake
        .decrypt_packet_len(remaining[..NUM_LENGTH_BYTES].try_into().unwrap())
        .unwrap();
    let mut version_packet = remaining[NUM_LENGTH_BYTES..NUM_LENGTH_BYTES + packet_len].to_vec();
    let mut bob = match resp_handshake.receive_version(&mut version_packet).unwrap() {
        VersionResult::Complete { cipher } => cipher,
        VersionResult::Decoy(_) => panic!("Should have completed"),
    };

    // Alice and Bob can freely exchange encrypted messages using the packet handler returned by each handshake.
    let message = b"Hello world".to_vec();
    let packet_len = bip324::OutboundCipher::encryption_buffer_len(message.len());
    let mut encrypted_message_to_alice = vec![0u8; packet_len];
    bob.outbound()
        .encrypt(
            &message,
            &mut encrypted_message_to_alice,
            PacketType::Genuine,
            None,
        )
        .unwrap();

    let alice_message_len = alice.inbound().decrypt_packet_len(
        encrypted_message_to_alice[..NUM_LENGTH_BYTES]
            .try_into()
            .unwrap(),
    );
    let mut decrypted_message =
        vec![0u8; bip324::InboundCipher::decryption_buffer_len(alice_message_len)];
    alice
        .inbound()
        .decrypt(
            &encrypted_message_to_alice[NUM_LENGTH_BYTES..],
            &mut decrypted_message,
            None,
        )
        .unwrap();
    assert_eq!(message, decrypted_message[1..].to_vec()); // Skip header byte

    let message = b"Goodbye!".to_vec();
    let packet_len = bip324::OutboundCipher::encryption_buffer_len(message.len());
    let mut encrypted_message_to_bob = vec![0u8; packet_len];
    alice
        .outbound()
        .encrypt(
            &message,
            &mut encrypted_message_to_bob,
            PacketType::Genuine,
            None,
        )
        .unwrap();

    let bob_message_len = bob.inbound().decrypt_packet_len(
        encrypted_message_to_bob[..NUM_LENGTH_BYTES]
            .try_into()
            .unwrap(),
    );
    let mut decrypted_message =
        vec![0u8; bip324::InboundCipher::decryption_buffer_len(bob_message_len)];
    bob.inbound()
        .decrypt(
            &encrypted_message_to_bob[NUM_LENGTH_BYTES..],
            &mut decrypted_message,
            None,
        )
        .unwrap();
    assert_eq!(message, decrypted_message[1..].to_vec()); // Skip header byte
}

#[test]
#[cfg(feature = "std")]
fn regtest_handshake() {
    use std::{
        io::{Read, Write},
        net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream},
        time::{SystemTime, UNIX_EPOCH},
    };

    use bip324::{
        serde::{deserialize, serialize, NetworkMessage},
        GarbageResult, Handshake, Initialized, PacketType, ReceivedKey, VersionResult,
        NUM_LENGTH_BYTES,
    };
    use bitcoin::p2p::{message_network::VersionMessage, Address, ServiceFlags};
    let bitcoind = regtest_process(TransportVersion::V2);

    let mut stream = TcpStream::connect(bitcoind.params.p2p_socket.unwrap()).unwrap();

    // Initialize handshake
    let handshake =
        Handshake::<Initialized>::new(bip324::Network::Regtest, bip324::Role::Initiator).unwrap();

    // Send our public key
    let mut public_key = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
    let handshake = handshake.send_key(None, &mut public_key).unwrap();
    println!("Writing public key to the remote node");
    stream.write_all(&public_key).unwrap();
    stream.flush().unwrap();

    // Read remote public key
    let mut remote_public_key = [0u8; 64];
    println!("Reading the remote node public key");
    stream.read_exact(&mut remote_public_key).unwrap();

    // Process remote key
    let handshake = handshake.receive_key(remote_public_key).unwrap();

    // Send garbage terminator and version
    let mut local_garbage_terminator_message =
        vec![0u8; Handshake::<ReceivedKey>::send_version_len(None)];
    println!("Sending our garbage terminator");
    let handshake = handshake
        .send_version(&mut local_garbage_terminator_message, None)
        .unwrap();
    stream.write_all(&local_garbage_terminator_message).unwrap();
    stream.flush().unwrap();

    // Read and authenticate remote response
    let mut max_response = [0; 4096];
    println!("Reading the response buffer");
    let size = stream.read(&mut max_response).unwrap();
    let response = &max_response[..size];
    println!("Authenticating the handshake");

    // First receive garbage
    let (mut handshake, consumed) = match handshake.receive_garbage(response).unwrap() {
        GarbageResult::FoundGarbage {
            handshake,
            consumed_bytes,
        } => {
            println!("Found garbage terminator after {consumed_bytes} bytes");
            (handshake, consumed_bytes)
        }
        GarbageResult::NeedMoreData(_) => panic!("Should have found garbage"),
    };

    // Then receive version - properly handle packet length and potential decoys.
    let mut remaining = &response[consumed..];
    let cipher_session = loop {
        // Check if we have enough data for packet length
        if remaining.len() < NUM_LENGTH_BYTES {
            panic!("Not enough data for packet length");
        }

        let packet_len = handshake
            .decrypt_packet_len(remaining[..NUM_LENGTH_BYTES].try_into().unwrap())
            .unwrap();

        if remaining.len() < NUM_LENGTH_BYTES + packet_len {
            panic!("Not enough data for full packet");
        }

        let mut version_packet =
            remaining[NUM_LENGTH_BYTES..NUM_LENGTH_BYTES + packet_len].to_vec();

        match handshake.receive_version(&mut version_packet).unwrap() {
            VersionResult::Complete { cipher } => {
                println!("Finalizing the handshake");
                break cipher;
            }
            VersionResult::Decoy(h) => {
                println!("Received decoy packet, continuing...");
                handshake = h;
                remaining = &remaining[NUM_LENGTH_BYTES + packet_len..];
                continue;
            }
        }
    };

    let (mut decrypter, mut encrypter) = cipher_session.into_split();
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("time went backwards")
        .as_secs();
    let ip = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), PORT);
    let from_and_recv = Address::new(&ip, ServiceFlags::NONE);
    let msg = VersionMessage {
        version: 70015,
        services: ServiceFlags::NONE,
        timestamp: now as i64,
        receiver: from_and_recv.clone(),
        sender: from_and_recv,
        nonce: 1,
        user_agent: "BIP-324 Client".to_string(),
        start_height: 0,
        relay: false,
    };
    let message = serialize(NetworkMessage::Version(msg));
    let packet_len = bip324::OutboundCipher::encryption_buffer_len(message.len());
    let mut packet = vec![0u8; packet_len];
    encrypter
        .encrypt(&message, &mut packet, PacketType::Genuine, None)
        .unwrap();
    println!("Serializing and writing version message");
    stream.write_all(&packet).unwrap();
    println!("Reading the response length buffer");
    let mut response_len = [0; NUM_LENGTH_BYTES];
    stream.read_exact(&mut response_len).unwrap();
    let message_len = decrypter.decrypt_packet_len(response_len);
    let mut response_message = vec![0; message_len];
    stream.read_exact(&mut response_message).unwrap();
    let mut decrypted_message =
        vec![0u8; bip324::InboundCipher::decryption_buffer_len(response_message.len())];
    let _ = decrypter
        .decrypt(&response_message, &mut decrypted_message, None)
        .unwrap();
    let message = deserialize(&decrypted_message[1..]).unwrap(); // Skip header byte
    assert_eq!(message.cmd(), "version");
}

#[test]
#[cfg(feature = "std")]
fn regtest_handshake_std() {
    use std::{
        net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream},
        time::{SystemTime, UNIX_EPOCH},
    };

    use bip324::{
        io::{Payload, Protocol},
        serde::{deserialize, serialize, NetworkMessage},
    };
    use bitcoin::p2p::{message_network::VersionMessage, Address, ServiceFlags};

    let bitcoind = regtest_process(TransportVersion::V2);

    let stream = TcpStream::connect(bitcoind.params.p2p_socket.unwrap()).unwrap();
    let reader = stream.try_clone().unwrap();
    let writer = stream;

    // Initialize high-level protocol with handshake
    println!("Starting BIP-324 handshake");
    let mut protocol = Protocol::new(
        bip324::Network::Regtest,
        bip324::Role::Initiator,
        None, // no garbage
        None, // no decoys
        reader,
        writer,
    )
    .unwrap();

    println!("Handshake completed successfully!");

    // Create version message.
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("time went backwards")
        .as_secs();
    let ip = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), PORT);
    let from_and_recv = Address::new(&ip, ServiceFlags::NONE);
    let msg = VersionMessage {
        version: 70015,
        services: ServiceFlags::NONE,
        timestamp: now as i64,
        receiver: from_and_recv.clone(),
        sender: from_and_recv,
        nonce: 1,
        user_agent: "BIP-324 Client".to_string(),
        start_height: 0,
        relay: false,
    };

    let message = serialize(NetworkMessage::Version(msg));
    println!("Sending version message using Protocol::write()");
    protocol.write(&Payload::genuine(message)).unwrap();

    println!("Reading version response using Protocol::read()");
    let payload = protocol.read().unwrap();

    let response_message = deserialize(payload.contents()).unwrap();
    assert_eq!(response_message.cmd(), "version");

    println!("Successfully exchanged version messages using Protocol API!");
}

#[tokio::test]
#[cfg(feature = "tokio")]
async fn regtest_handshake_async() {
    use std::{
        net::{IpAddr, Ipv4Addr, SocketAddr},
        time::{SystemTime, UNIX_EPOCH},
    };

    use bip324::{
        futures::Protocol,
        io::Payload,
        serde::{deserialize, serialize, NetworkMessage},
    };
    use bitcoin::p2p::{message_network::VersionMessage, Address, ServiceFlags};
    use tokio::net::TcpStream;

    let bitcoind = regtest_process(TransportVersion::V2);

    let stream = TcpStream::connect(bitcoind.params.p2p_socket.unwrap())
        .await
        .unwrap();

    let (reader, writer) = stream.into_split();

    // Initialize high-level async protocol with handshake
    println!("Starting async BIP-324 handshake");
    let mut protocol = Protocol::new(
        bip324::Network::Regtest,
        bip324::Role::Initiator,
        None, // no garbage
        None, // no decoys
        reader,
        writer,
    )
    .await
    .unwrap();

    println!("Async handshake completed successfully!");

    // Create version message.
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("time went backwards")
        .as_secs();
    let ip = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), PORT);
    let from_and_recv = Address::new(&ip, ServiceFlags::NONE);
    let msg = VersionMessage {
        version: 70015,
        services: ServiceFlags::NONE,
        timestamp: now as i64,
        receiver: from_and_recv.clone(),
        sender: from_and_recv,
        nonce: 1,
        user_agent: "BIP-324 Async Client".to_string(),
        start_height: 0,
        relay: false,
    };

    let message = serialize(NetworkMessage::Version(msg));
    println!("Sending version message using async Protocol::write()");
    protocol.write(&Payload::genuine(message)).await.unwrap();

    println!("Reading version response using async Protocol::read()");
    let payload = protocol.read().await.unwrap();

    let response_message = deserialize(payload.contents()).unwrap();
    assert_eq!(response_message.cmd(), "version");

    println!("Successfully exchanged version messages using async Protocol API!");
}

#[test]
#[should_panic]
#[cfg(feature = "std")]
fn regtest_handshake_v1_only() {
    use std::{
        io::{Read, Write},
        net::TcpStream,
    };

    use bip324::{Handshake, Initialized};
    let bitcoind = regtest_process(TransportVersion::V1);

    let mut stream = TcpStream::connect(bitcoind.params.p2p_socket.unwrap()).unwrap();

    let handshake =
        Handshake::<Initialized>::new(bip324::Network::Regtest, bip324::Role::Initiator).unwrap();
    let mut public_key = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
    let _handshake = handshake.send_key(None, &mut public_key).unwrap();
    println!("Writing public key to the remote node");
    stream.write_all(&public_key).unwrap();
    stream.flush().unwrap();
    let mut remote_public_key = [0u8; 64];
    println!("Reading the remote node public key");
    stream.read_exact(&mut remote_public_key).unwrap();
}

/// Bitcoind transport versions.
#[cfg(feature = "std")]
enum TransportVersion {
    V1,
    V2,
}

/// Fire up a managed regtest bitcoind process.
#[cfg(feature = "std")]
fn regtest_process(transport: TransportVersion) -> bitcoind::Node {
    // Pull executable from auto-downloaded location, unless
    // environment variable override is present. Some operating
    // systems (e.g. NixOS) don't like the downloaded executable
    // so the environment varible must be used.
    let exe_path = bitcoind::exe_path().unwrap();
    println!("Using bitcoind at {exe_path}");
    let mut conf = bitcoind::Conf::default();

    // Enable V2 if requested, otherwise disable.
    match transport {
        TransportVersion::V2 => conf.args.push("-v2transport=1"),
        TransportVersion::V1 => conf.args.push("-v2transport=0"),
    }

    // Enable p2p port for tests.
    conf.p2p = bitcoind::P2P::Yes;
    bitcoind::Node::with_conf(exe_path, &conf).unwrap()
}