示例#1
0
def get_service_status():
    """
        Retrieve user configuration, and status of wallet's rpc server, transaction index, and gpg's installation.
    """
    conf = get_config()
    if 'rpcpassword' in conf and conf['rpcpassword']:
        conf['rpcpassword'] = "******"

    try:
        rpc_raw = AuthServiceProxy(get_rpc_url())
        rpc_raw.getblockcount()
        conf['wallet_connected_status'] = "good"
    except:
        conf['wallet_connected_status'] = "bad"

    try:
        if not os.path.exists(peerapps.settings.BASE_DIR+"/test_gpg_setup"):
            os.mkdir(peerapps.settings.BASE_DIR+"/test_gpg_setup")
        gnupg.GPG(gnupghome=peerapps.settings.BASE_DIR+"/test_gpg_setup")
        shutil.rmtree(peerapps.settings.BASE_DIR+"/test_gpg_setup", ignore_errors=True)
        conf['gpg_suite_installed'] = "good"
    except:
        conf['gpg_suite_installed'] = "bad"

    return conf
示例#2
0
def main(max_items):
    db = anydbm.open(DB_FILENAME,'c')
    twister = AuthServiceProxy(RPC_URL)
    for feed_url in FEEDS:
        logging.info(feed_url)
        feed = feedparser.parse(feed_url)
        n_items = 0
        for e in feed.entries:
            eid = '{0}|{1}'.format(feed_url,e.id)
            if db.has_key(eid): # been there, done that (or not - for a reason)
                logging.debug('Skipping duplicate {0}'.format(eid))
            else: # format as a <=140 character string
                if len(e.link)<=MAX_URL_LENGTH:
                    msg = u'{0} {1}'.format(e.link,e.title)
                    if len(msg)>140: # Truncate (and hope it's still meaningful)
                        msg = msg[:137]+u'...'
                else: # Link too long. Not enough space left for text :(
                    msg = ''
                utfmsg = truncated_utf8(msg,140)# limit is 140 utf-8 bytes (not chars)
                msg = unicode(utfmsg,'utf-8') # AuthServiceProxy needs unicode [we just needed to know where to truncate, and that's utf-8]
                db[eid] = utfmsg # anydbm, on the other hand, can't handle unicode, so it's a good thing we've also kept the utf-8 :)
                if not msg: # We've marked it as "posted", but no sense really posting it.
                    logging.warn(u'Link too long at {0}'.format(eid))
                    continue
                if n_items>=max_items: # Avoid accidental flooding
                    logging.warn(u'Skipping "over quota" item: {0}'.format(msg))
                    continue
                logging.info(u'posting {0}'.format(msg))
                try:
                    twister.newpostmsg(USERNAME,get_next_k(twister,USERNAME),msg)
                except Exception,e:
                    logging.error(`e`) # usually not very informative :(
                n_items+=1
示例#3
0
def query_transactions(ticker=None):
    if not ticker:
        for c in Currency.objects.all():
            query_transactions.delay(c.ticker)
        return

    currency = Currency.objects.select_for_update().get(ticker=ticker)
    coin = AuthServiceProxy(currency.api_url)
    current_block = coin.getblockcount()
    processed_transactions = []

    block_hash = coin.getblockhash(currency.last_block)
    transactions = coin.listsinceblock(block_hash)['transactions']

    for tx in transactions:
        if tx['txid'] in processed_transactions:
            continue

        if tx['category'] not in ('receive', 'generate', 'immature'):
            continue

        process_deposite_transaction(tx, ticker)
        processed_transactions.append(tx['txid'])

    currency.last_block = current_block
    currency.save()

    for tx in Transaction.objects.filter(processed=False, currency=currency):
        query_transaction(ticker, tx.txid)
示例#4
0
class JsonRpc:
    def __init__(self, conn):
        self.conn = AuthServiceProxy(conn)

    def getTransactions(self, address):
        return self.conn.searchrawtransactions(address, 1, 0, 2000)

    def getTransaction(self, tx_id):
        return self.conn.getrawtransaction(tx_id, 1)

    def getInBalance(self, txs, address):
        inBalance = 0

        for tx in txs:
            for inputs in tx["vin"]:
                input_tx = self.getTransaction(inputs["txid"])
                vout = inputs["vout"]

                for outputs in input_tx["vout"]:
                    if outputs["n"] == vout and outputs["scriptPubKey"]["addresses"].__contains__(address):
                        inBalance += int(outputs["value"] * 10**8)

        return inBalance

    def getOutBalance(self, txs, address):
        outBalance = 0

        for tx in txs:
            for output in tx["vout"]:
                if output["scriptPubKey"]["addresses"].__contains__(address):
                    outBalance += int(output["value"] * 10**8)

        return outBalance
示例#5
0
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None):
    """
    Start a bitcoind and return RPC connection to it
    """
    datadir = os.path.join(dirname, "node"+str(i))
    if binary is None:
        binary = os.getenv("BITCOIND", "bitcoind")
    args = [ binary, "-datadir="+datadir, "-keypool=1", "-discover=0", "-rest" ]
    if extra_args is not None: args.extend(extra_args)
    bitcoind_processes[i] = subprocess.Popen(args)
    devnull = open("/dev/null", "w+")
    if os.getenv("PYTHON_DEBUG", ""):
        print "start_node: bitcoind started, calling bitcoin-cli -rpcwait getblockcount"
    subprocess.check_call([ os.getenv("BITCOINCLI", "bitcoin-cli"), "-datadir="+datadir] +
                          _rpchost_to_args(rpchost)  +
                          ["-rpcwait", "getblockcount"], stdout=devnull)
    if os.getenv("PYTHON_DEBUG", ""):
        print "start_node: calling bitcoin-cli -rpcwait getblockcount returned"
    devnull.close()
    url = "http://*****:*****@%s:%d" % (rpchost or '127.0.0.1', rpc_port(i))
    if timewait is not None:
        proxy = AuthServiceProxy(url, timeout=timewait)
    else:
        proxy = AuthServiceProxy(url)
    proxy.url = url # store URL on proxy for info
    return proxy
    def __init__(self, server=NAMECOIND_SERVER, port=NAMECOIND_PORT,
                 user=NAMECOIND_USER, passwd=NAMECOIND_PASSWD,
                 use_https=NAMECOIND_USE_HTTPS,
                 passphrase=NAMECOIND_WALLET_PASSPHRASE):

        global create_ssl_authproxy, do_wrap_socket
        
        if use_https:
            http_string = 'https://'
        else:
            http_string = 'http://'

        authproxy_config_uri = http_string + user + ':' + passwd + '@' + server + ':' + str(port)

        self.passphrase = passphrase
        self.server = server
        
        if do_wrap_socket:
           # ssl._create_unverified_context and ssl.create_default_context are not supported.
           # wrap the socket directly 
           connection = NamecoindConnection( server, int(port) )
           self.obj = AuthServiceProxy(authproxy_config_uri, connection=connection)
       
        elif create_ssl_authproxy:
           # ssl has _create_unverified_context, so we're good to go 
           self.obj = AuthServiceProxy(authproxy_config_uri)
    
        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 )
           self.obj = AuthServiceProxy(authproxy_config_uri, connection=connection)
    def getOrCreateAddress(self, subpath_number):
        try:
            return Address.objects.get(wallet=self, subpath_number=subpath_number)
        except Address.DoesNotExist:
            pass

        new_address_full_path = self.path + [subpath_number]
        new_address_full_path_str = '/'.join([str(i) for i in new_address_full_path])

        # Create raw bitcoin address and key
        key = Key.from_text(settings.MASTERWALLET_BIP32_KEY)
        subkey = key.subkeys(new_address_full_path_str).next()

        btc_address = subkey.address(use_uncompressed=False)
        btc_private_key = subkey.wif(use_uncompressed=False)

        # Make sure private key is stored to the database of bitcoind
        rpc = AuthServiceProxy('http://' + settings.BITCOIN_RPC_USERNAME + ':' + settings.BITCOIN_RPC_PASSWORD + '@' + settings.BITCOIN_RPC_IP + ':' + str(settings.BITCOIN_RPC_PORT))
        try:
            rpc.importprivkey(btc_private_key, '', False)
        except:
            raise Exception('Unable to store Bitcoin address to Bitcoin node!')

        # Create new Address and return it
        new_address = Address(wallet=self, subpath_number=subpath_number, address=btc_address)
        new_address.save()

        return new_address
示例#8
0
文件: tasks.py 项目: moki9/django-cc
def query_transaction(ticker, txid):
#    logger.info("Execute Query Transaction")
    currency = Currency.objects.select_for_update().get(ticker=ticker)
    coin = AuthServiceProxy(currency.api_url)
    for txdict in normalise_txifno(coin.gettransaction(txid)):
#        logger.info("Process deposit TX: {}".format(txdict))
        process_deposite_transaction(txdict, ticker)
示例#9
0
 def service_thread(ws_server, evt):
     from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
     from bitrisk.bitcoind_config import read_default_config
     import json
     import decimal
     config = read_default_config()
     testnet = ''
     if config.has_key('testnet'):
         testnet = config['testnet']
     rpc_user = config['rpcuser']
     rpc_password = config['rpcpassword']
     rpc_connection = AuthServiceProxy("http://%s:%s@%s:%s8332"%(rpc_user, rpc_password, host, testnet))
     
     conn = sqlite3.connect(db_filename)
     while not evt.wait(5):
         txs = get_db_txs(conn)
         for tx in txs:
             print 'tx:', tx
             tx = rpc_connection.gettransaction(tx)
             for details in tx['details']:
                 addr = details['address']
                 if ws_server.watched_addresses.has_key(addr):
                     def decimal_default(obj):
                         if isinstance(obj, decimal.Decimal):
                             return float(obj)
                         raise TypeError
                     msg = json.dumps(tx, default=decimal_default)
                     for client in ws_server.watched_addresses[addr]:
                         ws_server.send_message(client, msg)
         blocks = get_db_blocks(conn)
         for block in blocks:
             print 'block:', block
             for client in ws_server.block_watchers:
                 ws_server.send_message(client, block)
示例#10
0
def payouts():
    from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
    from bitrisk.bitcoind_config import read_default_config
    config = read_default_config()
    testnet = ''
    if config.has_key('testnet'):
        testnet = config['testnet']
    rpc_user = config['rpcuser']
    rpc_password = config['rpcpassword']
    host = os.getenv('HOST', '127.0.0.1')
    rpc_connection = AuthServiceProxy("http://%s:%s@%s:%s8332"%(rpc_user, rpc_password, host, testnet))

    while not evt.wait(1 * 60):
        print 'process payouts/refunds'
        balance = rpc_connection.getbalance(bitrisk.BETS, 1)
        print 'balance:', balance
        print 'payouts:'
        payouts = conn.execute('select * from payout').fetchall()
        for payout in payouts:
            if not payout['processed']:
                process_payout(rpc_connection, payout)
        print 'refunds:'
        refunds = conn.execute('select * from refund').fetchall()
        for refund in refunds:
            if not refund['processed']:
                process_refund(rpc_connection, refund)
示例#11
0
def local_push(tx, rpc_user=None, rpc_password=None):
    """ Pushes a bitcoin transaction to the network using a local rpc server.

    :param tx: transaction to be pushed.
    :type tx: hex str
    :param rpc_user: rpc user (could be set in bitcoin.conf).
    :type rpc_user: str
    :param rpc_password: rpc password ((could be set in bitcoin.conf).
    :type rpc_password: str
    :return: The response of the rpc server, corresponding to the transaction id and a 200 code if its correctly pushed. None and code 500 otherwise
    :rtype: str, int
    """

    rpc_connection = AuthServiceProxy("http://"+rpc_user+":"+rpc_password+"@127.0.0.1:18332")

    try:
        tx_hash = rpc_connection.sendrawtransaction(tx)
        code = 200
        print "Transaction broadcast " + tx_hash
    except JSONRPCException as e:
        print e.message
        tx_hash = None
        code = 500

    return tx_hash, code
