def change_mode(block_viewer, state, window, mode, poller):
    try:
        g.modes.index(mode)
    except ValueError:
        return False

    state['mode'] = mode
    block_viewer._mode = mode

    if mode == 'monitor':
        monitor.draw_window(state, window)
    elif mode == 'tx':
        tx.draw_window(state, window)
    elif mode == 'peers':
        peers.draw_window(state, window)
    elif mode == 'wallet':
        wallet.draw_window(state, window)
    elif mode == 'block':
        block_viewer.draw()
    elif mode == 'console':
        console.draw_window(state, window)
    elif mode == 'net':
        net.draw_window(state, window)
    elif mode == 'forks':
        forks.draw_window(state, window)

    footer.draw_window(state)
    poller.set_mode(mode)
Esempio n. 2
0
def change_mode(state, window, mode, rpc_queue=None):
    try:
        g.modes.index(mode)
    except ValueError:
        return False

    state['mode'] = mode

    if mode == 'home':
        monitor.draw_window(state, window, rpc_queue)
    elif mode == 'tx':
        tx.draw_window(state, window, rpc_queue)
    elif mode == 'peers':
        peers.draw_window(state, window, rpc_queue)
    elif mode == 'wallet':
        wallet.draw_window(state, window, rpc_queue)
    elif mode == 'mempool':
        mempool.draw_window(state, window, rpc_queue)
    elif mode == 'block':
        block.draw_window(state, window, rpc_queue)
    elif mode == 'console':
        console.draw_window(state, window, rpc_queue)
    elif mode == 'net':
        net.draw_window(state, window, rpc_queue)
    elif mode == 'forks':
        forks.draw_window(state, window, rpc_queue)
Esempio n. 3
0
def change_mode(block_viewer, state, window, mode, poller):
    try:
        g.modes.index(mode)
    except ValueError:
        return False

    state['mode'] = mode
    block_viewer._mode = mode

    if mode == 'monitor':
        monitor.draw_window(state, window)
    elif mode == 'tx':
        tx.draw_window(state, window)
    elif mode == 'peers':
        peers.draw_window(state, window)
    elif mode == 'wallet':
        wallet.draw_window(state, window)
    elif mode == 'block':
        block_viewer.draw()
    elif mode == 'console':
        console.draw_window(state, window)
    elif mode == 'net':
        net.draw_window(state, window)
    elif mode == 'forks':
        forks.draw_window(state, window)

    footer.draw_window(state)
    poller.set_mode(mode)
Esempio n. 4
0
def change_mode(state, window, mode):
    try:
        g.modes.index(mode)
    except ValueError:
        return False

    state['mode'] = mode

    if mode == 'overview':
        monitor.draw_window(state, window)
    elif mode == 'tx':
        tx.draw_window(state, window)
    elif mode == 'peers':
        peers.draw_window(state, window)
    elif mode == 'wallet':
        wallet.draw_window(state, window)
    elif mode == 'block':
        block.draw_window(state, window)
    elif mode == 'console':
        console.draw_window(state, window)
    elif mode == 'net':
        net.draw_window(state, window)
    elif mode == 'forks':
        forks.draw_window(state, window)
    elif mode == 'mnodes':
        mnode.draw_window(state, window)
def txid(s, state, window):
    if s['size'] < 0:
        if 'tx' in state:
            state.pop('tx')
        if state['mode'] == 'tx':
            tx.draw_window(state, window)
            footer.draw_window(state)
        return False

    state['tx'] = {
        'txid': s['txid'],
        'vin': [],
        'vout_string': [],
        'cursor': 0,
        'offset': 0,
        'out_offset': 0,
        'loaded': 1,
        'mode': 'inputs',
        'size': s['size'],
    }

    for vin in s['vin']:
        if 'coinbase' in vin:
            state['tx']['vin'].append({'coinbase':  vin['coinbase']})
        elif 'txid' in vin:
            if 'prev_tx' in vin:
                state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout'], 'prev_tx': vin['prev_tx']})
            else:
                state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout']})

    state['tx']['total_outputs'] = 0
    for vout in s['vout']:
        if 'value' in vout:
            if vout['scriptPubKey']['type'] == "pubkeyhash":
                buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['addresses'][0]
            else:
                buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['asm']

            if 'confirmations' in s:
                if 'spent' in vout:
                    if vout['spent'] == 'confirmed':
                        buffer_string += " [SPENT]"
                    elif vout['spent'] == 'unconfirmed':
                        buffer_string += " [UNCONFIRMED SPEND]"
                    else:
                        buffer_string += " [UNSPENT]"

            state['tx']['total_outputs'] += vout['value']
            state['tx']['vout_string'].extend(textwrap.wrap(buffer_string,70)) # change this to scale with window ?

    if 'total_inputs' in s:
        state['tx']['total_inputs'] = s['total_inputs']

    if 'confirmations' in s:
        state['tx']['confirmations'] = s['confirmations']

    if state['mode'] == 'tx':
        tx.draw_window(state, window)
        footer.draw_window(state)
