跳至主要內容
進階

Coin Control

深入了解 Bitcoin Core 的 Coin Control 功能,手動選擇交易輸入以優化隱私和手續費。

10 分鐘

什麼是 Coin Control?

Coin Control 允許用戶手動選擇哪些 UTXO 作為交易輸入,而不是讓錢包自動選擇。 這對於優化隱私、管理手續費和整合零散 UTXO 非常有用。

為什麼使用 Coin Control?

隱私保護

  • • 避免混合不同來源的幣
  • • 防止關聯不同地址
  • • 控制找零地址

費用優化

  • • 選擇合適大小的 UTXO
  • • 整合小額 UTXO
  • • 避免不必要的找零

列出可用 UTXO

# 列出所有未花費輸出
bitcoin-cli listunspent

# 輸出示例
[
  {
    "txid": "abc123...",
    "vout": 0,
    "address": "bc1q...",
    "label": "savings",
    "scriptPubKey": "0014...",
    "amount": 0.5,
    "confirmations": 1000,
    "spendable": true,
    "solvable": true,
    "desc": "wpkh([d34db33f/84'/0'/0'/0/5]02...)#...",
    "safe": true
  },
  ...
]

# 過濾特定確認數
bitcoin-cli listunspent 6 9999999

# 過濾特定地址
bitcoin-cli listunspent 1 9999999 '["bc1q..."]'

# 查看詳細資訊
bitcoin-cli listunspent | jq '.[] | {txid, vout, amount, confirmations, label}'

手動選擇輸入

# 方法 1: 使用 createrawtransaction + fundrawtransaction

# 創建原始交易(指定輸入)
bitcoin-cli createrawtransaction '[
  {"txid": "abc123...", "vout": 0},
  {"txid": "def456...", "vout": 1}
]' '[
  {"bc1qrecipient...": 0.8},
  {"bc1qchange...": 0.19}
]'

# 方法 2: 使用 send 命令(更簡單)
bitcoin-cli send '[{"bc1q...": 0.5}]' null "unset" null '{
  "inputs": [
    {"txid": "abc123...", "vout": 0}
  ]
}'

# 方法 3: 使用 walletcreatefundedpsbt
bitcoin-cli walletcreatefundedpsbt '[
  {"txid": "abc123...", "vout": 0}
]' '[
  {"bc1q...": 0.5}
]' 0 '{
  "add_inputs": false
}'

Send 命令選項

# send 命令的完整選項
bitcoin-cli send \
  '[{"bc1qrecipient...": 0.5}]' \
  6 \                    # conf_target(確認目標區塊數)
  "economical" \         # estimate_mode
  null \                 # fee_rate(或指定具體費率)
  '{
    "inputs": [
      {"txid": "abc123...", "vout": 0, "sequence": 4294967293}
    ],
    "add_inputs": false,           # 不自動添加輸入
    "include_watching": false,     # 不包含 watch-only
    "change_address": "bc1q...",   # 指定找零地址
    "change_position": 1,          # 找零輸出位置
    "fee_rate": 10,                # sat/vB
    "include_unsafe": false,       # 不包含未確認輸入
    "subtract_fee_from_outputs": [0], # 從輸出扣除手續費
    "replaceable": true            # 啟用 RBF
  }'

隱私考慮

Coin Control 隱私策略:

1. 避免混合不同來源
   ┌─────────────────────────────────────────────────────┐
   │ 不推薦:                                            │
   │ ├── 輸入 1: 交易所提款 (KYC)                        │
   │ ├── 輸入 2: Coinjoin 輸出 (匿名)                   │
   │ └── 結果: 匿名性被破壞                             │
   ├─────────────────────────────────────────────────────┤
   │ 推薦:                                              │
   │ ├── 只使用相同來源的 UTXO                          │
   │ └── 保持資金池隔離                                 │
   └─────────────────────────────────────────────────────┘

2. 找零地址管理
   - 使用新地址接收找零
   - 避免找零回到已公開的地址
   - 考慮使用 "subtract_fee_from_outputs" 消除找零