示例#12
0
    def getAccountAddress(self, acc=""):
        global walletProcess
        global rpc_conn

        if not walletProcess or not walletProcess.is_running():
            self.__copyMyriadcoinConf()

            self.openWallet(shell=False)

        count = 0
        walletAddress = None
        MAX_ITER = 60

        while not walletAddress and count < MAX_ITER:
            try:
                rpc_conn = AuthServiceProxy("http://%s:%[email protected]:%s"%(USER, self.password, PORT))
                walletAddress = rpc_conn.getaccountaddress('MyriadSwitcher_' + acc)
                #walletAddress = rpc_conn.getaccountaddress("Myriad_Switcher")
            except:
                pass

            count += 1
            time.sleep(1)

        return walletAddress
示例#13
0
def runbackup():

    access = AuthServiceProxy("http://*****:*****@127.0.0.1:8709")
    file_name = strftime("%Y%m%d%H%M%S", gmtime()) + '.bak'
    home_dir = '/home/pi/'
    usb = '/mnt/usb'
    move = 'sudo mv ' + home_dir + file_name + usb
    saved_file = usb + file_name

    if not os.path.exists(usb):
        os.system('sudo mkdir ' + usb)
    
    try:
        lcd_init()

        signal_success()                    # Flash drive located, starting to backup

        if os.path.exists("/dev/sda1"):
            lcd_byte(LCD_LINE_1, LCD_CMD)
            lcd_string("Pi Wallet", 2)
            lcd_byte(LCD_LINE_2, LCD_CMD)
            lcd_string("Backing Up", 2)

            os.system("sudo umount /dev/sda1")

            os.system("sudo mount -t vfat /dev/sda1 " + usb)
            sleep(1)
            access.backupwallet(home_dir + file_name)
            os.system(move)
            sleep(1)

            if os.path.exists(saved_file):
                lcd_byte(LCD_LINE_1, LCD_CMD)
                lcd_string("Backup Complete", 2)
                lcd_byte(LCD_LINE_2, LCD_CMD)
                lcd_string("Remove USB", 2)

            else:
                lcd_byte(LCD_LINE_1, LCD_CMD)
                lcd_string("Backup Failed", 2)
                lcd_byte(LCD_LINE_2, LCD_CMD)
                lcd_string("Check USB", 2)

        else:
            lcd_byte(LCD_LINE_1, LCD_CMD)
            lcd_string("Backup Failed", 2)
            lcd_byte(LCD_LINE_2, LCD_CMD)
            lcd_string("No USB Detected", 2)

        sleep(2)

    except:
        lcd_byte(LCD_LINE_1, LCD_CMD)
        lcd_string("Backup Failed", 2)
        lcd_byte(LCD_LINE_2, LCD_CMD)
        lcd_string("Check Wallet", 2)

    os.system('sudo umount ' + usb)

    sleep(2)
    def handle(self, *args, **options):

        rpc = AuthServiceProxy('http://' + settings.BITCOIN_RPC_USERNAME + ':' + settings.BITCOIN_RPC_PASSWORD + '@' + settings.BITCOIN_RPC_IP + ':' + str(settings.BITCOIN_RPC_PORT))

        private_keys_imported = False

        for address in Address.objects.all():
            address_found = True
            try:
                rpc.dumpprivkey(address.address)
            except JSONRPCException as e:
                if e.code == -4:
                    # Address is not found
                    print 'Address ' + address.address + ' was not found. Importing it...'

                    # Do some key magic
                    new_address_full_path = address.wallet.path + [address.subpath_number]
                    new_address_full_path_str = '/'.join([str(i) for i in new_address_full_path])
                    key = Key.from_text(settings.MASTERWALLET_BIP32_KEY)
                    subkey = key.subkeys(new_address_full_path_str).next()

                    # Check address and form the private key
                    btc_address = subkey.address(use_uncompressed=False)
                    assert btc_address == address.address
                    btc_private_key = subkey.wif(use_uncompressed=False)

                    # Do the importing
                    rpc.importprivkey(btc_private_key, '', False)
                    private_keys_imported = True

        if private_keys_imported:
            print 'Note! Private keys were added, but they were not scanned! Please restart bitcoin with -rescan option!'
示例#15
0
def process_withdraw_transactions(ticker=None):
    if not ticker:
        for c in Currency.objects.all():
            process_withdraw_transactions.delay(c.ticker)
        return

    currency = Currency.objects.select_for_update().get(ticker=ticker)
    coin = AuthServiceProxy(currency.api_url)

    wtxs = WithdrawTransaction.objects.select_for_update().select_related('wallet').filter(currency=currency, txid=None).order_by('wallet')

    transaction_hash = {}
    for tx in wtxs:
        if tx.address in transaction_hash:
            transaction_hash[tx.address] += tx.amount
        else:
            transaction_hash[tx.address] = tx.amount

    if currency.dust > Decimal('0'):
        for address, amount in transaction_hash.items():
            if amount < currency.dust:
                wtxs = wtxs.exclude(currency=currency, address=address)
                del transaction_hash[address]

    if not transaction_hash:
        return

    txid = coin.sendmany(settings.CC_ACCOUNT, transaction_hash)

    if not txid:
        return

    fee = coin.gettransaction(txid).get('fee', 0) * -1
    if not fee:
        fee_per_tx = 0
    else:
        fee_per_tx = (fee / len(wtxs))

    fee_hash = defaultdict(lambda : {'fee': Decimal("0"), 'amount': Decimal('0')})

    for tx in wtxs:
        fee_hash[tx.wallet]['fee'] += fee_per_tx
        fee_hash[tx.wallet]['amount'] += tx.amount

    for (wallet, data) in fee_hash.iteritems():
        Operation.objects.create(
            wallet=wallet,
            holded=-data['amount'],
            balance=-data['fee'],
            description='Network fee',
            reason=tx
        )

        wallet = Wallet.objects.get(id=tx.wallet.id)
        wallet.balance -= data['fee']
        wallet.holded -= data['amount']
        wallet.save()

    wtxs.update(txid=txid, fee=fee_per_tx)
示例#16
0
 def save(self, *args, **kwargs):
     if self.pk is None:
         if not self.currency in settings.CONNECTION_STRING:
             raise Exception("Connection string for %s is not defined" % self.currency)
         try:
             access = AuthServiceProxy(settings.CONNECTION_STRING[self.currency])
             self.addr = access.getnewaddress(settings.GENERATED_ADDRESSES_ACCOUNT)
         except Exception, e:
             raise Exception("Could not connect to crypto coin daemon")
示例#17
0
    def checkAddress(self, address):
        global rpc_conn

        if not rpc_conn:
            rpc_conn = AuthServiceProxy("http://%s:%[email protected]:%s"%(USER, self.password, PORT))

        validateData = rpc_conn.validateaddress(address)

        return validateData['isvalid'] and validateData['ismine']
示例#18
0
def api_decodebyclaim(claimid):
    connection_string = get_lbrycrdd_connection_details(os.path.expanduser("~")+"/.lbrycrd/lbrycrd.conf")
    rpc = AuthServiceProxy(connection_string)
    claim = rpc.getvalueforname(claimid)
    if claim:
        converted = ''.join([chr(ord(i)) for i in claim['value']])
        decoded = smart_decode(converted)
        claim['value'] = decoded.claim_dict
        return json.dumps(claim)
示例#19
0
def refill_addresses_queue():
    for currency in Currency.objects.all():
        coin = AuthServiceProxy(currency.api_url)
        count = Address.objects.filter(currency=currency, active=True, wallet=None).count()

        if count < settings.CC_ADDRESS_QUEUE:
            for i in range(count, settings.CC_ADDRESS_QUEUE):
                try:
                    Address.objects.create(address=coin.getnewaddress(settings.CC_ACCOUNT), currency=currency)
                except (socket_error, CannotSendRequest) :
                    pass
def main():
    # rpc_user and rpc_password are set in the bitcoin.conf file
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" % (rpc_user, rpc_password))
    best_block_hash = rpc_connection.getbestblockhash()
    print(rpc_connection.getblock(best_block_hash))

    # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips:
    commands = [["getblockhash", height] for height in range(100)]
    block_hashes = rpc_connection.batch_(commands)
    blocks = rpc_connection.batch_([["getblock", h] for h in block_hashes])
    block_times = [block["time"] for block in blocks]
    print(block_times)
class LongpollThread(threading.Thread):
    def __init__(self, node):
        threading.Thread.__init__(self)
        # query current longpollid
        templat = node.getblocktemplate()
        self.longpollid = templat['longpollid']
        # create a new connection to the node, we can't use the same
        # connection from two threads
        self.node = AuthServiceProxy(node.url, timeout=600)

    def run(self):
        self.node.getblocktemplate({'longpollid':self.longpollid})
示例#22
0
def getLastBlock(cfg):
    blk = 0
    while not done:
        try: # this tries to talk to bitcoind despite it being comatose
            rpc = AuthServiceProxy(cfg['rpc'], timeout=120)
            if blk == 0:
                blkinfo = rpc.getblockchaininfo()
                blk = blkinfo['blocks'] - 60
            blkhash = rpc.getblockhash(blk) # trailing by 60 to avoid reorg problems
            return blk,blkhash
        except Exception, e:
            log( 'Blkdat rpc ' + str(e) + ' trying again' )
            sleep(5) 
def rpc_loop(ncurses_q, json_q):
	config = ConfigParser.ConfigParser()
	config.read('bitcoind-ncurses.conf')
	rpcuser = config.get('rpc', 'rpcuser')
	rpcpassword = config.get('rpc', 'rpcpassword')
	rpcip = config.get('rpc', 'rpcip')
	rpcport = config.get('rpc', 'rpcport')

	rpcurl = "http://" + rpcuser + ":" + rpcpassword + "@" + rpcip + ":" + rpcport
	rpchandle = AuthServiceProxy(rpcurl, None, 500)

	last_blockcount = 0		# ensures block info is updated initially
	last_update = time.time() - 2
	while 1:
		try: s = json_q.get(False)
		except: s = {}
		
		if 'blockheight' in s:
			blockhash = rpchandle.getblockhash(s['blockheight'])
			blockinfo = rpchandle.getblock(blockhash)
			ncurses_q.put(blockinfo)
			last_blockcount = cur_blockcount
		elif 'txid' in s:
			raw_tx = rpchandle.getrawtransaction(s['txid'])
			decoded_tx = rpchandle.decoderawtransaction(raw_tx)
			ncurses_q.put(decoded_tx)

		if (time.time() - last_update) > 2: 
			info = rpchandle.getinfo()
			ncurses_q.put(info)
		
			nettotals = rpchandle.getnettotals()
			ncurses_q.put(nettotals)
	
			walletinfo = rpchandle.getwalletinfo()
			ncurses_q.put(walletinfo)

			cur_blockcount = info['blocks']
			if (cur_blockcount != last_blockcount):		# minimise RPC calls
				#if (last_blockcount == 0):
				lastblocktime = {'lastblocktime': time.time()}
				ncurses_q.put(lastblocktime)
				
				blockhash = rpchandle.getblockhash(cur_blockcount)
				blockinfo = rpchandle.getblock(blockhash)
				ncurses_q.put(blockinfo)
				last_blockcount = cur_blockcount
			
			last_update = time.time()

		time.sleep(0.5)		# minimise RPC calls
示例#24
0
class BitcoinService:
    def __init__(self):
        self.access = AuthServiceProxy("http://*****:*****@[email protected]:8332")

    def getInfo(self):
        return self.access.getinfo()
        
    def getlistreceivedbyaddress(self, num):
        return self.access.listreceivedbyaddress(num)

    def getBalance(self):
        return self.access.getbalance()

    def isValidAddress(self, addr):
        return self.access.validateaddress(addr)

    def generateAddress(self):
        return self.access.getnewaddress()

    def getAccount(self, addr):
        return self.access.getaccount(addr)

    def getAccountAddr(self, account):
        return self.access.getaccountaddress(account)
    
    def getreceivedbyaddress(self, addr):
        return self.access.getreceivedbyaddress(addr)

    def sendtoaddress(self, addr, amount, comment=""):
        return self.acccess.sendtoaddress(addr, amount, comment)
