SubscribeState
SubscribeState subscribes to the state of the wallet. The current wallet state will always be delivered immediately.
Source: stateservice.proto
gRPC
info
This is a server-streaming RPC
rpc SubscribeState (SubscribeStateRequest) returns (stream SubscribeStateResponse);
REST
HTTP Method | Path |
---|---|
GET | /v1/state/subscribe |
Code Samples
- gRPC
- REST
- Shell
- Javascript
- Python
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:10009'
const TLS_PATH = 'LND_DIR/tls.cert'
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync(['lightning.proto', 'stateservice.proto'], loaderOptions);
const lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
let client = new lnrpc.State(GRPC_HOST, sslCreds);
let request = {};
let call = client.subscribeState(request);
call.on('data', function(response) {
// A response was received from the server.
console.log(response);
});
call.on('status', function(status) {
// The current status of the stream.
});
call.on('end', function() {
// The server has closed the stream.
});
// Console output:
// {
// "state": <WalletState>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the stateservice.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import stateservice_pb2 as lnrpc, stateservice_pb2_grpc as stateservicestub
GRPC_HOST = 'localhost:10009'
TLS_PATH = 'LND_DIR/tls.cert'
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# make the request
channel = grpc.secure_channel(GRPC_HOST, ssl_creds)
stub = stateservicestub.StateStub(channel)
request = lnrpc.SubscribeStateRequest()
for response in stub.SubscribeState(request):
print(response)
# {
# "state": <WalletState>,
# }
- Javascript
- Python
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8080'
let options = {
url: `https://${REST_HOST}/v1/state/subscribe`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
}
request.get(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "state": <string>, // <WalletState>
// }
// --------------------------
// Example with websockets:
// --------------------------
const WebSocket = require('ws');
const fs = require('fs');
const REST_HOST = 'localhost:8080'
let ws = new WebSocket(`wss://${REST_HOST}/v1/state/subscribe?method=GET`, {
// Work-around for self-signed certificates.
rejectUnauthorized: false,
});
let requestBody = {
};
ws.on('open', function() {
ws.send(JSON.stringify(requestBody));
});
ws.on('error', function(err) {
console.log('Error: ' + err);
});
ws.on('message', function(body) {
console.log(body);
});
// Console output:
// {
// "state": <string>, // <WalletState>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8080'
TLS_PATH = 'LND_DIR/tls.cert'
url = f'https://{REST_HOST}/v1/state/subscribe'
r = requests.get(url, stream=True, verify=TLS_PATH)
for raw_response in r.iter_lines():
json_response = json.loads(raw_response)
print(json_response)
# {
# "state": <WalletState>,
# }
# There is no CLI command for this RPC
Messages
lnrpc.SubscribeStateRequest
Source: stateservice.proto
note
This request has no parameters.
lnrpc.SubscribeStateResponse
Source: stateservice.proto
Field | gRPC Type | REST Type |
---|---|---|
state | WalletState | string |
Enums
lnrpc.WalletState
Name | Number |
---|---|
NON_EXISTING | 0 |
LOCKED | 1 |
UNLOCKED | 2 |
RPC_ACTIVE | 3 |
SERVER_ACTIVE | 4 |
WAITING_TO_START | 255 |