///| /// RouterAddress: transport endpoint advertised inside a RouterInfo (e.g., NTCP2, SSU2). #derive(Eq, Debug) pub(all) struct RouterAddress { cost : Byte expiration : UInt64 // milliseconds since Unix epoch (0 = never) transport_style : String options : Mapping } ///| pub fn RouterAddress::new( cost : Byte, transport_style : String, options : Mapping, expiration~ : UInt64 = 0UL, ) -> RouterAddress { { cost, expiration, transport_style, options, } } ///| pub fn RouterAddress::encode( self : RouterAddress, w : @encoding.Writer, ) -> Unit raise CommonError { w.write_u8(self.cost) w.write_u64_be(self.expiration) w.write_i2p_string(self.transport_style) catch { e => raise Encoding(e) } self.options.encode(w) } ///| pub fn RouterAddress::decode( r : @encoding.Reader, ) -> RouterAddress raise CommonError { let cost = r.read_u8() catch { e => raise Encoding(e) } let expiration = r.read_u64_be() catch { e => raise Encoding(e) } let transport_style = r.read_i2p_string() catch { e => raise Encoding(e) } let options = Mapping::decode(r) { cost, expiration, transport_style, options, } } ///| /// RouterInfo: NetDB record advertising a router's identity, transport addresses, and capabilities. #derive(Eq, Debug) pub(all) struct RouterInfo { identity : RouterIdentity published : UInt64 // milliseconds since Unix epoch addresses : Array[RouterAddress] options : Mapping signature : Bytes } ///| /// Encodes the unsigned body of RouterInfo (the exact bytes signed by `signature`). pub fn RouterInfo::unsigned_bytes(self : RouterInfo) -> Bytes raise CommonError { let w = @encoding.Writer::new(capacity=512) self.identity.encode(w) w.write_u64_be(self.published) w.write_u8(self.addresses.length().to_byte()) for addr in self.addresses { addr.encode(w) } // peer_size is always 0 in I2P spec w.write_u8(b'\x00') self.options.encode(w) w.to_bytes() } ///| pub fn RouterInfo::encode( self : RouterInfo, w : @encoding.Writer, ) -> Unit raise CommonError { w.write_bytes(self.unsigned_bytes()) w.write_bytes(self.signature) } ///| pub fn RouterInfo::to_bytes(self : RouterInfo) -> Bytes raise CommonError { let w = @encoding.Writer::new(capacity=600) self.encode(w) w.to_bytes() } ///| /// Signs the `RouterInfo` using an Ed25519 32-byte private `seed` and returns /// a new `RouterInfo` with `signature` populated. pub fn RouterInfo::sign_ed25519( self : RouterInfo, seed : Bytes, ) -> RouterInfo raise CommonError { if self.identity.signing_type() != EdDsaSha512Ed25519 { raise InvalidFormat( "RouterInfo identity signing type must be EdDsaSha512Ed25519", ) } let body = self.unsigned_bytes() let signature = @crypto.ed25519_sign(seed, body) catch { e => raise Crypto(e) } { identity: self.identity, published: self.published, addresses: self.addresses, options: self.options, signature, } } ///| /// Verifies the `RouterInfo` Ed25519 signature against `self.identity.signing_public_key()`. pub fn RouterInfo::verify_signature(self : RouterInfo) -> Bool { if self.identity.signing_type() != EdDsaSha512Ed25519 { return false } let body = self.unsigned_bytes() catch { _ => return false } @crypto.ed25519_verify( self.identity.signing_public_key(), body, self.signature, ) } ///| pub fn RouterInfo::decode(r : @encoding.Reader) -> RouterInfo raise CommonError { let identity = RouterIdentity::decode(r) let published = r.read_u64_be() catch { e => raise Encoding(e) } let addr_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int() let addresses : Array[RouterAddress] = Array(capacity=addr_count) for _i = 0; _i < addr_count; _i = _i + 1 { addresses.push(RouterAddress::decode(r)) } let peer_size = (r.read_u8() catch { e => raise Encoding(e) }).to_int() for _i = 0; _i < peer_size; _i = _i + 1 { let _ = Hash::decode(r) } let options = Mapping::decode(r) let sig_len = identity.signing_type().signature_length() let signature = if sig_len > 0 { r.read_bytes(sig_len) catch { e => raise Encoding(e) } } else { r.read_remaining() } { identity, published, addresses, options, signature, } } ///| /// Legacy Lease (44 bytes): 32-byte tunnel gateway hash, 4-byte tunnel ID, 8-byte ms end date. #derive(Eq, Debug) pub(all) struct Lease { tunnel_gw : Hash tunnel_id : UInt32 end_date_ms : UInt64 } ///| pub fn Lease::encode(self : Lease, w : @encoding.Writer) -> Unit { self.tunnel_gw.encode(w) w.write_u32_be(self.tunnel_id) w.write_u64_be(self.end_date_ms) } ///| pub fn Lease::decode(r : @encoding.Reader) -> Lease raise CommonError { let tunnel_gw = Hash::decode(r) let tunnel_id = r.read_u32_be() catch { e => raise Encoding(e) } let end_date_ms = r.read_u64_be() catch { e => raise Encoding(e) } { tunnel_gw, tunnel_id, end_date_ms, } } ///| /// Modern Lease2 (40 bytes, used in LeaseSet2): 32-byte tunnel gateway hash, 4-byte tunnel ID, /// 4-byte end date in seconds since Unix epoch. #derive(Eq, Debug) pub(all) struct Lease2 { tunnel_gw : Hash tunnel_id : UInt32 end_date_sec : UInt32 } ///| pub fn Lease2::encode(self : Lease2, w : @encoding.Writer) -> Unit { self.tunnel_gw.encode(w) w.write_u32_be(self.tunnel_id) w.write_u32_be(self.end_date_sec) } ///| pub fn Lease2::decode(r : @encoding.Reader) -> Lease2 raise CommonError { let tunnel_gw = Hash::decode(r) let tunnel_id = r.read_u32_be() catch { e => raise Encoding(e) } let end_date_sec = r.read_u32_be() catch { e => raise Encoding(e) } { tunnel_gw, tunnel_id, end_date_sec, } } ///| /// Encryption key entry inside a LeaseSet2. #derive(Eq, Debug) pub(all) struct LeaseSet2EncryptionKey { key_type : CryptoPublicKeyType key_data : Bytes } ///| /// Modern LeaseSet2 (I2P Proposal 123, NetDB type 3). #derive(Eq, Debug) pub(all) struct LeaseSet2 { destination : Destination published_sec : UInt32 expires_offset : UInt32 // uint16 offset in seconds from published_sec flags : UInt32 // uint16 options : Mapping encryption_keys : Array[LeaseSet2EncryptionKey] leases : Array[Lease2] signature : Bytes } ///| /// Encodes the unsigned body of LeaseSet2 (everything before `signature`). pub fn LeaseSet2::unsigned_bytes(self : LeaseSet2) -> Bytes raise CommonError { let w = @encoding.Writer::new(capacity=512) self.destination.encode(w) w.write_u32_be(self.published_sec) w.write_u16_be(self.expires_offset) w.write_u16_be(self.flags) self.options.encode(w) w.write_u8(self.encryption_keys.length().to_byte()) for ek in self.encryption_keys { w.write_u16_be(ek.key_type.to_u16()) w.write_u16_be(ek.key_data.length().reinterpret_as_uint()) w.write_bytes(ek.key_data) } w.write_u8(self.leases.length().to_byte()) for lease in self.leases { lease.encode(w) } w.to_bytes() } ///| /// Returns the exact bytes covered by the LeaseSet2 signature: /// single-byte DatabaseStore type `0x03` prepended to `unsigned_bytes()` (Proposal 123). pub fn LeaseSet2::signing_bytes(self : LeaseSet2) -> Bytes raise CommonError { let body = self.unsigned_bytes() let w = @encoding.Writer::new(capacity=1 + body.length()) w.write_u8(b'\x03') w.write_bytes(body) w.to_bytes() } ///| pub fn LeaseSet2::encode( self : LeaseSet2, w : @encoding.Writer, ) -> Unit raise CommonError { w.write_bytes(self.unsigned_bytes()) w.write_bytes(self.signature) } ///| pub fn LeaseSet2::to_bytes(self : LeaseSet2) -> Bytes raise CommonError { let w = @encoding.Writer::new(capacity=600) self.encode(w) w.to_bytes() } ///| /// Signs the `LeaseSet2` using an Ed25519 32-byte private `seed` (over `0x03 || unsigned_bytes`). pub fn LeaseSet2::sign_ed25519( self : LeaseSet2, seed : Bytes, ) -> LeaseSet2 raise CommonError { if self.destination.inner.signing_type() != EdDsaSha512Ed25519 { raise InvalidFormat( "LeaseSet2 destination signing type must be EdDsaSha512Ed25519", ) } let to_sign = self.signing_bytes() let signature = @crypto.ed25519_sign(seed, to_sign) catch { e => raise Crypto(e) } { destination: self.destination, published_sec: self.published_sec, expires_offset: self.expires_offset, flags: self.flags, options: self.options, encryption_keys: self.encryption_keys, leases: self.leases, signature, } } ///| /// Verifies the `LeaseSet2` Ed25519 signature against `self.destination.inner.signing_public_key()`. pub fn LeaseSet2::verify_signature(self : LeaseSet2) -> Bool { if self.destination.inner.signing_type() != EdDsaSha512Ed25519 { return false } let to_sign = self.signing_bytes() catch { _ => return false } @crypto.ed25519_verify( self.destination.inner.signing_public_key(), to_sign, self.signature, ) } ///| pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError { let destination = Destination::decode(r) let published_sec = r.read_u32_be() catch { e => raise Encoding(e) } let expires_offset = r.read_u16_be() catch { e => raise Encoding(e) } let flags = r.read_u16_be() catch { e => raise Encoding(e) } let options = Mapping::decode(r) let key_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int() let encryption_keys : Array[LeaseSet2EncryptionKey] = Array( capacity=key_count, ) for _i = 0; _i < key_count; _i = _i + 1 { let kt = CryptoPublicKeyType::from_u16( r.read_u16_be() catch { e => raise Encoding(e) }, ) let klen = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int() let kd = r.read_bytes(klen) catch { e => raise Encoding(e) } encryption_keys.push({ key_type: kt, key_data: kd, }) } let lease_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int() let leases : Array[Lease2] = Array(capacity=lease_count) for _i = 0; _i < lease_count; _i = _i + 1 { leases.push(Lease2::decode(r)) } let sig_len = destination.inner.signing_type().signature_length() let signature = if sig_len > 0 { r.read_bytes(sig_len) catch { e => raise Encoding(e) } } else { r.read_remaining() } { destination, published_sec, expires_offset, flags, options, encryption_keys, leases, signature, } }