jimmysong / programmingbitcoin

Repository for the book
Other
1.75k stars 656 forks source link

invalid code snippet in examples chapter 12 example 5 #186

Closed scgbckbone closed 4 years ago

scgbckbone commented 4 years ago
while not found:
     message = node.wait_for(MerkleBlock, Tx)  # <8>
     if message.command == b'merkleblock':
         if not message.is_valid():  # <9>
             raise RuntimeError('invalid merkle proof')
     else:
         for i, tx_out in enumerate(message.tx_outs):
             if tx_out.script_pubkey.address(testnet=True) == address:  # <10>
                 print('found: {}:{}'.format(message.id(), i))
                 found = True
                 break

Above code snippet raises Attribute error as TxOut.script_pubkey has no attribute address. You probably wanted to do something like this:

while not found:
    message = node.wait_for(MerkleBlock, Tx)
    if message.command == b"merkleblock":
        if not message.is_valid():
            raise RuntimeError("invalid merkle proof")
    else:
        for i, tx_out in enumerate(message.tx_outs):
            # if we have p2pkh script we take h160 from index 2 and create address
            if tx_out.script_pubkey.is_p2pkh_script_pubkey():
                h160 = tx_out.script_pubkey.cmds[2]
                addr = h160_to_p2pkh_address(h160=h160, testnet=True)
            # if we have p2sh script we take h160 from index 1 and create address
            elif tx_out.script_pubkey.is_p2sh_script_pubkey():
                h160 = tx_out.script_pubkey.cmds[1]
                addr = h160_to_p2sh_address(h160=h160, testnet=True)
            if addr == address:
                print("found: {}:{}".format(message.id(), i))
                found = True
                break
scgbckbone commented 4 years ago

ok, I found your Script.address method in chapter 13...