|
| 1 | +package onionmessage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/lightningnetwork/lnd/lnutils" |
| 9 | + "github.com/lightningnetwork/lnd/lnwire" |
| 10 | + "github.com/lightningnetwork/lnd/msgmux" |
| 11 | + "github.com/lightningnetwork/lnd/subscribe" |
| 12 | +) |
| 13 | + |
| 14 | +// OnionMessageUpdate is onion message update dispatched to any potential |
| 15 | +// subscriber. |
| 16 | +type OnionMessageUpdate struct { |
| 17 | + // Peer is the peer pubkey |
| 18 | + Peer [33]byte |
| 19 | + |
| 20 | + // PathKey is the route blinding ephemeral pubkey to be used for |
| 21 | + // the onion message. |
| 22 | + PathKey [33]byte |
| 23 | + |
| 24 | + // OnionBlob is the raw serialized mix header used to relay messages in |
| 25 | + // a privacy-preserving manner. This blob should be handled in the same |
| 26 | + // manner as onions used to route HTLCs, with the exception that it uses |
| 27 | + // blinded routes by default. |
| 28 | + OnionBlob []byte |
| 29 | +} |
| 30 | + |
| 31 | +// OnionEndpoint handles incoming onion messages. |
| 32 | +type OnionEndpoint struct { |
| 33 | + // subscribe.Server is used for subscriptions to onion messages. |
| 34 | + onionMessageServer *subscribe.Server |
| 35 | +} |
| 36 | + |
| 37 | +// A compile-time check to ensure OnionEndpoint implements the Endpoint |
| 38 | +// interface. |
| 39 | +var _ msgmux.Endpoint = (*OnionEndpoint)(nil) |
| 40 | + |
| 41 | +// NewOnionEndpoint creates a new OnionEndpoint. |
| 42 | +func NewOnionEndpoint(messageServer *subscribe.Server) *OnionEndpoint { |
| 43 | + return &OnionEndpoint{ |
| 44 | + onionMessageServer: messageServer, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Name returns the unique name of the endpoint. |
| 49 | +func (o *OnionEndpoint) Name() string { |
| 50 | + return "OnionMessageHandler" |
| 51 | +} |
| 52 | + |
| 53 | +// CanHandle checks if the endpoint can handle the incoming message. |
| 54 | +// It returns true if the message is an lnwire.OnionMessage. |
| 55 | +func (o *OnionEndpoint) CanHandle(msg msgmux.PeerMsg) bool { |
| 56 | + _, ok := msg.Message.(*lnwire.OnionMessage) |
| 57 | + return ok |
| 58 | +} |
| 59 | + |
| 60 | +// SendMessage processes the incoming onion message. |
| 61 | +// It returns true if the message was successfully processed. |
| 62 | +func (o *OnionEndpoint) SendMessage(ctx context.Context, |
| 63 | + msg msgmux.PeerMsg) bool { |
| 64 | + |
| 65 | + onionMsg, ok := msg.Message.(*lnwire.OnionMessage) |
| 66 | + if !ok { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + peer := msg.PeerPub.SerializeCompressed() |
| 71 | + log.DebugS(ctx, "OnionEndpoint received OnionMessage", |
| 72 | + slog.String("peer", hex.EncodeToString(peer)), |
| 73 | + lnutils.LogPubKey("path_key", onionMsg.PathKey), |
| 74 | + lnutils.LogBytesPreview("onion_blob", onionMsg.OnionBlob), |
| 75 | + slog.Int("blob length", len(onionMsg.OnionBlob))) |
| 76 | + |
| 77 | + var peerArr [33]byte |
| 78 | + copy(peerArr[:], peer) |
| 79 | + |
| 80 | + // Convert blinding point []byte to [33]byte. |
| 81 | + blinding := onionMsg.PathKey.SerializeCompressed() |
| 82 | + var blindingArr [33]byte |
| 83 | + copy(blindingArr[:], blinding) |
| 84 | + |
| 85 | + err := o.onionMessageServer.SendUpdate(&OnionMessageUpdate{ |
| 86 | + Peer: peerArr, |
| 87 | + PathKey: blindingArr, |
| 88 | + OnionBlob: onionMsg.OnionBlob, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + log.ErrorS(ctx, "Failed to send onion message update", err) |
| 92 | + return false |
| 93 | + } |
| 94 | + |
| 95 | + return true |
| 96 | +} |
0 commit comments