From ab5e56ca045b153ad6f83a8ea462d26c4bead997 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 4 Oct 2022 13:08:55 +0000 Subject: [PATCH] ViewKey: relocate protocol-specific code --- mmgen/addr.py | 18 +++--------------- mmgen/data/version | 2 +- mmgen/proto/xmr/addrgen.py | 4 ++-- mmgen/proto/xmr/params.py | 7 +++++++ mmgen/proto/zec/addrgen.py | 10 +++++----- mmgen/proto/zec/params.py | 7 +++++++ mmgen/protocol.py | 3 +++ 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 7a34e746..5edc5202 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -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) diff --git a/mmgen/data/version b/mmgen/data/version index f81c04d5..fcb18719 100644 --- a/mmgen/data/version +++ b/mmgen/data/version @@ -1 +1 @@ -13.3.dev1 +13.3.dev2 diff --git a/mmgen/proto/xmr/addrgen.py b/mmgen/proto/xmr/addrgen.py index ccea038e..3bb297f4 100755 --- a/mmgen/proto/xmr/addrgen.py +++ b/mmgen/proto/xmr/addrgen.py @@ -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() ) diff --git a/mmgen/proto/xmr/params.py b/mmgen/proto/xmr/params.py index dbda29af..92942c82 100755 --- a/mmgen/proto/xmr/params.py +++ b/mmgen/proto/xmr/params.py @@ -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'} diff --git a/mmgen/proto/zec/addrgen.py b/mmgen/proto/zec/addrgen.py index 680e510a..57cae3ad 100755 --- a/mmgen/proto/zec/addrgen.py +++ b/mmgen/proto/zec/addrgen.py @@ -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 ) ) diff --git a/mmgen/proto/zec/params.py b/mmgen/proto/zec/params.py index 8e0c0a9b..695135a3 100755 --- a/mmgen/proto/zec/params.py +++ b/mmgen/proto/zec/params.py @@ -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' } diff --git a/mmgen/protocol.py b/mmgen/protocol.py index cb9e0eed..4aa6774d 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -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