Browse Source

mmgen-tool: add addr2scriptpubkey, scriptpubkey2addr commands

MMGen 6 years ago
parent
commit
7c63bb1e3d
3 changed files with 36 additions and 1 deletions
  1. 12 1
      mmgen/tool.py
  2. 10 0
      mmgen/tx.py
  3. 14 0
      test/tooltest2.py

+ 12 - 1
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):
 	"""

+ 10 - 0
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'

+ 14 - 0
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'],