lncm / noma

Noma - Bitcoin lightning node management CLI utility & Python API
https://lncm.io
Apache License 2.0
5 stars 2 forks source link

zapconnect functionality #15

Closed nolim1t closed 5 years ago

nolim1t commented 5 years ago

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

import os
import base64, codecs, json, requests

def loadlndfiles(filename='./admin.macaroon', tlsfile='./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()
    b['ip'] = '127.0.0.1:10009'
    j = json.dumps(b)
    print(j)
nolim1t commented 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)
nolim1t commented 5 years ago

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']))
AnotherDroog commented 5 years ago

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"]))
AnotherDroog commented 5 years ago

I think we can remove requests as it's unused?

AnotherDroog commented 5 years ago

Alright, I'm going to test and then integrate this

nolim1t commented 5 years ago

We might want to make the functions more reusable. That code was more proof that it works.

AnotherDroog commented 5 years ago

Right, I'll refactor a bit, let it pass in some args

nolim1t commented 5 years ago

I'm working on this right now. So far its working