Browse Source

mmgen-tool: add ‘hextob58chk’ and ‘b58chktohex’ cmds

MMGen 6 years ago
parent
commit
610d0dd243
3 changed files with 11 additions and 0 deletions
  1. 2 0
      mmgen/main_tool.py
  2. 1 0
      mmgen/protocol.py
  3. 8 0
      mmgen/tool.py

+ 2 - 0
mmgen/main_tool.py

@@ -72,7 +72,9 @@ General utilities:
   b58tostr     - convert a base 58 number to a string
   b58tostr     - convert a base 58 number to a string
   strtob58     - convert a string to base 58
   strtob58     - convert a string to base 58
   b58tohex     - convert a base 58 number to hexadecimal
   b58tohex     - convert a base 58 number to hexadecimal
+  b58chktohex  - convert a base58-check encoded number to hexadecimal
   hextob58     - convert a hexadecimal number to base 58
   hextob58     - convert a hexadecimal number to base 58
+  hextob58chk  - convert a hexadecimal number to base58-check encoding
   b32tohex     - convert a base 32 number to hexadecimal
   b32tohex     - convert a base 32 number to hexadecimal
   hextob32     - convert a hexadecimal number to base 32
   hextob32     - convert a hexadecimal number to base 32
 
 

+ 1 - 0
mmgen/protocol.py

@@ -56,6 +56,7 @@ def _b58chk_encode(hexstr):
 
 
 def _b58chk_decode(s):
 def _b58chk_decode(s):
 	hexstr = '{:x}'.format(_b58tonum(s))
 	hexstr = '{:x}'.format(_b58tonum(s))
+	if len(hexstr) % 2: hexstr = '0' + hexstr
 	if hexstr[-8:] == hash256(hexstr[:-8])[:8]:
 	if hexstr[-8:] == hash256(hexstr[:-8])[:8]:
 		return hexstr[:-8]
 		return hexstr[:-8]
 	raise ValueError,'_b58chk_decode(): checksum incorrect'
 	raise ValueError,'_b58chk_decode(): checksum incorrect'

+ 8 - 0
mmgen/tool.py

@@ -38,7 +38,9 @@ cmd_data = OrderedDict([
 	('Strtob58',     ['<string> [str-]','pad [int=0]']),
 	('Strtob58',     ['<string> [str-]','pad [int=0]']),
 	('B58tostr',     ['<b58 number> [str-]']),
 	('B58tostr',     ['<b58 number> [str-]']),
 	('Hextob58',     ['<hex number> [str-]','pad [int=0]']),
 	('Hextob58',     ['<hex number> [str-]','pad [int=0]']),
+	('Hextob58chk',  ['<hex number> [str-]']),
 	('B58tohex',     ['<b58 number> [str-]','pad [int=0]']),
 	('B58tohex',     ['<b58 number> [str-]','pad [int=0]']),
+	('B58chktohex',  ['<b58 number> [str-]']),
 	('B58randenc',   []),
 	('B58randenc',   []),
 	('B32tohex',     ['<b32 num> [str-]','pad [int=0]']),
 	('B32tohex',     ['<b32 num> [str-]','pad [int=0]']),
 	('Hextob32',     ['<hex num> [str-]','pad [int=0]']),
 	('Hextob32',     ['<hex num> [str-]','pad [int=0]']),
@@ -327,9 +329,15 @@ def Mn2hex(s,wordlist=dfl_wl_id): Msg(baseconv.tohex(s.split(),wordlist))
 
 
 def Strtob58(s,pad=None): Msg(baseconv.fromhex(binascii.hexlify(s),'b58',pad,tostr=True))
 def Strtob58(s,pad=None): Msg(baseconv.fromhex(binascii.hexlify(s),'b58',pad,tostr=True))
 def Hextob58(s,pad=None): Msg(baseconv.fromhex(s,'b58',pad,tostr=True))
 def Hextob58(s,pad=None): Msg(baseconv.fromhex(s,'b58',pad,tostr=True))
+def Hextob58chk(s):
+	from mmgen.protocol import _b58chk_encode
+	Msg(_b58chk_encode(s))
 def Hextob32(s,pad=None): Msg(baseconv.fromhex(s,'b32',pad,tostr=True))
 def Hextob32(s,pad=None): Msg(baseconv.fromhex(s,'b32',pad,tostr=True))
 def B58tostr(s):          Msg(binascii.unhexlify(baseconv.tohex(s,'b58')))
 def B58tostr(s):          Msg(binascii.unhexlify(baseconv.tohex(s,'b58')))
 def B58tohex(s,pad=None): Msg(baseconv.tohex(s,'b58',pad))
 def B58tohex(s,pad=None): Msg(baseconv.tohex(s,'b58',pad))
+def B58chktohex(s):
+	from mmgen.protocol import _b58chk_decode
+	Msg(_b58chk_decode(s))
 def B32tohex(s,pad=None): Msg(baseconv.tohex(s.upper(),'b32',pad))
 def B32tohex(s,pad=None): Msg(baseconv.tohex(s.upper(),'b32',pad))
 
 
 from mmgen.seed import Mnemonic
 from mmgen.seed import Mnemonic