Esempio n. 6
0
def txid(s, state, window):
    if s['size'] < 0:
        if 'tx' in state:
            state.pop('tx')
        if state['mode'] == 'tx':
            tx.draw_window(state, window)
            footer.draw_window(state)
        return False

    state['tx'] = {
        'txid': s['txid'],
        'vin': [],
        'vout_string': [],
        'cursor': 0,
        'offset': 0,
        'out_offset': 0,
        'loaded': 1,
        'mode': 'inputs',
        'size': s['size'],
    }

    for vin in s['vin']:
        if 'coinbase' in vin:
            state['tx']['vin'].append({'coinbase':  vin['coinbase']})
        elif 'txid' in vin:
            if 'prev_tx' in vin:
                state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout'], 'prev_tx': vin['prev_tx']})
            else:
                state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout']})

    state['tx']['total_outputs'] = 0
    for vout in s['vout']:
        if 'value' in vout:
            if vout['scriptPubKey']['type'] == "pubkeyhash":
                buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['addresses'][0]
            else:
                buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['asm']

            if 'confirmations' in s:
                if 'spent' in vout:
                    if vout['spent'] == 'confirmed':
                        buffer_string += " [SPENT]"
                    elif vout['spent'] == 'unconfirmed':
                        buffer_string += " [UNCONFIRMED SPEND]"
                    else:
                        buffer_string += " [UNSPENT]"

            state['tx']['total_outputs'] += vout['value']
            state['tx']['vout_string'].extend(textwrap.wrap(buffer_string,70)) # change this to scale with window ?

    if 'total_inputs' in s:
        state['tx']['total_inputs'] = s['total_inputs']

    if 'confirmations' in s:
        state['tx']['confirmations'] = s['confirmations']

    if state['mode'] == 'tx':
        tx.draw_window(state, window)
        footer.draw_window(state)
Esempio n. 7
0
def toggle_inputs_outputs(state, window, rpcc, poller):
    if state['mode'] == 'tx':
        if 'tx' in state:
            if 'mode' in state['tx']:
                if state['tx']['mode'] == 'inputs':
                    state['tx']['mode'] = 'outputs'
                else:
                    state['tx']['mode'] = 'inputs'
                tx.draw_window(state, window)
def toggle_inputs_outputs(state, window, rpcc, poller):
    if state['mode'] == 'tx':
        if 'tx' in state:
            if 'mode' in state['tx']:
                if state['tx']['mode'] == 'inputs':
                    state['tx']['mode'] = 'outputs'
                else:
                    state['tx']['mode'] = 'inputs'
                tx.draw_window(state, window)
Esempio n. 9
0
def toggle_submode(state, window, rpc_queue):
    if state['mode'] == 'tx':
        if 'tx' in state:
            if 'mode' in state['tx']:
                if state['tx']['mode'] == 'inputs':
                    state['tx']['mode'] = 'outputs'
                else:
                    state['tx']['mode'] = 'inputs'
                tx.draw_window(state, window, rpc_queue)
Esempio n. 10
0
def toggle_inputs_outputs(state, window, rpc_queue):
    if state['mode'] == "transaction":
        if 'tx' in state:
            if 'mode' in state['tx']:
                if state['tx']['mode'] == 'inputs':
                    state['tx']['mode'] = 'outputs'
                else:
                    state['tx']['mode'] = 'inputs'
                tx.draw_window(state, window)
Esempio n. 11
0
def toggle_submode(state, window, rpc_queue):
    if state['mode'] == 'tx':
        if 'tx' in state:
            if 'mode' in state['tx']:
                if state['tx']['mode'] == 'inputs':
                    state['tx']['mode'] = 'outputs'
                else:
                    state['tx']['mode'] = 'inputs'
                tx.draw_window(state, window, rpc_queue)
Esempio n. 12
0
def resize(s, state, window):
    if state['mode'] == 'tx':
        tx.draw_window(state, window)
    elif state['mode'] == 'block':
        block.draw_window(state, window)
    elif state['mode'] == 'peers':
        peers.draw_window(state, window)
    elif state['mode'] == 'wallet':
        wallet.draw_window(state, window)
    elif state['mode'] == 'monitor':
        monitor.draw_window(state, window)
    elif state['mode'] == 'console':
        console.draw_window(state, window)
    elif state['mode'] == 'net':
        net.draw_window(state, window)
