def test_endian(self): from pycoin.intbytes import int_from_bytes, int_to_bytes, from_bytes, to_bytes assert int_from_bytes(int_to_bytes(768)) == 768 assert int_from_bytes(int_to_bytes(3)) == 3 assert int_from_bytes(int_to_bytes(66051)) == 66051 for e in ("big", "little"): assert from_bytes(to_bytes(768, 2, e), e) == 768 assert from_bytes(to_bytes(3, 1, e), e) == 3 assert from_bytes(to_bytes(66051, 3, e), e) == 66051
def signature_hash(self, tx_out_script, unsigned_txs_out_idx, hash_type): """ Return the canonical hash for a transaction. We need to remove references to the signature, since it's a signature of the hash before the signature is applied. tx_out_script: the script the coins for unsigned_txs_out_idx are coming from unsigned_txs_out_idx: where to put the tx_out_script hash_type: one of SIGHASH_NONE, SIGHASH_SINGLE, SIGHASH_ALL, optionally bitwise or'ed with SIGHASH_ANYONECANPAY """ # In case concatenating two scripts ends up with two codeseparators, # or an extra one at the end, this prevents all those possible incompatibilities. tx_out_script = tools.delete_subscript(tx_out_script, int_to_bytes(171)) # blank out other inputs' signatures txs_in = [ self._tx_in_for_idx(i, tx_in, tx_out_script, unsigned_txs_out_idx) for i, tx_in in enumerate(self.txs_in) ] txs_out = self.txs_out # Blank out some of the outputs if (hash_type & 0x1f) == self.SIGHASH_NONE: # Wildcard payee txs_out = [] # Let the others update at will for i in range(len(txs_in)): if i != unsigned_txs_out_idx: txs_in[i].sequence = 0 elif (hash_type & 0x1f) == self.SIGHASH_SINGLE: # This preserves the ability to validate existing legacy # transactions which followed a buggy path in Satoshi's # original code; note that higher level functions for signing # new transactions (e.g., is_signature_ok and sign_tx_in) # check to make sure we never get here (or at least they # should) if unsigned_txs_out_idx >= len(txs_out): # This should probably be moved to a constant, but the # likelihood of ever getting here is already really small # and getting smaller return (1 << 248) # Only lock in the TransactionOut payee at same index as TransactionIn; delete # any outputs after this one and set all outputs before this # one to "null" (where "null" means an empty script and a # value of -1) txs_out = [self.TransactionOut(0xffffffffffffffff, b'') ] * unsigned_txs_out_idx txs_out.append(self.txs_out[unsigned_txs_out_idx]) # Let the others update at will for i in range(len(self.txs_in)): if i != unsigned_txs_out_idx: txs_in[i].sequence = 0 # Blank out other inputs completely, not recommended for open transactions if hash_type & self.SIGHASH_ANYONECANPAY: txs_in = [txs_in[unsigned_txs_out_idx]] tmp_tx = self.__class__(self.version, txs_in, txs_out, self.lock_time) return from_bytes_32(tmp_tx.hash(hash_type=hash_type))
def test_val(n): as_bytes = int_to_bytes(n) test_bytes(as_bytes)