ViewKey: relocate protocol-specific code

This commit is contained in:
The MMGen Project 2022-10-04 13:08:55 +00:00
commit ab5e56ca04
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
7 changed files with 28 additions and 23 deletions

View file

@ -23,7 +23,7 @@ addr.py: MMGen address-related types
from collections import namedtuple
from .objmethods import Hilite,InitErrors,MMGenObject
from .obj import ImmutableAttr,MMGenIdx,HexStr,get_obj
from .obj import ImmutableAttr,MMGenIdx,get_obj
from .seed import SeedID
ati = namedtuple('addrtype_info',
@ -171,17 +171,5 @@ def is_coin_addr(proto,s):
class TokenAddr(CoinAddr):
color = 'blue'
class ViewKey(object):
def __new__(cls,proto,viewkey):
if proto.name == 'Zcash':
return ZcashViewKey.__new__(ZcashViewKey,proto,viewkey)
elif proto.name == 'Monero':
return MoneroViewKey.__new__(MoneroViewKey,viewkey)
else:
raise ValueError(f'{proto.name}: protocol does not support view keys')
class MoneroViewKey(HexStr):
color,width,hexcase = 'cyan',64,'lower' # FIXME - no checking performed
class ZcashViewKey(CoinAddr):
hex_width = 128
def ViewKey(proto,viewkey_str):
return proto.viewkey(viewkey_str)

View file

@ -1 +1 @@
13.3.dev1
13.3.dev2

View file

@ -13,7 +13,7 @@ proto.xmr.addrgen: Monero address generation class for the MMGen suite
"""
from ...addrgen import addr_generator,check_data
from ...addr import CoinAddr,MoneroViewKey
from ...addr import CoinAddr
class monero(addr_generator.keccak):
@ -34,4 +34,4 @@ class monero(addr_generator.keccak):
@check_data
def to_viewkey(self,data):
return MoneroViewKey( data.viewkey_bytes.hex() )
return self.proto.viewkey( data.viewkey_bytes.hex() )

View file

@ -15,9 +15,13 @@ Monero protocol
from collections import namedtuple
from ...protocol import CoinProtocol,_nw
from ...obj import HexStr
parsed_addr = namedtuple('parsed_addr',['ver_bytes','data','payment_id'])
class MoneroViewKey(HexStr):
color,width,hexcase = 'cyan',64,'lower' # FIXME - no checking performed
# https://github.com/monero-project/monero/blob/master/src/cryptonote_config.h
class mainnet(CoinProtocol.DummyWIF,CoinProtocol.Base):
@ -76,5 +80,8 @@ class mainnet(CoinProtocol.DummyWIF,CoinProtocol.Base):
def pubhash2addr(self,*args,**kwargs):
raise NotImplementedError('Monero addresses do not support pubhash2addr()')
def viewkey(self,viewkey_str):
return MoneroViewKey.__new__(MoneroViewKey,viewkey_str)
class testnet(mainnet): # use stagenet for testnet
addr_ver_info = { '18': 'monero', '24': 'monero_sub', '19': 'monero_integrated' } # testnet is {'35','3f','36'}

View file

@ -13,7 +13,7 @@ proto.zec.addrgen: Zcash-Z address generation class for the MMGen suite
"""
from ...addrgen import addr_generator,check_data
from ...addr import CoinAddr,ZcashViewKey
from ...addr import CoinAddr
from ..common import b58chk_encode
class zcash_z(addr_generator.base):
@ -27,7 +27,7 @@ class zcash_z(addr_generator.base):
@check_data
def to_viewkey(self,data):
ret = b58chk_encode(
self.proto.addr_fmt_to_ver_bytes['viewkey']
+ data.viewkey_bytes )
return ZcashViewKey( self.proto, ret )
return self.proto.viewkey(
b58chk_encode(
self.proto.addr_fmt_to_ver_bytes['viewkey']
+ data.viewkey_bytes ) )

View file

@ -15,6 +15,10 @@ Zcash protocol
from ..btc.params import mainnet
from ..common import b58chk_decode
from ...protocol import decoded_wif,decoded_addr
from ...addr import CoinAddr
class ZcashViewKey(CoinAddr):
hex_width = 128
class mainnet(mainnet):
base_coin = 'ZEC'
@ -71,6 +75,9 @@ class mainnet(mainnet):
else:
raise ValueError(f'{hash_len}: incorrect pubkey hash length')
def viewkey(self,viewkey_str):
return ZcashViewKey.__new__(ZcashViewKey,self,viewkey_str)
class testnet(mainnet):
wif_ver_num = { 'std': 'ef', 'zcash_z': 'ac08' }
addr_ver_info = { '1d25': 'p2pkh', '1cba': 'p2sh', '16b6': 'zcash_z', 'a8ac0c': 'viewkey' }

View file

@ -163,6 +163,9 @@ class CoinProtocol(MMGenObject):
from .addr import MMGenAddrType
return MMGenAddrType( proto=self, id_str=id_str )
def viewkey(self,viewkey_str):
raise NotImplementedError(f'{self.name} protocol does not support view keys')
class Secp256k1(Base):
"""
Bitcoin and Ethereum protocols inherit from this class