Files
Go_I2p/lib/common/router_identity/router_identity.go

92 lines
2.8 KiB
Go
Raw Normal View History

2022-09-12 08:31:02 +00:00
// Package router_identity implements the I2P RouterIdentity common data structure
package router_identity
import (
2024-11-09 00:53:17 -05:00
"github.com/go-i2p/go-i2p/lib/common/certificate"
2024-11-17 21:44:32 -05:00
"github.com/go-i2p/go-i2p/lib/common/destination"
2024-11-09 00:53:17 -05:00
"github.com/go-i2p/go-i2p/lib/common/key_certificate"
. "github.com/go-i2p/go-i2p/lib/common/keys_and_cert"
2024-11-09 00:53:17 -05:00
"github.com/go-i2p/go-i2p/lib/crypto"
"github.com/go-i2p/logger"
2024-10-18 14:27:41 -04:00
"github.com/sirupsen/logrus"
)
var log = logger.GetGoI2PLogger()
2024-10-18 14:27:41 -04:00
2022-09-12 08:31:02 +00:00
/*
[RouterIdentity]
Accurate for version 0.9.49
2022-09-12 08:31:02 +00:00
Description
Defines the way to uniquely identify a particular router
2022-09-12 08:31:02 +00:00
Contents
Identical to KeysAndCert.
*/
2022-09-12 08:31:02 +00:00
// RouterIdentity is the represenation of an I2P RouterIdentity.
//
// https://geti2p.net/spec/common-structures#routeridentity
type RouterIdentity struct {
KeysAndCert
}
2022-09-12 08:31:02 +00:00
// ReadRouterIdentity returns RouterIdentity from a []byte.
// The remaining bytes after the specified length are also returned.
// Returns a list of errors that occurred during parsing.
2016-02-15 00:34:29 -08:00
func ReadRouterIdentity(data []byte) (router_identity RouterIdentity, remainder []byte, err error) {
2024-10-18 14:27:41 -04:00
log.WithFields(logrus.Fields{
"input_length": len(data),
}).Debug("Reading RouterIdentity from data")
keys_and_cert, remainder, err := ReadKeysAndCert(data)
2024-10-18 14:27:41 -04:00
if err != nil {
log.WithError(err).Error("Failed to read KeysAndCert for RouterIdentity")
return
}
router_identity = RouterIdentity{
2022-09-12 08:31:02 +00:00
keys_and_cert,
}
2024-10-18 14:27:41 -04:00
log.WithFields(logrus.Fields{
"remainder_length": len(remainder),
}).Debug("Successfully read RouterIdentity")
return
}
2024-11-09 00:53:17 -05:00
func NewRouterIdentity(publicKey crypto.RecievingPublicKey, signingPublicKey crypto.SigningPublicKey, cert certificate.Certificate, padding []byte) (*RouterIdentity, error) {
2024-11-09 00:53:17 -05:00
log.Debug("Creating new RouterIdentity")
// Step 1: Create keyCertificate from the provided certificate.
// Assuming NewKeyCertificate is a constructor that takes a Certificate and returns a keyCertificate.
2024-11-26 15:55:50 -05:00
keyCert, err := key_certificate.KeyCertificateFromCertificate(cert)
if err != nil {
log.WithError(err).Error("KeyCertificateFromCertificate failed.")
return nil, err
}
2024-11-09 00:53:17 -05:00
// Step 2: Create KeysAndCert instance.
keysAndCert, err := NewKeysAndCert(keyCert, publicKey, padding, signingPublicKey)
if err != nil {
log.WithError(err).Error("NewKeysAndCert failed.")
2024-11-26 15:55:50 -05:00
return nil, err
2024-11-09 00:53:17 -05:00
}
// Step 3: Initialize RouterIdentity with KeysAndCert.
routerIdentity := RouterIdentity{
KeysAndCert: *keysAndCert,
}
log.WithFields(logrus.Fields{
"public_key_type": keyCert.PublicKeyType(),
"signing_public_key_type": keyCert.SigningPublicKeyType(),
"padding_length": len(padding),
}).Debug("Successfully created RouterIdentity")
return &routerIdentity, nil
}
2024-11-17 21:44:32 -05:00
func (router_identity *RouterIdentity) AsDestination() destination.Destination {
return destination.Destination{
KeysAndCert: router_identity.KeysAndCert,
}
}