def open_channel(self, pubkey, local_funding_amount): pubkey_bytes = codecs.decode(pubkey, 'hex') request = ln.OpenChannelRequest( node_pubkey=pubkey_bytes, node_pubkey_string=pubkey, local_funding_amount=local_funding_amount) try: response = self.stub.OpenChannel(request) #TODO: return success when OpenChannel finishes except grpc._channel._Rendezvous as e: print "Error opening channel with " + pubkey + ' ' + str(e)
def open_channel(self, **kwargs): if not self.channel_exists_with_node( kwargs.get('node_pubkey_string')) or kwargs.get('force'): try: if kwargs.get('force') is not None: del kwargs['force'] request = ln.OpenChannelRequest(**kwargs) response = self.client.OpenChannelSync(request) return response except Exception as e: logger.exception(e) else: raise AssertionError('Channel already opened')
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)
def _fundchannel(self, node_id, satoshis=None): try: request = ln.OpenChannelRequest(node_pubkey_string=node_id, local_funding_amount=satoshis or self.channel_amount) response = self.stub.OpenChannelSync(request) logging.info(response) if response.funding_txid_str: return response.funding_txid_str else: return MessageToJson(response) except grpc.RpcError as e: logging.error(e) return e.details()
def open_chan_with_node(node): request_channel_open = ln.OpenChannelRequest( node_pubkey=node.pub_key.decode("hex"), node_pubkey_string=node.pub_key, local_funding_amount=our_chan_size, push_sat=0, target_conf=0, sat_per_byte=1, # Give some fee to miners private=0, min_htlc_msat=0, ) print("[NetView Upkeep] Attempting to open channel with " + term_print(node.pub_key, bcolors.WARNING)) try: response = stub.OpenChannelSync(request_channel_open, metadata=[('macaroon', macaroon)]) print( term_print("OPENED with TX:\t", bcolors.OKGREEN) + term_print(str(response.funding_txid_bytes.encode('hex')), bcolors.WARNING)) except Exception as e: print(term_print("FAILED\t", bcolors.FAIL) + str(e.code()) + "\t" + str(e.details()))
def open_channel(source_container, dest_container, amount): """Open a channel between the 2 specified containers.""" # source_info = get_info(source_container) dest_info = get_info(dest_container) try: connect(source_container, dest_container) except Exception: # TODO: Catch more specific error pass channel = get_channel(source_container) macaroon = codecs.encode( open(_macaroon(source_container), 'rb').read(), 'hex') stub = lnrpc.LightningStub(channel) request = ln.OpenChannelRequest( node_pubkey_string=dest_info.identity_pubkey, local_funding_amount=amount, spend_unconfirmed=True, ) stub.OpenChannelSync(request, metadata=[('macaroon', macaroon)])
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 command_line = "/Users/thanh_nc/go/bin/btcctl --simnet --rpcuser=kek --rpcpass=kek generate 4" print command_line args = shlex.split(command_line) call(args) time.sleep(10) while True: btcd.poll()
def channelpage(): 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) response = stub.ListChannels(ln.ListChannelsRequest(), metadata=[('macaroon', macaroon)]) walbal = stub.WalletBalance(ln.WalletBalanceRequest(), metadata=[('macaroon', macaroon)]) availablefunds = walbal.confirmed_balance channellist = response.channels closedchannelidentifiers = [] channelidentifiers = [] pendingchannelidentifiers = [] pendingresponse = stub.PendingChannels(ln.PendingChannelsRequest(), metadata=[('macaroon', macaroon)]) pend = pendingresponse.pending_open_channels for i in pend: k = ((str(i.channel.remote_node_pub)), str(i.channel.channel_point), int(i.channel.capacity), int(i.channel.local_balance)) pendingchannelidentifiers.append(k) length_of_pending = len(pendingchannelidentifiers) closedresponse = stub.ClosedChannels(ln.ClosedChannelsRequest(), metadata=[('macaroon', macaroon)]) for i in closedresponse.channels: p = (str(i.remote_pubkey), str(i.channel_point), int(i.capacity), int(i.close_height), int(i.settled_balance)) closedchannelidentifiers.append(p) length_of_closed = len(closedchannelidentifiers) for i in channellist: if i.active == True: k = (str(i.remote_pubkey), int(i.capacity), int(i.local_balance), int(i.remote_balance), int(i.commit_fee), str(i.channel_point)) channelidentifiers.append(k) conn = True length_of = len(channelidentifiers) try: outcap = sum(zip(*channelidentifiers)[2]) incap = sum(zip(*channelidentifiers)[3]) except: outcap = incap = 0 length_of = 0 except: conn = False if conn == True: if request.method == "POST": if request.form['action'] == "openchan": try: pkstring = str(request.form['channelpubkey']) locfundamt = int(request.form['channelamt']) response = stub.OpenChannelSync(ln.OpenChannelRequest(node_pubkey_string=pkstring, local_funding_amount=locfundamt, push_sat=0), metadata=[('macaroon', macaroon)]) if response.funding_txid_bytes: return render_template('channels.html', availablefunds=availablefunds, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, outcap=outcap, incap=incap, conn=conn, opened="True", length_of=length_of, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) except: return render_template('channels.html', availablefunds=availablefunds, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, outcap=outcap, incap=incap, conn=conn, opened="True", length_of=length_of, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) elif request.form['action'] == "closechan": try: return ln.ChannelPoint(funding_txid_string=str(request.form['channelpubkey'])) stub.CloseChannel(ln.CloseChannelRequest(channel_point=channelpoint), metadata=[('macaroon', macaroon)]) return render_template('channels.html', availablefunds=availablefunds, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, outcap=outcap, incap=incap, conn=conn, closed="pendclose", length_of=length_of, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) except: return render_template('channels.html', availablefunds=availablefunds, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, outcap=outcap, incap=incap, conn=conn, closed="couldntclose", length_of=length_of, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) elif request.form['action'] == "fclosechan": try: return ln.ChannelPoint(funding_txid_string=str(request.form['channelpubkey'])) stub.CloseChannel(ln.CloseChannelRequest(channel_point=channelpoint, force=True), metadata=[('macaroon', macaroon)]) return render_template('channels.html', availablefunds=availablefunds, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, outcap=outcap, incap=incap, conn=conn, closed="pendclose", length_of=length_of, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) except: return render_template('channels.html', availablefunds=availablefunds, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, outcap=outcap, incap=incap, conn=conn, closed="couldntclose", length_of=length_of, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) else: return render_template('channels.html', conn=conn, length_of=length_of, channelidentifiers=channelidentifiers, closedchannelidentifiers=closedchannelidentifiers, incap=incap, outcap=outcap, availablefunds=availablefunds, length_of_closed=length_of_closed, pendingchannelidentifiers=pendingchannelidentifiers, length_of_pending=length_of_pending) return render_template('channels.html', conn=conn, length_of=0, channelidentifiers=0, closedchannelidentifiers=0, incap=0, outcap=0, availablefunds=0, length_of_closed=0, pendingchannelidentifiers=0, length_of_pending=0)
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) try: response = stub.OpenChannel(request) print response sleep(10) print response except grpc._channel._Rendezvous as e: print "Error opening channel with " + pubkey + ' ' + str(e)