diff --git a/mmgen/tool.py b/mmgen/tool.py index c91eb3b4..815b11bc 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -433,7 +433,18 @@ class MMGenToolCmdCoin(MMGenToolCmdBase): def addr2pubhash(self,addr:'sstr'): "convert coin address to public key hash" - return g.proto.verify_addr(addr,CoinAddr.hex_width,return_dict=True)['hex'] + from mmgen.tx import addr2pubhash + return addr2pubhash(CoinAddr(addr)) + + def addr2scriptpubkey(self,addr:'sstr'): + "convert coin address to scriptPubKey" + from mmgen.tx import addr2scriptPubKey + return addr2scriptPubKey(CoinAddr(addr)) + + def scriptpubkey2addr(self,hexstr:'sstr'): + "convert scriptPubKey to coin address" + from mmgen.tx import scriptPubKey2addr + return scriptPubKey2addr(hexstr)[0] class MMGenToolCmdMnemonic(MMGenToolCmdBase): """ diff --git a/mmgen/tx.py b/mmgen/tx.py index 35211f2f..14412917 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -96,6 +96,16 @@ def segwit_is_active(exit_on_error=False): else: return False +def addr2pubhash(addr): + return g.proto.verify_addr(addr,addr.hex_width,return_dict=True)['hex'] + +def addr2scriptPubKey(addr): + return { + 'p2pkh': '76a914' + addr2pubhash(addr) + '88ac', + 'p2sh': 'a914' + addr2pubhash(addr) + '87', + 'bech32': g.proto.witness_vernum_hex + '14' + addr2pubhash(addr) + }[addr.addr_fmt] + def scriptPubKey2addr(s): if len(s) == 50 and s[:6] == '76a914' and s[-4:] == '88ac': return g.proto.pubhash2addr(s[6:-4],p2sh=False),'p2pkh' diff --git a/test/tooltest2.py b/test/tooltest2.py index 41cee7e0..97037ffe 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -303,6 +303,20 @@ tests = { ['--type=bech32'], 'opt.type="bech32"' ), ], }, + 'addr2scriptpubkey': { + 'btc_mainnet': [ + ( ['12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'], '76a914118089d66b4a5853765e94923abdd5de4616c6e588ac' ), + ( ['3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'], 'a9148e34586186551f6320fa3eb2d238a9c61ab8264b87' ), + ( ['bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'], '00143057f66ddd26fa6ef826b0d5ca067ec3e8f3c178' ), + ], + }, + 'scriptpubkey2addr': { + 'btc_mainnet': [ + ( ['76a914118089d66b4a5853765e94923abdd5de4616c6e588ac'], '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm' ), + ( ['a9148e34586186551f6320fa3eb2d238a9c61ab8264b87'], '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn' ), + ( ['00143057f66ddd26fa6ef826b0d5ca067ec3e8f3c178'], 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms' ), + ], + }, 'hex2wif': { 'btc_mainnet': [ ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],