Bitcoin Forum
June 06, 2024, 03:22:10 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: python scriptpubkey OP_CHECKMULTISIG to address  (Read 72 times)
linuxqq (OP)
Jr. Member
*
Offline Offline

Activity: 34
Merit: 6


View Profile
May 17, 2021, 06:28:24 AM
Last edit: May 17, 2021, 08:09:35 AM by linuxqq
 #1

Hello everyone, I am new to Bitcoin, I have a problem and need help, first of all, thank you, the following is my question:
I tried to use the following code to convert the scriptPubKey in the Bitcoin transaction to an address, but why p2sh cannot be converted? Is there a similar python package that can be converted to an address by drinking it directly

Code:
def cover_address(script_pub_key):
    script_pub_key_str=hashStr(script_pub_key)
    if script_pub_key_str[0:4]==b'76a9':
        #This is a pubkeyhash
        bytes=script_pub_key[2]  #number of bytes in the pub_key
        try:
            assert bytes==20
        except:
            return None
        public_key = script_pub_key[3:3+bytes] #20 bytes
        z=b'\00'+public_key
        #checksum=sha256(sha256(z))[:4]
        #address1=base58.b58encode(z + checksum)
        address2=base58.b58encode_check(z)   # adds checksum for you
        return format(address2.decode())
    elif script_pub_key_str[0:2]==b'a9':
        #this is a scripthash (pay-to-script address)
        bytes=script_pub_key[1]  #number of bytes in the pub_key
        try:
            assert bytes==20
        except:
            return None
        public_key = script_pub_key[2:2+bytes] #20 bytes
        z=b'\05'+public_key  #used for mainnet
        address2=base58.b58encode_check(z)
        return address2.decode()
    elif script_pub_key_str[0:2]==b'00':
        spk = binascii.unhexlify(script_pub_key_str.decode())
        version = spk[0] - 0x50 if spk[0] else 0
        program = spk[2:]
        return bech32.encode('bc', version, program)
    else:
        #print(script_pub_key_str)
        return None
I want to convert the following into an address, but the address cannot be resolved using the above method. What is wrong?

Code:
{'txid': 'ca0cae6cd13e9150757612b1a3ea2e0e2d990d31098252892d032ffa73fc4f83', 'hash': 'ca0cae6cd13e9150757612b1a3ea2e0e2d990d31098252892d032ffa73fc4f83', 'version': 1, 'size': 205, 'vsize': 205, 'weight': 820, 'locktime': 0, 'vin': [{'txid': '05a9f3f1ed8099af7f37f818ce92442a1dccf9b471375bcb7dd4771db8080361', 'vout': 0, 'scriptSig': {'asm': '0 3045022041a325a85809d9b0c14b7858d8a7169f6dd847d41dbf342bfb745d76d15699c1022100cd6e77a8c3ffb95372d5ea716e0522efe121f253cf2381fd1d004204a8feeee7[ALL]', 'hex': '00483045022041a325a85809d9b0c14b7858d8a7169f6dd847d41dbf342bfb745d76d15699c1022100cd6e77a8c3ffb95372d5ea716e0522efe121f253cf2381fd1d004204a8feeee701'}, 'sequence': 4294967295}], 'vout': [{'value': 1.999, 'n': 0, 'scriptPubKey': {'asm': '1 037953dbf08030f67352134992643d033417eaa6fcfb770c038f364ff40d761588 0002d4a196263b56e24c40a87f4f5adc89b74a17712cc540c20135fc50a2403937 2 OP_CHECKMULTISIG', 'hex': '5121037953dbf08030f67352134992643d033417eaa6fcfb770c038f364ff40d761588210002d4a196263b56e24c40a87f4f5adc89b74a17712cc540c20135fc50a240393752ae', 'type': 'nonstandard'}}], 'hex': '0100000001610308b81d77d47dcb5b3771b4f9cc1d2a4492ce18f8377faf9980edf1f3a905000000004a00483045022041a325a85809d9b0c14b7858d8a7169f6dd847d41dbf342bfb745d76d15699c1022100cd6e77a8c3ffb95372d5ea716e0522efe121f253cf2381fd1d004204a8feeee701ffffffff01603bea0b00000000475121037953dbf08030f67352134992643d033417eaa6fcfb770c038f364ff40d761588210002d4a196263b56e24c40a87f4f5adc89b74a17712cc540c20135fc50a240393752ae00000000', 'blockhash': '00000000000002b211876fa753c8bf5c3b4f43198c6a20d91b26cd1ed5b71918', 'confirmations': 471187, 'time': 1355895514, 'blocktime': 1355895514}
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!