Files
moon-i2p/common/identity.mbt
T
Antigravity e527b84bc3
check / check (ubuntu-latest) (push) Failing after 30s
check / check (macos-latest) (push) Failing after 33s
check / check (windows-latest) (push) Failing after 32s
feat: bootstrap moon-i2p core protocol foundation
2026-11-27 15:37:42 +00:00

257 lines
6.5 KiB
MoonBit

///|
/// KeysAndCert is the 387+ byte base structure shared by `RouterIdentity` and `Destination`:
/// - 256 bytes: crypto public key (with trailing padding if key < 256 bytes)
/// - 128 bytes: signing public key (right-aligned with leading padding if key < 128 bytes)
/// - 3+ bytes: `Certificate` (typically `Certificate::Key` for modern X25519 + Ed25519).
#derive(Eq, Debug)
pub struct KeysAndCert {
key_data : Bytes // exactly 384 bytes (256 + 128)
certificate : Certificate
}
///|
/// Constructs a modern X25519 + Ed25519 `KeysAndCert` (CryptoType=4, SigType=7).
/// Per I2P spec: X25519 public key (32 bytes) is placed at bytes 0..32,
/// `padding` (320 bytes, or zero-filled if omitted) occupies bytes 32..352,
/// and Ed25519 signing public key (32 bytes) is right-aligned at bytes 352..384.
pub fn KeysAndCert::new_x25519_ed25519(
crypto_pub : Bytes,
signing_pub : Bytes,
padding~ : Bytes = b"",
) -> KeysAndCert raise CommonError {
if crypto_pub.length() != 32 {
raise InvalidLength(
"X25519 public key must be 32 bytes, got \{crypto_pub.length()}",
)
}
if signing_pub.length() != 32 {
raise InvalidLength(
"Ed25519 public key must be 32 bytes, got \{signing_pub.length()}",
)
}
let buf = FixedArray::make(384, b'\x00')
for i = 0; i < 32; i = i + 1 {
buf[i] = crypto_pub[i]
}
let pad_len = if padding.length() < 320 { padding.length() } else { 320 }
for i = 0; i < pad_len; i = i + 1 {
buf[32 + i] = padding[i]
}
for i = 0; i < 32; i = i + 1 {
buf[352 + i] = signing_pub[i]
}
let cert = Certificate::Key(KeyCertificate::new(EdDsaSha512Ed25519, X25519))
{ key_data: Bytes::from_array(buf), certificate: cert, }
}
///|
pub fn KeysAndCert::crypto_type(self : KeysAndCert) -> CryptoPublicKeyType {
match self.certificate {
Key(kc) => kc.crypto_type
_ => ElGamal
}
}
///|
pub fn KeysAndCert::signing_type(self : KeysAndCert) -> SigningPublicKeyType {
match self.certificate {
Key(kc) => kc.sig_type
_ => DsaSha1
}
}
///|
/// Extracts the active cryptography public key from the 384-byte key block.
pub fn KeysAndCert::crypto_public_key(self : KeysAndCert) -> Bytes {
let klen = self.crypto_type().key_length()
let take = if klen <= 256 { klen } else { 256 }
let out = FixedArray::make(take, b'\x00')
for i = 0; i < take; i = i + 1 {
out[i] = self.key_data[i]
}
Bytes::from_array(out)
}
///|
/// Extracts the active signing public key (right-aligned in bytes 256..384, plus any excess in KeyCert).
pub fn KeysAndCert::signing_public_key(self : KeysAndCert) -> Bytes {
let klen = self.signing_type().public_key_length()
if klen <= 128 {
let start = 384 - klen
let out = FixedArray::make(klen, b'\x00')
for i = 0; i < klen; i = i + 1 {
out[i] = self.key_data[start + i]
}
Bytes::from_array(out)
} else {
let excess = match self.certificate {
Key(kc) => kc.excess_signing_key
_ => b""
}
let out = FixedArray::make(128 + excess.length(), b'\x00')
for i = 0; i < 128; i = i + 1 {
out[i] = self.key_data[256 + i]
}
for i = 0; i < excess.length(); i = i + 1 {
out[128 + i] = excess[i]
}
Bytes::from_array(out)
}
}
///|
pub fn KeysAndCert::encode(self : KeysAndCert, w : @encoding.Writer) -> Unit {
w.write_bytes(self.key_data)
self.certificate.encode(w)
}
///|
pub fn KeysAndCert::to_bytes(self : KeysAndCert) -> Bytes {
let w = @encoding.Writer::new(capacity=391)
self.encode(w)
w.to_bytes()
}
///|
pub fn KeysAndCert::decode(
r : @encoding.Reader,
) -> KeysAndCert raise CommonError {
let key_data = r.read_bytes(384) catch { e => raise Encoding(e) }
let certificate = Certificate::decode(r)
{ key_data, certificate, }
}
///|
pub fn KeysAndCert::hash(self : KeysAndCert) -> Hash {
Hash::sha256(self.to_bytes())
}
///|
/// I2P Destination: identifies a client endpoint (service or client tunnel) on the I2P network.
#derive(Eq, Debug)
pub struct Destination {
inner : KeysAndCert
}
///|
pub fn Destination::new(inner : KeysAndCert) -> Destination {
{ inner, }
}
///|
pub fn Destination::new_x25519_ed25519(
crypto_pub : Bytes,
signing_pub : Bytes,
padding~ : Bytes = b"",
) -> Destination raise CommonError {
{ inner: KeysAndCert::new_x25519_ed25519(crypto_pub, signing_pub, padding~), }
}
///|
pub fn Destination::encode(self : Destination, w : @encoding.Writer) -> Unit {
self.inner.encode(w)
}
///|
pub fn Destination::decode(
r : @encoding.Reader,
) -> Destination raise CommonError {
{ inner: KeysAndCert::decode(r), }
}
///|
pub fn Destination::to_bytes(self : Destination) -> Bytes {
self.inner.to_bytes()
}
///|
pub fn Destination::hash(self : Destination) -> Hash {
self.inner.hash()
}
///|
/// Returns the canonical `<52-char-base32>.b32.i2p` address for this Destination.
pub fn Destination::b32_address(self : Destination) -> String {
self.hash().to_b32_address()
}
///|
pub fn Destination::to_base64(self : Destination) -> String {
@encoding.base64_encode(self.to_bytes())
}
///|
pub fn Destination::from_base64(s : String) -> Destination raise CommonError {
let raw = @encoding.base64_decode(s) catch { e => raise Encoding(e) }
let r = @encoding.Reader::new(raw)
Destination::decode(r)
}
///|
/// I2P RouterIdentity: uniquely identifies an I2P router in the NetDB and NTCP2/SSU2 handshakes.
#derive(Eq, Debug)
pub struct RouterIdentity {
inner : KeysAndCert
}
///|
pub fn RouterIdentity::new(inner : KeysAndCert) -> RouterIdentity {
{ inner, }
}
///|
pub fn RouterIdentity::new_x25519_ed25519(
crypto_pub : Bytes,
signing_pub : Bytes,
padding~ : Bytes = b"",
) -> RouterIdentity raise CommonError {
{ inner: KeysAndCert::new_x25519_ed25519(crypto_pub, signing_pub, padding~), }
}
///|
pub fn RouterIdentity::encode(
self : RouterIdentity,
w : @encoding.Writer,
) -> Unit {
self.inner.encode(w)
}
///|
pub fn RouterIdentity::decode(
r : @encoding.Reader,
) -> RouterIdentity raise CommonError {
{ inner: KeysAndCert::decode(r), }
}
///|
pub fn RouterIdentity::to_bytes(self : RouterIdentity) -> Bytes {
self.inner.to_bytes()
}
///|
pub fn RouterIdentity::hash(self : RouterIdentity) -> Hash {
self.inner.hash()
}
///|
pub fn RouterIdentity::b32_address(self : RouterIdentity) -> String {
self.hash().to_b32_address()
}
///|
pub fn RouterIdentity::signing_type(
self : RouterIdentity,
) -> SigningPublicKeyType {
self.inner.signing_type()
}
///|
pub fn RouterIdentity::crypto_public_key(self : RouterIdentity) -> Bytes {
self.inner.crypto_public_key()
}
///|
pub fn RouterIdentity::signing_public_key(self : RouterIdentity) -> Bytes {
self.inner.signing_public_key()
}