def create_bitcoind_connection(rpc_username, rpc_password, server, port, use_https, timeout): """ Creates an RPC client to a bitcoind instance. It will have ".opts" defined as a member, which will be a dict that stores the above connection options. """ global do_wrap_socket, create_ssl_authproxy log.debug("[%s] Connect to bitcoind at %s://%s@%s:%s, timeout=%s" % (os.getpid(), 'https' if use_https else 'http', rpc_username, server, port, timeout)) protocol = 'https' if use_https else 'http' if not server or len(server) < 1: raise Exception('Invalid bitcoind host address.') if not port or not is_valid_int(port): raise Exception('Invalid bitcoind port number.') authproxy_config_uri = '%s://%s:%s@%s:%s' % (protocol, rpc_username, rpc_password, server, port) if use_https: # TODO: ship with a cert if do_wrap_socket: # ssl._create_unverified_context and ssl.create_default_context are not supported. # wrap the socket directly connection = BitcoindConnection(server, int(port), timeout=timeout) ret = AuthServiceProxy(authproxy_config_uri, connection=connection) elif create_ssl_authproxy: # ssl has _create_unverified_context, so we're good to go ret = AuthServiceProxy(authproxy_config_uri, timeout=timeout) else: # have to set up an unverified context ourselves ssl_ctx = ssl.create_default_context() ssl_ctx.check_hostname = False ssl_ctx.verify_mode = ssl.CERT_NONE connection = httplib.HTTPSConnection(server, int(port), context=ssl_ctx, timeout=timeout) ret = AuthServiceProxy(authproxy_config_uri, connection=connection) else: ret = AuthServiceProxy(authproxy_config_uri) # remember the options bitcoind_opts = { "bitcoind_user": rpc_username, "bitcoind_passwd": rpc_password, "bitcoind_server": server, "bitcoind_port": port, "bitcoind_use_https": use_https, "bitcoind_timeout": timeout } setattr(ret, "opts", bitcoind_opts) return ret
def blockstore_script_to_hex(script): """ Parse the readable version of a script, return the hex version. """ hex_script = '' parts = script.split(' ') for part in parts: if part.startswith("NAME_") or part.startswith("NAMESPACE_"): try: hex_script += '%0.2x' % ord(eval(part)) except: raise Exception('Invalid opcode: %s' % part) elif part.startswith("0x"): # literal hex string hex_script += part[2:] elif is_valid_int(part): hex_part = '%0.2x' % int(part) if len(hex_part) % 2 != 0: hex_part = '0' + hex_part hex_script += hex_part elif is_hex(part) and len(part) % 2 == 0: hex_script += part else: raise ValueError('Invalid script (at %s), contains invalid characters: %s' % (part, script)) if len(hex_script) % 2 != 0: raise ValueError('Invalid script: must have an even number of chars (got %s).' % hex_script) return hex_script
def zonefilemanage_script_to_hex(script): """ Parse the readable version of a script, return the hex version. """ hex_script = '' parts = script.split(' ') for part in parts: if part in NAME_OPCODES: try: hex_script += '{:02x}'.format(ord(NAME_OPCODES[part])) except: raise Exception('Invalid opcode: {}'.format(part)) elif part.startswith('0x'): # literal hex string hex_script += part[2:] elif is_valid_int(part): hex_part = '{:02x}'.format(int(part)) if len(hex_part) % 2 != 0: hex_part = '0' + hex_part hex_script += hex_part elif is_hex(part) and len(part) % 2 == 0: hex_script += part else: raise ValueError( 'Invalid script (at {}), contains invalid characters: {}'. format(part, script)) if len(hex_script) % 2 != 0: raise ValueError( 'Invalid script: must have an even number of chars (got {}).'. format(hex_script)) return hex_script
def create_bitcoind_connection(rpc_username=None, rpc_password=None, server=None, port=None, use_https=None): """ creates an auth service proxy object, to connect to bitcoind """ global bitcoin_opts if rpc_username is None: rpc_username = bitcoin_opts.get("bitcoind_user") if rpc_password is None: rpc_password = bitcoin_opts.get("bitcoind_passwd") if server is None: server = bitcoin_opts.get("bitcoind_server") if port is None: port = bitcoin_opts.get("bitcoind_port") if use_https is None: use_https = bitcoin_opts.get("bitcoind_use_https") log.debug("Connect to bitcoind at %s://%s@%s:%s" % ("https" if use_https else "http", rpc_username, server, port)) protocol = "https" if use_https else "http" if not server or len(server) < 1: raise Exception("Invalid bitcoind host address.") if not port or not is_valid_int(port): raise Exception("Invalid bitcoind port number.") authproxy_config_uri = "%s://%s:%s@%s:%s" % (protocol, rpc_username, rpc_password, server, port) return AuthServiceProxy(authproxy_config_uri)
def blockstack_script_to_hex(script): """ Parse the readable version of a script, return the hex version. """ hex_script = '' parts = script.split(' ') for part in parts: if part in NAME_OPCODES.keys(): try: hex_script += '%0.2x' % ord(NAME_OPCODES[part]) except: raise Exception('Invalid opcode: %s' % part) elif part.startswith("0x"): # literal hex string hex_script += part[2:] elif is_valid_int(part): hex_part = '%0.2x' % int(part) if len(hex_part) % 2 != 0: hex_part = '0' + hex_part hex_script += hex_part elif is_hex(part) and len(part) % 2 == 0: hex_script += part else: raise ValueError('Invalid script (at %s), contains invalid characters: %s' % (part, script)) if len(hex_script) % 2 != 0: raise ValueError('Invalid script: must have an even number of chars (got %s).' % hex_script) return hex_script
def create_bitcoind_connection( rpc_username, rpc_password, server, port, use_https, timeout ): """ Creates an RPC client to a bitcoind instance. It will have ".opts" defined as a member, which will be a dict that stores the above connection options. """ global do_wrap_socket, create_ssl_authproxy log.debug("[%s] Connect to bitcoind at %s://%s@%s:%s, timeout=%s" % (os.getpid(), 'https' if use_https else 'http', rpc_username, server, port, timeout) ) protocol = 'https' if use_https else 'http' if not server or len(server) < 1: raise Exception('Invalid bitcoind host address.') if not port or not is_valid_int(port): raise Exception('Invalid bitcoind port number.') authproxy_config_uri = '%s://%s:%s@%s:%s' % (protocol, rpc_username, rpc_password, server, port) if use_https: # TODO: ship with a cert if do_wrap_socket: # ssl._create_unverified_context and ssl.create_default_context are not supported. # wrap the socket directly connection = BitcoindConnection( server, int(port), timeout=timeout ) ret = AuthServiceProxy(authproxy_config_uri, connection=connection) elif create_ssl_authproxy: # ssl has _create_unverified_context, so we're good to go ret = AuthServiceProxy(authproxy_config_uri, timeout=timeout) else: # have to set up an unverified context ourselves ssl_ctx = ssl.create_default_context() ssl_ctx.check_hostname = False ssl_ctx.verify_mode = ssl.CERT_NONE connection = httplib.HTTPSConnection( server, int(port), context=ssl_ctx, timeout=timeout ) ret = AuthServiceProxy(authproxy_config_uri, connection=connection) else: ret = AuthServiceProxy(authproxy_config_uri) # remember the options bitcoind_opts = { "bitcoind_user": rpc_username, "bitcoind_passwd": rpc_password, "bitcoind_server": server, "bitcoind_port": port, "bitcoind_use_https": use_https, "bitcoind_timeout": timeout } setattr( ret, "opts", bitcoind_opts ) return ret
def create_bitcoind_connection( rpc_username=config.BITCOIND_USER, rpc_password=config.BITCOIND_PASSWD, server=config.BITCOIND_SERVER, port=config.BITCOIND_PORT, use_https=config.BITCOIND_USE_HTTPS): """ creates an auth service proxy object, to connect to bitcoind """ protocol = 'https' if use_https else 'http' if not server or len(server) < 1: raise Exception('Invalid bitcoind host address.') if not port or not is_valid_int(port): raise Exception('Invalid bitcoind port number.') authproxy_config_uri = '%s://%s:%s@%s:%s' % ( protocol, rpc_username, rpc_password, server, port) return AuthServiceProxy(authproxy_config_uri)
def name_script_to_hex(script): """ Parse the readable version of a name script, return the hex version. """ hex_script = '' parts = script.split(' ') for part in parts: if part[0:5] == 'NAME_': try: hex_script += '%0.2x' % ord(eval(part)) except: raise Exception('Invalid opcode: %s' % part) elif is_hex(part) and len(part) % 2 == 0: hex_script += part elif is_valid_int(part): hex_script += '%0.2x' % int(part) else: raise ValueError( 'Invalid script, contains invalid characters: %s' % script) if len(hex_script) % 2 != 0: raise ValueError('Invalid script: must have an even number of chars.') return hex_script
def create_bitcoind_connection(rpc_username=None, rpc_password=None, server=None, port=None, use_https=None): """ creates an auth service proxy object, to connect to bitcoind """ global bitcoin_opts if rpc_username is None: rpc_username = bitcoin_opts.get("bitcoind_user") if rpc_password is None: rpc_password = bitcoin_opts.get("bitcoind_passwd") if server is None: server = bitcoin_opts.get("bitcoind_server") if port is None: port = bitcoin_opts.get("bitcoind_port") if use_https is None: use_https = bitcoin_opts.get("bitcoind_use_https") log.debug("Connect to bitcoind at %s://%s@%s:%s" % ('https' if use_https else 'http', rpc_username, server, port)) protocol = 'https' if use_https else 'http' if not server or len(server) < 1: raise Exception('Invalid bitcoind host address.') if not port or not is_valid_int(port): raise Exception('Invalid bitcoind port number.') authproxy_config_uri = '%s://%s:%s@%s:%s' % (protocol, rpc_username, rpc_password, server, port) return AuthServiceProxy(authproxy_config_uri)
def test_hex_to_charset(self): s = "d9fa02e46cd3867f51279dfae592d3706022ee93c175b49c30c8c962722fc890" s2 = hex_to_charset(s, string.digits) self.assertTrue(is_valid_int(s2))
def test_is_valid_int_with_string(self): i = ("9859361987058468004536324485715460737416376519236217359320056" "4555477131708560") self.assertTrue(is_valid_int(i))