示例#25
0
def api_decode(txid, nout):
    connection_string = get_lbrycrdd_connection_details(os.path.expanduser("~")+"/.lbrycrd/lbrycrd.conf")
    rpc = AuthServiceProxy(connection_string)
    result = rpc.getclaimsfortx(txid)
    claim = None
    for claim_out in result:
        if claim_out['nOut'] == int(nout):
            claim = claim_out
            break
    if claim:
        converted = ''.join([chr(ord(i)) for i in claim['value']])
        decoded = smart_decode(converted)
        claim['value'] = decoded.claim_dict
        return json.dumps(claim)
示例#26
0
    def connect_JSON(self, connect, passphrase):
        """Connect to a bitcoin JSON-RPC server"""
        try:
            result = AuthServiceProxy(connect)
            # ServiceProxy is lazy-connect, so send an RPC command mostly to catch connection errors,
            # but also make sure the bitcoind we're talking to is/isn't testnet:
            if result.getmininginfo()['testnet']:
                self.logger.info("Connected to testnet")
            else:
                self.logger.info("Connected to mainnet")
            result.passphrase = passphrase

            return result
        except:
            raise
示例#27
0
def main():
    access = ServiceProxy("http://*****:*****@127.0.0.1:8332")
    txid = sys.argv[1]
    # print txid
    txinfo = access.gettransaction(txid)
    pprint.pprint(txinfo)

    # Check the details to make sure it's an incoming transaction
    # Ensure everything is incoming, to avoid 
    # mirroring change/consolidation transactions.    
    myaddresses = set()
    for details in txinfo["details"]:
        # print details
        if details["category"] != "receive":
            return
        myaddresses.add(details["address"])

    tx = access.decoderawtransaction(txinfo["hex"])
    pprint.pprint(tx)
    # Now gather all the outputs to send back
    newtx_inputs = []
    total_amount = Decimal(0)
    for vout in tx["vout"]:
        for address in vout["scriptPubKey"]["addresses"]:
            if address in myaddresses:
                newtx_inputs.append({"txid":txid,"vout":vout["n"]})
                total_amount += vout["value"]
                break

    print newtx_inputs
    print total_amount
    
    # Now find the sendign addresses, and choose one at random
    # to receive the funds.
    total_inputs = Decimal("0")
    addresses = []
    for vin in tx["vin"]:
        intxid = vin["txid"]
        invout = vin["vout"]

        # Get the outputs of the input transaction
        intx = access.getrawtransaction(intxid,1)
        # print intxid, invout
        vout = intx["vout"][invout]
        # pprint.pprint(vout)
        total_inputs += vout["value"]
        addresses.extend(vout["scriptPubKey"]["addresses"])
    print "Total inputs: %f" % total_inputs
    print addresses
    to_address = random.choice(addresses)

    # Build a transaction with the output of the original transaction as input
    # and to_address as output.
    newtx_hex = access.createrawtransaction(newtx_inputs,{to_address:float(total_amount)})
    newtx_hex = access.signrawtransaction(newtx_hex)["hex"]
    print newtx_hex
    # print >>open("/home/user/a.txt","a"), access.decoderawtransaction(newtx_hex)
    access.sendrawtransaction(newtx_hex)
示例#28
0
 def connect(self):
     "Connect to RPC endpoint"
     self.log.info("DogeRPC: Connecting to dogecoind")
     self.con = AuthServiceProxy("http://%s:%s@%s:%s" % (self.config["username"], self.config["password"],
                                 self.config["host"], self.config["port"]))
     self.con.getinfo()
     self.log.info("DogeRPC: Connected to %s:%s" % (self.config["host"], self.config["port"]))
示例#29
0
def run_allowip_test(tmpdir, allow_ips, rpchost, rpcport):
    """
    Start a node with rpcwallow IP, and request getinfo
    at a non-localhost IP.
    """
    base_args = ["-disablewallet", "-nolisten"] + ["-rpcallowip=" + x for x in allow_ips]
    nodes = start_nodes(1, tmpdir, [base_args])
    try:
        # connect to node through non-loopback interface
        url = "http://*****:*****@%s:%d" % (rpchost, rpcport)
        node = AuthServiceProxy(url)
        node.getinfo()
    finally:
        node = None  # make sure connection will be garbage collected and closed
        stop_nodes(nodes)
        wait_bitcoinds()
示例#30
0
def run_allowip_test(tmpdir, allow_ips, rpchost):
    '''
    Start a node with rpcwallow IP, and request getinfo
    at a non-localhost IP.
    '''
    base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips]
    nodes = start_nodes(1, tmpdir, [base_args])
    try:
        # connect to node through non-loopback interface
        url = "http://*****:*****@%s:%d" % (rpchost, START_RPC_PORT,)
        node = AuthServiceProxy(url)
        node.getinfo()
    finally:
        node = None # make sure connection will be garbage collected and closed
        stop_nodes(nodes)
        wait_creditsds()
示例#31
0
def rpc_conn(user=rpc_user, password=rpc_password):
    rpc_conn = AuthServiceProxy("http://%s:%s@localhost:19998" %
                                (user, password))
    return rpc_conn
示例#32
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import pprint
import struct

bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:8332")
mikes = "mgsUnLsMhphUyFiiCDBa6UWqhDivWaNwSe"
mine = "mpbJSfca2Z5vwB5ogMhPTJ3DBx2fZDMkGz"
pubkeymike = "03985457b91195038af91c1c7b09c693e9a141681319b683c46871b2dbf1beb09d"
pubkeymine = "03133255e240ded2cd1d0b8fa5b659fd7713b74f6ea074f4a80e3cb1f0677df061"
multiaddress = "2NC9uL5Qcnt4awdTBi3jB6ADPVdvwP9iR8L"
redeemscript = "522103133255e240ded2cd1d0b8fa5b659fd7713b74f6ea074f4a80e3cb1f0677df0612103985457b91195038af91c1c7b09c693e9a141681319b683c46871b2dbf1beb09d52ae"

random = "mrTRKLaDF86gLdKnacjxkXJdRFS6S8poTY"
txtorandom = "bdad390ea15f1828466266a97d4f219ff2e64111a60cb204b30148b199260bd0"
random2 = "mmQDjcub4ozy7e7ET6Yx75MDcLQHdhofbU"
#print "NEW", bitcoin.getnewaddress("")
#print bitcoin.validateaddress(mine)



print ""
print "Addresses and priv keys in entire wallet"
addresses = bitcoin.getaddressesbyaccount("")
print addresses
for add in addresses:
    if not add[0] == '2':
        print bitcoin.dumpprivkey(add), add



## Create a multi-sig
示例#33
0
import logging

from config import *

logging.basicConfig(format='%(asctime)s %(message)s',
                    filename=config.CHECK_LOG_FILE,
                    level=logging.INFO)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)-12s: %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

from bitcoinrpc.authproxy import AuthServiceProxy

access = AuthServiceProxy(RPC_URL)

vout_type = {
    'nonstandard': 0,
    'pubkey': 1,
    'pubkeyhash': 2,
    'scripthash': 3,
    'multisig': 4,
    'nulldata': 5
}


def read_tx(txhash):
    return json.loads(access.getrawtransaction(txhash, 1))

示例#34
0
 def __init__(self, url=None, path=None):
     super().__init__()
     self.rpc = AuthServiceProxy(url)
示例#35
0
def update_bitcoin_other_info():
    global mynode_block_height
    global bitcoin_blockchain_info
    global bitcoin_recent_blocks
    global bitcoin_recent_blocks_last_cache_height
    global bitcoin_peers
    global bitcoin_network_info
    global bitcoin_mempool
    global bitcoin_wallets

    while bitcoin_blockchain_info == None:
        # Wait until we have gotten the important info...
        # Checking quickly helps the API get started faster
        time.sleep(1)

    try:
        rpc_user = get_bitcoin_rpc_username()
        rpc_pass = get_bitcoin_rpc_password()

        rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" %
                                          (rpc_user, rpc_pass),
                                          timeout=60)

        # Get other less important info
        try:
            # Recent blocks
            if mynode_block_height != bitcoin_recent_blocks_last_cache_height:
                commands = [["getblockhash", height]
                            for height in range(mynode_block_height -
                                                9, mynode_block_height + 1)]
                block_hashes = rpc_connection.batch_(commands)
                bitcoin_recent_blocks = rpc_connection.batch_(
                    [["getblock", h] for h in block_hashes])
                bitcoin_recent_blocks_last_cache_height = mynode_block_height

            # Get peers
            bitcoin_peers = rpc_connection.getpeerinfo()

            # Get network info
            bitcoin_network_info = rpc_connection.getnetworkinfo()

            # Get mempool
            bitcoin_mempool = rpc_connection.getmempoolinfo()

            # Get wallet info
            wallets = rpc_connection.listwallets()
            wallet_data = []
            for w in wallets:
                wallet_name = urllib.pathname2url(w)
                wallet_rpc_connection = AuthServiceProxy(
                    "http://%s:%[email protected]:8332/wallet/%s" %
                    (rpc_user, rpc_pass, wallet_name),
                    timeout=60)
                wallet_info = wallet_rpc_connection.getwalletinfo()
                wallet_data.append(wallet_info)
            bitcoin_wallets = wallet_data
        except:
            pass

    except Exception as e:
        print("ERROR: In update_bitcoin_info - {}".format(str(e)))
        return False

    return True