Esempio n. 13
0
def resize(s, state, window):
    if state['mode'] == 'tx':
        tx.draw_window(state, window)
    elif state['mode'] == 'block':
        block.draw_window(state, window)
    elif state['mode'] == 'peers':
        peers.draw_window(state, window)
    elif state['mode'] == 'wallet':
        wallet.draw_window(state, window)
    elif state['mode'] == 'monitor':
        monitor.draw_window(state, window)
    elif state['mode'] == 'console':
        console.draw_window(state, window)
    elif state['mode'] == 'net':
        net.draw_window(state, window)
Esempio n. 14
0
def resize(s, state, window, rpc_queue):
    if state['mode'] == 'tx':
        tx.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'block':
        block.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'mempool':
        mempool.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'peers':
        peers.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'wallet':
        wallet.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'home':
        monitor.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'console':
        console.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'net':
        net.draw_window(state, window, rpc_queue)
    elif state['mode'] == 'forks':
        forks.draw_window(state, window, rpc_queue)
Esempio n. 15
0
def change_mode(state, window, mode):
    try:
        g.modes.index(mode)
    except ValueError:
        return False

    state['mode'] = mode

    if mode == 'monitor':
        monitor.draw_window(state, window)
    elif mode == 'transaction':
        tx.draw_window(state, window)
    elif mode == 'peers':
        peers.draw_window(state, window)
    elif mode == 'wallet':
        wallet.draw_window(state, window)
    elif mode == 'block':
        block.draw_window(state, window)
    elif mode == 'console':
        console.draw_window(state, window)
