Browse Source

Fix bugs in utxo parsing in add-utxo.py

Fixes #743. The utility function `get_utxo_info`
tests the validity of the utxo string and the private
key, so there is no need to repeat this test; but we
need to add utxo data in binary format, so an option
is included to return it in this form.
The 4 ways of adding external commitments were tested
as all working after this commit: read in from json,
read in from file (csv format), read in from command
line and read from wallet.
Also fixes #741 - help message refers to correct
scriptpubkey types.
master
Adam Gibson 5 years ago
parent
commit
f21c905128
No known key found for this signature in database
GPG Key ID: 141001A1AF77F20B
  1. 11
      jmclient/jmclient/commitment_utils.py
  2. 23
      scripts/add-utxo.py

11
jmclient/jmclient/commitment_utils.py

@ -9,15 +9,17 @@ def quit(parser, errmsg): #pragma: no cover
parser.error(errmsg)
sys.exit(EXIT_FAILURE)
def get_utxo_info(upriv):
def get_utxo_info(upriv, utxo_binary=False):
"""Verify that the input string parses correctly as (utxo, priv)
and return that.
and return that. If `utxo_binary` is true, the first element of
that return tuple is the standard internal form
(txid-in-binary, index-as-int).
"""
try:
u, priv = upriv.split(',')
u = u.strip()
priv = priv.strip()
success, utxo = utxostr_to_utxo(u)
success, utxo_bin = utxostr_to_utxo(u)
assert success, utxo
except:
#not sending data to stdout in case privkey info
@ -30,7 +32,8 @@ def get_utxo_info(upriv):
except:
jmprint("failed to parse privkey, make sure it's WIF compressed format.", "error")
raise
return u, priv
utxo_to_return = utxo_bin if utxo_binary else u
return utxo_to_return, priv
def validate_utxo_data(utxo_datas, retrieve=False, utxo_address_type="p2wpkh"):
"""For each (utxo, privkey), first

23
scripts/add-utxo.py

@ -74,7 +74,8 @@ def main():
"BE CAREFUL about handling private keys! "
"Don't do this in insecure environments. "
"Also note this ONLY works for standard (p2pkh or p2sh-p2wpkh) utxos."
"Also note this ONLY works for standard p2wpkh (native segwit) "
"or p2sh-p2wpkh (nested segwit) utxos."
)
add_base_options(parser)
parser.add_option(
@ -193,10 +194,10 @@ def main():
for ul in utxo_info:
ul = ul.rstrip()
if ul:
u, priv = get_utxo_info(ul)
u, priv = get_utxo_info(ul.decode("utf-8"), utxo_binary=True)
if not u:
quit(parser, "Failed to parse utxo info: " + str(ul))
utxo_data.append((utxostr_to_utxo(u), priv))
utxo_data.append((u, priv))
elif options.in_json:
if not os.path.isfile(options.in_json):
jmprint("File: " + options.in_json + " not found.", "error")
@ -208,15 +209,19 @@ def main():
jmprint("Failed to read json from " + options.in_json, "error")
sys.exit(EXIT_FAILURE)
for u, pva in iteritems(utxo_json):
utxo_data.append((utxostr_to_utxo(u), pva['privkey']))
utxobin, priv = get_utxo_info(",".join([u, pva["privkey"]]),
utxo_binary=True)
if not utxobin:
quit(parser, "Failed to load utxo from json: " + str(u))
utxo_data.append((utxobin, priv))
elif len(args) == 1:
u = args[0]
ul = args[0]
priv = input(
'input private key for ' + u + ', in WIF compressed format : ')
u, priv = get_utxo_info(','.join([u, priv]))
'input private key for ' + ul + ', in WIF compressed format : ')
u, priv = get_utxo_info(','.join([ul, priv]), utxo_binary=True)
if not u:
quit(parser, "Failed to parse utxo info: " + u)
utxo_data.append((utxostr_to_utxo(u), priv))
quit(parser, "Failed to parse utxo info: " + ul)
utxo_data.append((u, priv))
else:
quit(parser, 'Invalid syntax')
if options.validate or options.vonly:

Loading…
Cancel
Save