diff --git a/electrum/lnonion.py b/electrum/lnonion.py index 10fb2291f..a334fee6a 100644 --- a/electrum/lnonion.py +++ b/electrum/lnonion.py @@ -193,6 +193,17 @@ def get_blinded_node_id(node_id: bytes, shared_secret: bytes): return blinded_node_id.get_public_key_bytes() +def blinding_privkey(privkey: bytes, blinding: bytes) -> bytes: + shared_secret = get_ecdh(privkey, blinding) + b_hmac = get_bolt04_onion_key(b'blinded_node_id', shared_secret) + b_hmac_int = int.from_bytes(b_hmac, byteorder="big") + + our_privkey_int = int.from_bytes(privkey, byteorder="big") + our_privkey_int = our_privkey_int * b_hmac_int % ecc.CURVE_ORDER + our_privkey = our_privkey_int.to_bytes(32, byteorder="big") + return our_privkey + + def new_onion_packet( payment_path_pubkeys: Sequence[bytes], session_key: bytes, diff --git a/electrum/onion_message.py b/electrum/onion_message.py index e200aed64..8e4baffc8 100644 --- a/electrum/onion_message.py +++ b/electrum/onion_message.py @@ -40,7 +40,7 @@ from electrum.lnrouter import PathEdge from electrum.logging import get_logger, Logger from electrum.crypto import sha256, get_ecdh from electrum.lnmsg import OnionWireSerializer -from electrum.lnonion import (get_bolt04_onion_key, OnionPacket, process_onion_packet, +from electrum.lnonion import (get_bolt04_onion_key, OnionPacket, process_onion_packet, blinding_privkey, OnionHopsDataSingle, decrypt_onionmsg_data_tlv, encrypt_onionmsg_data_tlv, get_shared_secrets_along_route, new_onion_packet, encrypt_hops_recipient_data) from electrum.lnutil import LnFeatures, MIN_FINAL_CLTV_DELTA_ACCEPTED, MAXIMUM_REMOTE_TO_SELF_DELAY_ACCEPTED @@ -145,18 +145,6 @@ def encode_blinded_path(blinded_path: dict): return blinded_path_fd.getvalue() -def blinding_privkey(privkey: bytes, blinding: bytes) -> bytes: - shared_secret = get_ecdh(privkey, blinding) - b_hmac = get_bolt04_onion_key(b'blinded_node_id', shared_secret) - b_hmac_int = int.from_bytes(b_hmac, byteorder="big") - - our_privkey_int = int.from_bytes(privkey, byteorder="big") - our_privkey_int = our_privkey_int * b_hmac_int % ecc.CURVE_ORDER - our_privkey = our_privkey_int.to_bytes(32, byteorder="big") - - return our_privkey - - def is_onion_message_node(node_id: bytes, node_info: Optional['NodeInfo']) -> bool: if not node_info: return False diff --git a/tests/test_onion_message.py b/tests/test_onion_message.py index dd2be43df..d261bbe6d 100644 --- a/tests/test_onion_message.py +++ b/tests/test_onion_message.py @@ -17,12 +17,12 @@ from electrum.lnmsg import decode_msg, OnionWireSerializer from electrum.lnonion import ( OnionHopsDataSingle, OnionPacket, process_onion_packet, get_bolt04_onion_key, encrypt_onionmsg_data_tlv, get_shared_secrets_along_route, new_onion_packet, ONION_MESSAGE_LARGE_SIZE, HOPS_DATA_SIZE, InvalidPayloadSize, - encrypt_hops_recipient_data) + encrypt_hops_recipient_data, blinding_privkey) from electrum.crypto import get_ecdh, privkey_to_pubkey from electrum.lntransport import LNPeerAddr from electrum.lnutil import LnFeatures, Keypair, MIN_FINAL_CLTV_DELTA_ACCEPTED, REMOTE from electrum.onion_message import ( - blinding_privkey, create_blinded_path,OnionMessageManager, NoRouteFound, Timeout, get_blinded_paths_to_me, + create_blinded_path, OnionMessageManager, NoRouteFound, Timeout, get_blinded_paths_to_me, ) from electrum.util import bfh, read_json_file, OldTaskGroup, get_asyncio_loop from electrum.logging import console_stderr_handler