Esempio n. 16
0
def check(state, window, rpc_queue):
    c = window.getch()

    if c < 0:
        return False

    elif c == ord('q') or c == ord('Q'):
        # quit
        return True

    elif c == curses.KEY_LEFT:
        try:
            index = g.modes.index(state['mode']) - 1
        except:
            pass
        if index < 0:
            index = len(g.modes) - 2
        change_mode(state, window, g.modes[index])

    elif c == curses.KEY_RIGHT:
        try:
            index = g.modes.index(state['mode']) + 1
        except:
            pass
        if index > len(g.modes) - 2: # last index item is 'quit'
            index = 0
        change_mode(state, window, g.modes[index])

    elif c == ord('m') or c == ord('M'):
        change_mode(state, window, 'monitor')

    elif c == ord('w') or c == ord('W'):
        rpc_queue.put('listsinceblock')
        change_mode(state, window, 'wallet')

    elif c == ord('p') or c == ord('P'):
        rpc_queue.put('getpeerinfo')
        change_mode(state, window, 'peers')

    elif c == ord('b') or c == ord('B'):
        change_mode(state, window, 'block')

    elif c == ord('t') or c == ord('T'):
        change_mode(state, window, 'transaction')

    elif c == ord('c') or c == ord('C'):
        change_mode(state, window, 'console')

    elif c == ord('g') or c == ord('G'):
        if state['mode'] == "transaction":
            state['mode'] = "transaction-input"
            tx.draw_input_window(state, window, rpc_queue)
        elif state['mode'] == "block":
            state['mode'] = "block-input"
            block.draw_input_window(state, window, rpc_queue)
        elif state['mode'] == "console":
            console.draw_input_box(state, rpc_queue)

    elif c == ord('l') or c == ord('L'):
        if state['mode'] == "block":
            if 'blockcount' in state:
                if state['blockcount'] not in state['blocks']:
                    s = {'getblockhash': state['blockcount']}
                    rpc_queue.put(s)
                else:
                    state['blocks']['browse_height'] = state['blockcount']
                    block.draw_window(state, window)

    elif c == curses.KEY_DOWN:
        if state['mode'] == "transaction":
            if 'tx' in state:
                window_height = (state['y'] - 4) / 2
                if state['tx']['cursor'] < (len(state['tx']['vin']) - 1) and state['tx']['mode'] == 'inputs':
                    state['tx']['cursor'] += 1

                    if (state['tx']['cursor'] - state['tx']['offset']) > window_height-2:
                        state['tx']['offset'] += 1
                    tx.draw_inputs(state)

                elif state['tx']['out_offset'] < (len(state['tx']['vout_string']) - (window_height-1)) and state['tx']['mode'] == 'outputs':
                    state['tx']['out_offset'] += 1
                    tx.draw_outputs(state)

        elif state['mode'] == "block":
            if 'blocks' in state:
                height = str(state['blocks']['browse_height'])
                if height in state['blocks']:
                    blockdata = state['blocks'][height]
                    if state['blocks']['cursor'] < (len(blockdata['tx']) - 1):
                        state['blocks']['cursor'] += 1
                        window_height = state['y'] - 6
                        if (state['blocks']['cursor'] - state['blocks']['offset']) > window_height-2:
                            state['blocks']['offset'] += 1
                        block.draw_transactions(state)

        elif state['mode'] == "peers":
            if 'peerinfo' in state and 'peerinfo_offset' in state:
                window_height = state['y'] - 4
                if state['peerinfo_offset'] < (len(state['peerinfo']) - window_height):
                    state['peerinfo_offset'] += 1
                    peers.draw_peers(state)

        elif state['mode'] == "wallet":
            if 'wallet' in state: 
                if state['wallet']['cursor'] < (len(state['wallet']['transactions']) - 1):
                    state['wallet']['cursor'] += 1
                    window_height = state['y'] - 3
                    if ( (state['wallet']['cursor']*4 +1 ) - state['wallet']['offset']) > window_height-2:
                        state['wallet']['offset'] += 4
                    wallet.draw_transactions(state)

        elif state['mode'] == "console":
            if state['console']['offset'] > 0:
                state['console']['offset'] -= 1
                console.draw_buffer(state)

    elif c == curses.KEY_UP:
        if state['mode'] == "transaction":
            if 'tx' in state:
                if state['tx']['cursor'] > 0 and state['tx']['mode'] == 'inputs':
                    if (state['tx']['cursor'] - state['tx']['offset']) == 0:
                        state['tx']['offset'] -= 1
                    state['tx']['cursor'] -= 1
                    tx.draw_inputs(state)

                if state['tx']['out_offset'] > 0 and state['tx']['mode'] == 'outputs':
                    state['tx']['out_offset'] -= 1
                    tx.draw_outputs(state)

        elif state['mode'] == "block":
            if 'blocks' in state:
                if state['blocks']['cursor'] > 0:
                    if (state['blocks']['cursor'] - state['blocks']['offset']) == 0:
                        state['blocks']['offset'] -= 1
                    state['blocks']['cursor'] -= 1
                    block.draw_transactions(state)

        elif state['mode'] == "peers":
            if 'peerinfo' in state and 'peerinfo_offset' in state:
                if state['peerinfo_offset'] > 0:
                    state['peerinfo_offset'] -= 1
                    peers.draw_peers(state)

        elif state['mode'] == "wallet":
            if 'wallet' in state:
                if state['wallet']['cursor'] > 0:
                    state['wallet']['cursor'] -= 1
                    if ((state['wallet']['cursor']*4 +1) - state['wallet']['offset']) == -3:
                        state['wallet']['offset'] -= 4
                    wallet.draw_transactions(state)

        elif state['mode'] == "console":
            state['console']['offset'] += 1
            console.draw_buffer(state)

    elif c == curses.KEY_PPAGE:
        if state['mode'] == "console":
            window_height = state['y'] - 3 - 2
            state['console']['offset'] += window_height
            console.draw_buffer(state)

    elif c == curses.KEY_NPAGE:
        if state['mode'] == "console":
            window_height = state['y'] - 3 - 2
            if state['console']['offset'] > window_height:
                state['console']['offset'] -= window_height
            else:
                state['console']['offset'] = 0
            console.draw_buffer(state)

    elif c == ord('\t') or c == 9:
        if state['mode'] == "transaction":
            if 'tx' in state:
                if 'mode' in state['tx']:
                    if state['tx']['mode'] == 'inputs':
                        state['tx']['mode'] = 'outputs'
                    else:
                        state['tx']['mode'] = 'inputs'
                    tx.draw_window(state, window)

    elif c == curses.KEY_ENTER or c == ord('\n'):
        # TODO: some sort of indicator that a transaction is loading
        if state['mode'] == "transaction":
            if 'tx' in state:
                if 'txid' in state['tx']['vin'][ state['tx']['cursor'] ]: 
                    if state['tx']['loaded']:
                        state['tx']['loaded'] = 0
                        s = {'txid': state['tx']['vin'][ state['tx']['cursor'] ]['txid']}
                        rpc_queue.put(s)

        elif state['mode'] == "block":
            if 'blocks' in state:
                if state['blocks']['browse_height'] > 0: # block 0 is not indexed
                    height = str(state['blocks']['browse_height'])
                    if height in state['blocks']:
                        blockdata = state['blocks'][height]
                        s = {'txid': blockdata['tx'][ state['blocks']['cursor'] ]}
                        rpc_queue.put(s)
                        state['mode'] = "transaction"

        elif state['mode'] == "wallet":
            if 'wallet' in state:
                if 'transactions' in state['wallet']:
                    s = {'txid': state['wallet']['transactions'][ state['wallet']['cursor'] ]['txid']}
                    rpc_queue.put(s)
                    state['mode'] = "transaction"

    elif c == ord("v") or c == ord("V"):
        if state['mode'] == "transaction":
            if 'tx' in state:
                if 'txid' in state['tx']:
                    if state['tx']['loaded']:
                        state['tx']['loaded'] = 0

                        if 'total_inputs' not in state['tx']:
                            s = {'txid': state['tx']['txid'], 'verbose': 1}
                        else:
                            s = {'txid': state['tx']['txid']}

                        rpc_queue.put(s)

    elif c == ord('j') or c == ord('J'):
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) > 0:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] -= 1
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    elif c == ord('k') or c == ord('K'):
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) < state['blockcount']:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] += 1
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    elif c == curses.KEY_HOME:
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) > 999:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] -= 1000
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    elif c == curses.KEY_END:
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) < state['blockcount'] - 999:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] += 1000
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    return False
Esempio n. 17
0
def queue(state, window, interface_queue):
    try:
        s = interface_queue.get(False)
    except Queue.Empty:
        return False

    if 'stop' in s:
        return s['stop']

    elif 'resize' in s:
        if state['mode'] == 'transaction':
            tx.draw_window(state, window)
        elif state['mode'] == 'block':
            block.draw_window(state, window)
        elif state['mode'] == 'peers':
            peers.draw_window(state, window)
        elif state['mode'] == 'wallet':
            wallet.draw_window(state, window)
        elif state['mode'] == 'monitor':
            monitor.draw_window(state, window)
        elif state['mode'] == 'console':
            console.draw_window(state, window)
        # redraw_all_the_things
        pass

    elif 'getinfo' in s:
        state['version'] = str(s['getinfo']['version'] / 1000000)
        state['version'] += '.' + str((s['getinfo']['version'] % 1000000) / 10000)
        state['version'] += '.' + str((s['getinfo']['version'] % 10000) / 100)
        state['version'] += '.' + str((s['getinfo']['version'] % 100))
        if s['getinfo']['testnet'] == True:
            state['testnet'] = 1
        else:
            state['testnet'] = 0
        state['peers'] = s['getinfo']['connections']

        if state['mode'] == "splash":
            splash.draw_window(state, window)
    
    elif 'getconnectioncount' in s:
        state['peers'] = s['getconnectioncount']

    elif 'getblockcount' in s:
        state['blockcount'] = s['getblockcount']
        if 'browse_height' not in state['blocks']:
            state['blocks']['browse_height'] = state['blockcount']

    elif 'getbalance' in s:
        state['balance'] = s['getbalance']

    elif 'getunconfirmedbalance' in s:
        state['unconfirmedbalance'] = s['getunconfirmedbalance']

    elif 'getblock' in s:
        height = s['getblock']['height']

        state['blocks'][str(height)] = s['getblock']

        if state['mode'] == "monitor":
            monitor.draw_window(state, window)
        if state['mode'] == "block":
            if 'queried' in s['getblock']:
                state['blocks'][str(height)].pop('queried')
                state['blocks']['browse_height'] = height
                state['blocks']['offset'] = 0
                state['blocks']['cursor'] = 0
                block.draw_window(state, window)

    elif 'coinbase' in s:
        height = str(s['height'])
        if height in state['blocks']:
            state['blocks'][height]['coinbase_amount'] = s['coinbase']

    elif 'getdifficulty' in s:
        state['difficulty'] = s['getdifficulty']

    elif 'getnetworkhashps' in s:
        blocks = s['getnetworkhashps']['blocks']
        state['networkhashps'][blocks] = s['getnetworkhashps']['value']

        if state['mode'] == "splash" and blocks == 2016: # initialization complete
            state['mode'] = "monitor"
            monitor.draw_window(state, window)

    elif 'getnettotals' in s:
        state['totalbytesrecv'] = s['getnettotals']['totalbytesrecv']
        state['totalbytessent'] = s['getnettotals']['totalbytessent']

    elif 'getrawmempool' in s:
        state['rawmempool'] = s['getrawmempool']

    elif 'getpeerinfo' in s:
        state['peerinfo'] = s['getpeerinfo']
        state['peerinfo_offset'] = 0
        if state['mode'] == "peers":
            peers.draw_window(state, window)

    elif 'listsinceblock' in s:
        state['wallet'] = s['listsinceblock']
        state['wallet']['cursor'] = 0
        state['wallet']['offset'] = 0

        state['wallet']['view_string'] = []

        state['wallet']['transactions'].sort(key=lambda entry: entry['category'], reverse=True)

        # add cumulative balance field to transactiosn once ordered by time
        state['wallet']['transactions'].sort(key=lambda entry: entry['time'])
        state['wallet']['transactions'].sort(key=lambda entry: entry['confirmations'], reverse=True)
        cumulative_balance = 0
        nonce = 0 # ensures a definitive ordering of transactions for cumulative balance
        for entry in state['wallet']['transactions']:
            entry['nonce'] = nonce
            nonce += 1
            if 'amount' in entry:
                if 'fee' in entry:
                    cumulative_balance += entry['fee']
                cumulative_balance += entry['amount']
                entry['cumulative_balance'] = cumulative_balance

        state['wallet']['transactions'].sort(key=lambda entry: entry['nonce'], reverse=True)

        unit = 'BTC'
        if 'testnet' in state:
            if state['testnet']:
                unit = 'TNC'

        for entry in state['wallet']['transactions']: 
            if 'txid' in entry:
                entry_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(entry['time']))
                output_string = entry_time + " %8d" % entry['confirmations'] + " conf"
                delta = entry['amount']
                if 'fee' in entry:
                    delta += entry['fee'] # this fails if not all inputs owned by wallet; could be 'too negative'
                output_string += "% 17.8f" % delta + unit
                output_string += " " + "% 17.8f" % entry['cumulative_balance'] + unit
                state['wallet']['view_string'].append(output_string)

                output_string = entry['txid'].rjust(74)
                state['wallet']['view_string'].append(output_string)

                if 'address' in entry: # TODO: more sanity checking here
                    output_string = "          " + entry['category'].ljust(15) + entry['address']
                else:
                    output_string = "          unknown transaction type"
                state['wallet']['view_string'].append(output_string)

                state['wallet']['view_string'].append("")

        if state['mode'] == "wallet":
            wallet.draw_window(state, window)

    elif 'lastblocktime' in s:
        state['lastblocktime'] = s['lastblocktime']

    elif 'txid' in s:
        state['tx'] = {
            'txid': s['txid'],
            'vin': [],
            'vout_string': [],
            'cursor': 0,
            'offset': 0,
            'out_offset': 0,
            'loaded': 1,
            'mode': 'inputs',
            'size': s['size'],
        }

        for vin in s['vin']:
            if 'coinbase' in vin:
                state['tx']['vin'].append({'coinbase':  vin['coinbase']})
            elif 'txid' in vin:
                if 'prev_tx' in vin:
                    state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout'], 'prev_tx': vin['prev_tx']})
                else:
                    state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout']})

        state['tx']['total_outputs'] = 0
        for vout in s['vout']:
            if 'value' in vout:
                if vout['scriptPubKey']['type'] == "pubkeyhash":
                    buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['addresses'][0]
                else:
                    buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['asm']

                if 'confirmations' in s:
                    if 'spent' in vout:
                        if vout['spent'] == 'confirmed':
                            buffer_string += " [SPENT]"
                        elif vout['spent'] == 'unconfirmed':
                            buffer_string += " [UNCONFIRMED SPEND]"
                        else:
                            buffer_string += " [UNSPENT]"

                state['tx']['total_outputs'] += vout['value']
                state['tx']['vout_string'].extend(textwrap.wrap(buffer_string,70)) # change this to scale with window ?

        if 'total_inputs' in s:
            state['tx']['total_inputs'] = s['total_inputs']

        if 'confirmations' in s:
            state['tx']['confirmations'] = s['confirmations']

        if state['mode'] == "transaction":
            tx.draw_window(state, window)

    elif 'consolecommand' in s:
        state['console']['cbuffer'].append(s['consolecommand'])
        state['console']['rbuffer'].append(s['consoleresponse'])
        state['console']['offset'] = 0
        if state['mode'] == "console":
            console.draw_window(state, window)

    return False
