用Liquid网络学习比特币闪电支付

本教程介绍如何使用Liquid侧链的c-lightning来创建闪电支付通道。使用这些 支付通道,用户可以实时、私有地交易Liquid-BTC。

Liquid网络包含有对私密交易和资产发行的支持,因此用户可以使用闪电支付通道 交易任何在Liquid网络上发行的资产,例如通证化的发币、加密资产等等。另外, 对链上BTC和支付通道L-BTC的兑换的支持工作也在进行中。

使用Liquid网络上的c-lightning和比特币主网上流程一样,因此如果你熟悉哪些 步骤的话,这个教程就会很容易 —— 虽然本来就是件容易的事。

1、软件安装

首先我们需要一个同步的比特币节点和一个同步的Elements节点,才能在Liquid 侧链上开启闪电支付通道。你可以参考以下链接安装bitcoin节点软件和elements 节点软件:

  • bitcoin core:https://elementsproject.org/elements-code-tutorial/installing-bitcoin
  • elements:https://elementsproject.org/elements-code-tutorial/installing-elements

一旦两个节点都完成同步,安装了必须的依赖:

1
2
3
~$ sudo apt-get install -y \
autoconf automake build-essential git libtool libgmp-dev \
libsqlite3-dev python python3 net-tools zlib1g-dev libsodium-dev

然后从Christian Decker的github仓库拉取lightening-elements分支:

1
2
3
4
~$ git clone https://github.com/cdecker/lightning
~$ cd lightning
~/lightning$ git fetch --all
~/lightning$ git checkout lightning-elements

现在使用源代码构建:

1
2
~/lightning$ ./configure
~/lightning$ make

构建完成后,就可以得到lightingd/lightningdcli/lightning-cli

如果希望快速掌握区块链应用的开发,推荐汇智网的 区块链应用开发系列教程, 内容涵盖比特币、以太坊、eos、超级账本fabric和tendermint等多种区块链, 以及 java、go、nodejs、python、php、dart等多种开发语言

2、启动闪电服务进程

lightningd提供了一个--network参数来选择要在哪个链上运行,例如 比特币主链、测试链、私有开发链等。lightning-elements分支也支持liquid 侧链,因此我们使用这个选项:

1
~/lightning$ ./lightningd/lightningd --network liquid ... <other arguments>

