new IPPort class

This commit is contained in:
The MMGen Project 2021-06-21 17:25:38 +00:00
commit 0fec0b2f3b
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 56 additions and 1 deletions

View file

@ -20,7 +20,7 @@
obj.py: MMGen native classes
"""
import sys,os,unicodedata
import sys,os,re,unicodedata
from decimal import *
from string import hexdigits,ascii_letters,digits
@ -913,6 +913,33 @@ class SeedSplitSpecifier(str,Hilite,InitErrors,MMGenObject):
class SeedSplitIDString(MMGenPWIDString):
desc = 'seed split ID string'
class IPPort(str,Hilite,InitErrors,MMGenObject):
color = 'yellow'
width = 0
trunc_ok = False
min_len = 9 # 0.0.0.0:0
max_len = 21 # 255.255.255.255:65535
def __new__(cls,s):
if type(s) == cls:
return s
try:
m = re.fullmatch('{q}\.{q}\.{q}\.{q}:(\d{{1,10}})'.format(q=r'([0-9]{1,3})'),s)
assert m is not None, f'{s!r}: invalid IP:HOST specifier'
for e in m.groups():
if len(e) != 1 and e[0] == '0':
die(2,f'{e}: leading zeroes not permitted in dotted decimal element or port number')
res = [int(e) for e in m.groups()]
for e in res[:4]:
assert e <= 255, f'{e}: dotted decimal element > 255'
assert res[4] <= 65535, f'{res[4]}: port number > 65535'
me = str.__new__(cls,s)
me.ip = '{}.{}.{}.{}'.format(*res)
me.ip_num = sum( res[i] * ( 2 ** (-(i-3)*8) ) for i in range(4) )
me.port = res[4]
return me
except Exception as e:
return cls.init_fail(e,s)
from collections import namedtuple
ati = namedtuple('addrtype_info',
['name','pubkey_type','compressed','gen_method','addr_fmt','wif_label','extra_attrs','desc'])

View file

@ -330,4 +330,32 @@ tests = {
('1:2','2:2','alice:2:2','αβ:2:2','1:'+ssm,ssm+':'+ssm)
)
},
'IPPort': {
'arg1': 's',
'bad': (
'.0.0.0:0',
'127.0.0.1',
'127.0.0.1:',
'20',
':20',
'127.0.0.256:0',
'127.0.0.1:65536',
'127.0.0.01:60000',
'0.0.0.0:00',
),
'good': (
(
'0.0.0.0:0',
'0.0.0.1:0',
'0.0.1.0:0',
'0.0.1.1:0',
'0.1.0.0:0',
'1.0.0.0:0',
'10.0.0.1:0',
'127.0.0.1:65535',
'255.0.0.0:400',
'255.255.255.255:65535',
)
)
},
}