lndのnodeを建てる
環境
AWS EC2インスタンス。type: t2large、OS: ubuntu18.04 LTS
参考
Go langのインストール
公式にしたがって、Go ver 1.11以降のものを入れる。apt だと1.10までのパッケージしか提供されていなかったので、今回はsnapを使って、1.11.5をインストールした。
code: install go
$ sudo snap install --stable --classic go
goへのPATHを.bashrcに追加して環境変数をリロードする
code: add go path
echo 'export GOPATH=~/gocode' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc
source ~/.bashrc
goのバージョンを確認
code: confirm go version
$ go version
go version go1.11.5 linux/amd64
lndのインストール
go modules使ってるよとか書いてるけど、自分でexport GO111MODULE=on とかしなくてもmakeの中で自動的にやってくれるので、特に気にせずmakeとmake installを叩いてOK
code: install lnd
$ go get -d github.com/lightningnetwork/lnd
$ cd $GOPATH/src/github.com/lightningnetwork/lnd
$ make && make install
インストールできたかの確認もかねて、lndのバージョンをチェック
code: lnd version check
$ lnd --version
lnd version 0.5.1-beta commit=v0.5.1-beta-519-ge9889cb8995f678bb6418292d318395cdb17795d
今回はbitcoinのnodeとしてbitcoindを使うので、bitcoin.confにZeroMQに関する設定を追加する。
code: bitcoin.conf
testnet=3 # testnetを利用する
txindex=1 # indexを作成して全てのトランザクションIDを参照可能にする
server=1 # JSON RPCサーバとしてコマンドを受け付ける
rpcuser="rpcuser" # JSON RPCのためのユーザ名
rpcpassword="rpcpassword" # JSON RPCのためのパスワード
rpcport=18332 # JSON RPC用ポート番号
zmqpubrawblock=tcp://127.0.0.1:28332 # ←これと
zmqpubrawtx=tcp://127.0.0.1:28333 # ←これを追加した。
bitcoindの起動
sreen上で起動する。
code: launch bitcoind
$ screen -S bitcoin
-- screen bitcoin上 --
$ bitcoind -testnet -debug
CTL+A CTL+D
lndの起動
こっちも別のscreen上で起動
code: launch lnd
$ screen -S lnd
-- screen lnd上 --
$ lnd --bitcoin.active --bitcoin.testnet --bitcoin.node=bitcoind
CTL+A CTL+D
lndのwalletを作成する
下のコマンドを発行したあと、パスワードの設定やmnimonicのインポートなどの確認が行われるので適宜設定してください。また、パスワードは8文字以上じゃないと最後に弾かれるのでご注意。すでにwalletを作っている人はこの処理はスキップ。
ここではbitcoinのtestnetに繋げてるので、lncliのオプションとして -n testnetを指定しています。
code: create wallet
$ lncli -n testnet create
lndのwalletをunlockする
すでにwalletを作成済み(lncli createを発行済み。外部にwalletを持ってる場合は、createは必要)の人はunlockコマンドを使ってwalletをunlockすることでlightning nodeの機能が使えるようになります。この時にcreateで設定下パスワードの入力が求められます。
ここではbitcoinのtestnetに繋げてるので、lncliのオプションとして -n testnetを指定しています。
code: wallet unlock
$ lncli -n testnet unlock
lndで送金する
lndに資金を入れる
最初にアドレスを生成
code: newaddress
$ lncli -n testnet newaddress p2wkh
{
"address": "tb1qm2ccyrhud755x4mx9nte4rqragln832dxjmmw0"
}
sendtoaddressする
code: sendto
$ bitcoin-cli sendtoaddress tb1qm2ccyrhud755x4mx9nte4rqragln832dxjmmw0 0.00100200
69501398683e3346f84d4cebd93eb50c0b5dac8a3f36fc8c07bd5c9f20c5ba25
送金できたか確認。listunspentとwalletbalanceどっちでも確認できるぽい。
listunspentで確認してみる。引数はconfirmation数。まだunconfirmのものも見たいので0を指定しています。
code: confirm sendto
$ lncli -n testnet listunspent 0
{
"utxos": [
{
"address_type": "WITNESS_PUBKEY_HASH",
"address": "tb1qm2ccyrhud755x4mx9nte4rqragln832dxjmmw0",
"amount_sat": "100200",
"script_pubkey": "0014dab1820efc6fa94357662cd79a8c03ea3f33c54d",
"outpoint": {
"funding_txid_str": "69501398683e3346f84d4cebd93eb50c0b5dac8a3f36fc8c07bd5c9f20c5ba25",
"output_index": 0
},
"confirmations": "0"
}
]
}
walletbalanceでも確認できる。confirmed_balanceに値が入れば、txがconfirm済みです。
code: walletbalance
$ lncli -n testnet walletbalance
{
"total_balance": "100200",
"confirmed_balance": "0",
"unconfirmed_balance": "100200"
}
他のnodeにつなげる
とりまnode infoを確認する。
code: getinfo
$ lncli -n testnet getinfo
{
"version": "0.5.1-beta commit=v0.5.1-beta-519-ge9889cb8995f678bb6418292d318395cdb17795d",
"identity_pubkey": "03ac992ca2e6bd89dfbb892d25f686864f34e585fc4dc7deb864750534e02afaa3",
"alias": "03ac992ca2e6bd89dfbb",
"num_pending_channels": 0,
"num_active_channels": 0,
"num_inactive_channels": 0,
"num_peers": 3,
"block_height": 1478809,
"block_hash": "0000000000000b7269554b6d4c0219656dfa28751dbf4f97af43efd12c0fcf50",
"best_header_timestamp": 1550624838,
"synced_to_chain": true,
"testnet": true,
"chains": [
{
"chain": "bitcoin",
"network": "testnet"
}
],
"uris": null
}
<pubkey>@<ip>なフォーマットで指定して接続する。
code: connect
$ lncli -n testnet connect 03d5ae70d4e2146c8422f8a40ff03c535b0392aa225e70170edadbcd9513db413c@52.193.224.128
{
}
channelを開く
openchannel <pubkey> <dep_satoshi> <push_satoshi>でchannelをopenする。
code: open channel
$ lncli -n testnet openchannel 03d5ae70d4e2146c8422f8a40ff03c535b0392aa225e70170edadbcd9513db413c 100000 0
{
"funding_txid": "62ff2c9538d9a2b39e6421ab3fe4ffed2e1eed8be3d29873e1e7436ff41b07fe"
}
channelの確認。タイムラグがあるっぽい
code: channel list
$ lncli -n testnet listchannels
{
"channels": [
{
"active": true,
"remote_pubkey": "03d5ae70d4e2146c8422f8a40ff03c535b0392aa225e70170edadbcd9513db413c",
"channel_point": "62ff2c9538d9a2b39e6421ab3fe4ffed2e1eed8be3d29873e1e7436ff41b07fe:0",
"chan_id": "1625987481969491968",
"capacity": "100000",
"local_balance": "91848",
"remote_balance": "0",
"commit_fee": "8152",
"commit_weight": "600",
"fee_per_kw": "11261",
"unsettled_balance": "0",
"total_satoshis_sent": "0",
"total_satoshis_received": "0",
"num_updates": "0",
"pending_htlcs": [
],
"csv_delay": 144,
"private": false,
"initiator": true
}
]
}
送金する
invoiceを作る。 addinvoice <pay_satoshi> で作れるぽい。他のclientと違って、lndではsatoshi単位で指定する。
code: invoice
$ lncli -n testnet addinvoice 30000
{
"r_hash": "452be6940a9fe3163fdfa2e67286efe7b651d0ebbaff520138e03079a1f45806",
"pay_req": "lntb300u1pwxevf7pp5g547d9q2nl33v07l5tn89ph0u7m9r58thtl4yqfcuqc8ng05tqrqdqqcqzys2ee7ck559j7w7ly6vn5458szs8fv7rf0kw9tfzyz73da6lledjjyahudd43ztm6dga9mg0lfgl7nd3urh4xl363shygmz0rd8yxms4cqezrxc4",
"add_index": 1
}
invoiceを支払う
payinvoiceでbolt11 messageを指定すると、送金の確認を求められるので、yesを入力してEnterを押すと送金が行われる。
code: pay
$ lncli -n testnet payinvoice lntb300u1pwxevf7pp5g547d9q2nl33v07l5tn89ph0u7m9r58thtl4yqfcuqc8ng05tqrqdqqcqzys2ee7ck559j7w7ly6vn5458szs8fv7rf0kw9tfzyz73da6lledjjyahudd43ztm6dga9mg0lfgl7nd3urh4xl363shygmz0rd8yxms4cqezrxc4
Description:
Amount (in satoshis): 30000
Destination: 03d5ae70d4e2146c8422f8a40ff03c535b0392aa225e70170edadbcd9513db413c
Confirm payment (yes/no): yes
{
"payment_error": "",
"payment_preimage": "3dc1689e131ce1fd314ddb26abab00148763d47d8e33298dad2e66f62ffcf7ef",
"payment_route": {
"total_time_lock": 1478992,
"total_amt": 30000,
"hops": [
{
"chan_id": 1625987481969491968,
"chan_capacity": 100000,
"amt_to_forward": 30000,
"expiry": 1478992,
"amt_to_forward_msat": 30000000,
"pub_key": "03d5ae70d4e2146c8422f8a40ff03c535b0392aa225e70170edadbcd9513db413c"
}
],
"total_amt_msat": 30000000
}
}
チャンネルを閉じる
closeにはfunding_txidを指定する。この辺も他のclientとは違う。他のものはだいたいchannel_idもしくはnode_idを指定するので。
code: close
$ lncli -n testnet closechannel 62ff2c9538d9a2b39e6421ab3fe4ffed2e1eed8be3d29873e1e7436ff41b07fe
{
"closing_txid": "d75dd21194eef1f4e098c2a0a521e9bc0c78e989c3124125b594d1c4e4267e31"
}
資金の引き出し
lndの管理してる資金を引き出す時はsendcoinsとかを使う。amountはsatoshi単位で指定
code: withdraw
$ lncli -n testnet sendcoins tb1qmmker40hr2nf865n67m0cdpzwgwp85tvk08qaw 129800
{
"txid": "b25615f1960aacf32062dd8c2dd39cfe3d94029d882e969990f8a55e90f6483b"
}
その他
lndではいろんなcommandはそれぞれhelpが用意されてる。とても親切!
lncli -n testnet <command> --help でだいたいhelpが表示される。
code: help
$ lncli -n testnet closechannel --help
NAME:
lncli closechannel - Close an existing channel.
USAGE:
CATEGORY:
Channels
DESCRIPTION:
Close an existing channel. The channel can be closed either cooperatively,
or unilaterally (--force).
A unilateral channel closure means that the latest commitment
transaction will be broadcast to the network. As a result, any settled
funds will be time locked for a few blocks before they can be spent.
In the case of a cooperative closure, One can manually set the fee to
be used for the closing transaction via either the --conf_target or
--sat_per_byte arguments. This will be the starting value used during
fee negotiation. This is optional.
To view which funding_txids/output_indexes can be used for a channel close,
see the channel_point values within the listchannels command output.
The format for a channel_point is 'funding_txid:output_index'.
OPTIONS:
--funding_txid value the txid of the channel's funding transaction
--output_index value the output index for the funding output of the funding transaction (default: 0)
--time_limit value a relative deadline afterwhich the attempt should be abandoned
--force after the time limit has passed, attempt an uncooperative closure
--block block until the channel is closed
--conf_target value (optional) the number of blocks that the transaction *should* confirm in, will be used for fee estimation (default: 0)
--sat_per_byte value (optional) a manual fee expressed in sat/byte that should be used when crafting the transaction (default: 0)