def user_input(state, window, rpc_queue):
    c = window.getch()

    if not c:
        return False

    if c == ord('q') or c == ord('Q'):
        return True

    if c == ord('m') or c == ord('M'):
        state['mode'] = "monitor"
        monitor.draw_window(state, window)

    if c == ord('t') or c == ord('T'):
        state['mode'] = "transaction"
        tx.draw_window(state, window)

    if c == ord('p') or c == ord('P'):
        rpc_queue.put('getpeerinfo')
        state['mode'] = "peers"

    if c == ord('w') or c == ord('W'):
        rpc_queue.put('listsinceblock')
        state['mode'] = "wallet"

    if c == ord('g') or c == ord('G'):
        if state['mode'] == "transaction":
            state['mode'] = "transaction-input"
            tx.draw_input_window(state, window, rpc_queue)
        elif state['mode'] == "block":
            state['mode'] = "block-input"
            block.draw_input_window(state, window, rpc_queue)

    if c == ord('b') or c == ord('B'):
        state['mode'] = "block"
        block.draw_window(state, window)

    if c == ord('l') or c == ord('L'):
        if state['mode'] == "block":
            if 'blockcount' in state:
                if state['blockcount'] not in state['blocks']:
                    s = {'getblockhash': state['blockcount']}
                    rpc_queue.put(s)
                else:
                    state['blocks']['browse_height'] = state['blockcount']
                    block.draw_window(state, window)

    if c == curses.KEY_DOWN:
        if state['mode'] == "transaction":
            if 'tx' in state:
                if state['tx']['cursor'] < (len(state['tx']['vin']) - 1):
                    state['tx']['cursor'] += 1
                    if (state['tx']['cursor'] - state['tx']['offset']) > 6:
                        state['tx']['offset'] += 1
                    tx.draw_inputs(state)

        elif state['mode'] == "block":
            if 'blocks' in state:
                height = str(state['blocks']['browse_height'])
                if height in state['blocks']:
                    blockdata = state['blocks'][height]
                    if state['blocks']['cursor'] < (len(blockdata['tx']) - 1):
                        state['blocks']['cursor'] += 1
                        if (state['blocks']['cursor'] - state['blocks']['offset']) > 14:
                            state['blocks']['offset'] += 1
                        block.draw_transactions(state)

        elif state['mode'] == "peers":
            if 'peerinfo' in state and 'peerinfo_offset' in state:
                if state['peerinfo_offset'] < (len(state['peerinfo']) - 17):
                    state['peerinfo_offset'] += 1
                    peers.draw_peers(state)

        elif state['mode'] == "wallet":
            if 'wallet' in state: 
                if state['wallet']['offset'] < (len(state['wallet']['view_string']) - 16):
                    state['wallet']['offset'] += 4
                    wallet.draw_transactions(state)

    if c == curses.KEY_UP:
        if state['mode'] == "transaction":
            if 'tx' in state:
                if state['tx']['cursor'] > 0:
                    if (state['tx']['cursor'] - state['tx']['offset']) == 0:
                        state['tx']['offset'] -= 1
                    state['tx']['cursor'] -= 1
                    tx.draw_inputs(state)

        elif state['mode'] == "block":
            if 'blocks' in state:
                if state['blocks']['cursor'] > 0:
                    if (state['blocks']['cursor'] - state['blocks']['offset']) == 0:
                        state['blocks']['offset'] -= 1
                    state['blocks']['cursor'] -= 1
                    block.draw_transactions(state)

        elif state['mode'] == "peers":
            if 'peerinfo' in state and 'peerinfo_offset' in state:
                if state['peerinfo_offset'] > 0:
                    state['peerinfo_offset'] -= 1
                    peers.draw_peers(state)

        elif state['mode'] == "wallet":
            if 'wallet' in state:
                if state['wallet']['offset'] > 0:
                    state['wallet']['offset'] -= 4
                    wallet.draw_transactions(state)

    if c == curses.KEY_PPAGE:
        if state['mode'] == "transaction":
            if 'tx' in state:
                if state['tx']['out_offset'] > 1:
                    state['tx']['out_offset'] -= 2
                    tx.draw_outputs(state)

    if c == curses.KEY_NPAGE:
        if state['mode'] == "transaction":
            if 'tx' in state:
                if state['tx']['out_offset'] < (len(state['tx']['vout_string']) - 7):
                    state['tx']['out_offset'] += 2
                    tx.draw_outputs(state)

    if c == ord(' '):
        # TODO: some sort of indicator that a transaction is loading
        if state['mode'] == "transaction":
            if 'tx' in state:
                if 'txid' in state['tx']['vin'][ state['tx']['cursor'] ]: 
                    s = {'txid': state['tx']['vin'][ state['tx']['cursor'] ]['txid']}
                    rpc_queue.put(s)

        if state['mode'] == "block":
            if 'blocks' in state:
                if state['blocks']['browse_height'] > 0: # block 0 is not indexed
                    height = str(state['blocks']['browse_height'])
                    if height in state['blocks']:
                        blockdata = state['blocks'][height]
                        s = {'txid': blockdata['tx'][ state['blocks']['cursor'] ]}
                        rpc_queue.put(s)
                        state['mode'] = "transaction"

    if c == curses.KEY_LEFT:
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) > 0:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] -= 1
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    if c == curses.KEY_RIGHT:
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) < state['blockcount']:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] += 1
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    if c == curses.KEY_HOME:
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) > 999:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] -= 1000
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    if c == curses.KEY_END:
        if state['mode'] == "block":
            if 'blocks' in state:
                if (state['blocks']['browse_height']) < state['blockcount'] - 999:
                    if state['blocks']['loaded'] == 1:
                        state['blocks']['loaded'] = 0
                        state['blocks']['browse_height'] += 1000
                        state['blocks']['cursor'] = 0
                        state['blocks']['offset'] = 0
                        if str(state['blocks']['browse_height']) in state['blocks']:
                            block.draw_window(state, window)
                        else:
                            s = {'getblockhash': state['blocks']['browse_height']}
                            rpc_queue.put(s)

    return False
