Skip to content

Instantly share code, notes, and snippets.

@geeknik
Last active September 27, 2023 16:19
Show Gist options
  • Save geeknik/5a75459c89d0184dcc406dfe2768093b to your computer and use it in GitHub Desktop.
Save geeknik/5a75459c89d0184dcc406dfe2768093b to your computer and use it in GitHub Desktop.
lightning network anti-spam poc for sending/receiving email
from ecdsa import SigningKey, VerifyingKey
import smtplib
from email.mime.text import MIMEText
import lnd_grpc
import imaplib
import email
# Initialize Lightning Network client (Assumes LND is running)
lnd_client = lnd_grpc.Client()
# Generate ECDSA keys for sender and receiver
sk = SigningKey.generate() # Sender's private key
vk = sk.get_verifying_key() # Sender's public key
# Step 1: Initiate Lightning Network Payment
payment_request = lnd_client.add_invoice(value=10)
pending_payment = lnd_client.send_payment(payment_request)
# Step 2: Generate Cryptographic Signature
email_content = "Hello, this is a test email."
signature = sk.sign(email_content.encode())
# Step 3: Send Email with Signature
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login("sender@example.com", "password")
msg = MIMEText(email_content)
msg['Subject'] = 'Test Email'
msg['From'] = 'sender@example.com'
msg['To'] = 'receiver@example.com'
msg['X-Crypto-Signature'] = signature.hex()
server.send_message(msg)
server.quit()
# Receiver's Email Verification
mail = imaplib.IMAP4_SSL('imap.example.com')
mail.login('receiver@example.com', 'password')
mail.select('inbox')
status, messages = mail.search(None, 'ALL')
email_ids = messages[0].split()
latest_email_id = email_ids[-1]
status, msg_data = mail.fetch(latest_email_id, '(RFC822)')
raw_email = msg_data[0][1]
received_email = email.message_from_bytes(raw_email)
received_signature = received_email['X-Crypto-Signature']
received_content = received_email.get_payload()
# Verify the signature
if vk.verify(bytes.fromhex(received_signature), received_content.encode()):
print("Email is verified.")
lnd_client.settle_payment(pending_payment)
else:
print("Email verification failed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment