raspiblitz/home.admin/config.scripts/lnd.initwallet.py

175 lines
5.2 KiB
Python
Raw Normal View History

2019-04-12 02:06:16 +01:00
#!/usr/bin/python
2019-04-30 22:10:02 +02:00
import codecs, os, sys, base64
2019-04-12 01:15:31 +01:00
# display config script info
if len(sys.argv) <= 1 or sys.argv[1] == "-h" or sys.argv[1] == "help":
2019-04-30 22:10:02 +02:00
print("# ! always activate virtual env first: source /home/admin/python-env-lnd/bin/activate")
print("# ! and run with with: python /home/admin/config.scripts/lnd.initwallet.py")
2019-04-12 02:06:16 +01:00
print("# creating or recovering the LND wallet")
print("# lnd.winitwallet.py new [walletpassword] [?seedpassword]")
2019-04-30 22:17:29 +02:00
print("# lnd.winitwallet.py seed [walletpassword] [\"seeds-words-seperated-spaces\"] [?seedpassword]")
print("# lnd.winitwallet.py scb [walletpassword] [\"seeds-words-seperated-spaces\"] [filepathSCB] [?seedpassword]")
2019-04-12 02:06:16 +01:00
print("err='missing parameters'")
2019-04-12 01:15:31 +01:00
sys.exit(1)
2019-04-30 22:10:02 +02:00
import grpc
from lndlibs import rpc_pb2 as ln
from lndlibs import rpc_pb2_grpc as lnrpc
2019-04-30 23:03:05 +02:00
from pathlib2 import Path
2019-04-30 22:10:02 +02:00
2019-04-12 01:15:31 +01:00
walletpassword=""
seedwords=""
seedpassword=""
filepathSCB=""
mode=sys.argv[1]
if mode=="new":
2019-04-12 02:06:16 +01:00
print("# *** CREATING NEW LND WALLET ***")
2019-04-12 02:08:36 +01:00
if len(sys.argv)>2:
2019-04-12 02:06:16 +01:00
walletpassword=sys.argv[2]
if len(walletpassword)<8:
print("err='wallet password is too short'")
sys.exit(1)
else:
print("err='wallet password is too short'")
sys.exit(1)
2019-04-12 02:08:36 +01:00
if len(sys.argv)>3:
2019-04-12 02:06:16 +01:00
seedpassword=sys.argv[3]
2019-04-30 23:03:05 +02:00
elif mode=="seed" or mode=="scb":
2019-04-12 02:06:16 +01:00
2019-04-16 15:15:19 +01:00
if len(sys.argv)>2:
walletpassword=sys.argv[2]
if len(walletpassword)<8:
print("err='wallet password is too short'")
sys.exit(1)
else:
2019-04-30 23:03:05 +02:00
print("err='not correct amount of parameter - missing wallet password'")
2019-04-12 02:06:16 +01:00
sys.exit(1)
2019-04-16 15:15:19 +01:00
if len(sys.argv)>3:
seedwordString=sys.argv[3]
2019-04-30 22:17:29 +02:00
seedwords=seedwordString.split(" ")
2019-04-16 15:15:19 +01:00
if len(seedwords)<24:
2019-04-30 23:03:05 +02:00
print("err='not 24 seed words seperated by just spaces (surrounded with \")'")
2019-04-16 15:15:19 +01:00
sys.exit(1)
else:
2019-04-30 23:03:05 +02:00
print("err='not correct amount of parameter - missing seed string'")
2019-04-16 15:15:19 +01:00
sys.exit(1)
2019-04-30 23:11:47 +02:00
if mode=="seed":
2019-04-30 23:03:05 +02:00
2019-04-30 23:11:47 +02:00
if len(sys.argv)>4:
seedpassword=sys.argv[4]
2019-04-16 15:15:19 +01:00
2019-04-30 23:11:47 +02:00
elif mode=="scb":
2019-04-12 02:06:16 +01:00
2019-04-30 23:11:47 +02:00
if len(sys.argv)>4:
filepathSCB=sys.argv[4]
scbFile = Path(filepathSCB)
if scbFile.is_file():
print("# OK SCB file exists")
else:
print("err='the given filepathSCB - file does not exists or no permission'")
sys.exit(1)
2019-04-30 23:03:05 +02:00
else:
2019-04-30 23:11:47 +02:00
print("err='not correct amount of parameter - missing seed filepathSCB'")
2019-04-30 23:03:05 +02:00
sys.exit(1)
2019-04-30 23:11:47 +02:00
if len(sys.argv)>5:
seedpassword=sys.argv[4]
2019-04-30 23:03:05 +02:00
2019-04-12 01:15:31 +01:00
else:
2019-04-12 02:06:16 +01:00
print("err='unkown mode parameter - run without any parameters to see options'")
2019-04-12 01:15:31 +01:00
sys.exit(1)
2019-04-12 02:06:16 +01:00
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open('/mnt/hdd/lnd/tls.cert', 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
channel = grpc.secure_channel('localhost:10009', ssl_creds)
stub = lnrpc.WalletUnlockerStub(channel)
2019-04-12 02:48:57 +01:00
if mode=="new":
2019-04-12 11:15:11 +01:00
request = ln.GenSeedRequest()
2019-04-12 13:03:01 +01:00
try:
response = stub.GenSeed(request)
seedwords = response.cipher_seed_mnemonic
2019-04-15 01:33:29 +01:00
seedwordsString=', '.join(seedwords)
2019-04-12 13:03:01 +01:00
print("seedwords='"+seedwordsString+"'")
2019-04-15 01:15:57 +01:00
# add a 6x4 formatted version to the output
seedwords6x4=""
for i in range(0,len(seedwords)):
2019-04-15 01:25:02 +01:00
if i % 6 == 0 and i != 0:
2019-04-15 01:15:57 +01:00
seedwords6x4=seedwords6x4+"\n"
2019-04-15 01:25:02 +01:00
singleWord=str(i+1)+":"+seedwords[i]
while len(singleWord)<12:
2019-04-15 01:15:57 +01:00
singleWord=singleWord+" "
seedwords6x4=seedwords6x4+singleWord
print("seedwords6x4='"+seedwords6x4+"'")
2019-04-16 15:27:44 +01:00
except grpc.RpcError as rpc_error_call:
code = rpc_error_call.code()
print >> sys.stderr, code
details = rpc_error_call.details()
print("err='RPCError GenSeedRequest'")
print("errMore='"+details+"'")
2019-04-12 13:31:16 +01:00
sys.exit(1)
2019-04-16 15:27:44 +01:00
except:
e = sys.exc_info()[0]
print >> sys.stderr, e
print("err='GenSeedRequest'")
2019-04-12 13:31:16 +01:00
sys.exit(1)
2019-04-12 11:12:50 +01:00
2019-04-12 12:47:09 +01:00
request = ln.InitWalletRequest(
wallet_password=walletpassword,
cipher_seed_mnemonic=seedwords
)
2019-04-12 13:03:01 +01:00
try:
response = stub.InitWallet(request)
2019-04-16 15:27:44 +01:00
except grpc.RpcError as rpc_error_call:
code = rpc_error_call.code()
print >> sys.stderr, code
details = rpc_error_call.details()
print("err='RPCError InitWallet'")
print("errMore='"+details+"'")
sys.exit(1)
2019-04-12 13:03:01 +01:00
except:
e = sys.exc_info()[0]
2019-04-12 13:04:54 +01:00
print >> sys.stderr, e
2019-04-16 15:27:44 +01:00
print("err='InitWallet'")
2019-04-12 13:31:16 +01:00
sys.exit(1)
2019-04-12 02:48:57 +01:00
elif mode=="seed":
2019-04-16 15:10:25 +01:00
request = ln.InitWalletRequest(
wallet_password=walletpassword,
cipher_seed_mnemonic=seedwords,
2019-04-29 01:33:41 +02:00
recovery_window=2500,
2019-04-16 16:01:57 +01:00
aezeed_passphrase=seedpassword
2019-04-16 15:10:25 +01:00
)
try:
2019-04-16 15:27:44 +01:00
response = stub.InitWallet(request)
2019-04-16 15:28:47 +01:00
except grpc.RpcError as rpc_error_call:
2019-04-16 15:21:44 +01:00
code = rpc_error_call.code()
2019-04-16 15:23:12 +01:00
print >> sys.stderr, code
2019-04-16 15:27:44 +01:00
details = rpc_error_call.details()
print("err='RPCError InitWallet'")
2019-04-16 15:21:44 +01:00
print("errMore='"+details+"'")
2019-04-16 15:27:44 +01:00
sys.exit(1)
2019-04-16 15:10:25 +01:00
except:
e = sys.exc_info()[0]
print >> sys.stderr, e
2019-04-16 15:27:44 +01:00
print("err='InitWallet'")
2019-04-16 15:10:25 +01:00
sys.exit(1)
2019-04-12 02:48:57 +01:00
elif mode=="scb":
print("err='TODO: implement creating from seed/scb'")
2019-04-16 14:10:06 +01:00
sys.exit(1)