def hash160(self, is_compressed=None): """ Return the hash160 representation of this key, if available. """ if is_compressed is None: is_compressed = self.is_compressed() if is_compressed: if self._hash160_compressed is None: self._hash160_compressed = hash160(self.sec(is_compressed=is_compressed)) return self._hash160_compressed if self._hash160_uncompressed is None: self._hash160_uncompressed = hash160(self.sec(is_compressed=is_compressed)) return self._hash160_uncompressed
def f(solved_values, **kwargs): signature_type = kwargs.get("signature_type", DEFAULT_SIGNATURE_TYPE) generator_for_signature_type_f = kwargs[ "generator_for_signature_type_f"] signature_for_hash_type_f = m["signature_for_hash_type_f"] existing_script = kwargs.get("existing_script", b'') existing_signatures, secs_solved = _find_signatures( existing_script, generator_for_signature_type_f, signature_for_hash_type_f, len(m["sig_list"]), m["sec_list"]) sec_keys = m["sec_list"] signature_variables = m["sig_list"] signature_placeholder = kwargs.get("signature_placeholder", DEFAULT_PLACEHOLDER_SIGNATURE) db = kwargs.get("hash160_lookup", {}) # we reverse this enumeration to make the behaviour look like the old signer. BRAIN DAMAGE for signature_order, sec_key in reversed(list(enumerate(sec_keys))): sec_key = solved_values.get(sec_key, sec_key) if sec_key in secs_solved: continue if len(existing_signatures) >= len(signature_variables): break result = db.get(hash160(sec_key)) if result: secret_exponent = result[0] sig_hash = signature_for_hash_type_f(signature_type) generator = result[3] r, s = generator.sign(secret_exponent, sig_hash) else: # try existing signatures generator = generator_for_signature_type_f(signature_type) public_pair = sec_to_public_pair(sec_key, generator=generator) for sig in all_signature_hints(public_pair, signature_for_hash_type_f, **kwargs): sig_hash = signature_for_hash_type_f(indexbytes(sig, -1)) sig_pair = der.sigdecode_der(sig[:-1]) if generator.verify(public_pair, sig_hash, sig_pair): r, s = sig_pair break else: continue order = generator.order() if s + s > order: s = order - s binary_signature = der.sigencode_der(r, s) + int2byte(signature_type) existing_signatures.append((signature_order, binary_signature)) # pad with placeholder signatures if signature_placeholder is not None: while len(existing_signatures) < len(signature_variables): existing_signatures.append((-1, signature_placeholder)) existing_signatures.sort() return dict( zip(signature_variables, (es[-1] for es in existing_signatures)))
def address(self, address_api): if self.is_coinbase(): return "(coinbase)" # attempt to return the source address sec = self.public_key_sec() if sec: address = address_api.for_p2pkh(hash160(sec)) return address return "(unknown)"
def address(self, address_api): if self.is_coinbase(): return "(coinbase)" # attempt to return the source address sec = self.public_key_sec() if sec: address = address_api.for_p2pkh(hash160(sec)) return address return "(unknown)"
def hash160(self, use_uncompressed=None): """ Return the hash160 representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned. """ use_uncompressed = self._use_uncompressed(use_uncompressed) if self.public_pair() is None: if use_uncompressed is not None: return None return self._hash160 if use_uncompressed: if self._hash160_uncompressed is None: self._hash160_uncompressed = hash160(self.sec(use_uncompressed=use_uncompressed)) return self._hash160_uncompressed if self._hash160_compressed is None: self._hash160_compressed = hash160(self.sec(use_uncompressed=use_uncompressed)) return self._hash160_compressed
def f(solved_values, **kwargs): signature_type = kwargs.get("signature_type", DEFAULT_SIGNATURE_TYPE) signature_hints = kwargs.get("signature_hints", []) generator_for_signature_type_f = kwargs["generator_for_signature_type_f"] signature_for_hash_type_f = m["signature_for_hash_type_f"] existing_script = kwargs.get("existing_script", b'') existing_signatures, secs_solved = _find_signatures( existing_script, generator_for_signature_type_f, signature_for_hash_type_f, len(m["sig_list"]), m["sec_list"]) sec_keys = m["sec_list"] signature_variables = m["sig_list"] signature_placeholder = kwargs.get("signature_placeholder", DEFAULT_PLACEHOLDER_SIGNATURE) db = kwargs.get("hash160_lookup", {}) # we reverse this enumeration to make the behaviour look like the old signer. BRAIN DAMAGE for signature_order, sec_key in reversed(list(enumerate(sec_keys))): sec_key = solved_values.get(sec_key, sec_key) if sec_key in secs_solved: continue if len(existing_signatures) >= len(signature_variables): break result = db.get(hash160(sec_key)) if result: secret_exponent = result[0] sig_hash = signature_for_hash_type_f(signature_type) generator = result[3] r, s = generator.sign(secret_exponent, sig_hash) else: # try existing signatures for sig in signature_hints: sig_hash = signature_for_hash_type_f(indexbytes(sig, -1)) generator = generator_for_signature_type_f(signature_type) public_pair = sec_to_public_pair(sec_key, generator=generator) sig_pair = der.sigdecode_der(sig[:-1]) if generator.verify(public_pair, sig_hash, sig_pair): r, s = sig_pair break else: continue order = generator.order() if s + s > order: s = order - s binary_signature = der.sigencode_der(r, s) + int2byte(signature_type) existing_signatures.append((signature_order, binary_signature)) # pad with placeholder signatures if signature_placeholder is not None: while len(existing_signatures) < len(signature_variables): existing_signatures.append((-1, signature_placeholder)) existing_signatures.sort() return dict(zip(signature_variables, (es[-1] for es in existing_signatures)))
def hash160(self, use_uncompressed=None): """ Return the hash160 representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned. """ use_uncompressed = self._use_uncompressed(use_uncompressed) if self.public_pair() is None: if use_uncompressed is not None: return None return self._hash160 if use_uncompressed: if self._hash160_uncompressed is None: self._hash160_uncompressed = hash160( self.sec(use_uncompressed=use_uncompressed)) return self._hash160_uncompressed if self._hash160_compressed is None: self._hash160_compressed = hash160( self.sec(use_uncompressed=use_uncompressed)) return self._hash160_compressed
def get_address_name(inorout, network): try: if type(inorout) == TxOut: address = network.address.for_script(inorout.puzzle_script()) elif type(inorout) == Spendable: address = network.address.for_script(inorout.puzzle_script()) else: # address from tx input if (len(inorout.witness) == 2) and (inorout.script == b''): # Witness PubKeyHash (pay-to-witness-pubkeyhash / P2WPKH) address = bech32.encode(network.ui._bech32_hrp, 0x00, hash160(inorout.witness[1])) elif (len(inorout.witness) > 2) and (inorout.script == b''): # Witness ScriptHash (pay-to-witness-scripthash / P2WSH) address = bech32.encode(network.ui._bech32_hrp, 0x00, sha256(inorout.witness[-1])) else: address = inorout.address(network.ui) if address == '(unknown)': address = network.ui.address_for_p2sh(hash160(inorout.script[1:])) except: address = 'bad script' return address
def for_script_info(self, script_info): type = script_info.get("type") if type == "p2pkh": return self.for_p2pkh(script_info["hash160"]) if type == "p2pkh_wit": return self.for_p2pkh_wit(script_info["hash160"]) if type == "p2sh_wit": return self.for_p2sh_wit(script_info["hash256"]) if type == "p2pk": h160 = hash160(script_info["sec"]) # BRAIN DAMAGE: this isn't really a p2pkh return self.for_p2pkh(h160) if type == "p2sh": return self.for_p2sh(script_info["hash160"]) if type == "nulldata": return "(nulldata %s)" % b2h(script_info["data"]) return "???"
def address_for_script_info(self, script_info): type = script_info.get("type") if type == "p2pkh": return self.address_for_p2pkh(script_info["hash160"]) if type == "p2pkh_wit": return self.address_for_p2pkh_wit(script_info["hash160"]) if type == "p2sh_wit": return self.address_for_p2sh_wit(script_info["hash256"]) if type == "p2pk": h160 = hash160(script_info["sec"]) # BRAIN DAMAGE: this isn't really a p2pkh return self.address_for_p2pkh(h160) if type == "p2sh": return self.address_for_p2sh(script_info["hash160"]) if type == "nulldata": return "(nulldata %s)" % b2h(script_info["data"]) return "???"
def for_p2s(self, script): return self.for_p2sh(hash160(script))
def build_p2sh_lookup(scripts): d1 = dict((hash160(s), s) for s in scripts) d1.update((hashlib.sha256(s).digest(), s) for s in scripts) return d1
def build_sec_lookup(sec_values): d = {} for sec in sec_values or []: d[hash160(sec)] = sec return d
def do_test(blob, expected_hash): self.assertEqual(hash160(blob), expected_hash)
def script_for_p2s(self, underlying_script): return self.script_for_p2sh(hash160(underlying_script))
def address_for_p2s(self, script): return self.address_for_p2sh(hash160(script))
def annotate_pubkey(self, blob, da): is_compressed = is_sec_compressed(blob) address = self._address.for_p2pkh(hash160(blob)) da[blob].append("SEC for %scompressed %s" % ("" if is_compressed else "un", address))
def script_for_p2s(self, underlying_script): return self.script_for_p2sh(hash160(underlying_script))
def do_test(blob, expected_hash): self.assertEqual(hash160(blob), expected_hash)
def add_p2s_script(self, script): h160 = hash160(script) h256 = hashlib.sha256(script).digest() self._exec_sql("insert or ignore into P2S values (?, ?, ?)", h160, h256, script)
def for_p2s(self, script): return self.for_p2sh(hash160(script))
def address_for_p2s(self, script): return self.address_for_p2sh(hash160(script))