示例#1
0
 def hash_prevouts(self):
     if self._hash_prevouts is None:
         all_prevouts = b""
         all_sequence = b""
         for tx_in in self.tx_ins:
             all_prevouts += tx_in.prev_tx[::-1] + int_to_little_endian(
                 tx_in.prev_index, 4
             )
             all_sequence += int_to_little_endian(tx_in.sequence, 4)
         self._hash_prevouts = hash256(all_prevouts)
         self._hash_sequence = hash256(all_sequence)
     return self._hash_prevouts
示例#2
0
 def serialize(self):
     result = self.magic
     result += self.command + b"\x00" * (12 - len(self.command))
     result += int_to_little_endian(len(self.payload), 4)
     result += hash256(self.payload)[:4]
     result += self.payload
     return result
示例#3
0
 def hash_outputs(self):
     if self._hash_outputs is None:
         all_outputs = b""
         for tx_out in self.tx_outs:
             all_outputs += tx_out.serialize()
         self._hash_outputs = hash256(all_outputs)
     return self._hash_outputs
示例#4
0
 def parse(cls, s, testnet=False):
     magic = s.read(4)
     if magic == b"":
         raise IOError("Connection reset!")
     if testnet:
         expected_magic = TESTNET_NETWORK_MAGIC
     else:
         expected_magic = NETWORK_MAGIC
     if magic != expected_magic:
         raise SyntaxError("magic is not right {} vs {}".format(
             magic.hex(), expected_magic.hex()))
     command = s.read(12)
     command = command.strip(b"\x00")
     payload_length = little_endian_to_int(s.read(4))
     checksum = s.read(4)
     payload = s.read(payload_length)
     calculated_checksum = hash256(payload)[:4]
     if calculated_checksum != checksum:
         raise IOError("checksum does not match")
     return cls(command, payload, testnet=testnet)
示例#5
0
 def sig_hash_bip143(self, input_index, redeem_script=None, witness_script=None):
     tx_in = self.tx_ins[input_index]
     s = int_to_little_endian(self.version, 4)
     s += self.hash_prevouts() + self.hash_sequence()
     s += tx_in.prev_tx[::-1] + int_to_little_endian(tx_in.prev_index, 4)
     if witness_script:
         script_code = witness_script.serialize()
     elif redeem_script:
         script_code = p2pkh_script(redeem_script.cmds[1]).serialize()
     else:
         script_code = p2pkh_script(
             tx_in.script_pubkey(self.testnet).cmds[1]
         ).serialize()
     s += script_code
     s += int_to_little_endian(tx_in.value(), 8)
     s += int_to_little_endian(tx_in.sequence, 4)
     s += self.hash_outputs()
     s += int_to_little_endian(self.locktime, 4)
     s += int_to_little_endian(SIGHASH_ALL, 4)
     return int.from_bytes(hash256(s), "big")
示例#6
0
 def sig_hash(self, input_index, redeem_script=None):
     s = int_to_little_endian(self.version, 4)
     s += encode_varint(len(self.tx_ins))
     for i, tx_in in enumerate(self.tx_ins):
         if i == input_index:
             if redeem_script:
                 script_sig = redeem_script
             else:
                 script_sig = tx_in.script_pubkey(self.testnet)
         else:
             script_sig = None
         s += TxIn(
             prev_tx=tx_in.prev_tx,
             prev_index=tx_in.prev_index,
             script_sig=script_sig,
             sequence=tx_in.sequence,
         ).serialize()
     s += encode_varint(len(self.tx_outs))
     for tx_out in self.tx_outs:
         s += tx_out.serialize()
     s += int_to_little_endian(self.locktime, 4)
     s += int_to_little_endian(SIGHASH_ALL, 4)
     h256 = hash256(s)
     return int.from_bytes(h256, "big")
示例#7
0
 def hash(self):
     return hash256(self.serialize_legacy())[::-1]
示例#8
0
 def check_pow(self):
     sha = hash256(self.serialize())
     proof = little_endian_to_int(sha)
     return proof < self.target()
示例#9
0
 def hash(self):
     s = self.serialize()
     sha = hash256(s)
     return sha[::-1]
示例#10
0
def op_hash256(stack):
    if len(stack) < 1:
        return False
    element = stack.pop()
    stack.append(hash256(element))
    return True