Closed nolim1t closed 5 years ago
I tested the following code on one of my boxes. Seems to work fine
import os
import base64, codecs, json, requests
def loadlndfiles(filename='/media/important/important/lnd/data/chain/bitcoin/mainnet/admin.macaroon', tlsfile='/media/important/important/lnd/tls.cert'):
if not os.path.exists(filename):
return "FILENOTEXISTS"
else:
with open(os.path.expanduser(filename), "rb") as f:
macaroon_bytes = f.read()
with open(os.path.expanduser(tlsfile), "rb") as f:
tls_bytes = f.read()
macaroon = codecs.encode(macaroon_bytes, 'base64').decode().replace("\n", "")
tlsfile = tls_bytes.decode().replace("\n", "").replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "")
return {'c': tlsfile, 'm': macaroon}
if __name__ == "__main__":
b = loadlndfiles()
# Replace with with the external IP
b['ip'] = '0.tcp.ngrok.io:10009'
j = json.dumps(b)
print(j)
for LND connect functionality, I've tested out a function called lndconnect which spits out a connection string. This seems to work with zap desktop so far
import os
import base64, codecs, json, requests
def loadlndfiles(filename='/media/important/important/lnd/data/chain/bitcoin/mainnet/admin.macaroon', tlsfile='/media/important/important/lnd/tls.cert'):
if not os.path.exists(filename):
return "FILENOTEXISTS"
else:
with open(os.path.expanduser(filename), "rb") as f:
macaroon_bytes = f.read()
with open(os.path.expanduser(tlsfile), "rb") as f:
tls_bytes = f.read()
macaroon = codecs.encode(macaroon_bytes, 'base64').decode().replace("\n", "")
tlsfile = tls_bytes.decode().replace("\n", "").replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "")
return {'c': tlsfile, 'm': macaroon}
def lndconnect(filename='/media/important/important/lnd/data/chain/bitcoin/mainnet/admin.macaroon', tlsfile='/media/important/important/lnd/tls.cert'):
if not os.path.exists(filename):
return "FILENOTEXISTS"
else:
with open(os.path.expanduser(filename), "rb") as f:
macaroon_bytes = f.read()
with open(os.path.expanduser(tlsfile), "rb") as f:
tls_bytes = f.read()
macaroon = codecs.encode(macaroon_bytes, 'base64').decode().replace("\n", "").replace("=","").replace("/","_").replace("+","-")
tlsfile = tls_bytes.decode().replace("\n", "").replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "").replace("=","").replace("/","_").replace("+", "-")
return 'lndconnect://hostname?cert=' + tlsfile + '&macaroon=' + macaroon
if __name__ == "__main__":
b = loadlndfiles()
b['ip'] = 'NODEIP:10009'
j = json.dumps(b)
print(j)
print(lndconnect().replace("hostname", b['ip']))
Reformatted using black -l 79
:
import os
import base64, codecs, json, requests
def loadlndfiles(
filename="/media/important/important/lnd/data/chain/bitcoin/mainnet/admin.macaroon",
tlsfile="/media/important/important/lnd/tls.cert",
):
if not os.path.exists(filename):
return "FILENOTEXISTS"
else:
with open(os.path.expanduser(filename), "rb") as f:
macaroon_bytes = f.read()
with open(os.path.expanduser(tlsfile), "rb") as f:
tls_bytes = f.read()
macaroon = (
codecs.encode(macaroon_bytes, "base64").decode().replace("\n", "")
)
tlsfile = (
tls_bytes.decode()
.replace("\n", "")
.replace("-----BEGIN CERTIFICATE-----", "")
.replace("-----END CERTIFICATE-----", "")
)
return {"c": tlsfile, "m": macaroon}
def lndconnect(
filename="/media/important/important/lnd/data/chain/bitcoin/mainnet/admin.macaroon",
tlsfile="/media/important/important/lnd/tls.cert",
):
if not os.path.exists(filename):
return "FILENOTEXISTS"
else:
with open(os.path.expanduser(filename), "rb") as f:
macaroon_bytes = f.read()
with open(os.path.expanduser(tlsfile), "rb") as f:
tls_bytes = f.read()
macaroon = (
codecs.encode(macaroon_bytes, "base64")
.decode()
.replace("\n", "")
.replace("=", "")
.replace("/", "_")
.replace("+", "-")
)
tlsfile = (
tls_bytes.decode()
.replace("\n", "")
.replace("-----BEGIN CERTIFICATE-----", "")
.replace("-----END CERTIFICATE-----", "")
.replace("=", "")
.replace("/", "_")
.replace("+", "-")
)
return (
"lndconnect://hostname?cert=" + tlsfile + "&macaroon=" + macaroon
)
if __name__ == "__main__":
b = loadlndfiles()
b["ip"] = "NODEIP:10009"
j = json.dumps(b)
print(j)
print(lndconnect().replace("hostname", b["ip"]))
I think we can remove requests
as it's unused?
Alright, I'm going to test and then integrate this
We might want to make the functions more reusable. That code was more proof that it works.
Right, I'll refactor a bit, let it pass in some args
I'm working on this right now. So far its working
As discussed in https://github.com/lncm/pi-factory/issues/148 should add the functionality to generate a zap connect JSON file within the noma utility.
Just to get started.
Example python function