Esempio n. 1
0
    def connect_peer(self, pubkey, host, permanent=False):

        assert host, "Host is empty."
        assert pubkey, "Pubkey is empty."

        try:
            addr = ln.LightningAddress(pubkey=pubkey, host=host)
        except Exception as e:
            raise AssertionError(
                f'Cant create LightningAddress from host:{host} ->  pubkey:{pubkey}'
            )

        try:
            request = ln.ConnectPeerRequest(addr=addr, perm=permanent)
        except Exception as e:
            raise AssertionError('Cant create peer request')

        try:
            response = self.client.ConnectPeer(request)
            return response
        except Exception as e:
            if str(e.details()).startswith('already connected to peer'):
                pass
            else:
                raise AssertionError(
                    f'Can\'t connect to {host}! {e.details()}')

        return
Esempio n. 2
0
def connectAllNodes(networkData_nodes, stub, macaroon):
    nodesToTry = thunder.getNodesWithIPs(networkData_nodes)
    requestedNodes = []
    failedNodes = []
    # nodesToTry = [x for x in nodesToTry if ((not x in connectedNodes) and (not x in failedNodes))]

    # Try to connect to clients
    index = 0
    for node in nodesToTry:
        currentTarget = ln.LightningAddress()
        currentTarget.pubkey = node["pub_key"]
        currentTarget.host = node["addresses"][0]["addr"]
        print("[NetView Upkeep][" + str(index) + "/" + str(len(nodesToTry)) + "] Target: " + term_print(
            thunder.getNodeURI(node), bcolors.WARNING))
        # print(termPrint(currentTarget,bcolors.WARNING))
        try:
            requestConnect = ln.ConnectPeerRequest(
                addr=currentTarget,  # Pubkey@ip:port #getNodeURI(nodeInfo)
                perm=True,  # Normal too slow due to generous timeouts
            )
            response = stub.ConnectPeer(requestConnect, metadata=[('macaroon', macaroon)])
            print(term_print("[NetView Upkeep] Request Sent\t", bcolors.OKGREEN) + str(response))
            requestedNodes.append(node)
        except Exception as e:
            if str(e.details()).startswith("already connected to peer"):
                print(term_print("ALREADY CONNECTED\t", bcolors.WARNING) + str(e.details()))
                requestedNodes.append(node)
            else:
                print(term_print("FAILED\t", bcolors.FAIL) + str(e.code()) + "\t" + str(e.details()))
                failedNodes.append(node)
        index += 1
    return [requestedNodes, failedNodes]
Esempio n. 3
0
 def connect_peer(self, pubkey, host):
     addr = ln.LightningAddress(pubkey=pubkey, host=host)
     request = ln.ConnectPeerRequest(addr=addr)
     try:
         response = self.stub.ConnectPeer(request)
         return True
     except grpc._channel._Rendezvous as e:
         print "Error connecting to " + pubkey + ' ' + str(e)
         return False
Esempio n. 4
0
 def _connect(self, node_id, host=None, port=9735):
     addr = ln.LightningAddress(pubkey=node_id, host="%s:%s" % (host, port))
     try:
         request = ln.ConnectPeerRequest(addr=addr)
         response = self.stub.ConnectPeer(request)
         logging.info(response)
         return "Connected %s" % node_id
     except grpc.RpcError as e:
         logging.error(e)
         return e.details()
Esempio n. 5
0
def connect(source_container, dest_container):
    dest_info = get_info(dest_container)

    channel = get_channel(source_container)
    macaroon = codecs.encode(
        open(_macaroon(source_container), 'rb').read(), 'hex')
    stub = lnrpc.LightningStub(channel)
    addr = ln.LightningAddress(
        pubkey=dest_info.identity_pubkey,
        host=dest_container,
    )
    request = ln.ConnectPeerRequest(addr=addr)
    stub.ConnectPeer(request, metadata=[('macaroon', macaroon)])
