feat: bootstrap moon-i2p core protocol foundation
check / check (ubuntu-latest) (push) Failing after 30s
check / check (macos-latest) (push) Failing after 33s
check / check (windows-latest) (push) Failing after 32s

This commit is contained in:
Antigravity committed 2026-11-27 15:37:42 +00:00
commit e527b84bc3
47 files changed
+5350

No files matched your search

+244
View File
@@ -0,0 +1,244 @@
///|
/// 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::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
}
///|
pub fn LeaseSet2::encode(
self : LeaseSet2,
w : @encoding.Writer,
) -> Unit raise CommonError {
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.write_bytes(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,
}
}