示例#36
0
class LocalBlockchainRPCReader(BlockExplorerReader):

    rpc_connection = None

    #transaction_output_cache    = None ''' deprecated '''

    def __init__(self, database_connector=None):
        BlockExplorerReader.__init__(self, database_connector)  #super

        self.rpc_connection = AuthServiceProxy(
            "http://%s:%s@%s:%s" %
            (self.config.RPC_USERNAME, self.config.RPC_PASSWORD,
             self.config.RPC_HOST, self.config.RPC_PORT))

    def get_current_blockchain_block_height(self):
        raise NotImplementedError  #TODO maybe... for now can use another class

    #Retreives a list of transactions at specified block height. Each tx
    #   will be formatted as a BCI-like tuple per
    #   get_bci_like_tuple_for_tx_id().
    #param0: block_height: Height at which to get a list of txs for.
    #param1: use_tx_out_addr_cache_only (Optional): When looking up addresses
    #   for previous transactions, ONLY refer to cache in SQLite database,
    #   rather than slower option of using RPC interface. If set to True,
    #   process will sleep until the data is available in the cache. Default:
    #   False.
    def get_tx_list(self, block_height, use_tx_out_addr_cache_only=False):
        ids = self.get_tx_ids_at_height(block_height)

        txs = []
        for tx_id in ids:
            bci_like_tuple = self.get_bci_like_tuple_for_tx_id(
                tx_id, use_tx_out_addr_cache_only)
            txs.append(bci_like_tuple)
        return txs

    #Checks if the specified transaction is the first time the specified address
    #   has received funds. If it is, it will cache this for the specified
    #   block height in the database so subsequent lookups will answer
    #   correctly. IMPORTANT: This function assumes that that blocks are being
    #   processed in a complete, monotonically-increasing fashion from the
    #   genesis block. Otherwise, correct results not guaranteed! It is the
    #   caller's responsibility to ensure that enough blocks have been
    #   processed.
    def is_first_transaction_for_address(self,
                                         addr,
                                         tx_id,
                                         block_height,
                                         benchmarker=None):
        if self.database_connector.has_address_been_seen_cache_if_not(
                addr, block_height):
            dprint("Address %s at block height %d was already seen." %
                   (addr, block_height))
            return False
        else:
            dprint("Address %s at block height %d has no prior tx history." %
                   (addr, block_height))
            return True

    def get_block_hash_at_height(self, block_height):
        return self.rpc_connection.getblockhash(block_height)

    def get_tx_json_for_block_hash(self, block_hash):
        return self.rpc_connection.getblock(block_hash)

    def get_tx_ids_at_height(self, block_height):
        block_hash = self.get_block_hash_at_height(block_height)
        tx_json = self.get_tx_json_for_block_hash(block_hash)
        tx_ids = []
        for tx_id in tx_json['tx']:
            tx_ids.append(tx_id)
        return tx_ids

    #Returns the transaction in raw format. If the requested transaction is
    #   the sole transaction of the genesis block, bitcoind's RPC interface
    #   will throw an error 'No information available about transaction
    #   (code -5)' so we preempt this by raising a custom error that callers
    #   should handle; iterating callers should just move onto the next tx.
    #throws: NoDataAvailableForGenesisBlockError
    def get_raw_tx(self, tx_id):
        if tx_id == (
                '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7af'
                'deda33b'):
            raise custom_errors.NoDataAvailableForGenesisBlockError()
        else:
            return self.rpc_connection.getrawtransaction(tx_id)

    #Gets a human-readable string of the transaction in JSON format.
    def get_decoded_tx(self, tx_id):
        try:
            return self.rpc_connection.decoderawtransaction(
                self.get_raw_tx(tx_id))
        except custom_errors.NoDataAvailableForGenesisBlockError:
            #bitcoind won't generate this, but here's what it would look like
            genesis_json = {
                'txid': ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2'
                         '127b7afdeda33b'),
                'version':
                1,
                'locktime':
                0,
                'vin': [{
                    "sequence":
                    4294967295,
                    'coinbase':
                    ('04ffff001d0104455468652054696d65732030332f4a6'
                     '16e2f32303039204368616e63656c6c6f72206f6e2062'
                     '72696e6b206f66207365636f6e64206261696c6f75742'
                     '0666f722062616e6b73')
                }],
                'vout': [{
                    'value': 50.00000000,
                    'n': 0,
                    'scriptPubKey': {
                        'asm': ('04678afdb0fe5548271967f1a67130b7105cd6a828'
                                'e03909a67962e0ea1f61deb649f6bc3f4cef38c4f3'
                                '5504e51ec112de5c384df7ba0b8d578a4c702b6bf1'
                                '1d5f OP_CHECKSIG'),
                        'hex': ('4104678afdb0fe5548271967f1a67130b7105cd6a8'
                                '28e03909a67962e0ea1f61deb649f6bc3f4cef38c4'
                                'f35504e51ec112de5c384df7ba0b8d578a4c702b6b'
                                'f11d5fac'),
                        'reqSigs':
                        1,
                        'type':
                        'pubkey',
                        'addresses': ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa']
                    }
                }]
            }
            return genesis_json

    #Converts required infromation from local bitcoind RPC into a format similar
    #   to that returned by Blockchain.info's API. This helps to make the code
    #   more agnostic as to the source of blockchain data.
    #Note: When an output address cannot be decoded, BCI excludes the "addr"
    #   field from the JSON returned. Therefore, this function will do the same.
    #   See:
    #   https://blockchain.info/tx/cee16a9b222f636cd27d734da0a131cee5dd7a1d09cb5f14f4d1330b22aaa38e
    #Note: When a previous output address for an input cannot be decoded, BCI
    #   excludes the "addr" field from the JSON returned. Therefore, this
    #   function will do the same. See:
    #   https://blockchain.info/tx/8ebe1df6ebf008f7ec42ccd022478c9afaec3ca0444322243b745aa2e317c272
    #param0: tx_id: Specified transaction hash
    #param1: use_tx_out_addr_cache_only (Optional): When looking up addresses
    #   for previous transactions, ONLY refer to cache in SQLite database,
    #   rather than slower option of using RPC interface. If set to True,
    #   process will sleep until the data is available in the cache. Default:
    #   False.
    def get_bci_like_tuple_for_tx_id(self,
                                     tx_id,
                                     use_tx_out_addr_cache_only=False):
        json_tuple = {}
        json_tuple['hash'] = tx_id
        json_tuple['inputs'] = []
        json_tuple['out'] = []

        subscription = None
        if use_tx_out_addr_cache_only:
            subscription = data_subscription.TxOutputAddressCacheSubscriber(
                database=self.database_connector)

        tx_json = self.get_decoded_tx(tx_id)

        #populate input addresses
        for vin in tx_json['vin']:
            #look up address based on its previous transaction
            prev_txid = None
            if 'txid' in vin:
                prev_txid = vin['txid']
            prev_vout = None
            if 'vout' in vin:
                prev_vout_num = vin[
                    'vout']  #yes, this RPC field is poorly named
                prev_out = {'n': prev_vout_num}
                try:
                    if use_tx_out_addr_cache_only:
                        #flag specifies that we will wait for cache to catch up
                        #   before continuing this operation. Process/thread
                        #   will sleep until then.
                        subscription.next_tx_id_needed = prev_txid
                        subscription.next_prev_tx_ouput_pos_needed = prev_vout_num
                        dprint(
                            ("get_bci_like_tuple_for_tx_id: May sleep until "
                             "tx output address is cached..."))
                        subscription.do_sleep_until_producers_ready()

                    address = self.get_output_address(prev_txid, prev_vout_num)
                    prev_out['addr'] = address
                except custom_errors.PrevOutAddressCannotBeDecodedError:
                    pass
                current_input = {'prev_out': prev_out}
                json_tuple['inputs'].append(current_input)
            else:
                #If there's no index specifying the txo from prev tx, there's
                #   probably nothing to do here. Should only come up for
                #   coinbase transactions.
                continue

        #populate output addresses
        for vout in tx_json['vout']:
            output_index = vout['n']
            current_output = {'n': output_index}
            if 'scriptPubKey' in vout and 'addresses' in vout['scriptPubKey']:
                address = vout['scriptPubKey']['addresses'][0]
                current_output['addr'] = address
            json_tuple['out'].append(current_output)

        return json_tuple

    #Returns an ordered list of output addresses for the specified transaction
    #   JSON as returned by the bitcoind RPC interface. If an address cannot be
    #   decoded for one of the outputs, a value of None will be inserted
    #   at that position in the list.
    #TODO: This does not properly handle multisig outputs that list multiple
    #   addresses per output. See:
    #   http://bitcoin.stackexchange.com/questions/4687/can-a-scriptpubkey-have-multiple-addresses
    #   When support for this is added, make sure to add a test case.
    def get_output_addresses(self, tx_json):
        assert 'vout' in tx_json
        output_addresses = []
        for vout in tx_json['vout']:
            assert 'scriptPubKey' in vout
            if 'addresses' in vout['scriptPubKey']:
                ouput_address = vout['scriptPubKey']['addresses'][0]
                output_addresses.append(ouput_address)
            else:
                output_addresses.append(None)
        return output_addresses

    #Raises: custom_errors.PrevOutAddressCannotBeDecoded
    #TODO: This does not properly handle multisig outputs that list multiple
    #   addresses per output.
    def get_output_address(self, tx_id, output_index, tx_json=None):
        if USE_TX_OUTPUT_ADDR_CACHE_FIRST:
            addr = self.database_connector.get_output_address(
                tx_id, output_index)
            if addr is not None:
                return addr

        #not in cache, fall back to querying RPC interface
        if tx_json is None:
            tx_json = self.get_decoded_tx(tx_id)

        if 'vout' in tx_json and len(tx_json['vout']) > output_index and \
                'scriptPubKey' in tx_json['vout'][output_index]:
            if 'addresses' not in tx_json['vout'][output_index][
                    'scriptPubKey']:
                raise custom_errors.PrevOutAddressCannotBeDecodedError
            else:
                return tx_json['vout'][output_index]['scriptPubKey'][
                    'addresses'][0]
        else:
            msg = ("Missing element for vout in get_output_address() with tx "
                   "id %s and output index %d") % (tx_id, output_index)
            logger.log_and_die(msg)
示例#37
0
def connect():
    global rpc_connection
    username, password = vertcoin_config.read_vertcoin_config()
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:5888" %
                                      (username, password))
示例#38
0
def get_rpc_connection():
    from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
    connection = "http://%s:%[email protected]:%s" % (rpc_user, rpc_pass, rpc_port)
    #print("Connection: " + connection)
    rpc_connection = AuthServiceProxy(connection)
    return (rpc_connection)
示例#39
0
 def rpc_connection(self):
     return AuthServiceProxy("http://{0}:{1}@{2}:{3}".format(*self.creds))
    def __init__(self, url):
        self.rpc = AuthServiceProxy(url)

        self.height = self.rpc.getblockcount()
class DaemonBTC:

    def __init__(self, url):
        self.rpc = AuthServiceProxy(url)

        self.height = self.rpc.getblockcount()


    def get_block(self, i):
        block = self.rpc.getblockhash(i)
        block_data = self.rpc.getblock(block)
        block_data['transactions'] = len(block_data['tx'])
        # Elasticsearch struggles with these as integers
        #block_data['chainwork_int'] = int(block_data['chainwork'], 16)
        block_data['difficulty'] = int(block_data['difficulty'])
        del(block_data['tx'])

        return block_data

    def get_transaction(self, tx):
        rtx = self.rpc.getrawtransaction(tx)
        dtx = self.rpc.decoderawtransaction(rtx)

        return dtx

    def get_transactions(self, txs):
        rtx = self.rpc.batch_([["getrawtransaction", t] for t in txs])
        dtx = self.rpc.batch_([["decoderawtransaction", t] for t in rtx])

        return dtx

    def get_block_transactions(self, block):

        # The genesis block is special
        if block == 0:
            return []

        blockhash = self.rpc.getblockhash(block)
        block_data = self.rpc.getblock(blockhash)

        transactions = []

        rtx = self.rpc.batch_([["getrawtransaction", t] for t in block_data['tx']])
        dtx = self.rpc.batch_([["decoderawtransaction", t] for t in rtx])

        for tx in dtx:
            tx['height'] = block_data['height']
            tx['block'] = block_data['hash']
            tx['time'] = block_data['time']

            # We can't use this data, let's get rid of it
            for i in tx['vin']:
                if 'scriptSig' in i:
                    del(i['scriptSig'])
            for i in tx['vout']:
                if 'hex' in i['scriptPubKey']:
                    del(i['scriptPubKey']['hex'])
                if 'asm' in i['scriptPubKey']:
                    del(i['scriptPubKey']['asm'])

            transactions.append(tx)

        return transactions

    def get_block_transactions_bulk(self, block):
        "Return an iterable object for bulk transactions"

        transactions = self.get_block_transactions(block)
        tx = Transactions()
        for i in transactions:
            tx.add_transaction(i)

        return tx

    def get_blocks_bulk(self, blocks):

        rbh = self.rpc.batch_([["getblockhash", t] for t in blocks])

        dbh = self.rpc.batch_([["get_block", t] for t in rbh])

        output = []
        for block_data in dbh:
            block_data['transactions'] = len(block_data['tx'])
            block_data['chainwork_int'] = int(block_data['chainwork'], 16)
            del(block_data['tx'])
            output.append(block_data)

        return output

    def get_max_block(self):
        return self.rpc.getblockcount()
示例#42
0
def call_node(commands):
    """Creates a proxy for communicating with a full bitcoin node
    """
    return AuthServiceProxy(CONNECTION_STRING).batch_(commands)
