Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.5 kB
1
Indexable
Never
from asyncio import run_coroutine_threadsafe
from bitcoinutils.setup import setup
from bitcoinutils.keys import P2shAddress, PublicKey, Address
from bitcoinutils.script import Script
import hashlib, base58, binascii

# Setup the network
setup('testnet')

# This script ask the user 3 Public Keys in order to create a P2SH address with a 2-of-3 multisig

# Collect the 3 public keys
pkey1 = "027822baac734226f2bdecf7b483531e83c3400fcdbad6726b91a5cb9788260b7e"
pkey2 = "03c86fef055997c3b6f1b151bb2d6e3ad18c49d70b6c44c7704c952baf021ba746"
pkey3 = "03d0d743102243a76d4efc12b808c1cc78aa1ebefe108ea3398d001808fb5dd251"
print("You succesfully entered your public keys.")

def main():
    
    # CREATE THE REDEEM SCRIPT
    redeem_script = Script(['OP2', key1, key2, key3, 'OP3', 'OP_CHECKMULTISIG'])
    print(redeem_script)

    
    # HASH OF THE REDEEM SCRIPT
    hash = Script(['OP_HASH160', redeem_script])
    print(hash)
    # Possible alternative? hash = redeem_script._script_to_hash160()

    # hash_hex = hash.to_hex() # Why is this not working? 
    # print(hash_hex)
    

    # CREATE THE LOCKING SCRIPT
    locking_script = Script(['OP_HASH160', hash, 'OP_EQUAL'])
    print(locking_script)
    # Possible alternative? locking_script = redeem_script.to_p2sh_script_pub_key()

    
    # GENERATE THE P2SH ADDRESS
    address = Address()
    address = address.from_script(hash)
    print('The 2-of-3 multisig address is: ' + address)

    # Possible alternative? use hashlib -> too much code :/

main()