Files
net/endpoints/claimed_ip_port.go
zeekay f8e36531a8 codec: rip github.com/luxfi/codec from leaf-misc (Wave 1D)
Replace codec/wrappers.Packer with an in-package packer (binio.go).
endpoints/ only needs PackByte/PackShort/PackLong/PackFixedBytes for
the gossip-ID preimage and the endpoint signing bytes — a tiny self-
contained big-endian writer over a pre-sized slice.

Files touched:
- endpoints/binio.go (new): packer + ByteLen/ShortLen/IntLen/LongLen
- endpoints/claimed_ip_port.go: drop wrappers, use local packer
- endpoints/endpoint.go: drop wrappers, use local packer
- endpoints/ip_port.go: drop wrappers; remove dead PackIP (no callers)

go.mod direct dep on luxfi/codec removed.
2026-06-05 11:56:48 -07:00

74 lines
1.9 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package endpoints
import (
"net"
"net/netip"
"github.com/luxfi/crypto/hash"
"github.com/luxfi/ids"
luxtls "github.com/luxfi/tls"
)
const (
// Certificate length, signature length, IP, timestamp, tx ID
baseIPCertDescLen = 2*IntLen + net.IPv6len + ShortLen + LongLen + ids.IDLen
preimageLen = ids.IDLen + LongLen
)
// A self contained proof that a peer is claiming ownership of an IPPort at a
// given time.
type ClaimedIPPort struct {
// The peer's certificate.
Cert *luxtls.Certificate
// The peer's claimed IP and port.
AddrPort netip.AddrPort
// The time the peer claimed to own this IP and port.
Timestamp uint64
// [Cert]'s signature over the IPPort and timestamp.
// This is used in the networking library to ensure that this IPPort was
// actually claimed by the peer in question, and not by a malicious peer
// trying to get us to dial bogus IPPorts.
Signature []byte
// NodeID derived from the peer certificate.
NodeID ids.NodeID
// GossipID derived from the nodeID and timestamp.
GossipID ids.ID
}
func NewClaimedIPPort(
cert *luxtls.Certificate,
ipPort netip.AddrPort,
timestamp uint64,
signature []byte,
) *ClaimedIPPort {
// Convert luxtls.Certificate to ids.Certificate
idsCert := &ids.Certificate{
Raw: cert.Raw,
PublicKey: cert.PublicKey,
}
ip := &ClaimedIPPort{
Cert: cert,
AddrPort: ipPort,
Timestamp: timestamp,
Signature: signature,
NodeID: ids.NodeIDFromCert(idsCert),
}
p := packer{
Bytes: make([]byte, preimageLen),
}
p.PackFixedBytes(ip.NodeID[:])
p.PackLong(timestamp)
ip.GossipID = hash.ComputeHash256Array(p.Bytes)
return ip
}
// Returns the approximate size of the binary representation of this ClaimedIPPort.
func (i *ClaimedIPPort) Size() int {
return baseIPCertDescLen + len(i.Cert.Raw) + len(i.Signature)
}