比特币闪电网络教程#2:充提及转账

这是比特币闪电网络系列教程第二篇,我们在第一篇中熟悉了网络的搭建过程,下一步我们要真实的使用闪电网络进行充值,提现和转账。

一、充值比特币

1.生成Segwit格式的比特币地址

# newaddr : Get a new address to fund a channel
lightning-cli newaddr
#输出 "address" : "3DzEAHgbbKGxRaeqAryBVaVb7RQACrcWwY"

2.从比特币钱包转账0.001 bitcoin 到这个地址


3.六个确认后查看余额

# listfunds : Show funds available for opening channels
lightning-cli listfunds
#输出 单位是聪, 1个比特币等于1亿聪
{ "outputs" :[{ "txid" : "[HIDDEN]", "output" : 0, "value" : 100000 } ] }

二、连接其他闪电网络节点

可以从https://lnmainnet.gaben.win/ 找到一个你要连接的节点,我选择了 SLEEPYARK,信息如下:

1.建立连接

# connect : Connect to {public key} at {host} (which can end in ':port' if not default
lightning-cli connect 02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432 104.198.32.198 9735

2.查看连接状态

# listpeers : Show current peers, if {level} is set, include {log}s
lightning-cli listpeers
#输出
{ "peers" :[
{ "state" : "GOSSIPING", "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432",
 "netaddr" :[ "104.198.32.198:9735" ], "connected" : true, "owner" : "lightning_gossipd" } ] }

三、建立闪电网络通道

1.设置开启通道的费用 (好像不是很准,和当时的网络情况有关)

# dev-setfees  :  Set feerate in satoshi-per-kw for {immediate}, {normal} and {slow} (each optional) and show the value of those three feerates
lightning-cli dev-setfees 25000 10500

2.开启一个价值5万聪(大约5.42刀)的通道

# fundchannel : Fund channel with {id} using {satoshi} satoshis
lightning-cli fundchannel 02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432 50000
# 输出 "txid" : "f81567649be8c4f21a04c621a1dd439603a603af1f001a09e2c6db6173360ab2"

根据BlockChain显示,手续费为1048聪(大约0.11刀)

3.一个确认后查看通道建立情况

lightning-cli listpeers

{ "peers" :
    [
        { "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "connected" : true, "netaddr" :
            [ "104.198.32.198:9735" ], "channels" :
            [
                { "state" : "CHANNELD_NORMAL", "owner" : "lightning_channeld", "short_channel_id" : "506765:699:1", "funding_txid" : "f81567649be8c4f21a04c621a1dd439603a603af1f001a09e2c6db6173360ab2", "msatoshi_to_us" : 50000000, "msatoshi_total" : 50000000, "dust_limit_satoshis" : 546, "max_htlc_value_in_flight_msat" : 18446744073709551615, "channel_reserve_satoshis" : 0, "htlc_minimum_msat" : 0, "to_self_delay" : 144, "max_accepted_htlcs" : 483 } ] } ] }

state 为 CHANNELD_NORMAL,表示通道建立成功

四、闪电支付

1.获得一个闪电网络地址
我为了测试就临时对https://lnmainnet.gaben.win/ 的作者进行捐款,在网页右下角输入捐款额1万聪,生成一个BOLT11格式地址

2.解析收款地址

# decodepay :  Decode {bolt11}, using {description} if necessary
lightning-cli decodepay lnbc100u1pdxlmt7pp5sn99r4w9upp635qd82rxc2zrxu79nwhzvawug35ldmn4t023y59qdq6f38zq3tcwpkx7un9wgs9g6tswvcqzys47neyp9ef4jewwqslhy0t0etc9wwf30trmxlcts7f4dmwrlf7ssngm0fq5rfzdrdc08slefxdpv9a8dptmml92rrnuvrt6veq8y654sp7ska9q
# 截取输出:
.... "payee" : "035f1498c929d4cefba4701ae36a554691f526ff60b1766badd5a49b3c8b68e1d8" ...

3.查看发送路径

# getroute : Show route to {id} for {msatoshi}, using {riskfactor} and optional {cltv} (default 9) 
lightning-cli getroute 035f1498c929d4cefba4701ae36a554691f526ff60b1766badd5a49b3c8b68e1d8 10000000 0
# 输出
{ "route" :
    [
        { "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "channel" : "506765:699:1", "msatoshi" : 10001110, "delay" : 27 },
        { "id" : "03cbf298b068300be33f06c947b9d3f00a0f0e8089da3233f5db37e81d3a596fe1", "channel" : "506727:325:1", "msatoshi" : 10000010, "delay" : 13 },
        { "id" : "035f1498c929d4cefba4701ae36a554691f526ff60b1766badd5a49b3c8b68e1d8", "channel" : "504481:631:1", "msatoshi" : 10000000, "delay" : 9 } ] }

这证明我们是有通道连接到其收款人节点的

4.付款

# pay  : Send payment specified by {bolt11} with optional {msatoshi} (if and only if {bolt11} does not have amount), {description} (required if {bolt11} uses description hash) and {riskfactor} (default 1.0)
lightning-cli pay lnbc100u1pdxlmt7pp5sn99r4w9upp635qd82rxc2zrxu79nwhzvawug35ldmn4t023y59qdq6f38zq3tcwpkx7un9wgs9g6tswvcqzys47neyp9ef4jewwqslhy0t0etc9wwf30trmxlcts7f4dmwrlf7ssngm0fq5rfzdrdc08slefxdpv9a8dptmml92rrnuvrt6veq8y654sp7ska9q

结果在这里,我遇到问题了: 一直输出 "still in progress",这说明发送不成功..

五、关闭通道,取出余额

# close : Close the channel with peer {id} - id可以通过 lightning-cli listpeers 看到
lightning-cli close 02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432

# withdraw  :  Send to {destination} address {satoshi} (or 'all') amount via Bitcoin transaction
lightning-cli withdraw BITCOIN_ADDRESS all

这种单向通道的关闭,里边的余额要经过144 blocks (一天)后才能发送到原地址

总结

整体走了一遍,可能闪电网络还处于早期的原因,感觉用户体验并不好,程序还是有很多bug,打算再写一篇文章详细谈一谈感受。

(Picture from https://explorer.acinq.co/#/)


闪电网络系列教程:

比特币闪电网络教程#1:主网部署
比特币闪电网络教程#2:充提及转账

H2
H3
H4
3 columns
2 columns
1 column
14 Comments