def getTxStateLockScript(T: int, delta: int, pubkey_pay_right: PublicKey, pubkey_mulsig_left: PublicKey, pubkey_mulsig_right: PublicKey) -> Script: """ tx_state lock script encodes possibilities to either refund locked coins to left or pay to right :param T: locked funds can pe paid after this time wherever right user wants :param delta: upper bound on time for transaction to be confirmed by the network :param pubkey_pay_right: public key owned by right user for payment after time T :param pubkey_mulsig_left: public key owned by left user for refund if enable-refund tx is published :param pubkey_mulsig_right: public key owned by right user for refund if enable-refund tx is published :return: tx_state lock script """ # signature script: # - for refund (with enable-refund tx + ∆): "OP_0 <left_signature> <right_signature>" # - for payment (time() >= T): "<signature_right> <pubkey_right> OP_0 OP_0 OP_0 OP_0 OP_0 OP_0" lock_script = Script([ 'OP_2', pubkey_mulsig_left.to_hex(), pubkey_mulsig_right.to_hex(), 'OP_2', 'OP_CHECKMULTISIG', 'OP_IF', delta, 'OP_CHECKSEQUENCEVERIFY', 'OP_DROP', 'OP_TRUE', # check if refund and lock for ∆ 'OP_ELSE', T, 'OP_CHECKLOCKTIMEVERIFY', 'OP_DROP', 'OP_DUP', 'OP_HASH160', pubkey_pay_right.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG', # check if payment 'OP_ENDIF' ]) return lock_script
def getEnableTxOutputLockScript(pubkey: PublicKey, rel_timelock: int) -> Script: """ Create lock script for output of enable-(payment/refund) transaction :param pubkey: public key owned by corresponding payment participant :param rel_timelock: relative lock on outputs. Should be same for all outputs of transaction :return: lock script """ seq = Sequence(TYPE_RELATIVE_TIMELOCK, rel_timelock) return Script([ seq.for_script(), 'OP_CHECKSEQUENCEVERIFY', 'OP_DROP', 'OP_DUP', 'OP_HASH160', pubkey.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG' ])
def create_p2sh(proxy, block_height=10, address_pubk=None): """ Creates a P2SH address with an absolute block locktime. :param proxy: JSON RPC proxy for connecting to the network. :param block_height: Block height the lock is valid for. Default value is 10. :param address_pubk: Public key of the address locking the funds. If None, a new address will be created. """ # if a public key is not specified, create a new address and display its keys for future use if not address_pubk: print('Public key not provided. Created a new address.') address = proxy.getnewaddress() print('Address:', address) address_privk = proxy.dumpprivkey(address) print('Private key:', address_privk) address_pubk = proxy.getaddressinfo(address)['pubkey'] print('Public key:', address_pubk) # create the public key object p2pkh_pk = PublicKey(address_pubk) # create sequence for the redeem script seq = Sequence(TYPE_ABSOLUTE_TIMELOCK, block_height) # create the redeem script redeem_script = Script([ seq.for_script(), 'OP_CHECKLOCKTIMEVERIFY', 'OP_DROP', 'OP_DUP', 'OP_HASH160', p2pkh_pk.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]) # create the P2SH address from the redeem script p2sh_addr = P2shAddress.from_script(redeem_script) # insert the P2SH address into the wallet proxy.importaddress(p2sh_addr.to_string()) # display the P2SH address print('Created P2SH address:', p2sh_addr.to_string())
def test_pubkey_to_hash160(self): pub = PublicKey(self.public_key_hex) self.assertEqual(pub.get_address().to_hash160(), pub.to_hash160())