跳至主要內容
進階

Keysend 自發支付

了解 Keysend 如何實現無需發票的閃電網路支付,讓發送方可以主動向任何節點發送資金。

10 分鐘

什麼是 Keysend?

Keysend(也稱為 Spontaneous Payments 或 Sphinx Send)允許發送方向任何節點 發送支付,而無需接收方提供發票。發送方自己生成 preimage 並通過加密的 洋蔥包傳遞給接收方,接收方解密後即可結算支付。

使用場景: Keysend 非常適合打賞、捐贈、串流支付(streaming payments)等場景, 讓發送方可以主動付款而不需要接收方預先生成發票。

傳統發票 vs Keysend

Traditional Invoice Flow:

1. Bob generates preimage (32 bytes random)
2. Bob calculates payment_hash = SHA256(preimage)
3. Bob creates invoice containing payment_hash
4. Bob sends invoice to Alice
5. Alice pays invoice (HTLC locked to payment_hash)
6. Bob reveals preimage to settle

Problems:
• Requires bidirectional communication
• Bob must be online to generate invoice
• New invoice needed for each payment

--------------------------------------------

Keysend Flow:

1. Alice generates preimage (32 bytes random)
2. Alice calculates payment_hash = SHA256(preimage)
3. Alice encrypts preimage in onion packet
4. Alice sends HTLC (locked to payment_hash)
5. Bob decrypts onion packet, gets preimage
6. Bob uses preimage to settle

Advantages:
• Unidirectional communication
• No prior interaction needed
• Can proactively pay

技術實現

Keysend Onion Payload:

Uses TLV (Type-Length-Value) extension:

Final hop payload:
  amt_to_forward (type 2): payment amount
  outgoing_cltv_value (type 4): CLTV timelock
  keysend_preimage (type 5482373484): preimage
    32 bytes: sender-generated preimage

TLV Type 5482373484 origin:
• Non-standard experimental type
• Odd type = optional (receiver can ignore if unknown)
• Has become de facto standard

Receiver processing:
1. Decrypt onion payload
2. Find keysend_preimage TLV
3. Verify SHA256(preimage) == payment_hash
4. If matches, use preimage to settle HTLC

自定義數據

Keysend 還可以攜帶自定義數據,實現更豐富的功能:

Custom TLV Records:

Commonly used custom types:
  Type 34349334: text message
  Type 34349337: sender pubkey (for replies)
  Type 34349339: timestamp
  Type 34349343: content type
  Type 7629169: podcast info

Application example - Podcasting 2.0 value transfer:
{
  "keysend_preimage": "...",
  "7629169": {
    "podcast": "Bitcoin Audible",
    "episode": "Read 123",
    "ts": 1234,           // playback position (seconds)
    "action": "boost",
    "value_msat": 5000000,
    "sender_name": "Alice"
  }
}

This lets listeners tip creators directly while listening!

使用場景

Podcasting 2.0

聽眾可以在收聽播客時進行即時打賞(streaming sats)或 boost, 資金直接流向創作者。

社交媒體打賞

用戶可以直接向內容創作者的節點發送打賞,無需創作者在線生成發票。

遊戲內支付

遊戲中的即時交易,玩家之間可以快速轉帳而無需複雜的發票交換。

節點間結算

服務提供商可以主動向合作夥伴節點發送結算款項。

安全考量

付款證明問題

傳統發票由接收方生成 preimage,所以發送方獲得 preimage 就是付款證明。 但 Keysend 的 preimage 是發送方自己生成的,無法證明接收方確實收到了款項。

垃圾支付

任何人都可以向節點發送 Keysend 支付,可能被用於垃圾訊息。 節點可以選擇設定最低金額或完全禁用 Keysend。

緩解措施

  • • 設定最低接受金額
  • • 使用 BOLT 12 Offers 代替純 Keysend
  • • 結合發送方公鑰進行白名單

Keysend + AMP

AMP Keysend (LND):

Combines advantages of AMP and Keysend:

Traditional Keysend:
• Single path
• Limited by single channel liquidity

AMP Keysend:
• Multi-path
• Can send large Keysend payments
• Better privacy (each shard has different hash)

Usage (lncli):
lncli sendpayment \
  --dest <pubkey> \
  --amt 100000 \
  --amp \
  --data 34349334=$(echo -n "Hello!" | xxd -p)

This will:
1. Generate AMP root key
2. Split into multiple shards
3. Each shard carries keysend TLV
4. Receiver settles after collecting all

實現狀態

LND 完整支持

支持發送和接收 Keysend,包括 AMP Keysend。使用 --keysend 或 --amp 標誌。

Core Lightning 完整支持

通過 keysend 插件支持。需要配置 experimental-accept-extra-tlv-types。

Eclair 完整支持

Phoenix 錢包使用 Eclair,支持接收 Keysend 打賞。

BOLT 12 作為替代

BOLT 12 Offers 提供了更優雅的解決方案:

Keysend

  • • 簡單直接
  • • 無付款證明
  • • 可能收到垃圾
  • • 已廣泛部署

BOLT 12 Offers

  • • 有付款證明
  • • 接收方控制
  • • 支持 Route Blinding
  • • 逐步部署中

相關資源

下一步: 了解 Gossip 協議 如何在閃電網路中傳播網路拓撲資訊。

已複製連結
已複製到剪貼簿