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

282 lines
9.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import binascii
import os
import sys
from pathlib import Path
2019-04-12 01:15:31 +01:00
2019-04-30 22:10:02 +02:00
import grpc
2020-06-23 15:25:23 +02:00
from lndlibs import walletunlocker_pb2 as lnrpc
from lndlibs import walletunlocker_pb2_grpc as rpcstub
2019-04-30 22:10:02 +02:00
if sys.version_info < (3, 0):
print("Can't run on Python2")
sys.exit()
2019-04-12 02:06:16 +01:00
# display config script info
if len(sys.argv) <= 1 or sys.argv[1] in ["-h", "--help", "help"]:
print("# creating or recovering the LND wallet")
print("# lnd.initwallet.py new [walletpassword] [?seedpassword]")
print("# lnd.initwallet.py seed [walletpassword] [\"seeds-words-seperated-spaces\"] [?seedpassword]")
print("# lnd.initwallet.py scb [walletpassword] [\"seeds-words-seperated-spaces\"] [filepathSCB] [?seedpassword]")
print("# lnd.initwallet.py change-password [walletpassword-old] [walletpassword-new]")
print("err='missing parameters'")
sys.exit(1)
2019-04-12 02:06:16 +01:00
mode = sys.argv[1]
2019-04-12 02:06:16 +01:00
def new(stub, wallet_password="", seed_entropy=None):
if seed_entropy:
# provide 16-bytes of static data to get reproducible seeds for TESTING!)
print("WARNING: Use this for testing only!!")
2020-06-23 14:28:10 +02:00
request = lnrpc.GenSeedRequest(seed_entropy=seed_entropy)
2019-04-16 15:15:19 +01:00
else:
2020-06-23 14:28:10 +02:00
request = lnrpc.GenSeedRequest()
2019-04-12 01:15:31 +01:00
2019-04-12 13:03:01 +01:00
try:
response = stub.GenSeed(request)
seed_words = response.cipher_seed_mnemonic
seed_words_str = ', '.join(seed_words)
print("seedwords='" + seed_words_str + "'")
2019-04-15 01:15:57 +01:00
# add a 6x4 formatted version to the output
seed_words_6x4 = ""
for i in range(0, len(seed_words)):
2019-04-15 01:25:02 +01:00
if i % 6 == 0 and i != 0:
seed_words_6x4 = seed_words_6x4 + "\n"
single_word = str(i + 1) + ":" + seed_words[i]
while len(single_word) < 12:
single_word = single_word + " "
seed_words_6x4 = seed_words_6x4 + single_word
print("seedwords6x4='" + seed_words_6x4 + "'")
2019-04-15 01:15:57 +01:00
2019-04-16 15:27:44 +01:00
except grpc.RpcError as rpc_error_call:
code = rpc_error_call.code()
print(code, file=sys.stderr)
details = rpc_error_call.details()
2019-04-16 15:27:44 +01:00
print("err='RPCError GenSeedRequest'")
2020-02-19 10:52:06 +01:00
print("errMore=\"" + details + "\"")
2019-04-12 13:31:16 +01:00
sys.exit(1)
except:
2019-04-16 15:27:44 +01:00
e = sys.exc_info()[0]
print(e, file=sys.stderr)
2019-04-16 15:27:44 +01:00
print("err='GenSeedRequest'")
2019-04-12 13:31:16 +01:00
sys.exit(1)
2019-04-12 11:12:50 +01:00
2020-06-23 14:28:10 +02:00
request = lnrpc.InitWalletRequest(
wallet_password=wallet_password.encode(),
cipher_seed_mnemonic=seed_words
2019-04-12 12:47:09 +01:00
)
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(code, file=sys.stderr)
details = rpc_error_call.details()
2019-04-16 15:27:44 +01:00
print("err='RPCError InitWallet'")
2020-02-19 10:52:06 +01:00
print("errMore=\"" + details + "\"")
2019-04-16 15:27:44 +01:00
sys.exit(1)
except:
2019-04-12 13:03:01 +01:00
e = sys.exc_info()[0]
print(e, file=sys.stderr)
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
def seed(stub, wallet_password="", seed_words="", seed_password=""):
2020-06-23 14:28:10 +02:00
request = lnrpc.InitWalletRequest(
wallet_password=wallet_password.encode(),
cipher_seed_mnemonic=[x.encode() for x in seed_words],
2020-04-16 21:57:54 +02:00
recovery_window=5000,
aezeed_passphrase=seed_password.encode()
2019-04-16 15:10:25 +01:00
)
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()
print(code, file=sys.stderr)
details = rpc_error_call.details()
2019-04-16 15:27:44 +01:00
print("err='RPCError InitWallet'")
2020-02-19 10:52:06 +01:00
print("errMore=\"" + details + "\"")
2019-04-16 15:27:44 +01:00
sys.exit(1)
except:
2019-04-16 15:10:25 +01:00
e = sys.exc_info()[0]
print(e, file=sys.stderr)
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
def scb(stub, wallet_password="", seed_words="", seed_password="", file_path_scb=""):
with open(file_path_scb, 'rb') as f:
2019-04-30 23:37:17 +02:00
content = f.read()
scb_hex_str = binascii.hexlify(content)
print(scb_hex_str)
2019-04-30 23:37:17 +02:00
2020-06-23 14:28:10 +02:00
request = lnrpc.InitWalletRequest(
wallet_password=wallet_password.encode(),
cipher_seed_mnemonic=[x.encode() for x in seed_words],
2020-04-16 21:57:54 +02:00
recovery_window=5000,
aezeed_passphrase=seed_password.encode(),
channel_backups=scb_hex_str.encode()
2019-04-30 23:37:17 +02:00
)
try:
response = stub.InitWallet(request)
except grpc.RpcError as rpc_error_call:
code = rpc_error_call.code()
print(code, file=sys.stderr)
details = rpc_error_call.details()
2019-04-30 23:37:17 +02:00
print("err='RPCError InitWallet'")
2020-02-19 10:52:06 +01:00
print("errMore=\"" + details + "\"")
2019-04-30 23:37:17 +02:00
sys.exit(1)
except:
e = sys.exc_info()[0]
print(e, file=sys.stderr)
2019-04-30 23:37:17 +02:00
print("err='InitWallet'")
sys.exit(1)
# TODO(rootzoll) implement creating from seed/scb
2019-04-12 02:48:57 +01:00
print("err='TODO: implement creating from seed/scb'")
sys.exit(1)
def change_password(stub, wallet_password="", wallet_password_new=""):
request = lnrpc.ChangePasswordRequest(
current_password=wallet_password.encode(),
new_password=wallet_password_new.encode()
)
try:
response = stub.ChangePassword(request)
print("# ok - password changed")
#print(response.admin_macaroon)
except grpc.RpcError as rpc_error_call:
code = rpc_error_call.code()
print(code, file=sys.stderr)
details = rpc_error_call.details()
print("err='RPCError ChangePassword'")
print("errMore=\"" + details + "\"")
print("# make sure wallet is locked when trying to change password'", file=sys.stderr)
sys.exit(1)
except:
e = sys.exc_info()[0]
print(e, file=sys.stderr)
print("err='ChangePassword'")
sys.exit(1)
def parse_args():
wallet_password = ""
wallet_password_new = ""
seed_words = ""
seed_password = ""
filepath_scb = ""
if mode == "new":
if len(sys.argv) > 2:
wallet_password = sys.argv[2]
if len(wallet_password) < 8:
print("err='wallet password is too short'")
sys.exit(1)
else:
print("err='missing parameters'")
sys.exit(1)
if len(sys.argv) > 3:
seed_password = sys.argv[3]
elif mode == "change-password":
if len(sys.argv) > 3:
wallet_password = sys.argv[2]
if len(wallet_password) < 8:
print("err='wallet password is too short'")
sys.exit(1)
wallet_password_new = sys.argv[3]
if len(wallet_password_new ) < 8:
print("err='wallet password new is too short'")
sys.exit(1)
else:
print("err='missing parameters'")
sys.exit(1)
elif mode == "seed" or mode == "scb":
if len(sys.argv) > 2:
wallet_password = sys.argv[2]
if len(wallet_password) < 8:
print("err='wallet password is too short'")
sys.exit(1)
else:
print("err='not correct amount of parameter - missing wallet password'")
sys.exit(1)
if len(sys.argv) > 3:
seed_word_str = sys.argv[3]
seed_words = seed_word_str.split(" ")
if len(seed_words) < 24:
print("err='not 24 seed words separated by just spaces (surrounded with \")'")
sys.exit(1)
else:
print("err='not correct amount of parameter - missing seed string'")
sys.exit(1)
if mode == "seed":
if len(sys.argv) > 4:
seed_password = sys.argv[4]
elif mode == "scb":
if len(sys.argv) > 4:
filepath_scb = sys.argv[4]
scb_file = Path(filepath_scb)
if scb_file.is_file():
print("# OK SCB file exists")
else:
print("err='the given filepathSCB - file does not exists or no permission'")
sys.exit(1)
else:
print("err='not correct amount of parameter - missing seed filepathSCB'")
sys.exit(1)
if len(sys.argv) > 5:
seed_password = sys.argv[4]
else:
print("err='unknown mode parameter - run without any parameters to see options'")
sys.exit(1)
return wallet_password, seed_words, seed_password, filepath_scb, wallet_password_new
def main():
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)
2020-06-23 14:28:10 +02:00
stub = rpcstub.WalletUnlockerStub(channel)
wallet_password, seed_words, seed_password, file_path_scb, wallet_password_new = parse_args()
if mode == "new":
print("# *** CREATING NEW LND WALLET ***")
new(stub, wallet_password)
elif mode == "seed":
print("# *** RECOVERING LND WALLET FROM SEED ***")
seed(stub, wallet_password, seed_words, seed_password)
elif mode == "scb":
print("# *** RECOVERING LND WALLET FROM SEED + SCB ***")
scb(stub, wallet_password, seed_words, seed_password, file_path_scb)
elif mode == "change-password":
print("# *** SETTING NEW PASSWORD FOR WALLET ***")
change_password(stub, wallet_password, wallet_password_new)
if __name__ == '__main__':
main()