MCPcopy Index your code
hub / github.com/jimmysong/programmingbitcoin / address

Method address

code-ch13/script.py:278–290  ·  view source on GitHub ↗

Returns the address corresponding to the script

(self, testnet=False)

Source from the content-addressed store, hash-verified

276 # end::source5[]
277
278 def address(self, testnet=False):
279 '''Returns the address corresponding to the script'''
280 if self.is_p2pkh_script_pubkey(): # p2pkh
281 # hash160 is the 3rd cmd
282 h160 = self.cmds[2]
283 # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
284 return h160_to_p2pkh_address(h160, testnet)
285 elif self.is_p2sh_script_pubkey(): # p2sh
286 # hash160 is the 2nd cmd
287 h160 = self.cmds[1]
288 # convert to p2sh address using h160_to_p2sh_address (remember testnet)
289 return h160_to_p2sh_address(h160, testnet)
290 raise ValueError('Unknown ScriptPubKey')
291
292
293class ScriptTest(TestCase):

Callers 1

test_addressMethod · 0.45

Calls 4

is_p2sh_script_pubkeyMethod · 0.95
h160_to_p2pkh_addressFunction · 0.90
h160_to_p2sh_addressFunction · 0.90

Tested by 1

test_addressMethod · 0.36