Skip to content

Commit 8ca3c13

Browse files
authored
Merge pull request #36 from billygarrison/2021-03-07-send-test-tx
change _prepare_send to display info in coldcard format
2 parents 504deb2 + 6047868 commit 8ca3c13

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

coldcore

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ def run_setup(config, controller) -> t.Tuple[t.Any, t.Any]:
11801180

11811181
info("great - now let's test your ability to send")
11821182
info(
1183-
"we're going to send 95% of the value of the last UTXO over "
1183+
"we're going to send 90% of the value of the last UTXO over "
11841184
"to a new address:"
11851185
)
11861186
sendtoaddr = rpcw.getnewaddress()
@@ -1190,7 +1190,7 @@ def run_setup(config, controller) -> t.Tuple[t.Any, t.Any]:
11901190

11911191
# Send 90% of the value over.
11921192
# TODO this is only for testing and is potentially dangerous
1193-
send_amt = str((got_utxo.amount * 9) / 10)
1193+
send_amt = str(round(((got_utxo.amount * 9) / 10), 8))
11941194
prepared_tx = controller.prepare_send(
11951195
config,
11961196
rpcw,
@@ -2861,10 +2861,58 @@ def _prepare_send(
28612861
num_inputs = len(info["inputs"])
28622862
num_outputs = len(info["outputs"])
28632863

2864+
# Get info from psbt
2865+
psbtinfo = info
2866+
tx = info["tx"]
2867+
outs = tx["vout"]
2868+
total_in_amt = 0.0
2869+
total_out_amt = 0.0
2870+
change = 0.0
2871+
2872+
# Add up total input amount
2873+
for i in psbtinfo["inputs"]:
2874+
# TODO does this mean we only support segwit transactions?
2875+
wit = i["witness_utxo"]
2876+
amt = float(wit["amount"])
2877+
total_in_amt = total_in_amt + amt
2878+
2879+
# Add up total output amount
2880+
for o in outs:
2881+
addr = o['scriptPubKey']['addresses'][0]
2882+
try:
2883+
addr_info = rpcw.getaddressinfo(addr)
2884+
except Exception:
2885+
# TODO handle this
2886+
raise
2887+
amt = float(o["value"])
2888+
total_out_amt = total_out_amt + amt
2889+
yours = addr_info["ismine"] or addr_info["iswatchonly"]
2890+
if yours:
2891+
change = change + amt
2892+
28642893
fee = result["fee"]
28652894
perc = (fee / Decimal(amount)) * 100
2895+
2896+
F.info(f"total output amount: {total_out_amt} BTC")
28662897
F.info(f"{num_inputs} inputs, {num_outputs} outputs")
2867-
F.info(f"fee: {result['fee']} BTC ({perc:.2f}% of amount)")
2898+
F.info(f"network fee: {result['fee']} BTC ({perc:.2f}% of amount)")
2899+
F.info(f"change back: {change} BTC")
2900+
F.info("outputs:")
2901+
2902+
# Display outputs
2903+
for o in outs:
2904+
addr = o['scriptPubKey']['addresses'][0]
2905+
try:
2906+
addr_info = rpcw.getaddressinfo(addr)
2907+
except Exception:
2908+
# TODO handle this
2909+
raise
2910+
display_amt = bold(green(f"{o['value']} BTC"))
2911+
yours = addr_info["ismine"] or addr_info["iswatchonly"]
2912+
yours_str = " (your address)" if yours else ""
2913+
F.blank(f" -> {bold(addr)} ({display_amt}){yours_str}")
2914+
2915+
F.p()
28682916
F.done(f"wrote PSBT to {filename} - sign with coldcard")
28692917

28702918
return filename

src/coldcore/main.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,10 +935,58 @@ def _prepare_send(
935935
num_inputs = len(info["inputs"])
936936
num_outputs = len(info["outputs"])
937937

938+
# Get info from psbt
939+
psbtinfo = info
940+
tx = info["tx"]
941+
outs = tx["vout"]
942+
total_in_amt = 0.0
943+
total_out_amt = 0.0
944+
change = 0.0
945+
946+
# Add up total input amount
947+
for i in psbtinfo["inputs"]:
948+
# TODO does this mean we only support segwit transactions?
949+
wit = i["witness_utxo"]
950+
amt = float(wit["amount"])
951+
total_in_amt = total_in_amt + amt
952+
953+
# Add up total output amount
954+
for o in outs:
955+
addr = o['scriptPubKey']['addresses'][0]
956+
try:
957+
addr_info = rpcw.getaddressinfo(addr)
958+
except Exception:
959+
# TODO handle this
960+
raise
961+
amt = float(o["value"])
962+
total_out_amt = total_out_amt + amt
963+
yours = addr_info["ismine"] or addr_info["iswatchonly"]
964+
if yours:
965+
change = change + amt
966+
938967
fee = result["fee"]
939968
perc = (fee / Decimal(amount)) * 100
969+
970+
F.info(f"total output amount: {total_out_amt} BTC")
940971
F.info(f"{num_inputs} inputs, {num_outputs} outputs")
941-
F.info(f"fee: {result['fee']} BTC ({perc:.2f}% of amount)")
972+
F.info(f"network fee: {result['fee']} BTC ({perc:.2f}% of amount)")
973+
F.info(f"change back: {change} BTC")
974+
F.info("outputs:")
975+
976+
# Display outputs
977+
for o in outs:
978+
addr = o['scriptPubKey']['addresses'][0]
979+
try:
980+
addr_info = rpcw.getaddressinfo(addr)
981+
except Exception:
982+
# TODO handle this
983+
raise
984+
display_amt = bold(green(f"{o['value']} BTC"))
985+
yours = addr_info["ismine"] or addr_info["iswatchonly"]
986+
yours_str = " (your address)" if yours else ""
987+
F.blank(f" -> {bold(addr)} ({display_amt}){yours_str}")
988+
989+
F.p()
942990
F.done(f"wrote PSBT to {filename} - sign with coldcard")
943991

944992
return filename

src/coldcore/ui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def delay(t: float = 1.0):
458458

459459
info("great - now let's test your ability to send")
460460
info(
461-
"we're going to send 95% of the value of the last UTXO over "
461+
"we're going to send 90% of the value of the last UTXO over "
462462
"to a new address:"
463463
)
464464
sendtoaddr = rpcw.getnewaddress()
@@ -468,7 +468,7 @@ def delay(t: float = 1.0):
468468

469469
# Send 90% of the value over.
470470
# TODO this is only for testing and is potentially dangerous
471-
send_amt = str((got_utxo.amount * 9) / 10)
471+
send_amt = str(round(((got_utxo.amount * 9) / 10), 8))
472472
prepared_tx = controller.prepare_send(
473473
config,
474474
rpcw,

0 commit comments

Comments
 (0)