Exemple #1
0
 def verify_input(self, input_index):
     # '''Returns whether the input has a valid signature'''
     # get the relevant input
     tx_in = self.tx_ins[input_index]
     # grab the previous ScriptPubKey
     script_pubkey = tx_in.script_pubkey(testnet=self.testnet)
     # check to see if the ScriptPubkey is a p2sh
     if script_pubkey.is_p2sh_script_pubkey():
         # the last cmd has to be the RedeemScript to trigger
         cmd = tx_in.script_sig.cmds[-1]
         # parse the RedeemScript
         raw_redeem = int_to_little_endian(len(cmd), 1) + cmd
         redeem_script = Script.parse(BytesIO(raw_redeem))
         # the RedeemScript might be p2wpkh or p2wsh
         if redeem_script.is_p2wpkh_script_pubkey():
             z = self.sig_hash_bip143(input_index, redeem_script)
             witness = tx_in.witness
         elif redeem_script.is_p2wsh_script_pubkey():
             cmd = tx_in.witness[-1]
             raw_witness = encode_varint(len(cmd)) + cmd
             witness_script = Script.parse(BytesIO(raw_witness))
             z = self.sig_hash_bip143(input_index,
                                      witness_script=witness_script)
             witness = tx_in.witness
         else:
             z = self.sig_hash(input_index, redeem_script)
             witness = None
     else:
         # ScriptPubkey might be a p2wpkh or p2wsh
         if script_pubkey.is_p2wpkh_script_pubkey():
             z = self.sig_hash_bip143(input_index)
             witness = tx_in.witness
         elif script_pubkey.is_p2wsh_script_pubkey():
             cmd = tx_in.witness[-1]
             raw_witness = encode_varint(len(cmd)) + cmd
             witness_script = Script.parse(BytesIO(raw_witness))
             z = self.sig_hash_bip143(input_index,
                                      witness_script=witness_script)
             witness = tx_in.witness
         else:
             z = self.sig_hash(input_index)
             witness = None
     # combine the current ScriptSig and the previous ScriptPubKey
     combined = tx_in.script_sig + script_pubkey
     # evaluate the combined script
     return combined.evaluate(z, witness)
Exemple #2
0
    def parse(cls, s):
        #Takes a byte stream and parses the tx_output at the start
        #return a TxOut object

        # amount is an integer in 8 bytes, little endian
        amount = little_endian_to_int(s.read(8))
        # use Script.parse to get the ScriptPubKey
        script_pubkey = Script.parse(s)
        # return an instance of the class (see __init__ for args)
        return cls(amount, script_pubkey)
Exemple #3
0
    def parse(cls, s):
        # Takes a byte stream and parses the tx_input at the start. return a TxIn object

        # prev_tx is 32 bytes, little endian
        prev_tx = s.read(32)[::-1]
        # prev_index is an integer in 4 bytes, little endian
        prev_index = little_endian_to_int(s.read(4))
        # use Script.parse to get the ScriptSig
        script_sig = Script.parse(s)
        # sequence is an integer in 4 bytes, little-endian
        sequence = little_endian_to_int(s.read(4))
        # return an instance of the class (see __init__ for args)
        return cls(prev_tx, prev_index, script_sig, sequence)
Exemple #4
0
 def parse(cls, s):
     '''Takes a byte stream and parses the tx_input at the start
     return a TxIn object
     '''
     # prev_tx is 32 bytes, little endian
     # prev_index is an integer in 4 bytes, little endian
     # use Script.parse to get the ScriptSig
     # sequence is an integer in 4 bytes, little-endian
     # return an instance of the class (see __init__ for args)
     # raise NotImplementedError
     prev_tx = s.read(32)[::-1]
     prev_index = little_endian_to_int(s.read(4))
     script_sig = Script.parse(s)
     sequence = little_endian_to_int(s.read(4))
     return cls(prev_tx, prev_index, script_sig, sequence)