def seed_prefix(seed_type): if seed_type == 'standard': return Parameter().SEED_PREFIX elif seed_type == 'segwit': return Parameter().SEED_PREFIX_SW elif seed_type == '2fa': return Parameter().SEED_PREFIX_2FA
def is_address(addr): try: addrtype, h = bc_address_to_type_and_hash_160(addr) except Exception: return False if addrtype not in [Parameter().ADDRTYPE_P2PKH, Parameter().ADDRTYPE_P2SH]: return False return addr == hash_160_to_bc_address(h, addrtype)
def seed_type(x): if is_old_seed(x): return 'old' elif is_new_seed(x): return 'standard' elif Parameter().TESTNET and is_new_seed(x, Parameter().SEED_PREFIX_SW): return 'segwit' elif is_new_seed(x, Parameter().SEED_PREFIX_2FA): return '2fa' return ''
def connect_callback(future): if not self.client.is_connected: logger.debug('connect failed and retry') self.client = None self.start_client() else: self.client.add_message( Version([ Parameter().ELECTRUM_VERSION, Parameter().PROTOCOL_VERSION ]))
def get_scriptPubKey(addr): addrtype, hash_160 = bc_address_to_type_and_hash_160(addr) if addrtype == Parameter().ADDRTYPE_P2PKH: script = '76a9' # op_dup, op_hash_160 script += push_script(hash_160.encode('hex')) script += '88ac' # op_equalverify, op_checksig elif addrtype == Parameter().ADDRTYPE_P2SH: script = 'a9' # op_hash_160 script += push_script(hash_160.encode('hex')) script += '87' # op_equal else: raise BaseException('unknown address type') return script
def deserialize_xkey(xkey, prv): xkey = b58decode_check(xkey) if len(xkey) != 78: raise BaseException('Invalid length') depth = ord(xkey[4]) fingerprint = xkey[5:9] child_number = xkey[9:13] c = xkey[13:13 + 32] header = Parameter().XPRV_HEADER if prv else Parameter().XPUB_HEADER xtype = int('0x' + xkey[0:4].encode('hex'), 16) - header if xtype not in ([0, 1] if Parameter().TESTNET else [0]): raise BaseException('Invalid header') n = 33 if prv else 32 K_or_k = xkey[13 + n:] return xtype, depth, fingerprint, child_number, c, K_or_k
def get_pubkey_script(self, address): addrtype, hash_160 = bc_address_to_type_and_hash_160(address) if addrtype == Parameter().ADDRTYPE_P2PKH: script = '76a9' # op_dup, op_hash_160 script += push_script(hash_160.encode('hex')) script += '88ac' # op_equalverify, op_checksig return script
def pick_random_server(self, hostmap=None, protocol='t', exclude_set=set()): if hostmap is None: hostmap = Parameter().DEFAULT_SERVERS eligible = list( set(self.filter_protocol(hostmap, protocol)) - exclude_set) return random.choice(eligible) if eligible else None
def ASecretToSecret(key): addrtype = Parameter().ADDRTYPE_P2PKH vch = b58decode_check(key) if vch and vch[0] == chr((addrtype + 128) & 255): return vch[1:] elif is_minikey(key): return minikey_to_private_key(key) else: return False
def verify_header(self, header, prev_header, bits, target): if prev_header is None: prev_hash = '0' * 64 else: prev_hash = prev_header.block_hash if prev_hash != header.block_prev: logger.warning("prev hash mismatch: %s vs %s" % (prev_hash, header.block_prev)) return False if Parameter().TESTNET or Parameter().NOLNET: return True if bits != header.block_bits: logger.warning("bits mismatch: %s vs %s" % (bits, header.block_bits)) return False _hash = header.block_hash if int('0x' + _hash, 16) > target: logger.warning( "insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target)) return False return True
def hash_160_to_bc_address(h160, version=0, witness_program_version=1): if version.__class__ is int: version = chr(version) if version == chr(Parameter().ADDRTYPE_P2WPKH): vh160 = version + chr(witness_program_version) + chr(0) else: vh160 = version + h160 h3 = SHA256.new(SHA256.new(vh160).digest()).digest() addr = vh160 + h3[0:4] return b58encode(addr)
def init(self): retry = 5 while retry > 0: try: request = tornado.httpclient.HTTPRequest( url=Parameter().HEADERS_URL, connect_timeout=20.0, request_timeout=60 * 10) response = yield tornado.gen.Task(AsyncHTTPClient().fetch, request) # # response = yield AsyncHTTPClient().fetch(Parameter().HEADERS_URL) except Exception as ex: print ex.message retry -= 1 else: raise gen.Return(response.body)
def hash160_to_p2sh(h160): return hash_160_to_bc_address(h160, Parameter().ADDRTYPE_P2SH)
def SecretToASecret(secret, compressed=False): addrtype = Parameter().ADDRTYPE_P2PKH vchIn = chr((addrtype + 128) & 255) + secret if compressed: vchIn += '\01' return b58encode_check(vchIn)
def xpub_header(xtype): return ("%08x" % (Parameter().XPUB_HEADER + xtype)).decode('hex')
def is_p2sh(addr): if is_address(addr): addrtype, h = bc_address_to_type_and_hash_160(addr) return addrtype == Parameter().ADDRTYPE_P2SH
def bip44_derivation(account_id): if Parameter().TESTNET: return "m/44'/1'/%d'" % int(account_id) else: return "m/44'/0'/%d'" % int(account_id)
def is_new_seed(x, prefix=None): if prefix is None: prefix = Parameter().SEED_PREFIX x = normalize_text(x) s = hmac.new("Seed version", x.encode('utf8'), hashlib.sha512).digest().encode('hex') return s.startswith(prefix)
def public_key_to_p2wpkh(public_key): return hash_160_to_bc_address(hash_160(public_key), Parameter().ADDRTYPE_P2WPKH)