- 
                Notifications
    You must be signed in to change notification settings 
- Fork 421
Add API for constructing blinded payment paths #2412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            TheBlueMatt
  merged 13 commits into
  lightningdevkit:main
from
valentinewallace:2023-07-construct-blinded-paths
  
      
      
   
  Aug 23, 2023 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a5b7cf2
              
                Move blinded path util into blinded_path::utils
              
              
                valentinewallace 381cc64
              
                Move some blinded path message code into message submodule.
              
              
                valentinewallace fe5a076
              
                Move blinded message path util into message submodule
              
              
                valentinewallace 1b35661
              
                Move Padding into blinded_path module for use in blinded payments
              
              
                valentinewallace 7c1726b
              
                Update blinded path util to take iterator instead of slice
              
              
                valentinewallace 9777485
              
                Minor BlindedHop docs update
              
              
                valentinewallace 4a30d9e
              
                Rename ser macro
              
              
                valentinewallace cf64e3f
              
                Add new _init_and_read_tlv_stream ser macro
              
              
                valentinewallace d224f98
              
                Simplify onion message blinded hop construction
              
              
                valentinewallace 76f8cc1
              
                Support constructing BlindedPaths for payments.
              
              
                valentinewallace 0ddd3cb
              
                Blinded paths: rename encrypted_tlvs_ss to *_rho for precision
              
              
                valentinewallace ebb0676
              
                Fix documentation on onion message packet ControlTlvs
              
              
                valentinewallace ea84f2a
              
                Document _init_and_read_* ser macro requirements
              
              
                valentinewallace File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; | ||
|  | ||
| use crate::blinded_path::{BlindedHop, BlindedPath}; | ||
| use crate::blinded_path::utils; | ||
| use crate::io; | ||
| use crate::io::Cursor; | ||
| use crate::ln::onion_utils; | ||
| use crate::onion_message::ControlTlvs; | ||
| use crate::prelude::*; | ||
| use crate::sign::{NodeSigner, Recipient}; | ||
| use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter; | ||
| use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer}; | ||
|  | ||
| use core::mem; | ||
| use core::ops::Deref; | ||
|  | ||
| /// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded | ||
| /// route, they are encoded into [`BlindedHop::encrypted_payload`]. | ||
| pub(crate) struct ForwardTlvs { | ||
| /// The node id of the next hop in the onion message's path. | ||
| pub(crate) next_node_id: PublicKey, | ||
| /// Senders to a blinded path use this value to concatenate the route they find to the | ||
| /// introduction node with the blinded path. | ||
| pub(crate) next_blinding_override: Option<PublicKey>, | ||
| } | ||
|  | ||
| /// Similar to [`ForwardTlvs`], but these TLVs are for the final node. | ||
| pub(crate) struct ReceiveTlvs { | ||
| /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is | ||
| /// sending to. This is useful for receivers to check that said blinded path is being used in | ||
| /// the right context. | ||
| pub(crate) path_id: Option<[u8; 32]>, | ||
| } | ||
|  | ||
| impl Writeable for ForwardTlvs { | ||
| fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { | ||
| // TODO: write padding | ||
| encode_tlv_stream!(writer, { | ||
| (4, self.next_node_id, required), | ||
| (8, self.next_blinding_override, option) | ||
| }); | ||
| Ok(()) | ||
| } | ||
| } | ||
|  | ||
| impl Writeable for ReceiveTlvs { | ||
| fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { | ||
| // TODO: write padding | ||
| encode_tlv_stream!(writer, { | ||
| (6, self.path_id, option), | ||
| }); | ||
| Ok(()) | ||
| } | ||
| } | ||
|  | ||
| /// Construct blinded onion message hops for the given `unblinded_path`. | ||
| pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>( | ||
| secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey | ||
| ) -> Result<Vec<BlindedHop>, secp256k1::Error> { | ||
| let blinded_tlvs = unblinded_path.iter() | ||
| .skip(1) // The first node's TLVs contains the next node's pubkey | ||
| .map(|pk| { | ||
| ControlTlvs::Forward(ForwardTlvs { next_node_id: *pk, next_blinding_override: None }) | ||
| }) | ||
| .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None }))); | ||
|  | ||
| utils::construct_blinded_hops(secp_ctx, unblinded_path.iter(), blinded_tlvs, session_priv) | ||
| } | ||
|  | ||
| // Advance the blinded onion message path by one hop, so make the second hop into the new | ||
| // introduction node. | ||
| pub(crate) fn advance_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>( | ||
| path: &mut BlindedPath, node_signer: &NS, secp_ctx: &Secp256k1<T> | ||
| ) -> Result<(), ()> where NS::Target: NodeSigner { | ||
| let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?; | ||
| let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes()); | ||
| let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload; | ||
| let mut s = Cursor::new(&encrypted_control_tlvs); | ||
| let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64); | ||
| match ChaChaPolyReadAdapter::read(&mut reader, rho) { | ||
| Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs { | ||
| mut next_node_id, next_blinding_override, | ||
| })}) => { | ||
| let mut new_blinding_point = match next_blinding_override { | ||
| Some(blinding_point) => blinding_point, | ||
| None => { | ||
| onion_utils::next_hop_pubkey(secp_ctx, path.blinding_point, | ||
| control_tlvs_ss.as_ref()).map_err(|_| ())? | ||
| } | ||
| }; | ||
| mem::swap(&mut path.blinding_point, &mut new_blinding_point); | ||
| mem::swap(&mut path.introduction_node_id, &mut next_node_id); | ||
| Ok(()) | ||
| }, | ||
| _ => Err(()) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need the same utility for blinded payment paths (and do they have the same control tlvs)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was planning to do this as a follow-up to #2413, is your preference to get it out sooner? The control TLVs aren't the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's alright, I just wanted to make sure that the move made sense - I guess the follow-up will mean basically copying this and writing some new code to do the same thing for blinded payment paths?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's the thinking