def queue(state, window, interface_queue):
    try: s = interface_queue.get(False)
    except Queue.Empty: s = {}

    if 'stop' in s:
        return s['stop']

    if 'getinfo' in s:
        state['version'] = str(s['getinfo']['version'] / 1000000)
        state['version'] += '.' + str((s['getinfo']['version'] % 1000000) / 10000)
        state['version'] += '.' + str((s['getinfo']['version'] % 10000) / 100)
        state['version'] += '.' + str((s['getinfo']['version'] % 100))
        if s['getinfo']['testnet'] == True:
            state['testnet'] = 1
        else:
            state['testnet'] = 0
        state['peers'] = s['getinfo']['connections']
    
    elif 'getconnectioncount' in s:
        state['peers'] = s['getconnectioncount']

    elif 'getblockcount' in s:
        state['blockcount'] = s['getblockcount']
        if 'browse_height' not in state['blocks']:
            state['blocks']['browse_height'] = state['blockcount']

    elif 'getbalance' in s:
        state['balance'] = s['getbalance']

    elif 'getunconfirmedbalance' in s:
        state['unconfirmedbalance'] = s['getunconfirmedbalance']

    elif 'getblock' in s:
        height = s['getblock']['height']

        state['blocks'][str(height)] = s['getblock']

        if state['mode'] == "monitor":
            monitor.draw_window(state, window)
        if state['mode'] == "block":
            if 'queried' in s['getblock']:
                state['blocks'][str(height)].pop('queried')
                state['blocks']['browse_height'] = height
                state['blocks']['offset'] = 0
                state['blocks']['cursor'] = 0
                block.draw_window(state, window)

    elif 'coinbase' in s:
        height = str(s['height'])
        if height in state['blocks']:
            state['blocks'][height]['coinbase_amount'] = s['coinbase']

    elif 'getdifficulty' in s:
        state['difficulty'] = s['getdifficulty']

    elif 'getnetworkhashps' in s:
        blocks = s['getnetworkhashps']['blocks']
        state['networkhashps'][blocks] = s['getnetworkhashps']['value']

    elif 'getnettotals' in s:
        state['totalbytesrecv'] = s['getnettotals']['totalbytesrecv']
        state['totalbytessent'] = s['getnettotals']['totalbytessent']

    elif 'getrawmempool' in s:
        state['rawmempool'] = s['getrawmempool']

    elif 'getpeerinfo' in s:
        state['peerinfo'] = s['getpeerinfo']
        state['peerinfo_offset'] = 0
        if state['mode'] == "peers":
            peers.draw_window(state, window)

    elif 'listsinceblock' in s:
        state['wallet'] = s['listsinceblock']
        state['wallet']['cursor'] = 0
        state['wallet']['offset'] = 0

        state['wallet']['view_string'] = []

        state['wallet']['transactions'].sort(key=lambda entry: entry['category'], reverse=True)

        # add cumulative balance field to transactiosn once ordered by time
        state['wallet']['transactions'].sort(key=lambda entry: entry['time'])
        cumulative_balance = 0
        nonce = 0 # ensures a definitive ordering of transactions for cumulative balance
        for entry in state['wallet']['transactions']:
            entry['nonce'] = nonce
            nonce += 1
            if 'amount' in entry:
                if 'fee' in entry:
                    cumulative_balance += entry['fee']
                cumulative_balance += entry['amount']
                entry['cumulative_balance'] = cumulative_balance

        state['wallet']['transactions'].sort(key=lambda entry: entry['nonce'], reverse=True)

        for entry in state['wallet']['transactions']: 
            if 'txid' in entry:
                entry_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(entry['time']))
                output_string = entry_time + " %8d" % entry['confirmations'] + " conf"
                delta = entry['amount']
                if 'fee' in entry:
                    delta += entry['fee']
                output_string +=  "% 17.8f" % delta + "BTC "
                output_string +=  "% 17.8f" % entry['cumulative_balance'] + "BTC"
                state['wallet']['view_string'].append(output_string)

                output_string = entry['txid'].rjust(74)
                state['wallet']['view_string'].append(output_string)

                if 'address' in entry: # TODO: more sanity checking here
                    output_string = "          " + entry['category'].ljust(15) + entry['address']
                else:
                    output_string = "          unknown transaction type"
                state['wallet']['view_string'].append(output_string)

                state['wallet']['view_string'].append("")

        if state['mode'] == "wallet":
            wallet.draw_window(state, window)

    elif 'lastblocktime' in s:
        state['lastblocktime'] = s['lastblocktime']

    elif 'txid' in s:
        state['tx'] = {
            'txid': s['txid'],
            'vin': [],
            'vout_string': [],
            'cursor': 0,
            'offset': 0,
            'out_offset': 0,
            'size': s['size']
        }

        for vin in s['vin']:
            if 'coinbase' in vin:
                state['tx']['vin'].append({'coinbase':  vin['coinbase']})
            elif 'txid' in vin:
                state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout']})

        for vout in s['vout']:
            if 'value' in vout:
                if vout['scriptPubKey']['type'] == "pubkeyhash":
                    buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['addresses'][0]
                else:
                    buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['asm']
                state['tx']['vout_string'].extend(textwrap.wrap(buffer_string,70)) # change this to scale with window ?

        if state['mode'] == "transaction":
            tx.draw_window(state, window)

    return False