Send Messages With Keysend
Learn how to send messages to anyone in the Lightning Network from the command line
Last updated
Was this helpful?
Was this helpful?
def send_keysend(
amt: int,
dest_pubkey: str = "",
keysend_message: str = "sample message",
)
my_node = "https://127.0.0.1:8080/"
headers, cert = get_lnd_headers_cert(admin=True, node=node)
if not dest_pubkey:
dest_pubkey = destination_public_key
# Base 64 encoded destination bytes
dest = b64_hex_transform(dest_pubkey)
# We generate a random 32 byte Hex pre_image here.
pre_image = token_hex(32)
# This is the hash of the pre-image
payment_hash = sha256(bytes.fromhex(pre_image))
# The record 5482373484 is special: it carries the pre_image to the destination so it can be compared with the hash we pass via the payment_hash
dest_custom_records = {
5482373484: b64_hex_transform(pre_image),
34349334: b64_transform(keysend_message),
}
url = f"{node}v1/channels/transactions"
data = {
"dest": dest,
"amt": amt,
"payment_hash": b64_hex_transform(payment_hash.hexdigest()),
"dest_custom_records": dest_custom_records,
}
response = httpx.post(
url=url, headers=headers, data=json.dumps(data), verify=cert
)