def list_peers(self): try: peers = self.client.ListPeers(ln.ListPeersRequest()).peers return [p.pub_key for p in peers] except Exception as e: logger.exception(e) return []
def openchannel(self, node_id, host, port, satoshis): peers = self.rpc.stub.ListPeers(lnrpc.ListPeersRequest()).peers peers_by_pubkey = {p.pub_key: p for p in peers} if node_id not in peers_by_pubkey: raise ValueError("Could not find peer {} in peers {}".format(node_id, peers)) peer = peers_by_pubkey[node_id] self.rpc.stub.OpenChannel(lnrpc.OpenChannelRequest( node_pubkey=codecs.decode(peer.pub_key, 'hex_codec'), local_funding_amount=satoshis, push_sat=0 )) # Somehow broadcasting a tx is slow from time to time time.sleep(5)
from flask_cors import CORS # Change this to the port your lightning node is running on: default 10009 LND_PORT = "10009" # Change this to the path to your tls.cert, but keep /home/<user> format, not ~ PATH_TO_TLS_CERT = '/home/valentine/.lnd/tls.cert' SERVER_PUBKEY = "03a2102f6978b9e5c6a2dd39697f95b36a7992a60ca65e0316dcd517108e8da171" SERVER_HOST = "52.53.90.150:9735" cert = open(PATH_TO_TLS_CERT).read() creds = grpc.ssl_channel_credentials(cert) channel = grpc.secure_channel('localhost:' + LND_PORT, creds) stub = lnrpc.LightningStub(channel) # Add server as peer. listpeers_req = ln.ListPeersRequest() listpeers_res = stub.ListPeers(listpeers_req) already_peers = False for peer in listpeers_res.peers: if peer.pub_key == SERVER_PUBKEY: already_peers = True if not already_peers: ln_addr = ln.LightningAddress(pubkey=SERVER_PUBKEY, host=SERVER_HOST) req = ln.ConnectPeerRequest(addr=ln_addr) res = stub.ConnectPeer(req) app = Flask(__name__) CORS(app) @app.route('/<string:invoice>')
def peers(self): peers = self.rpc.stub.ListPeers(lnrpc.ListPeersRequest()).peers return [p.pub_key for p in peers]
def getPeers(stub, macaroon): responsePeers = stub.ListPeers(ln.ListPeersRequest(), metadata=[('macaroon', macaroon)]) return responsePeers
def listpeers(): return stub.ListPeers(ln.ListPeersRequest())
def ListPeers(self): response = stub.ListPeers(ln.ListPeersRequest()) return response
def peerpage(): try: rpc_connect = AuthServiceProxy("http://{}:{}@{}:{}".format(rpc_user,rpc_password,allowed_ip,rpc_port)) chain_type = rpc_connect.getblockchaininfo()['chain'] if chain_type == "test": chaintype_ln = "testnet" else: chaintype_ln = "mainnet" os.environ["GRPC_SSL_CIPHER_SUITES"] = 'HIGH+ECDSA' with open(os.path.expanduser(lnd_dir_location + 'data/chain/bitcoin/{}/admin.macaroon'.format(chaintype_ln)), 'rb') as f: macaroon_bytes = f.read() macaroon = codecs.encode(macaroon_bytes, 'hex') cert = open(os.path.expanduser(lnd_dir_location + 'tls.cert'), 'rb').read() creds = grpc.ssl_channel_credentials(cert) channel = grpc.secure_channel('localhost:10009', creds) stub = lnrpc.LightningStub(channel) pbks = stub.ListChannels(ln.ListChannelsRequest(), metadata=[('macaroon', macaroon)]) pubkey_list = [str(i.remote_pubkey) for i in pbks.channels] response = stub.ListPeers(ln.ListPeersRequest(), metadata=[('macaroon', macaroon)]) show_current_peers = response.peers show_current_peers_list = [] for peer in show_current_peers: if peer.pub_key in pubkey_list: show_current_peers_list.append((str(peer.pub_key), True)) else: show_current_peers_list.append((str(peer.pub_key), False)) conn = True length_of = len(show_current_peers_list) except: show_current_peers_list = ["Offline!"] length_of = 0 conn = False pubkey_list = ["Offline!"] if conn == True: if request.method == 'POST': if request.form['action'] == "connectbutton": if len(request.form['text']) > 10: response_uri = str(request.form['text']) result = response_uri.strip() def connect(host, port, node_id): addr = ln.LightningAddress(pubkey=node_id, host="{}:{}".format(host, port)) req = ln.ConnectPeerRequest(addr=addr, perm=True) stub.ConnectPeer(ln.ConnectPeerRequest(addr=addr,perm=False), metadata=[('macaroon',macaroon)]) try: nodeid, lnhost, lnport = result[:66], result[67:-5], result[-4:] connect(lnhost,lnport,nodeid) show_current_peers_list.append((nodeid, False)) length_of = len(show_current_peers_list) result = "Successfully connected!" return render_template('peerpage.html', len=len(show_current_peers_list), show_current_peers=show_current_peers_list, length_of=length_of, result="SuccessCon", conn=conn) except: return render_template('peerpage.html', len=len(show_current_peers_list), show_current_peers=show_current_peers_list, length_of=length_of, result="FalseCon", conn=conn) else: return render_template('peerpage.html', len=len(show_current_peers_list), show_current_peers=show_current_peers_list, length_of=length_of, result="FalseCon", conn=conn) else: if len(request.form['text']) > 10: response_uri = str(request.form['text']) result = response_uri.strip() def disconnect(pubkey): req = ln.DisconnectPeerRequest(pub_key=pubkey) stub.DisconnectPeer(ln.DisconnectPeerRequest(pub_key=pubkey), metadata=[('macaroon',macaroon)]) try: disconnect(result) del show_current_peers_list[-1] length_of = len(show_current_peers_list) return render_template('peerpage.html', len=len(show_current_peers_list), show_current_peers=show_current_peers_list, length_of=length_of, result="SuccessDis", conn=conn) except: return render_template('peerpage.html', len=len(show_current_peers_list), show_current_peers=show_current_peers_list, length_of=length_of, result="FalseDis", conn=conn) else: return render_template('peerpage.html', len=len(show_current_peers_list), show_current_peers=show_current_peers_list, length_of=length_of, result="FalseDis", conn=conn) return render_template('peerpage.html', len=len(show_current_peers_list), length_of=length_of, show_current_peers=show_current_peers_list, conn=conn)
# now build meta data credentials auth_creds = grpc.metadata_call_credentials(metadata_callback) # combine the cert credentials and the macaroon auth credentials combined_creds = grpc.composite_channel_credentials(cert_creds, auth_creds) # finally pass in the combined credentials when creating a channel channel = grpc.secure_channel("localhost:10009", combined_creds) stub = lnrpc.LightningStub(channel) # now every call will be made with the macaroon already included list_req = ln.ListInvoiceRequest(pending_only=True, num_max_invoices=1, reversed=True) invoice_num = stub.ListInvoices(list_req).invoices[-1].add_index peer_info = stub.ListPeers(ln.ListPeersRequest()) peers = [p.pub_key for p in peer_info.peers] channel_info = stub.ListChannels(ln.ListChannelsRequest()).channels channel_peers = set([c.remote_pubkey for c in channel_info]) fields = [ "remote_pubkey", "chan_id", "active", "capacity", "local_balance", "remote_balance", "csv_delay", ] chan_data = [[getattr(c, f) for f in fields] for c in channel_info]