|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Noise protocol examples for py-multiaddr. |
| 4 | +
|
| 5 | +This script demonstrates how to use the 'noise' protocol in py-multiaddr. |
| 6 | +The 'noise' protocol is a tag protocol, meaning it does not take an |
| 7 | +argument. Its presence signifies that the connection is secured using |
| 8 | +the Noise Protocol Framework. |
| 9 | +
|
| 10 | +## Overview |
| 11 | +
|
| 12 | +This script shows various examples of Noise protocol usage: |
| 13 | +
|
| 14 | +1. **Basic Noise Usage**: Creating and parsing a simple /noise address. |
| 15 | +2. **Protocol Validation**: Testing valid (/noise) and invalid (/noise/value) |
| 16 | + addresses. |
| 17 | +3. **Binary Encoding/Decoding**: Working with binary representations. |
| 18 | +4. **Multiaddr Integration**: Using 'noise' in a realistic multiaddr string |
| 19 | + (e.g., /ip4/127.0.0.1/tcp/12345/noise). |
| 20 | +5. **Error Handling**: Demonstrating errors when a value is incorrectly |
| 21 | + provided. |
| 22 | +
|
| 23 | +## Expected Output |
| 24 | +
|
| 25 | +When you run this script, you should see output similar to: |
| 26 | +
|
| 27 | +``` |
| 28 | +NOISE Protocol Examples |
| 29 | +================================================== |
| 30 | +=== Basic Noise Usage === |
| 31 | +Original Noise: /noise |
| 32 | + Protocols: ['noise'] |
| 33 | + Binary length: 2 bytes |
| 34 | + Valid Noise address: True |
| 35 | +
|
| 36 | +=== Protocol Validation === |
| 37 | +Testing valid Noise: /noise |
| 38 | + Valid: True |
| 39 | + Protocols: ['noise'] |
| 40 | +
|
| 41 | +Testing invalid Noise (with value): /noise/somevalue |
| 42 | + Valid: False |
| 43 | + Error: Protocol 'noise' does not take an argument |
| 44 | +
|
| 45 | +=== Binary Encoding/Decoding === |
| 46 | +Noise binary operations: |
| 47 | + Original: /noise |
| 48 | + Binary: 2 bytes |
| 49 | + Round-trip: /noise Match: True |
| 50 | +
|
| 51 | +=== Multiaddr Integration === |
| 52 | +Complex multiaddr with Noise: |
| 53 | + Address: /ip4/127.0.0.1/tcp/12345/noise/p2p/Qm... |
| 54 | + Protocols: ['ip4', 'tcp', 'noise', 'p2p'] |
| 55 | + Has 'noise' protocol: True |
| 56 | + |
| 57 | +================================================== |
| 58 | +All examples completed! |
| 59 | +``` |
| 60 | +
|
| 61 | +## Key Features Demonstrated |
| 62 | +
|
| 63 | +- **Noise Protocol**: Represents a connection secured by the Noise framework. |
| 64 | +- **Tag Protocol**: 'noise' is a tag-only protocol and takes no arguments. |
| 65 | +- **Validation**: Ensures no value is provided to the 'noise' protocol. |
| 66 | +- **Binary Operations**: Encoding and decoding to/from binary format. |
| 67 | +- **Multiaddr Integration**: Using 'noise' as part of a connection stack. |
| 68 | +- **Error Handling**: Proper error handling for invalid formats. |
| 69 | +
|
| 70 | +## Requirements |
| 71 | +
|
| 72 | +- Python 3.10+ |
| 73 | +- py-multiaddr library |
| 74 | +
|
| 75 | +## Usage |
| 76 | +
|
| 77 | +```bash |
| 78 | +python examples/noise_examples.py |
| 79 | +``` |
| 80 | +""" |
| 81 | + |
| 82 | +from multiaddr import Multiaddr |
| 83 | + |
| 84 | +NOISE_ADDR = "/noise" |
| 85 | + |
| 86 | +def basic_noise_usage(): |
| 87 | + """ |
| 88 | + Basic Noise usage example. |
| 89 | +
|
| 90 | + This function demonstrates: |
| 91 | + - Creating a Noise multiaddr |
| 92 | + - Extracting protocol information |
| 93 | + - Getting binary representation |
| 94 | + """ |
| 95 | + |
| 96 | + print(f'Original Noise: {NOISE_ADDR}') |
| 97 | + |
| 98 | + try: |
| 99 | + ma = Multiaddr(NOISE_ADDR) |
| 100 | + print(f"Protocols: {[p.name for p in ma.protocols()]}") |
| 101 | + |
| 102 | + # Get binary representation |
| 103 | + binary_data = ma.to_bytes() |
| 104 | + print(f"Binary length: {len(binary_data)} bytes") |
| 105 | + |
| 106 | + print("Valid Noise address: True") |
| 107 | + |
| 108 | + except Exception as e: |
| 109 | + print(f"Error: {e}") |
| 110 | + print("Valid Noise address: False") |
| 111 | + |
| 112 | +def protocol_validation(): |
| 113 | + """ |
| 114 | + Demonstrate protocol validation. |
| 115 | +
|
| 116 | + This function shows: |
| 117 | + - A valid Noise address |
| 118 | + - An invalid Noise address (with a value) |
| 119 | + - Error handling for validation failures |
| 120 | + """ |
| 121 | + print("\n=== Protocol Validation ===") |
| 122 | + |
| 123 | + # Test valid Noise |
| 124 | + print(f"Testing valid Noise: {NOISE_ADDR}") |
| 125 | + try: |
| 126 | + ma = Multiaddr(NOISE_ADDR) |
| 127 | + print(" Valid: True") |
| 128 | + print(f" Protocols: {[p.name for p in ma.protocols()]}") |
| 129 | + except Exception as e: |
| 130 | + print(" Valid: False") |
| 131 | + print(f" Error: {e}") |
| 132 | + |
| 133 | + # Test invalid Noise (with a value) |
| 134 | + invalid_addr_str = "/noise/somevalue" |
| 135 | + print(f"\nTesting invalid Noise (with value): {invalid_addr_str}") |
| 136 | + try: |
| 137 | + Multiaddr(invalid_addr_str) |
| 138 | + print(" Valid: True (ERROR: Should have failed)") |
| 139 | + except Exception as e: |
| 140 | + print(" Valid: False") |
| 141 | + print(f" Error: {e}") |
| 142 | + |
| 143 | +def binary_encoding_decoding(): |
| 144 | + """ |
| 145 | + Demonstrate binary encoding and decoding. |
| 146 | +
|
| 147 | + This function shows: |
| 148 | + - Converting multiaddr to binary |
| 149 | + - Converting binary back to multiaddr |
| 150 | + - Round-trip validation |
| 151 | + """ |
| 152 | + print("\n=== Binary Encoding/Decoding ===") |
| 153 | + |
| 154 | + print("Noise binary operations:") |
| 155 | + print(f" Original: {NOISE_ADDR}") |
| 156 | + |
| 157 | + try: |
| 158 | + ma = Multiaddr(NOISE_ADDR) |
| 159 | + binary_data = ma.to_bytes() |
| 160 | + print(f" Binary: {len(binary_data)} bytes") |
| 161 | + |
| 162 | + # Round-trip: binary back to multiaddr |
| 163 | + round_trip_ma = Multiaddr(binary_data) |
| 164 | + print(f" Round-trip: {round_trip_ma}") |
| 165 | + print(f" Match: {str(ma) == str(round_trip_ma)}") |
| 166 | + |
| 167 | + except Exception as e: |
| 168 | + print(f" Error: {e}") |
| 169 | + |
| 170 | +def multiaddr_integration(): |
| 171 | + """ |
| 172 | + Demonstrate Noise protocol integration with other protocols. |
| 173 | +
|
| 174 | + This function shows: |
| 175 | + - Using /noise as part of a libp2p stack. |
| 176 | + - Protocol stack analysis |
| 177 | + """ |
| 178 | + print("\n=== Multiaddr Integration ===") |
| 179 | + |
| 180 | + # Create a complex multiaddr with Noise |
| 181 | + # This is a typical address for a libp2p node |
| 182 | + peer_id = "QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" |
| 183 | + complex_addr = f"/ip4/127.0.0.1/tcp/12345/noise/p2p/{peer_id}" |
| 184 | + |
| 185 | + print("Complex multiaddr with Noise:") |
| 186 | + print(f" Address: {complex_addr}") |
| 187 | + |
| 188 | + try: |
| 189 | + ma = Multiaddr(complex_addr) |
| 190 | + protocols = [p.name for p in ma.protocols()] |
| 191 | + print(f" Protocols: {protocols}") |
| 192 | + |
| 193 | + # Check for 'noise' protocol |
| 194 | + has_noise = 'noise' in protocols |
| 195 | + print(f" Has 'noise' protocol: {has_noise}") |
| 196 | + |
| 197 | + except Exception as e: |
| 198 | + print(f" Error: {e}") |
| 199 | + |
| 200 | +def main(): |
| 201 | + """ |
| 202 | + Run all Noise protocol examples. |
| 203 | +
|
| 204 | + This function orchestrates all the Noise protocol examples: |
| 205 | + 1. Basic Noise usage |
| 206 | + 2. Protocol validation |
| 207 | + 3. Binary encoding/decoding |
| 208 | + 4. Multiaddr integration |
| 209 | +
|
| 210 | + Each example demonstrates different aspects of Noise protocol functionality |
| 211 | + and shows how to use it with py-multiaddr. |
| 212 | + """ |
| 213 | + print("Noise Protocol Examples") |
| 214 | + print("=" * 50) |
| 215 | + |
| 216 | + try: |
| 217 | + basic_noise_usage() |
| 218 | + protocol_validation() |
| 219 | + binary_encoding_decoding() |
| 220 | + multiaddr_integration() |
| 221 | + |
| 222 | + print("\n" + "=" * 50) |
| 223 | + print("All examples completed!") |
| 224 | + print("\nSummary:") |
| 225 | + print("- Noise protocol is working correctly") |
| 226 | + print("- Binary encoding/decoding functions properly") |
| 227 | + print("- Validation catches invalid use (with a value)") |
| 228 | + print("- Integration with libp2p stack works as expected") |
| 229 | + |
| 230 | + except KeyboardInterrupt: |
| 231 | + print("\nExamples interrupted by user") |
| 232 | + except Exception as e: |
| 233 | + print(f"\nUnexpected error: {e}") |
| 234 | + |
| 235 | +if __name__ == "__main__": |
| 236 | + main() |
0 commit comments