示例#43
0
class RpcClient:
    def __init__(self, rpc_protocol, rpc_host, rpc_user, rpc_password):
        # Lock for threads
        self.lock = threading.RLock()

        self.rpc_url = "%s://%s:%s@%s" % (rpc_protocol, rpc_user, rpc_password,
                                          rpc_host)

        host, port = rpc_host.split(":")
        if rpc_protocol == "https":
            self.httpConnection = httplib.HTTPSConnection(
                host,
                port,
                timeout=20,
                context=ssl._create_unverified_context())
        else:
            self.httpConnection = httplib.HTTPConnection(host,
                                                         port,
                                                         timeout=20)

        self.conn = AuthServiceProxy(self.rpc_url,
                                     timeout=1000,
                                     connection=self.httpConnection)

    @process_RPC_exceptions
    def getBlockCount(self):
        n = 0
        with self.lock:
            n = self.conn.getblockcount()

        return n

    @process_RPC_exceptions
    def getBlockHash(self, blockNum):
        h = None
        with self.lock:
            h = self.conn.getblockhash(blockNum)

        return h

    @process_RPC_exceptions
    def getBudgetVotes(self, proposal):
        votes = {}
        with self.lock:
            votes = self.conn.getbudgetvotes(proposal)

        return votes

    @process_RPC_exceptions
    def getFeePerKb(self):
        res = MINIMUM_FEE
        with self.lock:
            # get transaction data from last 200 blocks
            feePerKb = float(self.conn.getfeeinfo(200)['feeperkb'])
            res = (feePerKb if feePerKb > MINIMUM_FEE else MINIMUM_FEE)

        return res

    @process_RPC_exceptions
    def getMNStatus(self, address):
        mnStatus = None
        with self.lock:
            mnStatusList = self.conn.listmasternodes(address)
            if not mnStatusList:
                return None
            mnStatus = mnStatusList[0]
            mnStatus['mnCount'] = self.conn.getmasternodecount()['enabled']

        return mnStatus

    @process_RPC_exceptions
    def getMasternodeCount(self):
        ans = None
        with self.lock:
            ans = self.conn.getmasternodecount()

        return ans

    @process_RPC_exceptions
    def getMasternodes(self):
        printDbg("RPC: Getting masternode list...")
        mnList = {}
        score = []
        masternodes = []
        with self.lock:
            masternodes = self.conn.listmasternodes()

        for mn in masternodes:
            if mn.get('status') == 'ENABLED':
                # compute masternode score
                if mn.get('lastpaid') == 0:
                    mn['score'] = mn.get('activetime')
                else:
                    lastpaid_ago = now() - mn.get('lastpaid')
                    mn['score'] = min(lastpaid_ago, mn.get('activetime'))

            else:
                mn['score'] = 0

            score.append(mn)

        # sort masternodes by decreasing score
        score.sort(key=lambda x: x['score'], reverse=True)

        # save masternode position in the payment queue
        for mn in masternodes:
            mn['queue_pos'] = score.index(mn)

        mnList['masternodes'] = masternodes

        return mnList

    @process_RPC_exceptions
    def getNextSuperBlock(self):
        n = 0
        with self.lock:
            n = self.conn.getnextsuperblock()

        return n

    @process_RPC_exceptions
    def getProposals(self):
        printDbg("RPC: Getting proposals list...")
        proposals = []
        data = []
        with self.lock:
            # get proposals JSON data
            data = self.conn.getbudgetinfo()

        for p in data:
            # create proposal Object
            new_proposal = Proposal(p.get('Name'), p.get('URL'), p.get('Hash'),
                                    p.get('FeeHash'), p.get('BlockStart'),
                                    p.get('BlockEnd'),
                                    p.get('TotalPaymentCount'),
                                    p.get('RemainingPaymentCount'),
                                    p.get('PaymentAddress'), p.get('Yeas'),
                                    p.get('Nays'), p.get('Abstains'),
                                    float(p.get('TotalPayment')),
                                    float(p.get('MonthlyPayment')))
            # append object to list
            proposals.append(new_proposal)

        # return proposals list
        return proposals

    @process_RPC_exceptions
    def getProposalsProjection(self):
        printDbg("RPC: Getting proposals projection...")
        data = []
        proposals = []
        with self.lock:
            # get budget projection JSON data
            data = self.conn.getbudgetprojection()

        for p in data:
            # create proposal-projection dictionary
            new_proposal = {}
            new_proposal['Name'] = p.get('Name')
            new_proposal['Allotted'] = float(p.get("Alloted"))
            new_proposal['Votes'] = p.get('Yeas') - p.get('Nays')
            new_proposal['Total_Allotted'] = float(p.get('TotalBudgetAlloted'))
            # append dictionary to list
            proposals.append(new_proposal)

        # return proposals list
        return proposals

    @process_RPC_exceptions
    def getProtocolVersion(self):
        res = DEFAULT_PROTOCOL_VERSION
        with self.lock:
            prot_version = self.conn.getinfo().get('protocolversion')
            res = int(prot_version)

        return res

    @process_RPC_exceptions
    def getRawTransaction(self, txid):
        res = None
        with self.lock:
            res = self.conn.getrawtransaction(txid)

        return res

    @process_RPC_exceptions
    def getStatus(self):
        status = False
        statusMess = "Unable to connect to a PIVX RPC server.\n"
        statusMess += "Either the local PIVX wallet is not open, or the remote RPC server is not responding."
        n = 0
        response_time = None
        with self.lock:
            isTestnet = self.conn.getinfo()['testnet']
            n, response_time = timeThis(self.conn.getblockcount)
            if n is None:
                n = 0

        if n > 0:
            status = True
            statusMess = "Connected to PIVX Blockchain"

        return status, statusMess, n, response_time, isTestnet

    @process_RPC_exceptions
    def isBlockchainSynced(self):
        res = False
        response_time = None
        with self.lock:
            status, response_time = timeThis(self.conn.mnsync, 'status')
            if status is not None:
                res = status.get("IsBlockchainSynced")

        return res, response_time

    @process_RPC_exceptions
    def mnBudgetRawVote(self, mn_tx_hash, mn_tx_index, proposal_hash, vote,
                        time, vote_sig):
        res = None
        with self.lock:
            res = self.conn.mnbudgetrawvote(mn_tx_hash, mn_tx_index,
                                            proposal_hash, vote, time,
                                            vote_sig)

        return res

    @process_RPC_exceptions
    def decodemasternodebroadcast(self, work):
        printDbg("RPC: Decoding masternode broadcast...")
        res = ""
        with self.lock:
            res = self.conn.decodemasternodebroadcast(work.strip())

        return res

    @process_RPC_exceptions
    def relaymasternodebroadcast(self, work):
        printDbg("RPC: Relaying masternode broadcast...")
        res = ""
        with self.lock:
            res = self.conn.relaymasternodebroadcast(work.strip())

        return res

    @process_RPC_exceptions
    def sendRawTransaction(self, tx_hex, use_swiftx):
        dbg_mess = "RPC: Sending raw transaction"
        if use_swiftx:
            dbg_mess += " with SwiftX"
        dbg_mess += "..."
        printDbg(dbg_mess)
        tx_id = None
        with self.lock:
            tx_id = self.conn.sendrawtransaction(tx_hex, True,
                                                 bool(use_swiftx))

        return tx_id

    @process_RPC_exceptions
    def verifyMessage(self, pivxaddress, signature, message):
        printDbg("RPC: Verifying message...")
        res = False
        with self.lock:
            res = self.conn.verifymessage(pivxaddress, signature, message)

        return res
示例#44
0
# DB connection settings
WALLET_IP = config.get(CURRENCY, 'wallet_ip')
WALLET_PORT = config.get(CURRENCY, 'wallet_port')
WALLET_USERNAME = config.get(CURRENCY, 'wallet_username')
WALLET_PASSWORD = config.get(CURRENCY, 'wallet_password')
MIN_TX_CONFIRMATION = config.get(CURRENCY, 'min_tx_confirmation')

CURRENCY_NAME = config.get(CURRENCY, 'currency_name')
CURRENCY_SYMBOL = config.get(CURRENCY, 'currency_symbol')

# Constants
RE_EMOJI = re.compile('[\U00010000-\U0010ffff\U000026A1]', flags=re.UNICODE)

rpc = AuthServiceProxy(
    "http://%s:%s@%s:%s" %
    (WALLET_USERNAME, WALLET_PASSWORD, WALLET_IP, WALLET_PORT))


def strip_emoji(text):
    """
    Remove Emojis from tweet text to prevent issues with logging
    """
    logger.info('{}: removing emojis'.format(datetime.now()))
    text = str(text)
    return RE_EMOJI.sub(r'', text)