它会开始和Liquid侧链同步:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
~/lighting$ ./lightningd/lightningd --network liquid                                                                           
2019-06-29T23:14:40.599Z lightning_gossipd(29574): We seem to be missing gossip messages
2019-06-29T23:14:40.689Z lightningd(29562): --------------------------------------------------
2019-06-29T23:14:40.689Z lightningd(29562): Server started with public key 02954a618a5684fbb7454d0c1bc87579d5164c35829fccc54d9f4df1a369356b48, alias $
OUDFIRE (color #02954a) and lightningd v0.6rc1-1998-g6d4c40f
2019-06-29T23:22:46.297Z lightningd(29562): JSON-RPC shutdown
grubles@thinky:~/builds/lightning$ ./lightningd/lightningd --network liquid --log-level debug
2019-06-29T23:22:55.640Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_channeld
2019-06-29T23:22:55.641Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_closingd
2019-06-29T23:22:55.642Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_connectd
2019-06-29T23:22:55.643Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_gossipd
2019-06-29T23:22:55.645Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_hsmd
2019-06-29T23:22:55.646Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_onchaind
2019-06-29T23:22:55.647Z lightningd(29963): testing /home/grubles/builds/lightning/lightningd/lightning_openingd
2019-06-29T23:22:55.677Z lightning_hsmd(29973): pid 29973, msgfd 17
2019-06-29T23:22:55.687Z lightning_connectd(29974): pid 29974, msgfd 20
2019-06-29T23:22:55.688Z lightning_hsmd(29973): Client: Received message 11 from client
2019-06-29T23:22:55.688Z lightning_hsmd(29973): Client: Received message 9 from client
2019-06-29T23:22:55.688Z lightning_hsmd(29973): new_client: 0
2019-06-29T23:22:55.806Z lightning_connectd(29974): Broken DNS resolver detected, will check for dummy replies
2019-06-29T23:22:55.806Z lightning_connectd(29974): Created IPv6 listener on port 9735
2019-06-29T23:22:55.806Z lightning_connectd(29974): Failed to connect 10 socket: Network is unreachable
2019-06-29T23:22:55.806Z lightning_connectd(29974): Created IPv4 listener on port 9735
2019-06-29T23:22:55.806Z lightning_connectd(29974): REPLY WIRE_CONNECTCTL_INIT_REPLY with 0 fds
2019-06-29T23:22:55.807Z lightning_gossipd(29975): pid 29975, msgfd 19
2019-06-29T23:22:55.822Z lightning_hsmd(29973): Client: Received message 9 from client
2019-06-29T23:22:55.822Z lightning_hsmd(29973): new_client: 0
2019-06-29T23:22:55.822Z lightning_gossipd(29975): gossip_store_compact_offline: 0 deleted, 0 copied
2019-06-29T23:22:55.822Z lightning_gossipd(29975): total store load time: 0 msec
2019-06-29T23:22:55.822Z lightning_gossipd(29975): gossip_store: Read 0/0/0/0 cannounce/cupdate/nannounce/cdelete from store (0 deleted) in 1 bytes
2019-06-29T23:22:55.823Z lightning_gossipd(29975): We seem to be missing gossip messages
2019-06-29T23:22:55.859Z lightningd(29963): Adding block 367714: 8173bffd993028ee63fd46adc808d9820f752c550140028f2fd7ca67b654cdcc
2019-06-29T23:22:55.893Z lightningd(29963): Smoothed feerate estimate for urgent initialized to polled estimate 250
2019-06-29T23:22:55.893Z lightningd(29963): ... feerate estimate for urgent hit floor 253
2019-06-29T23:22:55.893Z lightningd(29963): Feerate estimate for urgent set to 253 (was 0)
2019-06-29T23:22:55.893Z lightningd(29963): Smoothed feerate estimate for normal initialized to polled estimate 250
2019-06-29T23:22:55.893Z lightningd(29963): ... feerate estimate for normal hit floor 253
2019-06-29T23:22:55.893Z lightningd(29963): Feerate estimate for normal set to 253 (was 0)
2019-06-29T23:22:55.893Z lightningd(29963): Smoothed feerate estimate for slow initialized to polled estimate 250
2019-06-29T23:22:55.893Z lightningd(29963): ... feerate estimate for slow hit floor 253
2019-06-29T23:22:55.893Z lightningd(29963): Feerate estimate for slow set to 253 (was 0)
2019-06-29T23:22:55.894Z lightningd(29963): Loaded 0 channels from DB
2019-06-29T23:22:55.894Z jsonrpc Listening on 'lightning-rpc'
2019-06-29T23:22:55.894Z lightning_connectd(29974): REPLY WIRE_CONNECTCTL_ACTIVATE_REPLY with 0 fds
2019-06-29T23:22:55.894Z lightningd(29963): --------------------------------------------------
2019-06-29T23:22:55.894Z lightningd(29963): Server started with public key 02954a618a5684fbb7454d0c1bc87579d5164c35829fccc54d9f4df1a369356b48, alias LOUDFIRE (color #02954a) and lightningd v0.6rc1-1998-g6d4c40f
2019-06-29T23:22:55.895Z plugin-autoclean autocleaning not active
2019-06-29T23:22:55.907Z lightningd(29963): Adding block 367715: 9efb9d3cc313bd403225a8a461603157c74b7c8878169c5140cafa808f3dd71f
2019-06-29T23:22:55.937Z lightningd(29963): Adding block 367716: 6b2df8e54a58a933d7d5e19e76428e06a6447db8254d875a966536b6e7eadbda
2019-06-29T23:22:55.980Z lightningd(29963): Adding block 367717: 2fed71e138f77b8ab6e3f286f308be829fb436154d83aa83a58275ac3745735f
2019-06-29T23:22:56.003Z lightningd(29963): Adding block 367718: aaf417b0e696d737b3e49ab24c0d1562f930be5a44e84136ef9b60a49ce70f65
2019-06-29T23:22:56.040Z lightningd(29963): Adding block 367719: 2563e72d0e7cade03e4005bba463baf80f06a310c373453bb1b22a1395ef46a3
2019-06-29T23:22:56.066Z lightningd(29963): Adding block 367720: dd33dd008c96fdb8f44018fe1d361fa84d7a444f41b1b522c69a9a21e0ce5353
2019-06-29T23:22:56.102Z lightningd(29963): Adding block 367721: 96b0fb6c2ffce86fad91fd001130c37674778312a9f13cccbbffecfd75d21d3a
2019-06-29T23:22:56.132Z lightningd(29963): Adding block 367722: 4fcf8c4b922eeaa604a6c86d3fbfcfd2f6380fad02b0c3150af524d7008fcbcd
2019-06-29T23:22:56.163Z lightningd(29963): Adding block 367723: ac7f818271dd1c7e4dd6bc0e7a4a578edb3ec18cf686cfe7390ab432f4d7cdc5
2019-06-29T23:22:56.199Z lightningd(29963): Adding block 367724: d1b6790da7dca153dd247efeaf11f0592df469cc588b92f07f954521a66cefb6
2019-06-29T23:22:56.235Z lightningd(29963): Adding block 367725: f0a4824ee24e0f327571bdc0acb238162a001be00f12d203e9f15fffba9de20b
2019-06-29T23:22:56.263Z lightningd(29963): Adding block 367726: c6e9b5088c32313272a2235b6865e416f8130a51b5c8fcae5329f80dc6ed1b88
2019-06-29T23:22:56.305Z lightningd(29963): Adding block 367727: 3564d2781de473b6c5cb83cfb819f77523cc4e3a53b3dd19244eeb9d5498aa1a
2019-06-29T23:22:56.336Z lightningd(29963): Adding block 367728: a3b0362a5b6dfc9060f36baa1eb801281eb29438fa0c5287bcba043c6c6a9e02
2019-06-29T23:22:56.398Z lightningd(29963): Adding block 367729: 7b741e9d21e6da7c10807734b8380ea133fafefe10d8e4ba5933ad3c15331317

3、开启闪电支付通道

现在我们可以开始充值一个地址以便开启闪电通道。只需要使用lightning-cli来 生成一个p2sh-segwit地址,然后向它发送一些L-BTC。

要获取Liquid-BTC(L-BTC),要么从某个Liquid成员交易所购买,例如Bitfinex、 The Rock Trading或者shapeshift.ai,或者按以下指南进行手工锚定。

首先生成一个p2sh-segwit地址:

1
2
3
4
5
~/lightning$ ./cli/lightning-cli newaddr p2sh-segwit                                                                                                                                                                                                                                    
{
"address" : "Gqkico7M9JGyQmk2TjFgqXwYMrrT2ExwXL",
"p2sh-segwit" : "Gqkico7M9JGyQmk2TjFgqXwYMrrT2ExwXL"
}

然后向这个地址转一些L-BTC,然后连接到Liquid LIghting对等节点。连接命令格式 如下:

1
connect public_key@ip_address:port

在本教程中,我们使用一个局域网上的对等节点:

1
2
3
4
~/lightning$ ./cli/lightning-cli connect 0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead@192.168.1.230
{
"id" : "0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead"
}

可以使用listpeers命令查看连接的对等节点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
~/lightning$ ./cli/lightning-cli listpeers
{
"peers" : [
{
"id" : "0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead",
"connected" : true,
"netaddr" : [
"192.168.1.230:9735"
],
"globalfeatures" : "",
"localfeatures" : "aa",
"channels" : []
}
]
}

L-BTC交易应该已经有确认了,因为在Liquid侧脸上的结算时间很快(1~2分钟):

1
2
3
4
5
6
7
8
9
10
11
12
13
~/lightnint$ ./cli/lightning-cli listfunds
{
"outputs" : [
{
"txid" : "1ca26aa0c5938997b82abd9de25e5207bedb08bf46635ff40b067e2e91d735e4",
"output" : 0,
"value" : 100000,
"amount_msat" : "100000000msat",
"address" : "Gqkico7M9JGyQmk2TjFgqXwYMrrT2ExwXL",
"status" : "confirmed"
}, ],
"channels" : []
}

可以看到,value值为100000,对应0.00100000 L-BTC。

现在我们开启通道。命令格式如下:

1
fundchannel <peer id> <amount in satoshis>

amount参数可以是all,以便使用钱包中所有可用的L-BTC:

1
~/lighting$ ./cli/lightning-cli fundchannel 0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead 100000

命令执行后,将会输出16进制表示的交易字符串、交易ID和通道ID:

1
2
3
4
5
{
"tx" : "020000000102167ff7b6501b78069db92d0dc1abc56bd12e3e12a049a7e668ea0023ed68db9f00000000171600144c36e234a654d79b3c052a98c087ca51a69077a9ffffffffe435d7912e7e060bf45f6346bf08dbbe07525ee29dbd2ab8978993c5a06aa21c00000000171600144c36e234a654d79b3c052a98c087ca51a69077a9ffffffff03016d521c38ec1ea15734ae22b7c46064412829c0d0579f0a713d1c04ede979026f0100000000000186a000220020f6c2e31f79596b12c103fa6b6d42dbe422e42bf3ca208247301ebb6a34f16f2e016d521c38ec1ea15734ae22b7c46064412829c0d0579f0a713d1c04ede979026f010000000000957e5d00160014aefd2d126fc8f9bbbeea3aad39dab5564c7b92ae016d521c38ec1ea15734ae22b7c46064412829c0d0579f0a713d1c04ede979026f010000000000000192000000000000000002483045022100fe77d62b82afb07cf77a4a332cec1e5bce9f9419ca17c7c7385515790c9c4e1102202ac8d4f74f9c4d40fb51515ea12603e3bea1d3fb5749beab475a3de9bc861b7e0121022b46e22dea88d566d894d98e406b1a24b8b985821457875dd4fc909dd2522bfc0000000247304402203f43266df0423aedc1a4c78999f470e34fd698ab993787644f76885c9c372ed802204665f7ff9644ff7f9b7c3a13ec16bf1ed96120a1d348aaa7a96fc1df49c9d6090121022b46e22dea88d566d894d98e406b1a24b8b985821457875dd4fc909dd2522bfc00000000000000",
"txid" : "e6dda930c3fca7102bae1222928d022b3cf8664667a1b3b33680955ad2c0cf41",
"channel_id" : "41cfc0d25a958036b3b3a1674666f83c2b028d922212ae2b10a7fcc330a9dde6"
}

使用listpeers命令查看的话,可以看到通道的状态为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
~/lightning$ ./cli/lightning-cli listpeers                                                                                      
{
"peers" : [
{ [35/1969]
"id" : "0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead",
"connected" : true,
"netaddr" : [
"192.168.1.230:9735"
],
"globalfeatures" : "",
"localfeatures" : "aa",
"channels" : [
{
"state" : "CHANNELD_AWAITING_LOCKIN",
"scratch_txid" : "1668cfe52945cef47f9310c07292b773f4a1c2c113d0a84fd905f01409931948",
"owner" : "lightning_channeld",
"channel_id" : "41cfc0d25a958036b3b3a1674666f83c2b028d922212ae2b10a7fcc330a9dde6",
"funding_txid" : "e6dda930c3fca7102bae1222928d022b3cf8664667a1b3b33680955ad2c0cf41",
"private" : false,
"funding_allocation_msat" : {
"0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead" : 0,
"0298f02eaad77c7c8c3d64bd5f8604c80593dc454918b28353d1ea814e541111f0" : 100000000
},
"funding_msat" : {
"0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead" : "0msat",
"0298f02eaad77c7c8c3d64bd5f8604c80593dc454918b28353d1ea814e541111f0" : "100000000msat"
},
"msatoshi_to_us" : 100000000,
"to_us_msat" : "100000000msat",
"msatoshi_to_us_min" : 100000000,
"min_to_us_msat" : "100000000msat",
"msatoshi_to_us_max" : 100000000,
"max_to_us_msat" : "100000000msat",
"msatoshi_total" : 100000000,
"total_msat" : "100000000msat",
"dust_limit_satoshis" : 546,
"dust_limit_msat" : "546000msat",

"max_total_htlc_in_msat" : "18446744073709551615msat",
"their_channel_reserve_satoshis" : 1000,
"their_reserve_msat" : "1000000msat",
"our_channel_reserve_satoshis" : 1000,
"our_reserve_msat" : "1000000msat",
"spendable_msatoshi" : 98774000,
"spendable_msat" : "98774000msat",
"htlc_minimum_msat" : 0,
"minimum_htlc_in_msat" : "0msat",
"their_to_self_delay" : 144,
"our_to_self_delay" : 144,
"max_accepted_htlcs" : 483,
"status" : [
"CHANNELD_AWAITING_LOCKIN:Funding needs 2 more confirmations for lockin."
],
"in_payments_offered" : 0,
"in_msatoshi_offered" : 0,
"in_offered_msat" : "0msat",
"in_payments_fulfilled" : 0,
"in_msatoshi_fulfilled" : 0,
"in_fulfilled_msat" : "0msat",
"out_payments_offered" : 0,
"out_msatoshi_offered" : 0,
"out_offered_msat" : "0msat",
"out_payments_fulfilled" : 0,
"out_msatoshi_fulfilled" : 0,
"out_fulfilled_msat" : "0msat",
"htlcs" : []
}
]
}
]
}

在3个Liquid区块后(3分钟),listpeers将显示“CHANNELD_NORMAL:Funding transaction locked”。

现在闪电通道已经开启了!

4、用闪电支付交易L-BTC

现在本地网络已经有了一个连接对等节点的通道,我们可以生成一个闪电发票, 然后发送L-BTC微支付。

生成发票的命令格式如下:

1
invoice msatoshi label description

在下面的示例中,将创建一个100万satoshi的L-BTC的发票,使用标签“test1” 和描述 “testing lightning on liquid” 。发票本身是一长串看起来随机的字符,ln开头:

1
2
3
4
5
6
7
~/lightning$ ./cli/lightning-cli invoice 1 test1 "testing lightning on liquid"
{
"payment_hash" : "1c03f18a5e3f36a7100c6524794cef9ef8873f3fb22ed5ec6311936e794b71cb",
"expires_at" : 1562629445,
"bolt11" : "lnex10p1pw34xk9pp5rsplrzj78um2wyqvv5j8jn80nmugw0elkghdtmrrzxfku72tw89sdpvw3jhxarfdenjqmrfva58gmnfdenjqmmwypkxjut4d9jqxqyjw5qcqp2mvzgw7fk48sn0n3jwuycs9ahqtl4nc7qp8j69g6sfphjyktztcnyqtsynjjykqc30820p5avqaf9af2mcsjctwwxj8avkpjfv2vh0dqq2wyzpa",

}

解码发票

在发送节点上,我们可以使用decodepay来解码bech32编码的闪电发票:

1
2
3
4
5
6
7
8
9
10
11
12
13
~/lightning$ ./cli/lightning-cli decodepay lnex10p1pw34xk9pp5rsplrzj78um2wyqvv5j8jn80nmugw0elkghdtmrrzxfku72tw89sdpvw3jhxarfdenjqmrfva58gmnfdenjqmmwypkxjut4d9jqxqyjw5qcqp2mvzgw7fk48sn0n3jwuycs9ahqtl4nc7qp8j69g6sfphjyktztcnyqtsynjjykqc30820p5avqaf9af2mcsjctwwxj8avkpjfv2vh0dqq2wyzpa
{
"currency" : "ex",
"created_at" : 1562024645,
"expiry" : 604800,
"payee" : "0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead",
"msatoshi" : 1,
"amount_msat" : "1msat",
"description" : "testing lightning on liquid",
"min_final_cltv_expiry" : 10,
"payment_hash" : "1c03f18a5e3f36a7100c6524794cef9ef8873f3fb22ed5ec6311936e794b71cb",
"signature" : "3045022100db04877936a9e137ce3277098817b702ff59e3c009e5a2a350486f2259625e260220402e049ca44b031179d4f0d3ac07525ea55bc42585b9c691facb0649629977b4"
}

支付发票

在发送节点上,使用pay命令来支付 0.00000000001 L-BTC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~/lightnint$ ./cli/lightning-cli pay lnex10p1pw34xk9pp5rsplrzj78um2wyqvv5j8jn80nmugw0elkghdtmrrzxfku72tw89sdpvw3jhxarfdenjqmrfva58gmnfdenjqmmwypkxjut4d9jqxqyjw5qcqp2mvzgw7fk48sn0n3jwuycs9ahqtl4nc7qp8j69g6sfphjyktztcnyqtsynjjykqc30820p5avqaf9af2mcsjctwwxj8avkpjfv2vh0dqq2wyzpa
{
"id" : 1,
"payment_hash" : "1c03f18a5e3f36a7100c6524794cef9ef8873f3fb22ed5ec6311936e794b71cb",
"destination" : "0322d8d3fb7e77a88f373fa369a3e91cb63a24f1313c480ae09c5ffc56dbef7ead",
"msatoshi" : 1,
"amount_msat" : "1msat",
"msatoshi_sent" : 1,
"amount_sent_msat" : "1msat",
"created_at" : 1562024759,
"status" : "complete",
"payment_preimage" : "7c6357460a3782ed806c8661b0b20c65287ecdc9cf7c6a9c2e85399ffff88f15",
"bolt11" : "lnex10p1pw34xk9pp5rsplrzj78um2wyqvv5j8jn80nmugw0elkghdtmrrzxfku72tw89sdpvw3jhxarfdenjqmrfva58gmnfdenjqmmwypkxjut4d9jqxqyjw5qcqp2mvzgw7fk48sn0n3jwuycs9ahqtl4nc7qp8j69g6sfphjyktztcnyqtsynjjykqc30820p5avqaf9af2mcsjctwwxj8avkpjfv2vh0dqq2wyzpa"
}

“status” 的值为”complete”,表示支付已经完成,发票已经支付!

关闭支付通道

可以在两个节点间随意发送一些L-BTC。在昨晚测试之后,可以按如下步骤关闭通道。

在其中一个节点上,使用close命令发起一个通道关闭:

1
~/lightnint$ ./cli/lightning-cli close <channel id>

上面的命令将会在链上提交关闭交易,通道将被关闭。


原文链接: Getting Started with Lightning on the Liquid Network

汇智网翻译整理,转载请标明出处