Skip to content

Commit 12d94b6

Browse files
committed
Correct accidentally ignored inital record by slip39-generator --receive
1 parent 6b7457d commit 12d94b6

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

README.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ by entering the mnemonics right on the device.
544544
#+END_SRC
545545

546546
#+RESULTS:
547+
: 0: [["ETH", "m/44'/60'/0'/0/0", "0xfc2077CA7F403cBECA41B1B0F62D91B5EA631B5E"], ["BTC", "m/84'/0'/0'/0/0", "bc1qk0a9hr7wjfxeenz9nwenw9flhq0tmsf6vsgnn2"]]
547548
: 1: [["ETH", "m/44'/60'/0'/0/1", "0xd1a7451beB6FE0326b4B78e3909310880B781d66"], ["BTC", "m/84'/0'/0'/0/1", "bc1qkd33yck74lg0kaq4tdcmu3hk4yruhjayxpe9ug"]]
548549
: 2: [["ETH", "m/44'/60'/0'/0/2", "0x578270B5E5B53336baC354756b763b309eCA90Ef"], ["BTC", "m/84'/0'/0'/0/2", "bc1qvr7e5aytd0hpmtaz2d443k364hprvqpm3lxr8w"]]
549550
: 3: [["ETH", "m/44'/60'/0'/0/3", "0x909f59835A5a120EafE1c60742485b7ff0e305da"], ["BTC", "m/84'/0'/0'/0/3", "bc1q6t9vhestkcfgw4nutnm8y2z49n30uhc0kyjl0d"]]

slip39/generator/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,23 @@ def accountgroups_input(
9696
if not record:
9797
continue
9898

99-
# See if records are indexed. Only int or 'nonce' is accepted for index.
99+
# See if records are indexed. Only int or 'nonce' is accepted for index. Ignore any
100+
# (strange) empty '<index>: ' records (anything possible w/ corruption on a serial link...)
100101
index = None
102+
payload = ''
101103
try:
102104
index,payload = record.split( ':', 1 ) # ValueError if not <index>:<payload>
103105
index = int( index ) # ValueError if <index> not int
104106
except ValueError:
105107
if index != 'nonce': # Otherwise, only 'nonce': <payload> acceptable
106108
index,payload = None,record # ...if not; then records are not indexed.
107109

110+
payload = payload.strip()
111+
log.debug( f"Detect record {index:>5}: {payload!r}" )
112+
if not payload:
113+
continue
114+
115+
# Non-empty payload; process as either encrypted or raw JSON.
108116
try:
109117
if cipher:
110118
if nonce is None or index == 'nonce':
@@ -113,7 +121,7 @@ def accountgroups_input(
113121
try:
114122
assert index == 'nonce', \
115123
f"Failed to find 'nonce' enumeration prefix on first record: {record!r}"
116-
ciphertext = bytearray( codecs.decode( payload.strip(), 'hex_codec' ))
124+
ciphertext = bytearray( codecs.decode( payload, 'hex_codec' ))
117125
nonce = bytes( cipher.decrypt( b'\x00' * 12, ciphertext ))
118126
except Exception as exc:
119127
message = f"Failed to recover nonce from {record!r}; cannot proceed: {exc}"
@@ -122,6 +130,7 @@ def accountgroups_input(
122130
log.info( f"Decrypting accountgroups with nonce: {nonce.hex()}" )
123131
continue
124132

133+
log.debug( f"Decrypt index {index:>5}: {payload!r}" )
125134
nonce_now = nonce_add( nonce, index )
126135
ciphertext = bytearray( bytes.fromhex( payload ))
127136
plaintext = bytes( cipher.decrypt( nonce_now, ciphertext ))
@@ -131,6 +140,7 @@ def accountgroups_input(
131140
log.warning( f"Discarding invalid record {record!r}: {exc!r}" )
132141
yield None, None
133142
else:
143+
log.debug( f"Decoded index {index:>5}: {group!r}" )
134144
yield int(index), group
135145

136146

slip39/generator/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def healthy_reset( file ):
234234
encoding = encoding,
235235
healthy = healthy,
236236
):
237-
if index and group:
237+
if index is not None and group:
238238
accountgroups_output( group, index=index )
239239
continue
240240
if healthy and healthy( file ):

0 commit comments

Comments
 (0)