LND_RPC_Client
Stage 3 - RPC Clientを和訳する。Stage 1 - Setting up a local clusterが前提となっている。
Stage 3 - RPC Client
このセクションでは、チュートリアルアプリの利用を最後として、LNDに接続するためのgRPCクライアントを作成する。
ウェブアプリを作る準備
ニュースサイトを立ち上げよう。始める前に、pipとvirtual envのinstallが必要である。
# Create a new workspace which will hold both the repo and the virtualenv. We
# recommend running this in a new terminal window.
mkdir ln-workspace
cd ln-workspace
# Clone the repo
git clone https://github.com/MaxFangX/lightning-coindesk
# Create virtualenv and activate it. Make sure to activate this environment
# whenever you are working with the coindesk app.
virtualenv deskenv
source deskenv/bin/activate
# Install webapp Python requirements
cd lightning-coindesk
pip install -r requirements.txt
ここまでの手順がおわったら、自分たちのサイトを作るためにpythonでgRPCを立ち上げて見よう。
# Install the dependencies required for gRPC
pip install grpcio grpcio-tools googleapis-common-protos
# Run our webserver
python manage.py runserver
coindesk/settings.py
でLND_RPCHOST = "localhost:10002"
が設定されていることに注意してほしい。これはつまり、BobのLNDノードに接続しているということであって、ここに送金するということはBobのnodeに送金をすることと等しい意味を持つ。
Testing the app
すべてがうまく行っていれば、公開したポートでweb serverを持っているはずだ。
サイトを見ると、文章を見るために支払いが求められることが確認できる。
emailとpasswordでのログインの煩雑性を避けるために、LNDの公開鍵と任意のメッセージにより認証を実行するスキームを用意している。
Aliceの新しいアカウントをログインすることで作成してみよう。作成されたメッセージをコピーして、
alice$ lncli-alice signmessage <GENERATED_MESSAGE>
{
"signature": <SIGNATURE>
}
サインを対応する箇所に貼り付けて、aliceを求められる箇所に入力すると、Aliceとしてのログインが完了する。
読みたい文章に進み、支払いをしてみよう。CLIでもwebでも良い。“Complete” をクリックすることが支払いの完了をwebサーバに知らせ、そこから文章を読めるようになる。
Setting up gRPC
gRPCコマンドの実行を練習してみよう。
# Enter the development environment
cd ln-workspace
# Activate Python virtualenv
source deskenv/bin/activate
# Clone the Google API repository, which is required due to the use of
# google/api/annotations.proto
git clone https://github.com/googleapis/googleapis.git
# Download the lnd rpc.proto file
curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
# Compile the proto file
python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. rpc.proto
rpc_pb2.py
とrpc_pb2_grpc.py
という二種類のファイルが作成されている。これはLNDと連携するために必要になるものだ。coindesk folderにこれを移動しよう。
mv rpc* lightning-coindesk/coindesk
CLIから、python gRPCのコマンドをいくつか試してみよう。
# Optionally install ipython for prettier command line output
pip install ipython
# Open the Django shell. This is a standard Python shell that also allows access to Django objects.
cd lightning-coindesk
python manage.py shell
# Import rpc files and grpc
In [1]: from coindesk import rpc_pb2 as ln, rpc_pb2_grpc as lnrpc
In [2]: import grpc, os
# Establish a secure connection with our RPC server. We will first have to
# gather our cert. Lnd cert is at ~/.lnd/tls.cert on Linux and
# ~/Library/Application Support/Lnd/tls.cert on Mac
In [3]: cert = open(os.path.expanduser('~/.lnd/tls.cert')).read()
In [4]: creds = grpc.ssl_channel_credentials(cert)
In [5]: channel = grpc.secure_channel('localhost:10009', creds)
# Create a new 'stub' object that will allow us to interact with our "Bob" lnd node.
In [6]: stub = lnrpc.LightningStub(channel)
# Make a call to the ListChannels API.
In [7]: listchannels_resp = stub.ListChannels(ln.ListChannelsRequest())
Out[7]:
channels {
active: true
remote_pubkey: "02244b8eff01be9f7b4ec1d73ab10fc36da48b01a685ac90ed09a63fe94ec08d0a"
channel_point: "2622b779a8acca471a738b0796cd62e4457b79b33265cbfa687aadccc329023a:0"
chan_id: 495879744192512
capacity: 1000000
local_balance: 21001
remote_balance: 970311
commit_fee: 8688
commit_weight: 724
fee_per_kw: 12000
total_satoshis_received: 21001
num_updates: 8
}
channels {
active: true
remote_pubkey: "032eed260ef71110a02a5da44d82fef9628ffa51113a2d0b9524e7d3bff615a1cf"
channel_point: "028088c354b26c33cfd5a5b2d4cca27c6e3a73b6752b5beff6f67ce779af5656:1"
chan_id: 554153860464641
capacity: 800000
local_balance: 190000
remote_balance: 601312
commit_fee: 8688
commit_weight: 724
fee_per_kw: 12000
total_satoshis_sent: 10000
num_updates: 2
}
何が起きているのだろう。list channelsコマンドのリクエストオブジェクト(ln.ListChannelsRequest()
)を作成して、ListChannels関数に渡している。レスポンスはlistchannels_resp変数に格納され、それぞれのプロパティーにアクセスできる。