def get_account_balance(account_name):
    """
    Get the current balance of an account
示例#45
0
from bitcoinrpc.authproxy import AuthServiceProxy

access = AuthServiceProxy("http://*****:*****@172.17.0.3:18332")
print access.getbalance()
#Envoie 0,001 bitcoin a une nouvelle adresse
access.sendtoaddress(access.getnewaddress(), 0.001)
示例#46
0
def initialize_chain(test_dir):
    """
    Create (or copy from cache) a 200-block-long chain and
    4 wallets.
    filokd and filok-cli must be in search path.
    """

    if not os.path.isdir(os.path.join("cache", "node0")):
        devnull = open("/dev/null", "w+")
        # Create cache directories, run filokd:
        for i in range(4):
            datadir = initialize_datadir("cache", i)
            args = [
                os.getenv("BITCOIND", "filokd"), "-keypool=1",
                "-datadir=" + datadir, "-discover=0"
            ]
            if i > 0:
                args.append("-connect=127.0.0.1:" + str(p2p_port(0)))
            bitcoind_processes[i] = subprocess.Popen(args)
            subprocess.check_call([
                os.getenv("BITCOINCLI", "filok-cli"), "-datadir=" + datadir,
                "-rpcwait", "getblockcount"
            ],
                                  stdout=devnull)
        devnull.close()
        rpcs = []
        for i in range(4):
            try:
                url = "http://*****:*****@127.0.0.1:%d" % (rpc_port(i), )
                rpcs.append(AuthServiceProxy(url))
            except:
                sys.stderr.write("Error connecting to " + url + "\n")
                sys.exit(1)

        # Create a 200-block-long chain; each of the 4 nodes
        # gets 25 mature blocks and 25 immature.
        # blocks are created with timestamps 10 minutes apart, starting
        # at 1 Jan 2014
        block_time = 1388534400
        for i in range(2):
            for peer in range(4):
                for j in range(25):
                    set_node_times(rpcs, block_time)
                    rpcs[peer].setgenerate(True, 1)
                    block_time += 10 * 60
                # Must sync before next peer starts generating blocks
                sync_blocks(rpcs)

        # Shut them down, and clean up cache directories:
        stop_nodes(rpcs)
        wait_bitcoinds()
        for i in range(4):
            os.remove(log_filename("cache", i, "debug.log"))
            os.remove(log_filename("cache", i, "db.log"))
            os.remove(log_filename("cache", i, "peers.dat"))
            os.remove(log_filename("cache", i, "fee_estimates.dat"))

    for i in range(4):
        from_dir = os.path.join("cache", "node" + str(i))
        to_dir = os.path.join(test_dir, "node" + str(i))
        shutil.copytree(from_dir, to_dir)
        initialize_datadir(test_dir, i)  # Overwrite port/rpcport in filok.conf
示例#47
0
def get_rpc_connection():
    from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18501"%(rpc_user, rpc_pass))
    return(rpc_connection)
    def open_internal(self):
        """
        Try to establish connection to polis RPC daemon for current connection config.
        :return: True, if connection successfully establishes, False if user Cancels the operation (not always
            cancelling will be possible - only when user is prompted for a password).
        """
        if not self.active:
            logging.debug("Trying to open connection: %s" % self.cur_conn_def.get_description())
            try:
                # make the owner know, we are connecting
                if self.on_connection_initiated_callback:
                    self.on_connection_initiated_callback()
            except:
                pass

            if self.cur_conn_def.use_ssh_tunnel:
                # RPC over SSH
                if self.ssh is None:
                    self.ssh = PolisdSSH(self.cur_conn_def.ssh_conn_cfg.host, self.cur_conn_def.ssh_conn_cfg.port,
                                        self.cur_conn_def.ssh_conn_cfg.username)
                try:
                    logging.debug('starting ssh.connect')
                    self.ssh.connect()
                    logging.debug('finished ssh.connect')
                except Exception as e:
                    logging.error('error in ssh.connect')
                    try:
                        # make the owner know, connection attempt failed
                        if self.on_connection_failed_callback:
                            self.on_connection_failed_callback()
                    except:
                        logging.exception('on_connection_try_fail_callback call exception')
                    raise

                # configure SSH tunnel
                # get random local unprivileged port number to establish SSH tunnel
                success = False
                local_port = None
                for try_nr in range(1, 10):
                    try:
                        logging.debug(f'beginning ssh.open_tunnel, try: {try_nr}')
                        local_port = randint(2000, 50000)
                        self.ssh.open_tunnel(local_port,
                                             self.cur_conn_def.host,
                                             int(self.cur_conn_def.port))
                        success = True
                        break
                    except Exception as e:
                        logging.exception('error in ssh.open_tunnel loop: ' + str(e))
                logging.debug('finished ssh.open_tunnel loop')
                if not success:
                    logging.error('finished ssh.open_tunnel loop with error')
                    return False
                else:
                    rpc_user = self.cur_conn_def.username
                    rpc_password = self.cur_conn_def.password
                    rpc_host = '127.0.0.1'  # SSH tunnel on loopback
                    rpc_port = local_port
            else:
                # direct RPC
                rpc_host = self.cur_conn_def.host
                rpc_port = self.cur_conn_def.port
                rpc_user = self.cur_conn_def.username
                rpc_password = self.cur_conn_def.password

            if self.cur_conn_def.use_ssl:
                self.rpc_url = 'https://'
                self.http_conn = httplib.HTTPSConnection(rpc_host, rpc_port, timeout=5, context=ssl._create_unverified_context())
            else:
                self.rpc_url = 'http://'
                self.http_conn = httplib.HTTPConnection(rpc_host, rpc_port, timeout=5)

            self.rpc_url += rpc_user + ':' + rpc_password + '@' + rpc_host + ':' + str(rpc_port)
            logging.debug('AuthServiceProxy configured to: %s' % self.rpc_url)
            self.proxy = AuthServiceProxy(self.rpc_url, timeout=1000, connection=self.http_conn)

            try:
                # check the connection
                self.http_conn.connect()
                logging.debug('Successfully connected AuthServiceProxy')

                try:
                    # make the owner know, we successfully finished connection
                    if self.on_connection_successful_callback:
                        self.on_connection_successful_callback()
                except:
                    logging.exception('on_connection_finished_callback call exception')
            except:
                logging.exception('Connection failed')

                try:
                    # make the owner know, connection attempt failed
                    if self.on_connection_failed_callback:
                        self.on_connection_failed_callback()

                    if self.ssh:
                        # if there is a ssh connection established earlier, disconnect it because apparently it isn't
                        # functioning
                        self.ssh.disconnect()
                except:
                    logging.exception('on_connection_try_fail_callback call exception')
                raise
            finally:
                logging.debug('http_conn.close()')
                self.http_conn.close()
                # timeout hase been initially set to 5 seconds to perform 'quick' connection test
                self.http_conn.timeout = 20

            self.active = True
        return self.active
示例#49
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

rpc_user = '******'
rpc_password = '******'
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:6969"%(rpc_user, rpc_password))

print rpc_connection
print rpc_connection.getbalance()
class PolisdInterface(WndUtils):
    def __init__(self, window,
                 on_connection_initiated_callback=None,
                 on_connection_failed_callback=None,
                 on_connection_successful_callback=None,
                 on_connection_disconnected_callback=None):
        WndUtils.__init__(self, app_config=None)

        self.config = None
        self.db_intf = None
        self.connections = []
        self.cur_conn_index = 0
        self.cur_conn_def = None

        # below is the connection with which particular RPC call has started; if connection is switched because of
        # problems with some nodes, switching stops if we close round and return to the starting connection
        self.starting_conn = None

        self.masternodes = []  # cached list of all masternodes (Masternode object)
        self.masternodes_by_ident = {}
        self.payment_queue = []

        self.ssh = None
        self.window = window
        self.active = False
        self.rpc_url = None
        self.proxy = None
        self.http_conn = None  # HTTPConnection object passed to the AuthServiceProxy (for convinient connection reset)
        self.on_connection_initiated_callback = on_connection_initiated_callback
        self.on_connection_failed_callback = on_connection_failed_callback
        self.on_connection_successful_callback = on_connection_successful_callback
        self.on_connection_disconnected_callback = on_connection_disconnected_callback
        self.last_error_message = None
        self.http_lock = threading.Lock()

    def initialize(self, config: AppConfig, connection=None, for_testing_connections_only=False):
        self.config = config
        self.app_config = config
        self.db_intf = self.config.db_intf

        # conn configurations are used from the first item in the list; if one fails, then next is taken
        if connection:
            # this parameter is used for testing specific connection
            self.connections = [connection]
        else:
            # get connection list orderd by priority of use
            self.connections = self.config.get_ordered_conn_list()

        self.cur_conn_index = 0
        if self.connections:
            self.cur_conn_def = self.connections[self.cur_conn_index]
        else:
            self.cur_conn_def = None

        if not for_testing_connections_only:
            self.load_data_from_db_cache()

    def load_data_from_db_cache(self):
        self.masternodes.clear()
        self.masternodes_by_ident.clear()
        cur = self.db_intf.get_cursor()
        cur2 = self.db_intf.get_cursor()
        db_modified = False
        try:
            tm_start = time.time()
            db_correction_duration = 0.0
            logging.debug("Reading masternodes' data from DB")
            cur.execute("SELECT id, ident, status, protocol, payee, last_seen, active_seconds,"
                        " last_paid_time, last_paid_block, IP from MASTERNODES where pmt_active=1")
            for row in cur.fetchall():
                db_id = row[0]
                ident = row[1]

                # correct duplicated masternodes issue
                mn_first = self.masternodes_by_ident.get(ident)
                if mn_first is not None:
                    continue

                # delete duplicated (caused by breaking the app while loading)
                tm_start_1 = time.time()
                cur2.execute('DELETE from MASTERNODES where ident=? and id<>?', (ident, db_id))
                if cur2.rowcount > 0:
                    db_modified = True
                db_correction_duration += (time.time() - tm_start_1)

                mn = Masternode()
                mn.db_id = db_id
                mn.ident = ident
                mn.status = row[2]
                mn.protocol = row[3]
                mn.payee = row[4]
                mn.lastseen = row[5]
                mn.activeseconds = row[6]
                mn.lastpaidtime = row[7]
                mn.lastpaidblock = row[8]
                mn.ip = row[9]
                self.masternodes.append(mn)
                self.masternodes_by_ident[mn.ident] = mn

            tm_diff = time.time() - tm_start
            logging.info('DB read time of %d MASTERNODES: %s s, db fix time: %s' %
                         (len(self.masternodes), str(tm_diff), str(db_correction_duration)))
            self.update_mn_queue_values()
        except Exception as e:
            logging.exception('SQLite initialization error')
        finally:
            if db_modified:
                self.db_intf.commit()
            self.db_intf.release_cursor()
            self.db_intf.release_cursor()

    def reload_configuration(self):
        """Called after modification of connections' configuration or changes having impact on the file name
        associated to database cache."""

        # get connection list orderd by priority of use
        self.disconnect()
        self.connections = self.config.get_ordered_conn_list()
        self.cur_conn_index = 0
        if len(self.connections):
            self.cur_conn_def = self.connections[self.cur_conn_index]
            self.load_data_from_db_cache()
        else:
            self.cur_conn_def = None

    def disconnect(self):
        if self.active:
            logging.debug('Disconnecting')
            if self.ssh:
                self.ssh.disconnect()
                del self.ssh
                self.ssh = None
            self.active = False
            if self.on_connection_disconnected_callback:
                self.on_connection_disconnected_callback()

    def mark_call_begin(self):
        self.starting_conn = self.cur_conn_def

    def switch_to_next_config(self):
        """
        If there is another polisd config not used recently, switch to it. Called only when there was a problem
        with current connection config.
        :return: True if successfully switched or False if there was no another config
        """
        if self.cur_conn_def:
            self.config.conn_cfg_failure(self.cur_conn_def)  # mark connection as defective
        if self.cur_conn_index < len(self.connections)-1:
            idx = self.cur_conn_index + 1
        else:
            idx = 0

        conn = self.connections[idx]
        if conn != self.starting_conn and conn != self.cur_conn_def:
            logging.debug("Trying to switch to another connection: %s" % conn.get_description())
            self.disconnect()
            self.cur_conn_index = idx
            self.cur_conn_def = conn
            if not self.open():
                return self.switch_to_next_config()
            else:
                return True
        else:
            logging.warning('Failed to connect: no another connection configurations.')
            return False

    def mark_cur_conn_cfg_is_ok(self):
        if self.cur_conn_def:
            self.config.conn_cfg_success(self.cur_conn_def)

    def open(self):
        """
        Opens connection to polis RPC. If it fails, then the next enabled conn config will be used, if any exists.
        :return: True if successfully connected, False if user cancelled the operation. If all of the attempts
            fail, then appropriate exception will be raised.
        """
        try:
            if not self.cur_conn_def:
                raise Exception('There is no connections to Polis network enabled in the configuration.')

            while True:
                try:
                    if self.open_internal():
                        break
                    else:
                        if not self.switch_to_next_config():
                            return False
                except UserCancelledConnection:
                    return False
                except (socket.gaierror, ConnectionRefusedError, TimeoutError, socket.timeout,
                        NoValidConnectionsError) as e:
                    # exceptions raised by not likely functioning polisd node; try to switch to another node
                    # if there is any in the config
                    if not self.switch_to_next_config():
                        raise e  # couldn't use another conn config, raise exception
                    else:
                        break
        except Exception as e:
            self.last_error_message = str(e)
            raise

        return True

    def reset_connection(self):
        """
        Called when communication errors are detected while sending RPC commands. Here we are closing the SSH-tunnel
        (if used) and HTTP connection object to prepare for another try.
        :return:
        """
        if self.active:
            if self.http_conn:
                self.http_conn.close()
            if self.ssh:
                self.ssh.disconnect()
                self.active = False

    def open_internal(self):
        """
        Try to establish connection to polis RPC daemon for current connection config.
        :return: True, if connection successfully establishes, False if user Cancels the operation (not always
            cancelling will be possible - only when user is prompted for a password).
        """
        if not self.active:
            logging.debug("Trying to open connection: %s" % self.cur_conn_def.get_description())
            try:
                # make the owner know, we are connecting
                if self.on_connection_initiated_callback:
                    self.on_connection_initiated_callback()
            except:
                pass

            if self.cur_conn_def.use_ssh_tunnel:
                # RPC over SSH
                if self.ssh is None:
                    self.ssh = PolisdSSH(self.cur_conn_def.ssh_conn_cfg.host, self.cur_conn_def.ssh_conn_cfg.port,
                                        self.cur_conn_def.ssh_conn_cfg.username)
                try:
                    logging.debug('starting ssh.connect')
                    self.ssh.connect()
                    logging.debug('finished ssh.connect')
                except Exception as e:
                    logging.error('error in ssh.connect')
                    try:
                        # make the owner know, connection attempt failed
                        if self.on_connection_failed_callback:
                            self.on_connection_failed_callback()
                    except:
                        logging.exception('on_connection_try_fail_callback call exception')
                    raise

                # configure SSH tunnel
                # get random local unprivileged port number to establish SSH tunnel
                success = False
                local_port = None
                for try_nr in range(1, 10):
                    try:
                        logging.debug(f'beginning ssh.open_tunnel, try: {try_nr}')
                        local_port = randint(2000, 50000)
                        self.ssh.open_tunnel(local_port,
                                             self.cur_conn_def.host,
                                             int(self.cur_conn_def.port))
                        success = True
                        break
                    except Exception as e:
                        logging.exception('error in ssh.open_tunnel loop: ' + str(e))
                logging.debug('finished ssh.open_tunnel loop')
                if not success:
                    logging.error('finished ssh.open_tunnel loop with error')
                    return False
                else:
                    rpc_user = self.cur_conn_def.username
                    rpc_password = self.cur_conn_def.password
                    rpc_host = '127.0.0.1'  # SSH tunnel on loopback
                    rpc_port = local_port
            else:
                # direct RPC
                rpc_host = self.cur_conn_def.host
                rpc_port = self.cur_conn_def.port
                rpc_user = self.cur_conn_def.username
                rpc_password = self.cur_conn_def.password

            if self.cur_conn_def.use_ssl:
                self.rpc_url = 'https://'
                self.http_conn = httplib.HTTPSConnection(rpc_host, rpc_port, timeout=5, context=ssl._create_unverified_context())
            else:
                self.rpc_url = 'http://'
                self.http_conn = httplib.HTTPConnection(rpc_host, rpc_port, timeout=5)

            self.rpc_url += rpc_user + ':' + rpc_password + '@' + rpc_host + ':' + str(rpc_port)
            logging.debug('AuthServiceProxy configured to: %s' % self.rpc_url)
            self.proxy = AuthServiceProxy(self.rpc_url, timeout=1000, connection=self.http_conn)

            try:
                # check the connection
                self.http_conn.connect()
                logging.debug('Successfully connected AuthServiceProxy')

                try:
                    # make the owner know, we successfully finished connection
                    if self.on_connection_successful_callback:
                        self.on_connection_successful_callback()
                except:
                    logging.exception('on_connection_finished_callback call exception')
            except:
                logging.exception('Connection failed')

                try:
                    # make the owner know, connection attempt failed
                    if self.on_connection_failed_callback:
                        self.on_connection_failed_callback()

                    if self.ssh:
                        # if there is a ssh connection established earlier, disconnect it because apparently it isn't
                        # functioning
                        self.ssh.disconnect()
                except:
                    logging.exception('on_connection_try_fail_callback call exception')
                raise
            finally:
                logging.debug('http_conn.close()')
                self.http_conn.close()
                # timeout hase been initially set to 5 seconds to perform 'quick' connection test
                self.http_conn.timeout = 20

            self.active = True
        return self.active

    def get_active_conn_description(self):
        if self.cur_conn_def:
            return self.cur_conn_def.get_description()
        else:
            return '???'

    @control_rpc_call
    def getblockcount(self):
        if self.open():
            return self.proxy.getblockcount()
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getinfo(self, verify_node: bool = True):
        if self.open():
            info = self.proxy.getinfo()
            if verify_node:
                node_under_testnet = info.get('testnet')
                if self.config.is_testnet() and not node_under_testnet:
                    raise Exception('This RPC node works under Polis MAINNET, but your current configuration is '
                                    'for TESTNET.')
                elif self.config.is_mainnet() and node_under_testnet:
                    raise Exception('This RPC node works under Polis TESTNET, but your current configuration is '
                                    'for MAINNET.')
            return info
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def issynchronized(self):
        if self.open():
            # if connecting to HTTP(S) proxy do not check if polis daemon is synchronized
            if self.cur_conn_def.is_http_proxy():
                return True
            else:
                syn = self.proxy.mnsync('status')
                return syn.get('IsSynced')
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def mnsync(self):
        if self.open():
            # if connecting to HTTP(S) proxy do not call this function - it will not be exposed
            if self.cur_conn_def.is_http_proxy():
                return {}
            else:
                return self.proxy.mnsync('status')
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def masternodebroadcast(self, what, hexto):
        if self.open():
            return self.proxy.masternodebroadcast(what, hexto)
        else:
            raise Exception('Not connected')

    def update_mn_queue_values(self):
        """
        Updates masternode payment queue order values.
        """

        start_tm = time.time()
        self.payment_queue = []
        d = datetime.datetime.utcnow()
        now = int(time.mktime((d.year, d.month, d.day, d.hour, d.minute, d.second, 0, 0, 0)))

        for mn in self.masternodes:
            if mn.status == 'ENABLED':
                # estimate payment queue position: after loading all masternodes
                # queue_position will be used to sort mn list and count the real queue position
                if mn.lastpaidtime == 0:
                    mn.queue_position = mn.activeseconds
                else:
                    lastpaid_ago = now - mn.lastpaidtime
                    mn.queue_position = min(lastpaid_ago, mn.activeseconds)
                self.payment_queue.append(mn)
            else:
                mn.queue_position = None

        duration1 = time.time() - start_tm
        self.payment_queue.sort(key=lambda x: x.queue_position, reverse=True)
        duration2 = time.time() - start_tm

        for mn in self.masternodes:
            if mn.status == 'ENABLED':
                mn.queue_position = self.payment_queue.index(mn)
            else:
                mn.queue_position = None
        duration3 = time.time() - start_tm
        logging.info('Masternode queue build time1: %s, time2: %s, time3: %s' %
                     (str(duration1), str(duration2), str(duration3)))

    @control_rpc_call
    def get_masternodelist(self, *args, data_max_age=MASTERNODES_CACHE_VALID_SECONDS) -> List[Masternode]:
        """
        Returns masternode list, read from the Polis network or from the internal cache.
        :param args: arguments passed to the 'masternodelist' RPC call
        :param data_max_age: maximum age (in seconds) of the cached masternode data to used; if the
            cache is older than 'data_max_age', then an RPC call is performed to load newer masternode data;
            value of 0 forces reading of the new data from the network
        :return: list of Masternode objects, matching the 'args' arguments
        """
        def parse_mns(mns_raw) -> List[Masternode]:
            """
            Parses dictionary of strings returned from the RPC to Masternode object list.
            :param mns_raw: Dict of masternodes in format of RPC masternodelist command
            :return: list of Masternode object
            """
            tm_begin = time.time()
            ret_list = []
            for mn_id in mns_raw.keys():
                mn_raw = mns_raw.get(mn_id)
                mn_raw = mn_raw.strip()
                elems = mn_raw.split()
                if len(elems) >= 8:
                    mn = Masternode()
                    # (status, protocol, payee, lastseen, activeseconds, lastpaidtime, pastpaidblock, ip)
                    mn.status, mn.protocol, mn.payee, mn.lastseen, mn.activeseconds, mn.lastpaidtime, \
                        mn.lastpaidblock, mn.ip = elems

                    mn.lastseen = int(mn.lastseen)
                    mn.activeseconds = int(mn.activeseconds)
                    mn.lastpaidtime = int(mn.lastpaidtime)
                    mn.lastpaidblock = int(mn.lastpaidblock)
                    mn.ident = mn_id
                    ret_list.append(mn)
            duration = time.time() - tm_begin
            logging.info('Parse masternodelist time: ' + str(duration))
            return ret_list

        def update_masternode_data(existing_mn, new_data, cursor):
            # update cached masternode's properties
            existing_mn.modified = False
            existing_mn.monitor_changes = True
            existing_mn.ident = new_data.ident
            existing_mn.status = new_data.status
            existing_mn.protocol = new_data.protocol
            existing_mn.payee = new_data.payee
            existing_mn.lastseen = new_data.lastseen
            existing_mn.activeseconds = new_data.activeseconds
            existing_mn.lastpaidtime = new_data.lastpaidtime
            existing_mn.lastpaidblock = new_data.lastpaidblock
            existing_mn.ip = new_data.ip

            # ... and finally update MN db record
            if cursor and existing_mn.modified:
                cursor.execute("UPDATE MASTERNODES set ident=?, status=?, protocol=?, payee=?,"
                               " last_seen=?, active_seconds=?, last_paid_time=?, "
                               " last_paid_block=?, ip=?"
                               "WHERE id=?",
                               (new_data.ident, new_data.status, new_data.protocol, new_data.payee,
                                new_data.lastseen, new_data.activeseconds, new_data.lastpaidtime,
                                new_data.lastpaidblock, new_data.ip, existing_mn.db_id))

        if self.open():

            if len(args) == 1 and args[0] == 'full':
                last_read_time = app_cache.get_value(f'MasternodesLastReadTime_{self.app_config.polis_network}', 0, int)
                logging.info("MasternodesLastReadTime: %d" % last_read_time)

                if self.masternodes and data_max_age > 0 and \
                   int(time.time()) - last_read_time < data_max_age:
                    logging.info('Using cached masternodelist (data age: %s)' % str(int(time.time()) - last_read_time))
                    return self.masternodes
                else:
                    logging.info('Loading masternode list from Polis daemon...')
                    mns = self.proxy.masternodelist(*args)
                    mns = parse_mns(mns)
                    logging.info('Finished loading masternode list')

                    # mark already cached masternodes to identify those to delete
                    for mn in self.masternodes:
                        mn.marker = False

                    # save masternodes to the db cache
                    db_modified = False
                    cur = None
                    try:
                        if self.db_intf.db_active:
                            cur = self.db_intf.get_cursor()

                        for mn in mns:
                            # check if newly-read masternode already exists in the cache
                            existing_mn = self.masternodes_by_ident.get(mn.ident)
                            if not existing_mn:
                                mn.marker = True
                                self.masternodes.append(mn)
                                self.masternodes_by_ident[mn.ident] = mn

                                if self.db_intf.db_active:
                                    cur.execute("INSERT INTO MASTERNODES(ident, status, protocol, payee, last_seen,"
                                            " active_seconds, last_paid_time, last_paid_block, ip, pmt_active,"
                                            " pmt_create_time) "
                                            "VALUES (?,?,?,?,?,?,?,?,?,?,?)",
                                            (mn.ident, mn.status, mn.protocol, mn.payee, mn.lastseen,
                                             mn.activeseconds, mn.lastpaidtime, mn.lastpaidblock, mn.ip, 1,
                                             datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
                                    mn.db_id = cur.lastrowid
                                    db_modified = True
                            else:
                                existing_mn.marker = True
                                update_masternode_data(existing_mn, mn, cur)
                                db_modified = True

                        # remove from the cache masternodes that no longer exist
                        for mn_index in reversed(range(len(self.masternodes))):
                            mn = self.masternodes[mn_index]

                            if not mn.marker:
                                if self.db_intf.db_active:
                                    cur.execute("UPDATE MASTERNODES set pmt_active=0, pmt_deactivation_time=?"
                                                "WHERE ID=?",
                                                (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                                                mn.db_id))
                                    db_modified = True
                                self.masternodes_by_ident.pop(mn.ident,0)
                                del self.masternodes[mn_index]

                        app_cache.set_value(f'MasternodesLastReadTime_{self.app_config.polis_network}', int(time.time()))
                        self.update_mn_queue_values()
                    finally:
                        if db_modified:
                            self.db_intf.commit()
                        if cur is not None:
                            self.db_intf.release_cursor()

                    return self.masternodes
            else:
                mns = self.proxy.masternodelist(*args)
                mns = parse_mns(mns)
                return mns
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getaddressbalance(self, addresses):
        if self.open():
            return self.proxy.getaddressbalance({'addresses': addresses})
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getaddressutxos(self, addresses):
        if self.open():
            return self.proxy.getaddressutxos({'addresses': addresses})
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getrawtransaction(self, txid, verbose):
        if self.open():
            return json_cache_wrapper(self.proxy.getrawtransaction, self, 'tx-' + str(verbose) + '-' + txid)(txid, verbose)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getblockhash(self, blockid):
        if self.open():
            return json_cache_wrapper(self.proxy.getblockhash, self, 'blockhash-' + str(blockid))(blockid)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getblockheader(self, blockhash):
        if self.open():
            return json_cache_wrapper(self.proxy.getblockheader, self, 'blockheader-' + str(blockhash))(blockhash)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def validateaddress(self, address):
        if self.open():
            return self.proxy.validateaddress(address)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def decoderawtransaction(self, rawtx):
        if self.open():
            return self.proxy.decoderawtransaction(rawtx)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def sendrawtransaction(self, tx, use_instant_send):
        if self.open():
            return self.proxy.sendrawtransaction(tx, False, use_instant_send)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getcurrentvotes(self, hash):
        if self.open():
            return self.proxy.getcurrentvotes(hash)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def gobject(self, *args):
        if self.open():
            return self.proxy.gobject(*args)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def masternode(self, *args):
        if self.open():
            return self.proxy.masternode(*args)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getgovernanceinfo(self):
        if self.open():
            return self.proxy.getgovernanceinfo()
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def getsuperblockbudget(self, block_index):
        if self.open():
            return self.proxy.getsuperblockbudget(block_index)
        else:
            raise Exception('Not connected')

    @control_rpc_call
    def voteraw(self, masternode_tx_hash, masternode_tx_index, governance_hash, vote_signal, vote, sig_time, vote_sig):
        if self.open():
            return self.proxy.voteraw(masternode_tx_hash, masternode_tx_index, governance_hash, vote_signal, vote,
                                      sig_time, vote_sig)
        else:
            raise Exception('Not connected')
示例#51
0
文件: rpc.py 项目: justinmoon/bitboy
 def rpc(self):
     rpc_template = "http://%s:%s@%s:%s/wallet/%s"
     url = rpc_template % ('bitcoin', 'python', 'localhost', 18332,
                           self.wallet_name)
     return AuthServiceProxy(url, timeout=60 * 5)  # 5 minute timeouts
        # This is a python file that was run in the AWS Instance running a full  #
        # bitcoin node to traverse and build the network topology of the Bitcoin #
        # network. It uses RPC commands to query the raw transaction historical  #
        # data and from there builds the network by tracing the inputs of these  #
        # transactions to their addresses and so on. It was also used as a means #
        # to test the algorithm on a high powered machine since the instance was #
        # a r3.8xlarge EC2 instance                                              #
        #                                                                        #
        ##########################################################################


rpc_user="******"
rpc_password="******"

#Connect to the aws instance's Bitcoin node running on port 8332
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%(rpc_user, rpc_password))

# Select a random address from a random transaction id from a random block to be the starting transaction and address.
# address:14JThBAeM4DSXMuChoJEreY7qTzZUYx8Bp from txid:4d17a5713c5555122540dda109e8c70025076781d55008d69b076731fb786787 from Block 250 000 chosen

def crawler(maxTxDepth):
    depth = 0
    seen = deque() #queue of transactions that have to be traversed
    seen.append('4d17a5713c5555122540dda109e8c70025076781d55008d69b076731fb786787') #append initial transaction
    nodes = []
    while depth < maxTxDepth:
        if len(seen) == 0:
           break
        item = seen.popleft()
        try:
           transaction = rpc_connection.getrawtransaction(item,1) # the "1" is to decode the raw transaction
示例#53
0
class CryptoEngineBitcoin(CryptoEngine):
    BITCOIN_FEE = 0.0001
    BITCOIN_FINAL_CONFIRMATIONS = 6

    def __init__(self, url=None, path=None):
        super().__init__()
        self.rpc = AuthServiceProxy(url)

    def certify(self, data: bytes) -> str:
        #if len(data) <1 or len(data) > 32:
        #    raise ValueError("data length must be > 0 and <= 32")

        blockchain_payload = DATA_PREFIX.encode() + data

        avail_coin = self.__get_available_coin()
        if avail_coin is None:
            raise ValueError("No coin available")

        inputs = [{'txid': avail_coin['txid'], 'vout': avail_coin['vout']}]
        outputs = {'data': binascii.hexlify(blockchain_payload).decode(), avail_coin['address']: avail_coin['amount'] - decimal.Decimal(self.BITCOIN_FEE)}
        rawtrans = self.rpc.createrawtransaction(inputs, outputs)
        #print("RAW Transaction: %s" % rawtrans)

        signedtrans = self.rpc.signrawtransaction(rawtrans)
        #print("Signed transaction: %s" % signedtrans)

        if not signedtrans['complete']:
            raise RuntimeError("Error signing transaction: %s" % (signedtrans))

        txid = self.rpc.sendrawtransaction(signedtrans['hex'])
        #print("Transaction sent, TXID: %s" % txid)

        return txid

    def __get_available_coin(self):
        unspent = self.rpc.listunspent()
        target = None
        #print("# of unspent entries: %d" % len(unspent))
        for i in range(0, len(unspent)):
            ue = unspent[i]
            #print("UE: %s" % ue)
            if ue['spendable'] and ue['amount'] >= self.BITCOIN_FEE:
                #print("Spendable entry found: %s" % ue)
                target = ue
                break

        return target

    def __get_fee(self):
        pass

    def check_cert(self, data: bytes):
        return True

    def cert_status(self, txid: str):
        tx = self.rpc.gettransaction(txid)
        if tx is None:
            return self.generate_result(STATUS_NOT_FOUND, "TX not found")

        confirmations = tx['confirmations']

        status = -1
        msg = None

        if confirmations == 0:
            status = STATUS_IN_MEMPOOL
            msg = "In mempool"
        elif confirmations < self.BITCOIN_FINAL_CONFIRMATIONS:
            status = STATUS_PARTIALLY_CONFIRMED
            msg = "Partially confirmed: %d" % confirmations
        else:
            status = STATUS_CONFIRMED
            msg = "Confirmed: %d" % confirmations

        result = super().generate_result(status, msg, confirmations)

        return result

    def unlock(self, password = None, timeout = None) -> bool:
        self.rpc.walletpassphrase(password, timeout)
        return self.is_locked()

    def lock(self):
        return self.rpc.walletlock()

    def is_locked(self) -> bool:
        code = None
        try:
            self.rpc.signmessage("", "")
        except JSONRPCException as err:
            code = err.code

        if code is None or code == -3:
            return False

        return True
示例#54
0
## brew install python
## python -m ensurepip --upgrade    OR    python3 -m ensurepip --upgrade
## pip install python-bitcoinrpc    OR    pip3 install python-bitcoinrpc
## command: "/usr/local/bin/python3",
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

from datetime import datetime
import json
import socket
from os import walk

print('-----------------------------------------------')
print("\n")
# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" %
                                  ("mynode", "13Xu4dSoPPKt8AaeBdmjeA5k"))
best_block_hash = rpc_connection.getbestblockhash()
best_block_height = rpc_connection.getblockcount()

# Current block height 683878 ~340 difficulty adjustments
print("best_block_hash: " + best_block_hash)
print("best_block_height: " + str(best_block_height))
print("\n")

data_folder = 'rcp_bitcoin_block_data_v6'
data_file_name = data_folder + '_'
data_files = next(walk(data_folder), (None, None, []))[2]

fetch_num_of_blocks = 2016
bottomBlock = 0
topBlock = fetch_num_of_blocks - 1
示例#55
0
def get_rpc():
    return AuthServiceProxy(
        f'http://{rpc_user}:{rpc_password}@{rpc_ip}:{rpc_port}',
        timeout=timeout)
示例#56
0
class ApiReceiveApplication(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/api/receive", ReceiveHandler),
            (r"/api/walletnotify/(?P<txid>[^\/]+)", WalletNotifyHandler),
            (r"/api/blocknotify/(?P<hash>[^\/]+)", BlockNotifyHandler),
        ]
        settings = dict(cookie_secret='cookie_secret')
        tornado.web.Application.__init__(self, handlers, **settings)

        input_log_file_handler = logging.handlers.TimedRotatingFileHandler(
            options.log, when='MIDNIGHT')
        formatter = logging.Formatter('%(asctime)s - %(message)s')
        input_log_file_handler.setFormatter(formatter)

        self.bitcoind = AuthServiceProxy(options.rpc_url, options.rpc_username,
                                         options.rpc_password)
        self.paytxfee = self.bitcoind.getinfo()['paytxfee']

        self.replay_logger = logging.getLogger("REPLAY")
        self.replay_logger.setLevel(logging.INFO)
        self.replay_logger.addHandler(input_log_file_handler)
        self.replay_logger.info('START')

        from models import engine, db_bootstrap
        self.db_session = scoped_session(sessionmaker(bind=engine))
        db_bootstrap(self.db_session)

        self.log_start_data()

    def invoke_callback_url(self, forwarding_address):
        url = forwarding_address.get_callback_url()
        self.log('EXECUTE', 'curl ' + url)

        http_client = httpclient.AsyncHTTPClient()
        http_client.fetch(
            url, partial(self.on_handle_callback_url, forwarding_address.id))

    def on_handle_callback_url(self, forwarding_address_id, response):
        from models import ForwardingAddress
        forwarding_address = ForwardingAddress.get_by_id(
            self.db_session, forwarding_address_id)

        if response.error:
            self.log('ERROR', str(response.error))
            forwarding_address.callback_number_of_errors += 1
            self.db_session.add(forwarding_address)
            self.db_session.commit()
        else:
            if response.body == '*ok*':
                forwarding_address.is_confirmed_by_client = True
                self.db_session.add(forwarding_address)
                self.db_session.commit()

    def log(self, command, key, value=None):
        log_msg = command + ',' + key
        if value:
            try:
                log_msg += ',' + value
            except Exception, e:
                try:
                    log_msg += ',' + str(value)
                except Exception, e:
                    try:
                        log_msg += ',' + unicode(value)
                    except Exception, e:
                        log_msg += ', [object]'
示例#57
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import logging

logging.basicConfig()
logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)

rpc_connection = AuthServiceProxy("http://*****:*****@192.168.51.202:8332")
print(rpc_connection.getinfo())
示例#58
0
def RPC():
    name = 'username'
    wallet_name = ''
    uri = wallet_template.format(**settings, wallet_name=wallet_name)
    rpc = AuthServiceProxy(uri, timeout=600)  # 1 minute timeout
    return rpc
# MULTISIGS - PART TWO - SPENDING FROM A 2-of-3 MULTISIG ADDRESS
# This simple wallet works with bitcoind and will only work with 2-of-3 multisigs
# wobine code for world bitcoin network blackboard 101
# Educational Purposes only
# Python 2.7.6 and relies on bitcoind & bitcoinrpc from https://github.com/jgarzik/python-bitcoinrpc

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
# rpc_user and rpc_password are set in the bitcoin.conf file
bitcoin = AuthServiceProxy("http://%s:%[email protected]:8332" %
                           (rpc_user, rpc_password))

# YOU NEED AT LEAST TWO OF THE PRIVATE KEYS FROM PART ONE linked to your MULTI-SIG ADDRESS
multisigprivkeyone = "L2M1uRgdwgCotoP8prWJYYwz2zwWgsMa9TJwqARG7nFxkpdvBSsm"  #your key/brother one
multisigprivkeytwo = "L1M2ZgjoAtDVu9uemahiZBQPwFA5Tyj4GLd1ECkDryviFrGp6m7k"  #wallet service/brother two
multisigprivkeythree = "L5PkVBzR4SdQimMsfHnRqRegJZDFJ22sGjSbfp3SsPSnVoB8vRFE"  #safe deposit box/brother three
ChangeAddress = "35Z3xG92YkW5Xo4ngQw6w5b3Ce6MDw94A8"  #!!! Makes Sure to set your own personal Change Address

SetTxFee = int(
    0.00005461 * 100000000
)  # Lets proper good etiquette & put something aside for our friends the miners

unspent = bitcoin.listunspent(
)  # Query wallet.dat file for unspent funds to see if we have multisigs to spend from

print "Your Bitcoin-QT/d has", len(unspent), "unspent outputs"
for i in range(0, len(unspent)):
    print
    print "Output", i + 1, "has", unspent[i]["amount"], "bitcoins, or", int(
        unspent[i]["amount"] * 100000000), "satoshis"
    print "The transaction id for output", i + 1, "is"
    print unspent[i]["txid"]
示例#60
0
# Run bitcoind in regtest mode
# bitcoind -daemon -regtest -zmqpubrawtx=tcp://127.0.0.1:28332 -rpcallowip=0.0.0.0/0 -server  \
# -rpcuser=username -rpcpassword=password
# bitcoin-cli -regtest -rpcuser=username -rpcpassword=password generate 101

# Run the keyserver
# cargo run
HOST_A = "127.0.0.1:8080"
HOST_B = "127.0.0.1:8090"
BASE_URL = "http://%s" % HOST_A
bitcoin.SelectParams("regtest")

# Init Bitcoin RPC
rpc_user = "******"
rpc_password = "******"
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18443" %
                                  (rpc_user, rpc_password))

# Generate keys
secret = os.urandom(16)
keypair = CECKey()
keypair.set_compressed(True)
keypair.set_secretbytes(secret)
private_key = keypair.get_privkey()
public_key = keypair.get_pubkey()

# Generate key addr
key_addr = str(P2PKHBitcoinAddress.from_pubkey(public_key))

# Construct Payload
header = Header(name="Something wicked", value="this way comes")
entry = Entry(headers=[header], entry_data=b'This gonna be so f*****g fast')