Esempio n. 6
0
def connect_to_peer(peer_data):
    currentTarget = ln.LightningAddress()
    try:
        pk, host = peer_data.pub_key, peer_data.address
    except:
        pk, host = peer_data["pub_key"], peer_data["address"]

    currentTarget.pubkey = pk
    currentTarget.host = host
    print("[NetView Upkeep] Target: " + term_print(pk, bcolors.WARNING))
    try:
        requestConnect = ln.ConnectPeerRequest(
            addr=currentTarget,  # Pubkey@ip:port #getNodeURI(nodeInfo)
            perm=True,  # Normal too slow due to generous timeouts
        )
        response = stub.ConnectPeer(requestConnect, metadata=[('macaroon', macaroon)])
        print(term_print("[NetView Upkeep] Request Sent\t", bcolors.OKGREEN) + str(response))
        # connectedNodes.append(node)
    except Exception as e:
        if str(e.details()).startswith("already connected to peer"):
            print(term_print("ALREADY CONNECTED\t", bcolors.WARNING) + str(e.details()))
        else:
            print(term_print("FAILED\t", bcolors.FAIL) + str(e.code()) + "\t" + str(e.details()))
Esempio n. 7
0
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 pay_invoice(invoice):
    send_req = ln.SendRequest(payment_request=invoice)
    sent_payment = stub.SendPaymentSync(send_req)
    if "unable to find a path to destination" in sent_payment.payment_error:
        return sent_payment.payment_error
    return invoice
Esempio n. 8
0
 def connect(self, host, port, node_id):
     addr = lnrpc.LightningAddress(pubkey=node_id, host="{}:{}".format(host, port))
     req = lnrpc.ConnectPeerRequest(addr=addr, perm=True)
     logging.debug(self.rpc.stub.ConnectPeer(req))
Esempio n. 9
0
    print response

for i in range(n):
    os.chdir("/Users/thanh_nc/lnd/simnet/" + str(i))
    print(directory + str(i) + '/lnd/tls.cert')
    cert = open(directory + str(i) + '/lnd/tls.cert').read()
    creds = grpc.ssl_channel_credentials(cert)
    port = str(i) if i >= 10 else '0' + str(i)
    channel = grpc.secure_channel('localhost:100' + port, creds)
    stub = lnrpc.LightningStub(channel)

    for j in data[i].split():
        print j
        port2 = str(j) if int(j) >= 10 else '0' + str(j)
        print port2
        request = ln.ConnectPeerRequest(addr=ln.LightningAddress(
            pubkey=address[int(j)], host='localhost:201' + port2))
        print address[int(j)] + '@localhost:201' + port2
        response = stub.ConnectPeer(request)
        print response
        print str.encode(str(address[int(j)]))
        time.sleep(10)

        print address[int(j)]
        request = ln.OpenChannelRequest(node_pubkey=str(address[int(j)]),
                                        node_pubkey_string=str(
                                            address[int(j)]),
                                        local_funding_amount=5000000,
                                        push_sat=3000000)
        response = stub.OpenChannelSync(request)
        # Do something
        print response
Esempio n. 10
0
 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)])
Esempio n. 11
0
import sys
from time import sleep

# Lnd cert is at ~/.lnd/tls.cert on Linux and
# ~/Library/Application Support/Lnd/tls.cert on Mac
cert = open(os.path.expanduser('~/.lnd/tls.cert')).read()
creds = grpc.ssl_channel_credentials(cert)
channel = grpc.secure_channel('localhost:10009', creds)
stub = lnrpc.LightningStub(channel)

ls = lnrpc.LightningServicer()
if 1:
    n = sys.argv[1]
    pubkey, host = n.split('@')
    addr = ln.LightningAddress(pubkey=pubkey, host=host)
    request = ln.ConnectPeerRequest(addr=addr)
    try:
        response = stub.ConnectPeer(request)
        print response
    except grpc._channel._Rendezvous as e:
        print "Error connecting to " + pubkey + ' ' + str(e)
        sys.exit()

    # try to open a channel to the new peer
    import codecs
    dest_hex = pubkey
    dest_bytes = codecs.decode(dest_hex, 'hex')

    request = ln.OpenChannelRequest(node_pubkey=dest_bytes,
                                    node_pubkey_string=pubkey,
                                    local_funding_amount=1000000)