3. UTXO 整合時機
   - 在低手續費時期整合
   - 使用 Coinjoin 整合以增強隱私
   - 避免在交易時整合無關的 UTXO

隱私提示: 使用同一交易中的多個輸入會將這些地址關聯起來,這是鏈分析的基礎。

UTXO 整合

# 整合小額 UTXO

# 1. 找出小額 UTXO
bitcoin-cli listunspent | jq '[.[] | select(.amount < 0.001)] | length'

# 2. 創建整合交易(所有輸入到一個地址)
INPUTS=$(bitcoin-cli listunspent | jq '[.[] | select(.amount < 0.001) | {txid, vout}]')
TOTAL=$(bitcoin-cli listunspent | jq '[.[] | select(.amount < 0.001) | .amount] | add')

# 3. 使用 send 整合
bitcoin-cli send "[{\"$(bitcoin-cli getnewaddress)\": $TOTAL}]" null "unset" null "{
  \"inputs\": $INPUTS,
  \"subtract_fee_from_outputs\": [0]
}"

# 或使用 sendall(v24.0+)
bitcoin-cli sendall '["bc1qconsolidation..."]' null "unset" null '{
  "inputs": [...],
  "send_max": true
}'

標籤管理

# 為地址設置標籤
bitcoin-cli setlabel "bc1q..." "exchange-deposit"

# 查看特定標籤的 UTXO
bitcoin-cli listunspent | jq '.[] | select(.label == "exchange-deposit")'

# 列出所有標籤
bitcoin-cli listlabels

# 按標籤選擇 UTXO 創建交易
INPUTS=$(bitcoin-cli listunspent | jq '[.[] | select(.label == "savings") | {txid, vout}]')
bitcoin-cli send '[{"bc1q...": 1.0}]' null "unset" null "{\"inputs\": $INPUTS}"

避免地址重用

# 啟用避免重用功能
bitcoin-cli createwallet "privacy-wallet" false false "" false false true
#                                                              ^^ avoid_reuse

# 或對現有錢包
bitcoin-cli setwalletflag "avoid_reuse" true

# 查看錢包標誌
bitcoin-cli getwalletinfo | jq '.avoid_reuse'

# 效果:
# - 已使用過的地址會被標記為 "dirty"
# - 這些地址的 UTXO 不會被自動選中
# - 需要手動使用 Coin Control 才能花費

完整範例

# 場景:從特定來源發送精確金額,無找零

# 1. 列出符合條件的 UTXO
bitcoin-cli listunspent | jq '.[] | select(.label == "salary" and .amount >= 0.5)'

# 2. 選擇最接近目標金額的 UTXO
# 假設找到一個 0.502 BTC 的 UTXO

# 3. 計算手續費(假設 141 vB,10 sat/vB)
# 手續費 = 141 * 10 = 1410 satoshis = 0.0000141 BTC

# 4. 發送金額 + 手續費 ≈ UTXO 金額
# 0.5 + 0.002 = 0.502 BTC(剛好用完,無找零)

# 5. 創建交易
bitcoin-cli send '[{"bc1qrecipient...": 0.50198590}]' null "unset" null '{
  "inputs": [{"txid": "abc123...", "vout": 0}],
  "add_inputs": false,
  "subtract_fee_from_outputs": [0]
}'

# 或使用 PSBT 進行更精細控制
bitcoin-cli walletcreatefundedpsbt '[
  {"txid": "abc123...", "vout": 0}
]' '[
  {"bc1qrecipient...": 0.502}
]' 0 '{
  "add_inputs": false,
  "subtractFeeFromOutputs": [0],
  "fee_rate": 10
}'

總結

  • 手動選擇:精確控制交易輸入
  • 隱私增強:避免不必要的地址關聯
  • 費用優化:選擇合適大小的 UTXO
  • 注意:需要理解 UTXO 模型和隱私影響
已複製連結
已複製到剪貼簿